// Coroutine to control attacking IEnumerator attack() { // Get the appropriate target object if (enemyBehavior.targetIsPlayer()) attackTarget = References.player.GetComponent<PlayerStats>(); else if (enemyBehavior.targetIsCastle()) attackTarget = References.castle.GetComponent<Castle>(); else attackTarget = enemyBehavior.getTarget().GetComponent<KillableInstance>(); // Subscribe to the target's death alert attackTarget.alertOnDeath += stopAttacking; // As long as the target is alive, wait for a short bit, then attack while (attackTarget.isAlive) { yield return new WaitForSeconds(enemyStats.secondsBetweenAttacks / 2); attackTarget.Damage(enemyStats.attackValue, this.gameObject); yield return new WaitForSeconds(enemyStats.secondsBetweenAttacks / 2); } // Now that the target is dead, start wandering enemyBehavior.changeIntent(this.gameObject); stopAttacking(); }
// Coroutine to check when to damage the target IEnumerator attack() { // Get the target and subscribe to its alert event attackTarget = playerBehavior.getTarget().GetComponent<KillableInstance>(); attackTarget.alertOnDeath += stopAttacking; // As long as the target is alive... while (attackTarget.isAlive) { // ...if we are in range of it... while (inRangeOfTarget()) { // Wait for a short bit yield return new WaitForSeconds(playerStats.secondsBetweenAttacks / 2); // If we can see the target... if (playerBehavior.seesTarget()) { // ...and we are still in range of it... if (inRangeOfTarget()) { // Start the attacking animation playerAnimations.attackAnimation(true); // Damage the target attackTarget.Damage(playerStats.activeDamage, this.gameObject); // Wait for the animation to finish yield return new WaitForSeconds(playerStats.secondsBetweenAttacks / 2); // Stop the attacking animation playerAnimations.attackAnimation(false); } } // If we can't see the target... else { Debug.Log("player started rotating"); // Pause script execution and rotate towards the target yield return playerBehavior.startRotating(); } } yield return null; } }
// Coroutine to start attacking IEnumerator attack() { if (troopBehavior.targetIsEnemy()) { // Get the target object attackTarget = troopBehavior.getTarget().GetComponent<EnemyStats>(); // Subscribe to the target's death alert attackTarget.alertOnDeath += stopAttacking; // As long as the target is alive, wait for a short bit, then attack while (attackTarget.isAlive) { yield return new WaitForSeconds(troopStats.secondsBetweenAttacks / 2); attackTarget.Damage(troopStats.attackValue, this.gameObject); yield return new WaitForSeconds(troopStats.secondsBetweenAttacks / 2); } // Now that the target is dead, start wandering troopBehavior.changeIntent(this.gameObject); } }
// Stops attacking void stopAttacking() { // Stop the animation playerAnimations.attackAnimation(false); // If we have a target... if (attackTarget != null) { // Unsubscribe from its alert event and set the reference to null attackTarget.alertOnDeath -= stopAttacking; attackTarget = null; } // If we were busy attacking... if (attackCoroutine != null) { // Stop attacking and set the reference to null StopCoroutine(attackCoroutine); attackCoroutine = null; } }
// Stop all attacking void stopAttacking() { // If we have a target, unsubscribe from their death event and set them to null if (attackTarget != null) { attackTarget.alertOnDeath -= stopAttacking; attackTarget = null; } // If we are attacking, stop attacking if (attackCoroutine != null) { StopCoroutine(attackCoroutine); attackCoroutine = null; } }