Exemple #1
0
        /// <summary>
        ///  <para>
        ///   Method used to command your creature to start attacking another
        ///   creature.  You can only attack one creature per round, and a single
        ///   call to BeginAttacking will only attack a target creature in the
        ///   upcoming tick.  Calling BeginAttacking multiple times in the same
        ///   turn will only result in your creature attacking the target specified
        ///   in the last call to BeginAttacking.
        ///  </para>
        ///  <para>
        ///   Attacking is asynchronous so you'll need to handle the AttackCompleted
        ///   event in order to get the status of your attack.  A single attack might
        ///   not kill a target enemy so you should detect if the enemy is still
        ///   alive and call BeginAttacking once per round until the target creature
        ///   is either dead or has escaped.
        ///  </para>
        /// </summary>
        /// <param name="targetAnimal">
        ///  The AnimalState object that represents the creature you want your creature to attack.
        /// </param>
        /// <exception cref="System.ArgumentNullException">
        ///  Thrown if the targetAnimal parameter is null.
        /// </exception>
        /// <exception cref="NotHungryException">
        ///  Thrown if the creature is not hungry enough to attack.
        /// </exception>
        public void BeginAttacking(AnimalState targetAnimal)
        {
            if (targetAnimal == null)
            {
                throw new ArgumentNullException("targetAnimal", "The argument 'targetAnimal' cannot be null");
            }

            if (!CanAttack(targetAnimal))
            {
                throw new NotHungryException();
            }

            var actionID = GetNextActionID();
            var action = new AttackAction(ID, actionID, targetAnimal);
            lock (PendingActions)
            {
                PendingActions.SetAttackAction(action);
                InProgressActions.SetAttackAction(action);
            }
        }
        ///<summary>
        ///</summary>
        ///<param name="attackAction"></param>
        ///<exception cref="ApplicationException"></exception>
        public void SetAttackAction(AttackAction attackAction)
        {
            if (IsImmutable)
            {
                throw new ApplicationException("PendingActions must be mutable to modify actions.");
            }

            AttackAction = attackAction;
        }