WolfHuntGameMode.cs 6.3 KB

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