WolfHuntGameMode.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityStandardAssets.ImageEffects;
  5. using DG.Tweening;
  6. using UnityEngine.UI;
  7. /* 野狼关卡-单人模式 */
  8. public class WolfHuntGameMode : ChallengeGameMode
  9. {
  10. public WolfHuntGameMode(GameMgr gameMgr) : base(gameMgr) {
  11. this.animalTypeID = 2;
  12. animalPrefab = animalsBaseT.Find("Wolf").gameObject;
  13. this.time = 18000;
  14. this.arrowCount = 10000;
  15. // this.SetLevel(5);
  16. // gameMgr.transform.Find("HunterGameView").gameObject.SetActive(true);
  17. BanBowReady();
  18. }
  19. public override void Start() {
  20. UnbanBowReady();
  21. GameMgr.ins.transform.Find("WolfActGrid").gameObject.SetActive(true);
  22. if (nextLevel != null) {
  23. SetLevel(int.Parse(nextLevel));
  24. AddHuntGameView();
  25. } else {
  26. AddSelectLevelView();
  27. }
  28. }
  29. //动物的创建列表(数组元素表示动物颜色样式)
  30. int[] animalCreateList;
  31. //动物创建列表的当前索引值
  32. int animalCreateIndex = 0;
  33. //设置关卡级别
  34. public override void SetLevel(int level) {
  35. currentlevel = level;
  36. if (level == 1) {
  37. animalCreateList = new int[]{1, 1};
  38. }
  39. else if (level == 2) {
  40. animalCreateList = new int[]{1, 1, 1, 1};
  41. }
  42. else if (level == 3) {
  43. animalCreateList = new int[]{2, 2};
  44. }
  45. else if (level == 4) {
  46. animalCreateList = new int[]{1, 1, 2, 2};
  47. }
  48. else if (level == 5) {
  49. animalCreateList = new int[]{1, 1, 1, 1, 2, 2, 2, 2};
  50. }
  51. if (animalCreateList != null) animalCountMax = animalCount = animalCreateList.Length;
  52. canStartCreateAniaml = true;
  53. }
  54. float baseCreateDistance = 25;
  55. // float baseCreateDistance = 15;
  56. float plusCreateDistance = 0;
  57. public bool banCreateAnimal = false;
  58. void CreateAnimal() {
  59. if (banCreateAnimal) return;
  60. if (animalCreateIndex >= animalCreateList.Length) return;
  61. int animalStyleID = animalCreateList[animalCreateIndex++];
  62. //计算初始生成位置,后面的兔子生成越来越远,在标准方向的±30°内生成
  63. #region
  64. float createDistance = baseCreateDistance;
  65. baseCreateDistance += plusCreateDistance;
  66. Vector3 standardPointer = animalsBaseT.forward;
  67. Vector3 displace = standardPointer * createDistance;
  68. displace = Quaternion.AngleAxis(Random.Range(-14f, 14f), Vector3.up) * displace;
  69. Vector3 newPos = animalsBaseT.position + displace;
  70. #endregion
  71. GameObject animalObject = GameObject.Instantiate(animalPrefab, newPos, Quaternion.identity, animalsBaseT);
  72. animalObject.SetActive(true);
  73. Wolf wolf = animalObject.GetComponent<Wolf>();
  74. WolfActGrid.ins.areaMatrix.occupyPos(newPos, wolf);
  75. wolf.transform.position = newPos;
  76. wolf.hp = animalStyleID == 1 ? 5 : 8;
  77. wolf.onlineHandler.uid = animalCreateIndex + 1;
  78. wolf.ChangeColorByType(animalStyleID);
  79. wolf.RotateByWorldY(Random.value * 360);
  80. wolf.onDie += delegate(Wolf wf) {
  81. animalCount--;
  82. animalSet.Remove(wf);
  83. CreateAnimalIgnoreAndClearCountDown();
  84. };
  85. wolf.onAttack += OnAttacked;
  86. animalSet.Add(wolf);
  87. }
  88. bool canStartCreateAniaml = false; //等选完关卡才能开始倒计时
  89. float createAnimalCountDown = 0; //生产动物的倒计时
  90. float createAnimalCountDownDefault = 30;
  91. public override void Update()
  92. {
  93. base.Update();
  94. CheckAutoCreateAnimalByUpdate(Time.deltaTime);
  95. }
  96. public void CheckAutoCreateAnimalByUpdate(float dt) {
  97. //固定时间间隔生成一头狼
  98. if (!gameMgr.gameOver && canStartCreateAniaml) {
  99. createAnimalCountDown -= dt;
  100. if (createAnimalCountDown <= 0) {
  101. createAnimalCountDown = createAnimalCountDownDefault;
  102. CreateAnimal();
  103. }
  104. }
  105. }
  106. //当动物死亡后,如果场上没有活着的动物,就立即生成一头狼,并清空生产动物的倒计时
  107. void CreateAnimalIgnoreAndClearCountDown() {
  108. if (animalSet.Count == 0) {
  109. createAnimalCountDown = createAnimalCountDownDefault;
  110. CreateAnimal();
  111. }
  112. }
  113. public int hp = 20;
  114. public int hpMax = 20;
  115. public System.Action onHpZero;
  116. public System.Action onHurtEffect;
  117. public void OnAttacked(Wolf wolf, int hurtValue) {
  118. if (hp <= 0) return;
  119. //看下是否满足受伤距离
  120. Vector3 p1 = wolf.transform.position;
  121. Vector3 p2 = hunterT.position;
  122. p1.y = p2.y = 0;
  123. float distance = Vector3.Distance(p1, p2);
  124. if (distance > 3) return;
  125. hp -= hurtValue;
  126. AudioMgr.ins.PlayAnimalEffect("man_injured", AudioMgr.GetAudioSource(this.gameMgr.gameObject));
  127. DoTweenHurt();
  128. onHurtEffect?.Invoke();
  129. if (hp <= 0) {
  130. hp = 0;
  131. if (onHpZero == null) {
  132. Sequence cb = DOTween.Sequence();
  133. cb.AppendInterval(1f);
  134. cb.AppendCallback(AnnounceGameOver);
  135. } else {
  136. onHpZero.Invoke();
  137. }
  138. }
  139. }
  140. Sequence hurtSeq = null;
  141. float hurtTweenV1 = 0;
  142. public void DoTweenHurt() {
  143. GameObject screenInjuredObject = this.gameMgr.transform.Find("ScreenInjured").gameObject;
  144. Image screenInjuredImage = screenInjuredObject.GetComponentInChildren<Image>();
  145. Color screenInjuredColor = new Color(1, 75f/255f, 75f/255f, 0);
  146. Blur blur = BowCamera.ins.transform.GetComponent<Blur>();
  147. if (hurtSeq != null) {
  148. if (hurtSeq.IsActive()) {
  149. hurtSeq.Complete();
  150. }
  151. hurtSeq = null;
  152. }
  153. hurtSeq = DOTween.Sequence();
  154. hurtSeq.AppendCallback(delegate() {
  155. hurtTweenV1 = 0;
  156. screenInjuredObject.SetActive(true);
  157. screenInjuredImage.color = screenInjuredColor;
  158. blur.enabled = true;
  159. blur.iterations = 0;
  160. });
  161. Tween toMax = DOTween.To(() => hurtTweenV1, value => {
  162. hurtTweenV1 = value;
  163. blur.iterations = Mathf.CeilToInt(hurtTweenV1 * 3f);
  164. screenInjuredColor.a = hurtTweenV1;
  165. screenInjuredImage.color = screenInjuredColor;
  166. }, 1f, 0.6f);
  167. hurtSeq.Append(toMax);
  168. Tween toMin = DOTween.To(() => hurtTweenV1, value => {
  169. hurtTweenV1 = value;
  170. blur.iterations = Mathf.CeilToInt(hurtTweenV1 * 3f);
  171. screenInjuredColor.a = hurtTweenV1;
  172. screenInjuredImage.color = screenInjuredColor;
  173. }, 0f, 0.6f);
  174. hurtSeq.Append(toMin);
  175. hurtSeq.AppendCallback(delegate() {
  176. hurtTweenV1 = 0;
  177. screenInjuredObject.SetActive(false);
  178. blur.enabled = false;
  179. });
  180. hurtSeq.SetUpdate(true);
  181. }
  182. public void QuicklyCreateAnimalForDebug() {
  183. baseCreateDistance = 15;
  184. createAnimalCountDownDefault = 10;
  185. }
  186. }