59 lines
1.9 KiB
C#
59 lines
1.9 KiB
C#
using OECS;
|
|
|
|
namespace Game.CardWars;
|
|
|
|
/// <summary>
|
|
/// Calculates each player's battle power, awards the castle to the winner,
|
|
/// and checks for game end.
|
|
/// </summary>
|
|
public class ScoringSystem : ISystem
|
|
{
|
|
public void Run(World world)
|
|
{
|
|
var state = world.ReadSingleton<GameState>();
|
|
if (state.Phase != GamePhase.Scoring) return;
|
|
|
|
ref var mutable = ref world.GetSingleton<GameState>();
|
|
|
|
var players = GameUtil.FindAllEntities<Player>(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<Castle>(world);
|
|
if (castles.Count > 0)
|
|
{
|
|
var castle = castles[0];
|
|
world.AddComponent(castle, new HeldBy { Target = winner });
|
|
world.RemoveComponent<Castle>(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<GameState>(World.SingletonEntity);
|
|
}
|
|
} |