Guest User

Untitled

a guest
Jan 30th, 2017
1,157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.90 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5.  
  6. public class Combat : NetworkBehaviour {
  7.  
  8.     public const int maxHealth = 100;
  9.     public bool destroyOnDeath;
  10.     public bool isEnemyNPC;
  11.    
  12.     public GameObject PlayerLastObject;
  13.     public PlayerConfig playerLastHit;
  14.    
  15.    
  16.     //dont forget to use function to generate exp //later calculate on server
  17.     private int maxExp = 15;
  18.     private int minExp = 10;
  19.  
  20.     [SyncVar]
  21.     public int health = maxHealth;
  22.     [SyncVar]
  23.     public int LastPlayerNetworkID;
  24.  
  25.     [Command]
  26.     public void CmdGiveExp()
  27.     {
  28.         if (PlayerLastObject != null && isServer)
  29.         {
  30.             playerLastHit = PlayerLastObject.GetComponent<PlayerConfig>();
  31.             if (playerLastHit != null)
  32.             {
  33.                 int amountToGive = Random.Range(minExp, maxExp);
  34.  
  35.                 //ADJUST EXP ON CHARACTER THAT LAST HIT
  36.              
  37.               //  int playerID = playerLastHit.GetComponent<NetworkConnection>().connectionId;
  38.              //   NetworkServer.SendToClient(playerID, sInt, );
  39.                 playerLastHit.recieveExp(amountToGive);
  40.             }
  41.  
  42.         }
  43.     }
  44.     public void TakeDamage(int amount) {
  45.     if(!isServer) { return; }
  46.  
  47.         health -= amount;
  48.         if (health <= 0)
  49.         {
  50.             health = 0;
  51.             if(isEnemyNPC) {
  52.                 //GIVE PLAYER EXP
  53.                 CmdGiveExp();
  54.  
  55.             }
  56.             Debug.LogError("DEAD");
  57.             if (destroyOnDeath)
  58.             {
  59.                 Destroy(gameObject);
  60.             }
  61.             else
  62.             {
  63.                 RpcRespawn();
  64.             }
  65.         }
  66.  
  67.     }
  68.  
  69.  
  70.     [ClientRpc]
  71.     void RpcRespawn() {
  72.         if(isLocalPlayer) {
  73.  
  74.             transform.position = Vector3.zero;
  75.             health = maxHealth;
  76.         }
  77.  
  78.  
  79.     }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment