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