Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.Networking;
- using System.Collections;
- [RequireComponent(typeof(playerSetup))]
- public class player : NetworkBehaviour
- {
- [SyncVar]
- private bool _isDead = false;
- public bool isDead
- {
- get { return _isDead; }
- protected set { _isDead = value; }
- }
- [SerializeField]
- private int maxHealth = 100;
- [SyncVar]
- private int currentHealth;
- [SerializeField]
- private Behaviour[] disableOnDeath;
- private bool[] wasEnabled;
- [SerializeField]
- private GameObject[] disableGameObjectsOnDeath;
- [SerializeField]
- private GameObject deathEffect;
- [SerializeField]
- private GameObject spawnEffect;
- public void PlayerSetup()
- {
- //Switch cameras
- gameManager.instance.SetSceneActive(false);
- GetComponent<playerSetup>().playerUIInstance.SetActive(true);
- }
- CmdBroadcastNewPlayerSetup();
- }
- [Command]
- private void CmdBroadcastNewPlayerSetup ()
- {
- RpcSetupPlayerOnAllClients();
- }
- [ClientRpc]
- private void RpcSetupPlayerOnAllClients ()
- {
- wasEnabled = new bool[disableOnDeath.Length];
- for (int i = 0; i < wasEnabled.Length; i++)
- {
- wasEnabled[i] = disableOnDeath[i].enabled;
- }
- SetDefaults();
- }
- //void Update()
- //{
- // if (!isLocalPlayer)
- // return;
- // if (Input.GetKeyDown(KeyCode.K))
- // {
- // RpcTakeDamage(9999);
- // }
- //}
- [ClientRpc]
- public void RpcTakeDamage(int _amount)
- {
- if (isDead)
- return;
- currentHealth -= _amount;
- Debug.Log(transform.name + "now has " + currentHealth + " health.");
- if (currentHealth <= 0)
- {
- Die();
- }
- }
- private void Die()
- {
- isDead = true;
- ///Disabel componets
- for (int i = 0; i < disableOnDeath.Length; i++)
- {
- disableOnDeath[i].enabled = false;
- }
- //Disable game objects
- for (int i = 0; i < disableGameObjectsOnDeath.Length; i++)
- {
- disableGameObjectsOnDeath[i].SetActive(false);
- }
- //Disable collider
- Collider _col = GetComponent<Collider>();
- if (_col != null)
- _col.enabled = false;
- //Spawn a death effect
- GameObject _gfxIns = (GameObject)Instantiate(deathEffect, transform.position, Quaternion.identity);
- Destroy(_gfxIns, 3f);
- //DISABLE COMPONENTS
- Debug.Log(transform.name + " is DEAD!");
- //CALL RESPAWN METHOD
- StartCoroutine(Respawn());
- }
- private IEnumerator Respawn ()
- {
- yield return new WaitForSeconds(gameManager.instance.MatchSettings.respawnTime);
- Transform _spawnpoint = NetworkManager.singleton.GetStartPosition();
- transform.position = _spawnpoint.position;
- transform.rotation = _spawnpoint.rotation;
- gameManager.instance.SetSceneActive(false);
- GetComponent<playerSetup>().playerUIInstance.SetActive(true);
- SetDefaults();
- DoSpawnEffect();
- Debug.Log(transform.name + " Respawned ");
- }
- private void DoSpawnEffect ()
- {
- //Create spawn effect
- GameObject _gfxIns = (GameObject)Instantiate(spawnEffect, transform.position, Quaternion.identity);
- Destroy(_gfxIns, 3f);
- }
- public void SetDefaults()
- {
- isDead = false;
- currentHealth = maxHealth;
- //Set componets active
- for (int i = 0; i < disableOnDeath.Length; i++)
- {
- disableOnDeath[i].enabled = wasEnabled[i];
- }
- //Enable game objects
- for (int i = 0; i < disableGameObjectsOnDeath.Length; i++)
- {
- disableGameObjectsOnDeath[i].SetActive (true);
- }
- //Enable the collider
- Collider _col = GetComponent<Collider>();
- if (_col != null)
- _col.enabled = true;
- //Enable the scene camera.
- if (isLocalPlayer)
- {
- gameManager.instance.SetSceneActive(false);
- GetComponent<playerSetup>().playerUIInstance.SetActive(true);
- }
- }
- void Start()
- {
- DoSpawnEffect();
- }
- void Update()
- {
- if (!isLocalPlayer)
- return;
- if (Input.GetKeyDown(KeyCode.K))
- {
- RpcTakeDamage(9999);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement