示例#1
0
        private bool AttackIfValid(UnitCell unit, HexCoordinates coords)
        {
            if (!game.IsCurrentPlayer(player) || unit.HasAttacked)
            {
                return(false);
            }
            var target  = game.Cells.GetCellAtPosition(coords);
            var isValid = game.Cells.IsValidAttackTarget(unit, target, player.ID);

            if (isValid)
            {
                state = States.None;
                indicator.Clear();

                var damage          = ValidatedAttack.GetDamage(unit, target);
                var validatedAttack = new ValidatedAttack(player.GetNewActionID(), unit, target, damage);
                game.CommandValidator.RegisterAttack(validatedAttack);

                unit.ShowAttack(target, validatedAttack.OnAttackLanded);
                player.CmdAttackCell(
                    (Int16)player.ID, validatedAttack.ID,
                    (Int16)unit.ID, (Int16)target.ID
                    );
            }
            return(isValid);
        }
        public void CmdAttackCell(Int16 playerID, Int16 actionID, Int16 attackerID, Int16 targetID)
        {
            if (!game.IsCurrentPlayer(playerID))
            {
                return;
            }

            var attackingCell = game.Cells.GetCell(attackerID);

            if (attackingCell == null || !(attackingCell is UnitCell attackingUnit))
            {
                return;
            }
            if (attackingUnit.HasAttacked)
            {
                return;
            }

            var target = game.Cells.GetCell(targetID);

            if (!game.Cells.IsValidAttackTarget(attackingUnit, target, ID))
            {
                return;
            }

            var damage      = ValidatedAttack.GetDamage(attackingUnit, target);
            var isDestroyed = target.CurrentHP <= damage;
            var gameWon     = isDestroyed && game.DestroyingCellWinsGame(target);

            game.Grid.RpcAttack(
                playerID, actionID,
                attackerID, targetID,
                (Int16)damage, isDestroyed
                );

            if (gameWon)
            {
                game.Data.RpcGameWon((Int16)attackingCell.PlayerId);
            }
        }