using MessagePack; using OECS; namespace Blackjack; /// /// Player hits: draws one card from the deck to the player's hand. /// [MessagePackObject] public struct HitCommand : ICommand { public void Execute(World world) { ref var state = ref world.GetSingleton(); if (state.Phase != GamePhase.PlayerTurn) return; DrawCard(world); } /// /// Draws the top card from the deck and adds it to the given hand. /// internal static void DrawCard(World world) where THand : struct { var singletonEntity = World.SingletonEntity; var deckEntity = FindEntity(world, singletonEntity); var handEntity = FindEntity(world, singletonEntity); if (deckEntity == Entity.Null || handEntity == Entity.Null) return; // Find a card still in the deck. var cardsInDeck = world.GetSources(deckEntity); if (cardsInDeck.Count == 0) return; var cardEntity = cardsInDeck.First(); // Remove from deck, add to hand. world.RemoveComponent(cardEntity); world.AddComponent(cardEntity, new Holds { Source = cardEntity, Target = handEntity }); } internal static Entity FindEntity(World world, Entity singletonEntity) where T : struct { using var iter = world.Select(); while (iter.MoveNext()) { if (iter.CurrentEntity != singletonEntity) return iter.CurrentEntity; } return Entity.Null; } }