示例#1
0
        /// <summary>
        /// This function will perform the Attack Action.
        /// </summary>
        /// <returns>true if the action is completed, false if it is not.</returns>
        public override bool work()
        {
            // target is dead, return true.
            if (target.getState().getPrimaryState() == State.PrimaryState.Dead)
            {
                return(true);
            }

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

            // Target is in range attack target.
            if (targetIsInRange())
            {
                // Create a SimpleAttackAction if it is needed.
                if (this.attackAction == null)
                {
                    attackAction = new SimpleAttackAction(unit, target);
                }

                // Call the SimpleAttackAction
                attackAction.work();

                // Set the MoveAction to null.
                moveAction = null;
            }
            // Target is not in range, move to it.
            else
            {
                // Create a MoveAction if it is needed.
                if (moveAction == null)
                {
                    if (target.getEntityType() == Entity.EntityType.Unit)
                    {
                        Unit temp = (Unit)target;
                        moveAction = new MoveAction(temp.x, temp.y, gw, unit);
                    }
                    else
                    {
                        StaticEntity temp = (StaticEntity)target;
                        moveAction = new MoveAction(temp.orginCell.Xcoord, temp.orginCell.Ycoord, gw, unit);
                    }
                }
                // Set the SimpleAttackAction to null.
                attackAction = null;

                // Call the MoveAction.
                moveAction.work();
            }

            return(false);
        }
示例#2
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);
        }