EnemySpawner.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. [System.Serializable]
  10. public class EnemySpwan_property
  11. {
  12. public string name; //enemy name
  13. public int enemyLevel; //等级
  14. public int total; // 总数
  15. public EnemyMovement enemy;//enemy 本体
  16. }
  17. public class EnemySpawner : MonoBehaviour {
  18. public float WaveReloadTime; //每回合等待时间
  19. public float EnemyInterval; // 每个怪刷出间隔
  20. float currReloadTime;//当前时间
  21. float currInterval;//当前刷怪间隔
  22. public List<EnemySpwan_property> enemyList = new List<EnemySpwan_property>(); //
  23. int currentWave = 0; //当前波数初始值
  24. public float currentReloadTime { get { return currReloadTime; } }
  25. void Start () {
  26. currReloadTime = 5;
  27. StartCoroutine(EnemySpawn());
  28. }
  29. /// <summary>
  30. /// 敌人刷新控制
  31. /// </summary>
  32. /// <returns></returns>
  33. IEnumerator EnemySpawn()
  34. {
  35. while (currentWave < enemyList.Count)
  36. {
  37. var EL = enemyList[currentWave];
  38. if (EL.total > 0 && currReloadTime <= 0)
  39. {
  40. if (TimeDown(ref currInterval))
  41. {
  42. EnemyMovement enemy = EL.enemy;
  43. enemy.level = EL.enemyLevel;
  44. Instantiate(enemy, transform.position, Quaternion.identity);
  45. enemyList[currentWave].total--;
  46. currInterval = EnemyInterval;
  47. }
  48. }
  49. else
  50. {
  51. TimeDown(ref currReloadTime);
  52. if (enemyList[currentWave].total <= 0)
  53. {
  54. currentWave++;
  55. currReloadTime = WaveReloadTime;
  56. }
  57. }
  58. MonsterInfo.instance.TimeUpdate(currentReloadTime.ToString("f2"));
  59. if (EL.total > 0 && currReloadTime <= 0)
  60. {
  61. //显示下一波
  62. MonsterInfo.instance.TimeUpdate(EL.name + " Lv: " + EL.enemyLevel);
  63. }
  64. yield return new WaitForFixedUpdate();
  65. }
  66. }
  67. //void Update()
  68. //{
  69. //}
  70. bool TimeDown(ref float time)
  71. {
  72. time -= Time.deltaTime;
  73. if (time <= 0) { time = 0; return true; }
  74. return false;
  75. }
  76. EnemyMovement SetEnemy(EnemyMovement enemy,EnemySpwan_property ESP)
  77. {
  78. enemy.level = ESP.enemyLevel;
  79. enemy.name = ESP.name;
  80. return enemy;
  81. }
  82. }