WolfHuntGameMode.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. public override void Update()
  84. {
  85. base.Update();
  86. //固定时间间隔生成一头狼
  87. if (!gameMgr.gameOver && canStartCreateAniaml) {
  88. createAnimalCountDown -= Time.deltaTime;
  89. if (createAnimalCountDown <= 0) {
  90. createAnimalCountDown = 30;
  91. CreateAnimal();
  92. }
  93. }
  94. }
  95. //当动物死亡后,如果场上没有活着的动物,就立即生成一头狼,并清空生产动物的倒计时
  96. void CreateAnimalIgnoreAndClearCountDown() {
  97. if (animalSet.Count == 0) {
  98. createAnimalCountDown = 0;
  99. CreateAnimal();
  100. }
  101. }
  102. public int hp = 20;
  103. public int hpMax = 20;
  104. public void OnAttacked(Wolf wolf, int hurtValue) {
  105. if (hp <= 0) return;
  106. //看下是否满足受伤距离
  107. Vector3 p1 = wolf.transform.position;
  108. Vector3 p2 = hunterT.position;
  109. p1.y = p2.y = 0;
  110. float distance = Vector3.Distance(p1, p2);
  111. if (distance > 3) return;
  112. hp -= hurtValue;
  113. AudioMgr.ins.PlayAnimalEffect("man_injured", AudioMgr.GetAudioSource(this.gameMgr.gameObject));
  114. DoTweenHurt();
  115. if (hp <= 0) {
  116. hp = 0;
  117. Sequence cb = DOTween.Sequence();
  118. cb.AppendInterval(1f);
  119. cb.AppendCallback(AnnounceGameOver);
  120. }
  121. }
  122. Sequence hurtSeq = null;
  123. float hurtTweenV1 = 0;
  124. public void DoTweenHurt() {
  125. GameObject screenInjuredObject = this.gameMgr.transform.Find("ScreenInjured").gameObject;
  126. Image screenInjuredImage = screenInjuredObject.GetComponentInChildren<Image>();
  127. Color screenInjuredColor = new Color(1, 75f/255f, 75f/255f, 0);
  128. Blur blur = BowCamera.ins.transform.GetComponent<Blur>();
  129. if (hurtSeq != null) {
  130. if (hurtSeq.IsActive()) {
  131. hurtSeq.Complete();
  132. }
  133. hurtSeq = null;
  134. }
  135. hurtSeq = DOTween.Sequence();
  136. hurtSeq.AppendCallback(delegate() {
  137. hurtTweenV1 = 0;
  138. screenInjuredObject.SetActive(true);
  139. screenInjuredImage.color = screenInjuredColor;
  140. blur.enabled = true;
  141. blur.iterations = 0;
  142. });
  143. Tween toMax = DOTween.To(() => hurtTweenV1, value => {
  144. hurtTweenV1 = value;
  145. blur.iterations = Mathf.CeilToInt(hurtTweenV1 * 3f);
  146. screenInjuredColor.a = hurtTweenV1;
  147. screenInjuredImage.color = screenInjuredColor;
  148. }, 1f, 0.6f);
  149. hurtSeq.Append(toMax);
  150. Tween toMin = DOTween.To(() => hurtTweenV1, value => {
  151. hurtTweenV1 = value;
  152. blur.iterations = Mathf.CeilToInt(hurtTweenV1 * 3f);
  153. screenInjuredColor.a = hurtTweenV1;
  154. screenInjuredImage.color = screenInjuredColor;
  155. }, 0f, 0.6f);
  156. hurtSeq.Append(toMin);
  157. hurtSeq.AppendCallback(delegate() {
  158. hurtTweenV1 = 0;
  159. screenInjuredObject.SetActive(false);
  160. blur.enabled = false;
  161. });
  162. hurtSeq.SetUpdate(true);
  163. }
  164. }