using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; using System.Collections; public class Hp : NetworkBehaviour { [SyncVar] public float Health = 1000; [SyncVar] public float maxHealth = 1000; public Slider HealthSlider; private GameObject DeathObj; private GameObject DeathText; private GameObject InGameHUD; public delegate void Death(string playerID, string attackerID); public static event Death playerDeath; private void OnEnable() { playerDeath += deathCD; } private void OnDisable() { playerDeath -= deathCD; } private IEnumerator Start() { yield return new WaitForSeconds(1); DeathObj = GameObject.Find("GameManager").GetComponent().deathUI; DeathText = GameObject.Find("GameManager").GetComponent().deathText; HealthSlider = GameObject.Find("GameManager").GetComponent().HealthSlider; InGameHUD = GameObject.Find("GameManager").GetComponent().InGameHUD; } [Command] public void CmdDamage (float degat, string playerID, string attackerID) { RpcDamage (degat, playerID, attackerID); } [ClientRpc] void RpcDamage (float degat, string playerID, string attackerID) { GameObject player = GameObject.Find(playerID); GameObject attacker = GameObject.Find(attackerID); player.GetComponent ().Health -= degat; player.GetComponent().DamageTaken += (int) degat; attacker.GetComponent().DamageDealt += (int)degat; print(player); player.GetComponent().NickName.transform.Find("Hp").GetComponent().text = player.GetComponent().Health.ToString() + "/" + player.GetComponent().maxHealth.ToString() + "Hp"; if (player.GetComponent ().Health <= 0) { player.GetComponent().Deaths += 1; attacker.GetComponent().Kills += 1; if (player.GetComponent().BestKillStreak < player.GetComponent().KillStreak) player.GetComponent().BestKillStreak = player.GetComponent().KillStreak; player.GetComponent().KillStreak = 0; attacker.GetComponent().KillStreak += 1; if (playerDeath != null) playerDeath(playerID, attackerID); } if (transform.name == playerID) { HealthSlider.value = Health; } } void deathCD (string playerID, string attackerID) { GameObject player = GameObject.Find(playerID); if (player.GetComponent().isLocalPlayer) { DeathObj.SetActive(true); DeathText.GetComponent().text = "5"; InGameHUD.SetActive(false); StartCoroutine(Respawn()); } } private IEnumerator Respawn () { yield return new WaitForSeconds(1); DeathText.GetComponent().text = "4"; yield return new WaitForSeconds(1); DeathText.GetComponent().text = "3"; yield return new WaitForSeconds(1); DeathText.GetComponent().text = "3"; yield return new WaitForSeconds(1); DeathText.GetComponent().text = "2"; yield return new WaitForSeconds(1); DeathText.GetComponent().text = "1"; yield return new WaitForSeconds(1); transform.position = GameObject.Find("Spawn (" + Random.Range(1, 8).ToString() + ")").transform.position; Health = maxHealth; InGameHUD.SetActive(true); DeathObj.SetActive(false); } }