using OECS; namespace Game.CardWars; /// /// Calculates each player's battle power, awards the castle to the winner, /// and checks for game end. /// public class ScoringSystem : ISystem { public void Run(World world) { var state = world.ReadSingleton(); if (state.Phase != GamePhase.Scoring) return; ref var mutable = ref world.GetSingleton(); var players = GameUtil.FindAllEntities(world); if (players.Count == 0) return; int bestPower = int.MinValue; Entity winner = Entity.Null; int winnerIndex = -1; for (int i = 0; i < players.Count; i++) { int idx = (state.StartingPlayerIndex + i) % players.Count; var player = players[idx]; int power = GameUtil.CalculatePower(world, player); if (power > bestPower) { bestPower = power; winner = player; winnerIndex = idx; } } if (winner != Entity.Null) { var castles = GameUtil.FindAllEntities(world); if (castles.Count > 0) { var castle = castles[0]; world.AddComponent(castle, new HeldBy { Target = winner }); world.RemoveComponent(castle); } var colors = new[] { CastleColor.Black, CastleColor.White, CastleColor.Blue, CastleColor.Brown, CastleColor.Yellow, CastleColor.Indigo, CastleColor.Gold }; int colorIdx = Mulberry32.NextInt(ref mutable.Seed, 0, colors.Length - 1); var newCastle = world.CreateEntity(); world.AddComponent(newCastle, new Castle { Color = colors[colorIdx] }); } mutable.Phase = GamePhase.Cleanup; world.MarkModified(World.SingletonEntity); } }