PlayerHealth.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using UnityEngine.SceneManagement;
  5. using System.IO;
  6. using System.Collections.Generic;
  7. public class PlayerHealth : MonoBehaviour
  8. {
  9. public int startingHealth = 100;
  10. public int currentHealth;
  11. public Image healthSlider;
  12. public Image damageImage;
  13. public AudioClip deathClip;
  14. public float flashSpeed = 5f;
  15. public Color flashColor = new Color(1f, 0, 0, 0.1f);
  16. Animator anim;
  17. AudioSource playerAudio;
  18. PlayerMovement playerMovement;
  19. PlayerShooting playerShooting;
  20. bool isDead;
  21. bool damaged;
  22. public AudioSource BGM;
  23. Text Bufftext;
  24. bool isplusbuff;
  25. float buffdisplaytime = 1f;
  26. Renderer Damage_color;
  27. Color p_color;
  28. private float NextLvExp;
  29. public float currExp;
  30. public int Level;
  31. public Text[] lvexp;
  32. public Image expslider;
  33. List<int> scores;
  34. public GameObject DieCanvas;
  35. void Awake()
  36. {
  37. anim = GetComponent<Animator>();
  38. playerAudio = GetComponent<AudioSource>();
  39. playerMovement = GetComponent<PlayerMovement>();
  40. playerShooting = GetComponentInChildren<PlayerShooting>();
  41. currentHealth = startingHealth;
  42. Bufftext = GetComponentInChildren<Text>();
  43. Bufftext.enabled = false;
  44. foreach(var color in transform.GetComponentsInChildren<Renderer>())
  45. {
  46. if (color.transform.name == "Player")
  47. {
  48. Damage_color = color;
  49. }
  50. }
  51. p_color = Damage_color.material.color;
  52. NextLvExp = (((int)Mathf.Pow((Level - 1), 3)) + 15) / 5 * ((Level - 1) * 2 + 20) + (10 - ((((int)Mathf.Pow((Level - 1), 3)) + 15) / 5 * ((Level - 1) * 2 + 20) % 10)) + (Level - 1) * 30;
  53. foreach(var t in lvexp)
  54. {
  55. if (t.name == "Lv") t.text = "Lv." + Level;
  56. if (t.name == "exp") t.text = currExp + "/" + NextLvExp;
  57. }
  58. expslider.fillAmount = currExp / NextLvExp;
  59. }
  60. void Update()
  61. {
  62. if (damaged)
  63. {
  64. damageImage.color = flashColor;
  65. p_color = new Color(1, 0.1f, 0.1f);
  66. }
  67. else
  68. {
  69. damageImage.color = Color.Lerp(damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
  70. p_color = Color.Lerp(p_color, new Color(1, 1, 1), 0.05f);
  71. }
  72. damaged = false;
  73. if (isplusbuff)
  74. {
  75. Vector3 camera = Camera.main.transform.position;
  76. Bufftext.transform.GetComponentInParent<Canvas>().transform.LookAt(camera);
  77. buffdisplaytime -= Time.deltaTime;
  78. if (buffdisplaytime <= 0)
  79. {
  80. isplusbuff = false;
  81. Bufftext.enabled = false;
  82. buffdisplaytime = 1f;
  83. }
  84. }
  85. Damage_color.material.color = p_color;
  86. foreach (var t in lvexp)
  87. {
  88. if (t.name == "Lv") t.text = "Lv." + Level;
  89. if (t.name == "exp") t.text = currExp + "/" + NextLvExp;
  90. }
  91. }
  92. public void TakeDamage(int amount)
  93. {
  94. damaged = true;
  95. currentHealth -= amount;
  96. healthSlider.fillAmount = (float)currentHealth / (float)startingHealth;
  97. playerAudio.Play();
  98. if(currentHealth<=0 && !isDead)
  99. {
  100. StartCoroutine(Death());
  101. }
  102. }
  103. /// <summary>
  104. /// 玩家死亡
  105. /// </summary>
  106. IEnumerator Death()
  107. {
  108. isDead = true;
  109. playerShooting.DisableEffects();
  110. anim.SetTrigger("Die");
  111. playerAudio.clip = deathClip;
  112. playerAudio.Play();
  113. playerMovement.enabled = false;
  114. playerShooting.enabled = false;
  115. BGM.Stop();
  116. yield return new WaitForSeconds(3);
  117. DieCanvas.SetActive(true);
  118. }
  119. /// <summary>
  120. /// 读取场景
  121. /// </summary>
  122. public void RestartLevel(InputField obj)
  123. {
  124. //打开排名文档,如果没有创建文档
  125. FileStream ifr = new FileStream(Application.dataPath + "/ScoreList.txt", FileMode.OpenOrCreate);
  126. ifr.Close();//关闭文档
  127. string[] txts = File.ReadAllLines(Application.dataPath + "/ScoreList.txt");
  128. List<string> list = new List<string>();
  129. System.Array.ForEach(txts, t => list.Add(t));//将文档每行添加到列表中
  130. string playertxt = obj.text +","+Level + "," + ScoreManager.score;
  131. list.Add(playertxt);//将本次玩家信息添加到文档
  132. //根据分数重新排序
  133. list.Sort((x, y) => int.Parse(y.Split(',')[2]) - int.Parse(x.Split(',')[2]));
  134. File.WriteAllLines(Application.dataPath + "/ScoreList.txt", list.ToArray());
  135. SceneManager.LoadScene(0);
  136. }
  137. void OnTriggerEnter(Collider col)
  138. {
  139. if (col.CompareTag("Item"))
  140. {
  141. currentHealth = currentHealth + 10 > startingHealth ? currentHealth = startingHealth : currentHealth + 10;
  142. Destroy(col.gameObject);
  143. healthSlider.fillAmount = (float)currentHealth / (float)startingHealth;
  144. Bufftext.enabled = true;
  145. Bufftext.text = "+10";
  146. isplusbuff = true;
  147. }
  148. }
  149. public void ExpUpdate(float exp)
  150. {
  151. currExp = currExp + exp >= NextLvExp ? LevelUp(currExp+exp) : currExp + exp;
  152. expslider.fillAmount = currExp / NextLvExp;
  153. }
  154. float LevelUp(float exp)
  155. {
  156. Level += 1;
  157. startingHealth += Level * 5;
  158. currExp = exp - NextLvExp;
  159. NextLvExp = (((int)Mathf.Pow((Level - 1), 3)) + 15) / 5 * ((Level - 1) * 2 + 20) + (10 - ((((int)Mathf.Pow((Level - 1), 3)) + 15) / 5 * ((Level - 1) * 2 + 20) % 10)) + (Level - 1) * 30;
  160. if (currExp >= NextLvExp) LevelUp(currExp);
  161. return currExp;
  162. }
  163. }