Facebook
From UrbanMaster, 7 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 281
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerStats : MonoBehaviour {
  5.  
  6.         private float maxHealth = 100;
  7.         private float currentHealth = 100;
  8.         private float maxArmour = 100;
  9.         private float currentArmour = 100;
  10.         private float maxStamina = 100;
  11.         private float currentStamina = 100;
  12.  
  13.         private float barWidth = 100;
  14.         private float barHeight = 100;
  15.  
  16.         private float canHeal = 0.0f;
  17.         private float canRegenerate = 0.0f;
  18.  
  19.         private CharacterController chCont;
  20.         private UnityStandardAssets.Characters.FirstPerson.FirstPersonController fpsC;
  21.         private Vector3 lastPosition;
  22.  
  23.         public Texture2D healthTexture;
  24.         public Texture2D armourTexture;
  25.         public Texture2D staminaTexture;
  26.  
  27.         public float walkSpeed = 10.0f;
  28.         public float runSpeed = 20.0f;
  29.  
  30.         public GUITexture hitTexture;
  31.  
  32.  
  33.         void Awake()
  34.         {
  35.                 barHeight = Screen.height * 0.02f;
  36.                 barWidth = barHeight * 10.0f;
  37.  
  38.                 chCont = GetComponent<CharacterController>();
  39.                 fpsC = gameObject.GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ();
  40.  
  41.                 lastPosition = transform.position;
  42.         }
  43.  
  44.         void OnGUI()
  45.         {
  46.                 GUI.DrawTexture(new Rect(Screen.width - barWidth - 10,
  47.                         Screen.height - barHeight - 10,
  48.                         currentHealth * barWidth / maxHealth,
  49.                         barHeight),
  50.                         healthTexture);
  51.                 GUI.DrawTexture(new Rect(Screen.width - barWidth - 10,
  52.                         Screen.height - barHeight * 2 - 20,
  53.                         currentArmour * barWidth / maxArmour,
  54.                         barHeight),
  55.                         armourTexture);
  56.                 GUI.DrawTexture(new Rect(Screen.width - barWidth - 10,
  57.                         Screen.height - barHeight * 3 - 30,
  58.                         currentStamina * barWidth / maxStamina,
  59.                         barHeight),
  60.                         staminaTexture);
  61.         }
  62.  
  63.         void Start()
  64.         {
  65.                 Rect currentRes = new Rect(-Screen.width * 0.5f,
  66.                         -Screen.height * 0.5f,
  67.                         Screen.width,
  68.                         Screen.height);
  69.                 hitTexture.pixelInset = currentRes;
  70.         }
  71.  
  72.         void Update()
  73.         {
  74.                 if(Input.GetKeyDown(KeyCode.P)) {
  75.                         takeHit(30);
  76.                 }
  77.  
  78.                 if(canHeal > 0.0f) {
  79.                         canHeal -= Time.deltaTime;
  80.                 }
  81.                 if(canRegenerate > 0.0f) {
  82.                         canRegenerate -= Time.deltaTime;
  83.                 }
  84.  
  85.                 if(canHeal <= 0.0f && currentHealth < maxHealth) {
  86.                         regenerate(ref currentHealth, maxHealth);
  87.                 }
  88.                 if(canRegenerate <= 0.0f && currentStamina < maxStamina) {
  89.                         regenerate(ref currentStamina, maxStamina);
  90.                 }
  91.  
  92.         }
  93.  
  94.         void FixedUpdate ()
  95.         {
  96.                 float speed = walkSpeed;
  97.             Vector3 lastPosition;
  98.  
  99.                 if(chCont.isGrounded && Input.GetKey(KeyCode.LeftShift) && lastPosition != transform.position && currentStamina > 0) {
  100.                         lastPosition = transform.position;
  101.                         speed = runSpeed;
  102.                         currentStamina -= 1;
  103.                         currentStamina = Mathf.Clamp(currentStamina, 0, maxStamina);
  104.                         canRegenerate = 5.0f;
  105.                 }      
  106.  
  107.                 if (currentStamina > 0) {
  108.                         fpsC.CanRun = true;
  109.                 } else {
  110.                         fpsC.CanRun = false;
  111.                 }
  112.         }
  113.  
  114.         void takeHit(float damage)
  115.         {
  116.  
  117.                 Destroy(Instantiate(hitTexture), 0.15f);
  118.  
  119.                 if(currentArmour > 0) {
  120.                         currentArmour = currentArmour - damage;
  121.                         if(currentArmour < 0) {
  122.                                 currentHealth += currentArmour;
  123.                                 currentArmour = 0;
  124.                         }
  125.                 } else {
  126.                         currentHealth -= damage;
  127.                 }
  128.  
  129.                 if(currentHealth < maxHealth) {
  130.                         canHeal = 5.0f;
  131.                 }
  132.  
  133.                 currentArmour = Mathf.Clamp(currentArmour, 0, maxArmour);
  134.                 currentHealth = Mathf.Clamp(currentHealth, 0, maxHealth);
  135.         }
  136.  
  137.         void regenerate(ref float currentStat, float maxStat)
  138.         {
  139.                 currentStat += maxStat * 0.005f;
  140.                 Mathf.Clamp(currentStat, 0, maxStat);
  141.         }
  142.  
  143. }