From 64aec99f637ef16f7ba1bcbfc0e2e2767425939a Mon Sep 17 00:00:00 2001 From: CoderGamester Date: Tue, 22 Oct 2024 16:29:07 +0100 Subject: [PATCH] - Added IRngData to PoolService to suppprt read only data structure and allow abtract injection of data into other objects **Changed**: - Changed RngData to a class in orther to avoid boxing/unboxing performance when injecting IRngData. --- CHANGELOG.md | 7 +++ Runtime/RngService.cs | 122 +++++++++++++++++++++++------------------- package.json | 4 +- 3 files changed, 76 insertions(+), 57 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53bc4ab..08d8a25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ All notable changes to this package will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [0.12.0] - 2024-10-22 + +- Added IRngData to PoolService to suppprt read only data structure and allow abtract injection of data into other objects + +**Changed**: +- Changed RngData to a class in orther to avoid boxing/unboxing performance when injecting IRngData. + ## [0.11.0] - 2024-10-19 - Added Spawn(T data) method to PoolService to allow spawning new objects with defined spawning data diff --git a/Runtime/RngService.cs b/Runtime/RngService.cs index 2cacd72..003bac9 100644 --- a/Runtime/RngService.cs +++ b/Runtime/RngService.cs @@ -1,30 +1,57 @@ using System; +using System.Collections.Generic; namespace GameLovers.Services { /// - /// Contains all the data in the scope to generate and maintain the random generated values + /// Implement this interface if you use a data structure in a class to pass the values by reference + /// and thus updating directly your internal data container. /// - [Serializable] - public struct RngData + public interface IRngData { - public int Seed; - public int Count; - public int[] State; + /// + /// Gets the seed used to initialize the RNG. + /// + int Seed { get; } + + /// + /// Gets the number of random numbers generated so far. + /// + int Count { get; } + + /// + /// Gets the current state of the RNG. + /// + IReadOnlyList State { get; } } /// - /// Implement this interface if you use a data structure in a class to pass the values by reference - /// and thus updating directly your internal data container + /// Represents a data structure for storing random number generation (RNG) state. /// - public interface IRngData + public class RngData : IRngData { /// - /// The data container the state of the RNG information change. + /// The seed used to initialize the RNG. /// - RngData Data { get; set; } -} + public int Seed; + + /// + /// The number of random numbers generated so far. + /// + public int Count; + + /// + /// The current state of the RNG. + /// + public int[] State; + /// + int IRngData.Seed => Seed; + /// + int IRngData.Count => Count; + /// + IReadOnlyList IRngData.State => Array.AsReadOnly(State); + } /// /// This Service provides the necessary behaviour to manage the random generated values with always a deterministic result /// Based on the .Net library Random class @@ -32,9 +59,9 @@ public interface IRngData public interface IRngService { /// - /// The that this service is manipulating + /// The that this service is manipulating /// - public RngData Data { get; } + public IRngData Data { get; } /// /// Returns the number of times the Rng has been counted; @@ -96,7 +123,7 @@ public class RngService : IRngService private const int _helperInc = 21; private const int _valueIndex = 0; - private IRngData _rngData; + private RngData _rngData; /// public int Counter => Data.Count; @@ -114,67 +141,46 @@ public class RngService : IRngService public floatP Nextfloat => Range((floatP) 0, floatP.MaxValue); /// - public RngData Data - { - get => _rngData.Data; - private set => _rngData.Data = value; - } + public IRngData Data => _rngData; public RngService(RngData rngData) { - _rngData = new InternalRngData { Data = rngData }; - } - - public RngService(IRngData data) - { - _rngData = data; + _rngData = rngData; } /// public int PeekRange(int min, int max, bool maxInclusive = false) { - return Range(min, max, CopyRngState(Data.State), maxInclusive); + return Range(min, max, CopyRngState(_rngData.State), maxInclusive); } /// public floatP PeekRange(floatP min, floatP max, bool maxInclusive = true) { - return Range(min, max, CopyRngState(Data.State), maxInclusive); + return Range(min, max, CopyRngState(_rngData.State), maxInclusive); } /// public int Range(int min, int max, bool maxInclusive = false) { - var data = Data; - - data.Count++; - - Data = data; + _rngData.Count++; - return Range(min, max, Data.State, maxInclusive); + return Range(min, max, _rngData.State, maxInclusive); } /// public floatP Range(floatP min, floatP max, bool maxInclusive = true) { - var data = Data; + _rngData.Count++; - data.Count++; - - Data = data; - - return Range(min, max, Data.State, maxInclusive); + return Range(min, max, _rngData.State, maxInclusive); } /// public void Restore(int count) { - var data = Data; - - data.Count = count; - data.State = Restore(count, Data.Seed); - - Data = data; + _rngData.Count = count; + _rngData.State = Restore(count, _rngData.Seed); } /// @@ -254,6 +260,21 @@ public static int[] CopyRngState(int[] state) return newState; } + /// + /// Creates a new instance of with the given . + /// + /// The seed value for the RNG. + /// A new instance of with the given and an initial count of 0. + public static RngData CreateRngData(int seed) + { + return new RngData + { + Seed = seed, + Count = 0, + State = GenerateRngState(seed) + }; + } + /// /// Generates a completely new state rng state based on the given . /// Based on the publish work of D.E. Knuth @@ -319,14 +340,5 @@ private static int NextNumber(int[] rndState) return ret; } - - /// - /// Used only in the scope of this service in case that no class is passed into the object - /// - private class InternalRngData : IRngData - { - /// - public RngData Data { get; set; } - } } } diff --git a/package.json b/package.json index a0c9d17..b3945fe 100644 --- a/package.json +++ b/package.json @@ -2,13 +2,13 @@ "name": "com.gamelovers.services", "displayName": "Services", "author": "Miguel Tomas", - "version": "0.11.0", + "version": "0.12.0", "unity": "2022.4", "license": "MIT", "description": "The purpose of this package is to provide a set of services to ease the development of a basic game architecture", "type": "library", "hideInEditor": false, "dependencies": { - "com.gamelovers.dataextensions": "0.5.0" + "com.gamelovers.dataextensions": "0.6.0" } } \ No newline at end of file