Skip to content

Commit

Permalink
minor: update store
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasteles committed Nov 22, 2024
1 parent 838f0fa commit 3ad902c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
18 changes: 17 additions & 1 deletion samples/ConsoleGame/View.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Numerics;

namespace ConsoleGame;

public class View
Expand All @@ -7,6 +8,7 @@ public class View
readonly ConsoleColor targetColor = ConsoleColor.Yellow;
readonly ConsoleColor[] playerColors = [ConsoleColor.Green, ConsoleColor.Red];
public View() => Console.CursorVisible = false;

public void Draw(in GameState currentState, NonGameState nonGameState)
{
Console.Clear();
Expand All @@ -17,6 +19,7 @@ public void Draw(in GameState currentState, NonGameState nonGameState)
if (nonGameState.RemotePlayerStatus is PlayerStatus.Running)
DrawStats(nonGameState);
}

void DrawHeader(NonGameState nonGameState)
{
if (nonGameState.LocalPlayer is { } localPlayer)
Expand All @@ -31,8 +34,10 @@ void DrawHeader(NonGameState nonGameState)
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("-- Spectator --\n");
}

Console.ForegroundColor = defaultColor;
}

void DrawScore(in GameState state)
{
Console.Write("Score: ");
Expand All @@ -45,6 +50,7 @@ void DrawScore(in GameState state)
Console.ForegroundColor = defaultColor;
Console.WriteLine();
}

void DrawField(in GameState currentState, NonGameState nonGameState)
{
var status1 = nonGameState.RemotePlayer.Number is 1
Expand All @@ -70,12 +76,16 @@ void DrawField(in GameState currentState, NonGameState nonGameState)
Console.ForegroundColor = defaultColor;
continue;
}

Console.Write(".");
}

Console.WriteLine();
}

Console.WriteLine();
}

bool DrawPlayer(Vector2 pos, int col, int row, ConsoleColor color, PlayerStatus status)
{
if ((int)pos.X == col && (int)pos.Y == row)
Expand All @@ -90,8 +100,10 @@ bool DrawPlayer(Vector2 pos, int col, int row, ConsoleColor color, PlayerStatus
Console.ForegroundColor = defaultColor;
return true;
}

return false;
}

void DrawConnection(NonGameState nonGameState)
{
Console.Write(" ");
Expand Down Expand Up @@ -125,9 +137,11 @@ void DrawConnection(NonGameState nonGameState)
Console.WriteLine("Disconnected.");
break;
}

Console.ForegroundColor = defaultColor;
Console.WriteLine();
}

static void DrawProgressBar(double percent)
{
const int loadingSize = 10;
Expand All @@ -139,9 +153,11 @@ static void DrawProgressBar(double percent)
Console.ForegroundColor = i <= loaded ? ConsoleColor.DarkGreen : ConsoleColor.White;
Console.Write('\u2588');
}

Console.ForegroundColor = lastColor;
}
void DrawStats(NonGameState nonGameState)

static void DrawStats(NonGameState nonGameState)
{
var peer = nonGameState.PeerNetworkStats;
var info = nonGameState.SessionInfo;
Expand Down
9 changes: 6 additions & 3 deletions src/Backdash/Synchronizing/State/SavedFrame.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Runtime.InteropServices;
using Backdash.Data;

namespace Backdash.Synchronizing.State;
Expand All @@ -9,15 +10,17 @@ namespace Backdash.Synchronizing.State;
/// <param name="GameState">Game state on <paramref name="Frame"/></param>
/// <param name="Checksum">Checksum of state</param>
/// <typeparam name="TState">Game state type</typeparam>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public record struct SavedFrame<TState>(Frame Frame, TState GameState, int Checksum)
where TState : notnull
{
/// <summary>Saved frame number</summary>
public Frame Frame = Frame;

/// <summary>Saved game state</summary>
public TState GameState = GameState;

/// <summary>Saved checksum</summary>
public int Checksum = Checksum;

/// <summary>Saved game state</summary>
public TState GameState = GameState;
}
4 changes: 2 additions & 2 deletions src/Backdash/Synchronizing/State/Stores/ArrayStateStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Backdash.Synchronizing.State.Stores;
/// <inheritdoc />
public void Initialize(int saveCount)
{
savedStates = new SavedFrame<TState>[saveCount];
savedStates = GC.AllocateArray<SavedFrame<TState>>(saveCount, pinned: true);
for (int i = 0; i < saveCount; i++)
savedStates[i] = new(Frame.Null, new(), 0);
}
Expand All @@ -25,7 +25,7 @@ public ref readonly SavedFrame<TState> Load(Frame frame)
for (var i = 0; i < savedStates.Length; i++)
{
ref var current = ref savedStates[i];
if (current.Frame != frame) continue;
if (current.Frame.Number != frame.Number) continue;
head = i;
AdvanceHead();
return ref current;
Expand Down

0 comments on commit 3ad902c

Please sign in to comment.