Nexus.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* =============================
  2. * 作者:Snowe (斯诺)
  3. * QQ:275273997
  4. * Email:snowe0517@gmail.com ,snowe@isnowe.com
  5. *==============================*/
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using UnityEngine;
  9. using UnityEngine.SceneManagement;
  10. public class Nexus : MonoBehaviour {
  11. static Nexus currentInstance;
  12. public int maxHealth = 1000;
  13. public int maxMana = 1000;
  14. public float manaRegenRate = 1.0f; // per second
  15. int currentHealth;
  16. int currentMana;
  17. int manaSpent = 0; //计算金钱消耗总量
  18. //float manaRegenCurrent = 0;
  19. MeshRenderer mRen;//模型网格
  20. //基地爆炸的对象组建
  21. public GameObject explosionPrefab;
  22. /// <summary>
  23. /// 获取当前血量
  24. /// </summary>
  25. public int CurrentHealth
  26. {
  27. get { return currentHealth; }
  28. }
  29. /// <summary>
  30. /// 当前血量百分比
  31. /// </summary>
  32. public float CurrentHealthPercent
  33. {
  34. get { return (float)currentHealth / (float)maxHealth; }
  35. }
  36. /// <summary>
  37. /// 获取当前能源
  38. /// </summary>
  39. public int CurrentMana
  40. {
  41. get { return currentMana; }
  42. }
  43. /// <summary>
  44. /// 能源百分比
  45. /// </summary>
  46. public float CurrentManaPercent
  47. {
  48. get { return (float)currentMana / (float)maxMana; }
  49. }
  50. /// <summary>
  51. /// 死亡判定值
  52. /// </summary>
  53. public bool IsDead
  54. {
  55. get { return currentHealth <= 0; }
  56. }
  57. /// <summary>
  58. /// 获取静态化对象
  59. /// </summary>
  60. public static Nexus CurrentInstance
  61. {
  62. get { return currentInstance; }
  63. }
  64. // 初始化
  65. void Awake () {
  66. currentHealth = maxHealth;
  67. currentMana = 200;
  68. currentInstance = this;
  69. mRen = GetComponent<MeshRenderer>();
  70. explosionPrefab.SetActive(false);
  71. }
  72. public void GoToTitle()
  73. {
  74. SceneManager.LoadScene(0);
  75. }
  76. // Update is called once per frame
  77. void Update () {
  78. //manaRegenCurrent += Time.deltaTime * manaRegenRate;
  79. //while (manaRegenCurrent > 1)
  80. //{
  81. // manaRegenCurrent -= 1;
  82. // currentMana += 1;
  83. //}
  84. if (Input.GetKey(KeyCode.Alpha1)) Time.timeScale = 5;
  85. else Time.timeScale = 1;
  86. // if(Input.GetKeyUp())
  87. if (IsDead)
  88. {
  89. Time.timeScale = 0;
  90. if (Input.GetKeyDown(KeyCode.Space))
  91. SceneManager.LoadScene(0);
  92. }
  93. }
  94. /// <summary>
  95. /// 增加伤害
  96. /// </summary>
  97. /// <param name="power">伤害</param>
  98. public void Damage(int power)
  99. {
  100. bool alreadyDead = IsDead;
  101. currentHealth -= power;
  102. if (IsDead && !alreadyDead) Death();
  103. }
  104. /// <summary>
  105. /// 增加能源
  106. /// </summary>
  107. /// <param name="amount"></param>
  108. public void GainMana(int amount)
  109. {
  110. currentMana += amount;
  111. }
  112. /// <summary>
  113. /// 消费能源
  114. /// </summary>
  115. /// <param name="amount"></param>
  116. /// <returns></returns>
  117. public bool UseMana(int amount)
  118. {
  119. //判断能源是否足够
  120. if(amount > currentMana)
  121. {
  122. Debug.Log("钱不够!");
  123. return false;
  124. }
  125. //减少当前能源
  126. currentMana -= amount;
  127. //增加消费总量
  128. manaSpent += amount;
  129. return true;
  130. }
  131. /// <summary>
  132. /// 死亡判定
  133. /// </summary>
  134. void Death()
  135. {
  136. explosionPrefab.SetActive(true);
  137. //Instantiate(explosionPrefab, transform.position, transform.rotation);
  138. mRen.enabled = false;
  139. for(int i = 0; i < transform.childCount; i++)
  140. {
  141. transform.GetChild(i).gameObject.SetActive(false);
  142. }
  143. }
  144. }