Example #1
0
        public void StartExecution()
        {
            if (Attacker == null)
            {
                throw new InvalidOperationException("Cannot execute while Attacker is null");
            }

            if (LocationToAttack == null)
            {
                throw new InvalidOperationException("Cannot execute while LocationToAttack is null");
            }

            Status = CommandStatus.Running;

            var bestDefender = AttackOrderLogic.GetNextAttackTargetOnCell(LocationToAttack);

            if (bestDefender == null)
            {
                Status = CommandStatus.Failed;
            }
            else if (CombatType == CombatType.Melee)
            {
                if (CombatExecuter.CanPerformMeleeAttack(Attacker, bestDefender))
                {
                    CombatExecuter.PerformMeleeAttack(
                        Attacker, bestDefender, () => Status = CommandStatus.Succeeded, () => Status = CommandStatus.Failed
                        );
                }
                else
                {
                    Status = CommandStatus.Failed;
                }
            }
            else
            {
                if (CombatExecuter.CanPerformRangedAttack(Attacker, bestDefender))
                {
                    CombatExecuter.PerformRangedAttack(Attacker, bestDefender);

                    Status = CommandStatus.Succeeded;
                }
                else
                {
                    Status = CommandStatus.Failed;
                }
            }
        }
        private void OnCellPointerEntered(IHexCell cell)
        {
            if (!IsDragging)
            {
                return;
            }

            IUnit attackCandidate = AttackOrderLogic.GetNextAttackTargetOnCell(cell);

            if (attackCandidate == SelectedUnit)
            {
                return;
            }

            if (attackCandidate != null && CombatExecuter.CanPerformMeleeAttack(SelectedUnit, attackCandidate))
            {
                SetUnitToAttack(attackCandidate);
                return;
            }

            var cityOnCell = CityLocationCanon.GetPossessionsOfOwner(cell).FirstOrDefault();

            if (cityOnCell != null && CombatExecuter.CanPerformMeleeAttack(SelectedUnit, cityOnCell.CombatFacade))
            {
                SetCityToAttack(cityOnCell);
                return;
            }

            var unitLocation = UnitPositionCanon.GetOwnerOfPossession(SelectedUnit);

            if (unitLocation != cell && UnitPositionCanon.CanChangeOwnerOfPossession(SelectedUnit, cell))
            {
                SetProspectiveTravelGoal(unitLocation, cell);
            }
            else
            {
                Clear();
            }
        }