/// <summary> /// Helper function used to fire attack events. /// </summary> /// <param name="e">The attack event arguments.</param> /// <param name="clearOnly">Only clear the action, don't fire the event.</param> private void OnAttackCompleted(AttackCompletedEventArgs e, bool clearOnly) { if (InProgressActions.AttackAction != null && e.ActionID == InProgressActions.AttackAction.ActionID) { InProgressActions.SetAttackAction(null); } if (!clearOnly && AttackCompleted != null) { AttackCompleted(this, e); } }
/// <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); } }