EnemyMovement.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.AI;
  10. public class EnemyMovement : MonoBehaviour {
  11. NavMeshAgent agent; //寻路
  12. Transform Exit; //出口
  13. public int baseMaxHealth = 100;//基础血量
  14. public int speed = 10; //速度
  15. public int scoreValue = 10;//分数
  16. public int killManaReward = 5;//
  17. public int level = 0;
  18. public int currentHealth;
  19. float slowTime = 0f;
  20. Vector3 startPos;
  21. public int CurrentHealth
  22. {
  23. get { return currentHealth; }
  24. }
  25. public int MaxHealth
  26. {
  27. get { return baseMaxHealth + 10 * level; }
  28. }
  29. public float CurrentHealthPercent
  30. {
  31. get { return (float)currentHealth / (float)MaxHealth; }
  32. }
  33. public int EffectiveSpeed // Halves speed if slowed.
  34. {
  35. get
  36. {
  37. int output = speed;
  38. if (slowTime > 0) output /= 5;
  39. return output;
  40. }
  41. }
  42. void Start () {
  43. //获取寻路组件
  44. agent = GetComponent<NavMeshAgent>();
  45. //设定目的地
  46. agent.destination = Nexus.CurrentInstance.transform.position;
  47. //初始化血量
  48. currentHealth = MaxHealth;
  49. //初始化价值
  50. killManaReward = killManaReward + level * 5;
  51. //起始坐标
  52. startPos = transform.position;
  53. }
  54. // Update is called once per frame
  55. void FixedUpdate () {
  56. if (slowTime > 0)
  57. {
  58. slowTime -= Time.deltaTime;
  59. //if (slowTime <= 0) agent.speed = speed;
  60. }
  61. agent.speed = EffectiveSpeed;
  62. }
  63. /// <summary>
  64. /// 剩余距离
  65. /// </summary>
  66. public float RemainingDistance
  67. {
  68. get
  69. {
  70. float distance = 0.0f;
  71. if (agent == null) return float.MaxValue;//如果为空返回最大值
  72. Vector3[] corners = agent.path.corners;//获取路径坐标点组
  73. for (int c = 0; c < corners.Length - 1; ++c)
  74. {
  75. //累加坐标距离
  76. distance += Mathf.Abs((corners[c] - corners[c + 1]).magnitude);
  77. }
  78. //返回距离
  79. return distance;
  80. }
  81. }
  82. /// <summary>
  83. /// 伤害
  84. /// </summary>
  85. /// <param name="amount">值</param>
  86. public void Damage(int amount)
  87. {
  88. //减少血量
  89. currentHealth -= amount;
  90. //死亡判定
  91. if (currentHealth <= 0)
  92. {
  93. Destroy(gameObject);
  94. //增加能源
  95. Nexus.CurrentInstance.GainMana(killManaReward);
  96. //GameStateManager.CurrentInstance.AddScore(scoreValue);
  97. }
  98. }
  99. /// <summary>
  100. /// 减速时间
  101. /// </summary>
  102. /// <param name="time">时间</param>
  103. public void SlowDown(float time)
  104. {
  105. if (slowTime < time) slowTime = time;
  106. //agent.speed = EffectiveSpeed;
  107. }
  108. //碰撞
  109. void OnTriggerEnter(Collider col)
  110. {
  111. //是否碰到的是基地
  112. if (col.CompareTag("Exit"))
  113. {
  114. //给基地提交伤害
  115. col.GetComponent<Nexus>().Damage(CurrentHealth);
  116. //初始化路坐标
  117. agent.Warp(startPos);
  118. //设定目的地
  119. agent.destination = Nexus.CurrentInstance.transform.position;
  120. }
  121. }
  122. }