Beispiel #1
0
        /// <summary>
        /// Performs the GuardAttack action.
        /// </summary>
        /// <returns>false if the action is not complete, true if the action is complete.</returns>
        public override bool work()
        {
            State targetState = target.getState();

            // Check if target's state is DEAD or REMOVE
            if (!targetDead && targetState.getPrimaryState() == State.PrimaryState.Dead || targetState.getPrimaryState() == State.PrimaryState.Remove)
            {
                targetDead = true;
            }

            // unit cannot attack, return true.
            if (!unit.stats.canAttack)
            {
                return(true);
            }

            // Distance between unit and the Cell it is guarding.
            float dis = EntityLocController.findDistance(unit.x, unit.y, unit.getGuardCell().Xcoord, unit.getGuardCell().Ycoord);

            // Is target dead or out of range?
            if (targetDead || dis > MAX_DIST)
            {
                // Create a new MoveAction if it is needed.
                if (moveAction == null)
                {
                    moveAction = new MoveAction(unit.getGuardCell().Xcoord, unit.getGuardCell().Ycoord, gw, unit);
                }

                // Move back to original position
                return(moveAction.work());
            }
            // Attack the enemy.
            else
            {
                // Create a new AttackAction if it is needed.
                if (attackAction == null)
                {
                    attackAction = new AttackAction(unit, target, gw);
                }

                // Chase or attack the target.
                attackAction.work();
            }

            return(false);
        }
        /// <summary>
        /// Performs the GuardAttack action.
        /// </summary>
        /// <returns>false if the action is not complete, true if the action is complete.</returns>
        public override bool work()
        {
            State targetState = target.getState();

            // Check if target's state is DEAD or REMOVE
            if (!targetDead && targetState.getPrimaryState() == State.PrimaryState.Dead || targetState.getPrimaryState() == State.PrimaryState.Remove)
            {
                targetDead = true;
            }

            // unit cannot attack, return true.
            if (!unit.stats.canAttack)
            {
                return true;
            }

            // Distance between unit and the Cell it is guarding.
            float dis = EntityLocController.findDistance(unit.x, unit.y, unit.getGuardCell().Xcoord, unit.getGuardCell().Ycoord);

            // Is target dead or out of range?
            if (targetDead || dis > MAX_DIST)
            {
                // Create a new MoveAction if it is needed.
                if (moveAction == null)
                {
                    moveAction = new MoveAction(unit.getGuardCell().Xcoord, unit.getGuardCell().Ycoord, gw, unit);
                }

                // Move back to original position
                return moveAction.work();
            }
            // Attack the enemy.
            else
            {
                // Create a new AttackAction if it is needed.
                if (attackAction == null)
                {
                    attackAction = new AttackAction(unit, target, gw);
                }

                // Chase or attack the target.
                attackAction.work();
            }

            return false;
        }
        /// <summary>
        /// This function will process a MoveEvent and will make the unit "react" to that move event.
        /// </summary>
        /// <param name="unit">The Unit that observed the event.</param>
        /// <param name="gameEvent">The MoveEvent</param>
        /// <param name="gw">The GameWorld this is occurring in.</param>
        private static void handleMoveEventUnit(Unit unit, GameEvent gameEvent, GameWorld gw)
        {
            Console.Write("Unit at (" + unit.x + ", " + unit.y + "): ");
            // Check if unit caused this MoveEvent
            if (gameEvent.sourceEntity == (Entity)unit)
            {
                Unit target = searchCellsForEnemy(unit, gw);

                if (target != null)
                {
                    Console.WriteLine("See Enemy at " + target.getCell().Xcoord + ", ("+ target.getCell().Ycoord + ")");
                }
            }

            // unit did not cause move event
            else
            {
                // Check if sourceEntity is an enemy
                if (unit.getOwner().isEnemy(gameEvent.sourceEntity.getOwner()))
                {
                    // Check if unit is in the Aggressive AttackStance
                    if (unit.getAttackStance() == Unit.AttackStance.Agressive)
                    {
                        if(isInterruptable(unit))
                        {
                            AttackAction attackAction = new AttackAction(unit, gameEvent.sourceEntity, gw);
                            ActionController.Instance.giveCommand(unit, attackAction);
                            Console.WriteLine("Aggressive attack the entity in cell: " + gameEvent.orginCell.Xcoord + ", " + gameEvent.orginCell.Ycoord + ")");
                        }
                    }

                    // Check if unit is in the Guard AttackStance
                    else if (unit.getAttackStance() == Unit.AttackStance.Guard)
                    {
                        GuardAttack guardAttack = new GuardAttack(unit, gameEvent.sourceEntity, gw);
                        ActionController.Instance.giveCommand(unit, guardAttack);
                        Console.WriteLine("Guard attack the entity in cell: " + gameEvent.orginCell.Xcoord + ", " + gameEvent.orginCell.Ycoord + ")");
                    }
                }
            }
        }