RabbitHuntGameMode.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. public class RabbitHuntGameMode : GameMode
  6. {
  7. GameObject rabbitPrefab;
  8. Transform hunter;
  9. Transform animalsBaseT;
  10. public RabbitHuntGameMode(GameMgr gameMgr) : base(gameMgr) {
  11. animalsBaseT = GameObject.Find("Animals").transform;
  12. rabbitPrefab = animalsBaseT.Find("Rabbit").gameObject;
  13. hunter = this.gameMgr.GetComponentInChildren<ArmBow>().transform;
  14. lastCreateDistance = GetMeasureVec().magnitude;
  15. this.gameMgr.transform.Find("RabbitHuntGameSelectLevelView").gameObject.SetActive(true);
  16. }
  17. HashSet<Rabbit> rabbits = new HashSet<Rabbit>();
  18. float lastCreateDistance = 0;
  19. float time = 60;
  20. Vector3 GetMeasureVec() {
  21. Vector3 hunterPos = hunter.position;
  22. Vector3 animalsBasePos = animalsBaseT.position;
  23. hunterPos.y = animalsBasePos.y;
  24. return animalsBasePos - hunterPos;
  25. }
  26. void CreateRabbit() {
  27. if (animalTypeCreateIndex >= animalTypeCreateList.Length) return;
  28. int animalType = animalTypeCreateList[animalTypeCreateIndex++];
  29. Vector3 measureVec = GetMeasureVec();
  30. Vector3 displace = measureVec.normalized * lastCreateDistance;
  31. lastCreateDistance += 1.5f;
  32. Vector3 newPos = hunter.position + Quaternion.AngleAxis(30 - Random.value * 60, Vector3.up) * displace;
  33. GameObject o = GameObject.Instantiate(rabbitPrefab, newPos, Quaternion.identity, animalsBaseT);
  34. o.SetActive(true);
  35. Rabbit rabbit = o.GetComponent<Rabbit>();
  36. rabbit.hp = animalType;
  37. rabbit.ChangeColorByType(animalType);
  38. rabbit.RandomRotate(Random.value * 360);
  39. rabbit.onDie += delegate(Rabbit rb) {
  40. killCount++;
  41. rabbits.Remove(rb);
  42. CreateRabbit();
  43. };
  44. rabbits.Add(rabbit);
  45. if (rabbits.Count < maxAnimalCountAtTheSameTime) {
  46. CreateRabbit();
  47. }
  48. }
  49. int killCount = 0;
  50. int maxAnimalCountAtTheSameTime = 2;
  51. int[] animalTypeCreateList;
  52. int animalTypeCreateIndex = 0;
  53. //设置关卡级别
  54. public void SetLevel(int level) {
  55. if (level == 1) {
  56. animalTypeCreateList = new int[]{1, 1};
  57. }
  58. else if (level == 2) {
  59. animalTypeCreateList = new int[]{2, 2};
  60. }
  61. else if (level == 3) {
  62. animalTypeCreateList = new int[]{3, 3};
  63. }
  64. else if (level == 4) {
  65. animalTypeCreateList = new int[]{1, 1, 2, 2};
  66. }
  67. else if (level == 5) {
  68. animalTypeCreateList = new int[]{1, 1, 1, 3, 3, 3};
  69. }
  70. CreateRabbit();
  71. }
  72. public override bool DoNextShoot() {
  73. if (GameMgr.ins.gameOver) return false;
  74. if (animalTypeCreateIndex >= animalTypeCreateList.Length && rabbits.Count == 0) {
  75. gameMgr.gameOver = true;
  76. gameMgr.StopGame();
  77. this.gameMgr.transform.Find("HunterGameSettleView").gameObject.SetActive(true);
  78. return false;
  79. }
  80. return true;
  81. }
  82. public override object[] Settle() {
  83. return new object[]{GetSurplusAnimalCount() <= 0 ? "胜利" : "失败"};
  84. }
  85. public override void Update() {
  86. if (gameMgr.gameOver || pauseTimeCounting) return;
  87. if (this.time > 0) {
  88. this.time -= Time.deltaTime;
  89. } else {
  90. this.time = 0;
  91. gameMgr.gameOver = true;
  92. gameMgr.StopGame();
  93. //添加结算界面
  94. this.gameMgr.transform.Find("HunterGameSettleView").gameObject.SetActive(true);
  95. }
  96. }
  97. public string GetTimeStr()
  98. {
  99. int seconds = (int) Mathf.Ceil(this.time);
  100. string str = "";
  101. int m = seconds / 60;
  102. if (m < 10) {
  103. str += 0;
  104. }
  105. str += m;
  106. str += " : ";
  107. int s = seconds % 60;
  108. if (s < 10)
  109. {
  110. str += 0;
  111. }
  112. str += s;
  113. return str;
  114. }
  115. public int GetSurplusAnimalCount() {
  116. return this.animalTypeCreateList.Length - killCount;
  117. }
  118. }