//This function is called when a non-player dies, and is run on the server. //If the item has assigned drops, it will randomly generate one to show when destroyed. //Regardless, it will destroy the given object. void ItemSpawnDestroy() { /* * print("rpcitemspawn "+dropOnDeath.lootDropItems.Count); * for (int i = 0; i < dropOnDeath.lootDropItems.Count; i++) * { * if (dropOnDeath.lootDropItems[i].item != null) * print(dropOnDeath.lootDropItems[i].item.name); * } */ if (dropOnDeath.lootDropItems.Count > 0) { //GameObject powerUp = Instantiate(dropOnDeath[Random.Range(0, dropOnDeath.Length)], transform.position, Quaternion.identity); GenericLootDropItemGameObject powerUp = dropOnDeath.PickLootDropItem(); if (powerUp.item != null) { GameObject newPowerUp = Instantiate(powerUp.item, transform.position, Quaternion.identity); } } EnemyBaseEntity enemyScript = GetComponent <EnemyBaseEntity>(); if (enemyScript != null) { enemyScript.DeathAnimation(); } else { Destroy(gameObject); } }
public void TakeDamage(int amount, bool canGenerateDrop = true) { if (alreadyDead) { return; } if (currentInvulnTime > 0) { return; } print("I will now take damage." + amount); if (hudForPlayer != null) { //Players will not take any damage if the level has completed. if (hudForPlayer.gameOver) { return; } } currentHealth -= amount; print("Current HP: " + currentHealth); if (hudForPlayer != null) { hudForPlayer.UpdateLives(); } if (currentHealth <= 0) { //Play death sound if it has one. if (AUDIO != null || dieSound != null) { AUDIO.pitch = 1f; AUDIO.timeSamples = 0; AUDIO.clip = dieSound; AUDIO.Play(); } alreadyDead = true; //This bool is set to make sure that the death scripting isn't run more than once. //This is important for objects that spawn things when destroyed, such as powerups. if (isAPlayer) { //Reset all the player's stats. PlayerSP playerScript = GetComponent <PlayerSP>(); if (playerScript != null) { playerScript.currentLives--; if (hudForPlayer != null) { hudForPlayer.UpdateLives(); } print("reduced to " + playerScript.currentLives + " lives."); playerScript.DIE(); /* * if (playerScript.currentLives > 0) * Respawn(); * else * { * gameObject.SetActive(false); * } */ } } else if (canGenerateDrop) { ItemSpawnDestroy(); } else { EnemyBaseEntity enemyScript = GetComponent <EnemyBaseEntity>(); if (enemyScript != null) { enemyScript.DeathAnimation(); } } if (isAnEnemy) { hudForPlayer.AddOrRemoveEnemy(-1); } } else { looksInvuln = true; if (invulnEffect != null) { invulnEffect.SetActive(true); } currentInvulnTime = invulnTime; } }