WolfHuntGameMode.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. };
  77. wolf.onAttack += OnAttacked;
  78. animalSet.Add(wolf);
  79. }
  80. bool canStartCreateAniaml = false;
  81. float timeForCreateAnimal = 0;
  82. public override void Update()
  83. {
  84. base.Update();
  85. if (!gameMgr.gameOver && canStartCreateAniaml) {
  86. timeForCreateAnimal += Time.deltaTime;
  87. if ((int)(timeForCreateAnimal / 6) >= animalCreateIndex) {
  88. CreateAnimal();
  89. }
  90. }
  91. }
  92. public int hp = 20;
  93. public int hpMax = 20;
  94. public void OnAttacked(Wolf wolf, int hurtValue) {
  95. if (hp <= 0) return;
  96. AudioMgr.ins.PlayAnimalEffect("man_injured", AudioMgr.GetAudioSource(this.gameMgr.gameObject));
  97. //看下是否满足受伤距离
  98. Vector3 p1 = wolf.transform.position;
  99. Vector3 p2 = hunterT.position;
  100. p1.y = p2.y = 0;
  101. float distance = Vector3.Distance(p1, p2);
  102. if (distance > 3) return;
  103. hp -= hurtValue;
  104. DoTweenHurt();
  105. if (hp <= 0) {
  106. hp = 0;
  107. Sequence cb = DOTween.Sequence();
  108. cb.AppendInterval(1f);
  109. cb.AppendCallback(AnnounceGameOver);
  110. }
  111. }
  112. Sequence hurtSeq = null;
  113. float hurtTweenV1 = 0;
  114. public void DoTweenHurt() {
  115. GameObject screenInjuredObject = this.gameMgr.transform.Find("ScreenInjured").gameObject;
  116. Image screenInjuredImage = screenInjuredObject.GetComponentInChildren<Image>();
  117. Color screenInjuredColor = new Color(1, 75f/255f, 75f/255f, 0);
  118. Blur blur = BowCamera.ins.transform.GetComponent<Blur>();
  119. if (hurtSeq != null) {
  120. if (hurtSeq.IsActive()) {
  121. hurtSeq.Complete();
  122. }
  123. hurtSeq = null;
  124. }
  125. hurtSeq = DOTween.Sequence();
  126. hurtSeq.AppendCallback(delegate() {
  127. hurtTweenV1 = 0;
  128. screenInjuredObject.SetActive(true);
  129. screenInjuredImage.color = screenInjuredColor;
  130. blur.enabled = true;
  131. blur.iterations = 0;
  132. });
  133. Tween toMax = DOTween.To(() => hurtTweenV1, value => {
  134. hurtTweenV1 = value;
  135. blur.iterations = Mathf.CeilToInt(hurtTweenV1 * 3f);
  136. screenInjuredColor.a = hurtTweenV1;
  137. screenInjuredImage.color = screenInjuredColor;
  138. }, 1f, 0.6f);
  139. hurtSeq.Append(toMax);
  140. Tween toMin = DOTween.To(() => hurtTweenV1, value => {
  141. hurtTweenV1 = value;
  142. blur.iterations = Mathf.CeilToInt(hurtTweenV1 * 3f);
  143. screenInjuredColor.a = hurtTweenV1;
  144. screenInjuredImage.color = screenInjuredColor;
  145. }, 0f, 0.6f);
  146. hurtSeq.Append(toMin);
  147. hurtSeq.AppendCallback(delegate() {
  148. hurtTweenV1 = 0;
  149. screenInjuredObject.SetActive(false);
  150. blur.enabled = false;
  151. });
  152. hurtSeq.SetUpdate(true);
  153. }
  154. }