Implement deterministic simulation spine
This commit is contained in:
52
src/SideScrollerGame.Sim/Runtime/SimulationState.cs
Normal file
52
src/SideScrollerGame.Sim/Runtime/SimulationState.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace SideScrollerGame.Sim.Runtime;
|
||||
|
||||
public sealed class SimulationState
|
||||
{
|
||||
public SimulationState(int tick, int seed, ulong randomState, ulong lastRandomValue, ImmutableArray<PlayerState> players)
|
||||
{
|
||||
Tick = tick;
|
||||
Seed = seed;
|
||||
RandomState = randomState;
|
||||
LastRandomValue = lastRandomValue;
|
||||
Players = players.IsDefault ? ImmutableArray<PlayerState>.Empty : players;
|
||||
}
|
||||
|
||||
public SimulationState Clone()
|
||||
{
|
||||
var builder = ImmutableArray.CreateBuilder<PlayerState>(Players.Length);
|
||||
foreach (var player in Players)
|
||||
builder.Add(player.Clone());
|
||||
|
||||
return new(Tick, Seed, RandomState, LastRandomValue, builder.MoveToImmutable());
|
||||
}
|
||||
|
||||
public PlayerState GetRequiredPlayer(PlayerId playerId)
|
||||
{
|
||||
foreach (var player in Players)
|
||||
{
|
||||
if (player.PlayerId == playerId)
|
||||
return player;
|
||||
}
|
||||
|
||||
throw new InvalidOperationException($"Unknown player id {playerId.Value}.");
|
||||
}
|
||||
|
||||
public void AdvanceTick(int tick, ulong randomState, ulong lastRandomValue)
|
||||
{
|
||||
Tick = tick;
|
||||
RandomState = randomState;
|
||||
LastRandomValue = lastRandomValue;
|
||||
}
|
||||
|
||||
public int Tick { get; private set; }
|
||||
|
||||
public int Seed { get; }
|
||||
|
||||
public ulong RandomState { get; private set; }
|
||||
|
||||
public ulong LastRandomValue { get; private set; }
|
||||
|
||||
public ImmutableArray<PlayerState> Players { get; }
|
||||
}
|
||||
Reference in New Issue
Block a user