Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.58 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Networking;
  3. using System.Collections;
  4.  
  5.  
  6. [RequireComponent(typeof(playerSetup))]
  7.  
  8. public class player : NetworkBehaviour
  9. {
  10.  
  11.     [SyncVar]
  12.     private bool _isDead = false;
  13.     public bool isDead
  14.     {
  15.         get { return _isDead; }
  16.         protected set { _isDead = value; }
  17.     }
  18.  
  19.  
  20.     [SerializeField]
  21.     private int maxHealth = 100;
  22.  
  23.     [SyncVar]
  24.     private int currentHealth;
  25.  
  26.     [SerializeField]
  27.     private Behaviour[] disableOnDeath;
  28.     private bool[] wasEnabled;
  29.  
  30.     [SerializeField]
  31.     private GameObject[] disableGameObjectsOnDeath;
  32.  
  33.     [SerializeField]
  34.     private GameObject deathEffect;
  35.  
  36.     [SerializeField]
  37.     private GameObject spawnEffect;
  38.  
  39.    
  40.     public void PlayerSetup()
  41.     {
  42.         //Switch cameras
  43.             gameManager.instance.SetSceneActive(false);
  44.             GetComponent<playerSetup>().playerUIInstance.SetActive(true);
  45.         }
  46.  
  47.         CmdBroadcastNewPlayerSetup();
  48.     }
  49.  
  50.     [Command]
  51.     private void CmdBroadcastNewPlayerSetup ()
  52.     {
  53.         RpcSetupPlayerOnAllClients();
  54.     }
  55.  
  56.     [ClientRpc]
  57.     private void RpcSetupPlayerOnAllClients ()
  58.     {
  59.         wasEnabled = new bool[disableOnDeath.Length];
  60.         for (int i = 0; i < wasEnabled.Length; i++)
  61.         {
  62.             wasEnabled[i] = disableOnDeath[i].enabled;
  63.         }
  64.  
  65.         SetDefaults();
  66.     }
  67.  
  68.     //void Update()
  69.     //{
  70.     //    if (!isLocalPlayer)
  71.     //        return;
  72.  
  73.     //    if (Input.GetKeyDown(KeyCode.K))
  74.     //    {
  75.     //        RpcTakeDamage(9999);
  76.     //    }
  77.     //}
  78.  
  79.  
  80.     [ClientRpc]
  81.     public void RpcTakeDamage(int _amount)
  82.     {
  83.         if (isDead)
  84.             return;
  85.  
  86.         currentHealth -= _amount;
  87.  
  88.         Debug.Log(transform.name + "now has " + currentHealth + " health.");
  89.  
  90.         if (currentHealth <= 0)
  91.         {
  92.             Die();
  93.         }
  94.     }
  95.  
  96.     private void Die()
  97.     {
  98.         isDead = true;
  99.  
  100.         ///Disabel componets
  101.         for (int i = 0; i < disableOnDeath.Length; i++)
  102.         {
  103.             disableOnDeath[i].enabled = false;
  104.         }
  105.  
  106.         //Disable game objects
  107.         for (int i = 0; i < disableGameObjectsOnDeath.Length; i++)
  108.         {
  109.             disableGameObjectsOnDeath[i].SetActive(false);
  110.         }
  111.  
  112.         //Disable collider
  113.         Collider _col = GetComponent<Collider>();
  114.         if (_col != null)
  115.             _col.enabled = false;
  116.  
  117.         //Spawn a death effect
  118.         GameObject _gfxIns = (GameObject)Instantiate(deathEffect, transform.position, Quaternion.identity);
  119.         Destroy(_gfxIns, 3f);
  120.  
  121.         //DISABLE COMPONENTS
  122.  
  123.         Debug.Log(transform.name + " is DEAD!");
  124.  
  125.         //CALL RESPAWN METHOD
  126.  
  127.         StartCoroutine(Respawn());
  128.  
  129.  
  130.     }
  131.  
  132.     private IEnumerator Respawn ()
  133.     {
  134.         yield return new WaitForSeconds(gameManager.instance.MatchSettings.respawnTime);
  135.  
  136.  
  137.        
  138.         Transform _spawnpoint = NetworkManager.singleton.GetStartPosition();
  139.         transform.position = _spawnpoint.position;
  140.         transform.rotation = _spawnpoint.rotation;
  141.  
  142.         gameManager.instance.SetSceneActive(false);
  143.         GetComponent<playerSetup>().playerUIInstance.SetActive(true);
  144.  
  145.     SetDefaults();
  146.         DoSpawnEffect();
  147.  
  148.  
  149.         Debug.Log(transform.name + " Respawned ");
  150.     }
  151.  
  152.     private void DoSpawnEffect ()
  153.     {
  154.         //Create spawn effect
  155.         GameObject _gfxIns = (GameObject)Instantiate(spawnEffect, transform.position, Quaternion.identity);
  156.         Destroy(_gfxIns, 3f);
  157.     }
  158.  
  159.     public void SetDefaults()
  160.     {
  161.         isDead = false;
  162.  
  163.         currentHealth = maxHealth;
  164.  
  165.  
  166.         //Set componets active
  167.         for (int i = 0; i < disableOnDeath.Length; i++)
  168.         {
  169.             disableOnDeath[i].enabled = wasEnabled[i];
  170.         }
  171.  
  172.         //Enable game objects
  173.         for (int i = 0; i < disableGameObjectsOnDeath.Length; i++)
  174.         {
  175.             disableGameObjectsOnDeath[i].SetActive (true);
  176.         }
  177.  
  178.         //Enable the collider
  179.         Collider _col = GetComponent<Collider>();
  180.         if (_col != null)
  181.             _col.enabled = true;
  182.  
  183.         //Enable the scene camera.
  184.  
  185.         if (isLocalPlayer)
  186.         {
  187.             gameManager.instance.SetSceneActive(false);
  188.             GetComponent<playerSetup>().playerUIInstance.SetActive(true);
  189.         }
  190.     }
  191.  
  192.     void Start()
  193.     {
  194.         DoSpawnEffect();
  195.     }
  196.  
  197.     void Update()
  198.     {
  199.         if (!isLocalPlayer)
  200.             return;
  201.  
  202.         if (Input.GetKeyDown(KeyCode.K))
  203.         {
  204.             RpcTakeDamage(9999);
  205.         }
  206.  
  207.     }
  208.  
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement