public void TakeDamage(int amount, PlayerConfiguration.PlayerNumber attacker) { // Ignore any damage we take if we're currently invincible if (isInvincible) { return; } // Record the change in damage currentHealth -= amount; // Start the smoke system if health <= 50% if (smokeParticles != null && smokeParticles.isStopped) { var healthPercentage = currentHealth / (float)startingHealth; if (healthPercentage <= smokeVisibilityTreshold) { smokeParticles.Play(); } } // If we're at zero or less health, we're dead if (currentHealth <= 0) { Die(); // Give our attacker the point var attackerController = PlayerController.FindWithNumber(attacker); if (attackerController != null) { if (configuration != null && configuration.playerNumber == attacker) { // If attackerController is us, tell it that it destroyed // itself. attackerController.DestroyedSelf(); } else { // Otherwise, tell it that it destroyed another player. attackerController.DestroyedOtherPlayer(); } } } }
// Returns the PlayerController that is associated with the given player number. public static PlayerController FindWithNumber(PlayerConfiguration.PlayerNumber number) { // Loop through the list of all player controllers until we find the // one with the requested player number. var allPlayerControllers = FindObjectsOfType <PlayerController>(); foreach (var controller in allPlayerControllers) { if (controller.config.playerNumber == number) { return(controller); } } // We didn't find one with this number; return null. return(null); }