58 lines
1.8 KiB
C#
58 lines
1.8 KiB
C#
|
|
using OECS;
|
||
|
|
|
||
|
|
namespace Game.CardWars;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Manages the play phase: players take turns. If a PendingChoice exists,
|
||
|
|
/// blocks advancement until the player resolves it.
|
||
|
|
/// </summary>
|
||
|
|
public class PlayPhaseSystem : ISystem
|
||
|
|
{
|
||
|
|
public void Run(World world)
|
||
|
|
{
|
||
|
|
ref var state = ref world.GetSingleton<GameState>();
|
||
|
|
if (state.Phase != GamePhase.PlayPhase) return;
|
||
|
|
|
||
|
|
// Block if a pending choice exists.
|
||
|
|
if (PendingChoice.Any(world))
|
||
|
|
return;
|
||
|
|
|
||
|
|
// If the current player has skipped, move to the next player.
|
||
|
|
if (state.PlayPhaseEnded)
|
||
|
|
{
|
||
|
|
state.PlayPhaseEnded = false;
|
||
|
|
AdvanceTurn(world, ref state);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void AdvanceTurn(World world, ref GameState state)
|
||
|
|
{
|
||
|
|
int next = (state.CurrentPlayerIndex + 1) % state.PlayerCount;
|
||
|
|
|
||
|
|
// Duelist: if the next player is the one who played duelist, end the phase.
|
||
|
|
if (world.HasSingleton<PendingDuelist>())
|
||
|
|
{
|
||
|
|
var duelist = world.ReadSingleton<PendingDuelist>();
|
||
|
|
if (next == duelist.PlayerIndex)
|
||
|
|
{
|
||
|
|
world.RemoveSingleton<PendingDuelist>();
|
||
|
|
state.Phase = GamePhase.FlipPhase;
|
||
|
|
state.CurrentPlayerIndex = state.StartingPlayerIndex;
|
||
|
|
world.MarkModified<GameState>(World.SingletonEntity);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
state.CurrentPlayerIndex = next;
|
||
|
|
|
||
|
|
// If we've come back to the starting player, everyone has skipped.
|
||
|
|
if (state.CurrentPlayerIndex == state.StartingPlayerIndex)
|
||
|
|
{
|
||
|
|
state.Phase = GamePhase.FlipPhase;
|
||
|
|
world.RemoveSingleton<PendingDuelist>(); // Clean up in case duelist was never triggered.
|
||
|
|
}
|
||
|
|
|
||
|
|
world.MarkModified<GameState>(World.SingletonEntity);
|
||
|
|
}
|
||
|
|
}
|