66 lines
2.0 KiB
C#
66 lines
2.0 KiB
C#
using OECS;
|
|
|
|
namespace Game.CardWars;
|
|
|
|
/// <summary>
|
|
/// Discards field cards, clears tokens, draws new hands, checks game over.
|
|
/// </summary>
|
|
public class CleanupSystem : ISystem
|
|
{
|
|
public void Run(World world)
|
|
{
|
|
var state = world.ReadSingleton<GameState>();
|
|
if (state.Phase != GamePhase.Cleanup) return;
|
|
|
|
ref var mutable = ref world.GetSingleton<GameState>();
|
|
|
|
var players = GameUtil.FindAllEntities<Player>(world);
|
|
var publicDeck = GameUtil.FindEntity<PublicDeck>(world);
|
|
|
|
foreach (var player in players)
|
|
{
|
|
GameUtil.DiscardField(world, player, publicDeck);
|
|
|
|
// Clear tokens on banners.
|
|
var banner = CardEffectHelpers.GetBanner(world, player);
|
|
if (banner != Entity.Null)
|
|
{
|
|
var tokens = world.GetSources<PlacedOn>(banner).ToList();
|
|
foreach (var token in tokens)
|
|
world.DestroyEntity(token);
|
|
}
|
|
}
|
|
|
|
// Draw 2 public cards per player.
|
|
foreach (var player in players)
|
|
{
|
|
for (int i = 0; i < 2; i++)
|
|
GameUtil.DrawCard(world, publicDeck, player);
|
|
}
|
|
|
|
// Check game over.
|
|
int maxScore = 0;
|
|
foreach (var player in players)
|
|
{
|
|
var castles = world.GetSources<HeldBy>(player)
|
|
.Where(c => world.HasComponent<Castle>(c))
|
|
.ToList();
|
|
maxScore = Math.Max(maxScore, castles.Count);
|
|
}
|
|
|
|
if (maxScore >= 4)
|
|
{
|
|
mutable.Phase = GamePhase.GameOver;
|
|
}
|
|
else
|
|
{
|
|
mutable.Phase = GamePhase.PlayPhase;
|
|
mutable.RoundNumber++;
|
|
mutable.CurrentPlayerIndex = (mutable.StartingPlayerIndex + 1) % mutable.PlayerCount;
|
|
mutable.StartingPlayerIndex = mutable.CurrentPlayerIndex;
|
|
mutable.PlayPhaseEnded = false;
|
|
}
|
|
|
|
world.MarkModified<GameState>(World.SingletonEntity);
|
|
}
|
|
} |