WolfHuntGameMode.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityStandardAssets.ImageEffects;
  5. using DG.Tweening;
  6. public class WolfHuntGameMode : ChallengeGameMode
  7. {
  8. public WolfHuntGameMode(GameMgr gameMgr) : base(gameMgr) {
  9. this.animalTypeID = 2;
  10. animalPrefab = animalsBaseT.Find("Wolf").gameObject;
  11. // AddSelectLevelView();
  12. this.time = 1800;
  13. this.arrowCount = 100;
  14. this.SetLevel(5);
  15. gameMgr.transform.Find("HunterGameView").gameObject.SetActive(true);
  16. }
  17. //动物的创建列表(数组元素表示动物颜色样式)
  18. int[] animalCreateList;
  19. //动物创建列表的当前索引值
  20. int animalCreateIndex = 0;
  21. //设置关卡级别
  22. public override void SetLevel(int level) {
  23. if (level == 1) {
  24. animalCreateList = new int[]{1, 1};
  25. }
  26. else if (level == 2) {
  27. animalCreateList = new int[]{1, 1, 1, 1};
  28. }
  29. else if (level == 3) {
  30. animalCreateList = new int[]{2, 2};
  31. }
  32. else if (level == 4) {
  33. animalCreateList = new int[]{1, 1, 2, 2};
  34. }
  35. else if (level == 5) {
  36. animalCreateList = new int[]{1, 1, 1, 1, 2, 2, 2, 2};
  37. }
  38. if (animalCreateList != null) animalCount = animalCreateList.Length;
  39. }
  40. float baseCreateDistance = 25;
  41. float plusCreateDistance = 0;
  42. void CreateAnimal() {
  43. if (animalCreateIndex >= animalCreateList.Length) return;
  44. int animalStyleID = animalCreateList[animalCreateIndex++];
  45. //计算初始生成位置,后面的兔子生成越来越远,在标准方向的±30°内生成
  46. #region
  47. float createDistance = baseCreateDistance;
  48. baseCreateDistance += plusCreateDistance;
  49. Vector3 standardPointer = animalsBaseT.forward;
  50. Vector3 displace = standardPointer * createDistance;
  51. displace = Quaternion.AngleAxis(15 - Random.value * 30, Vector3.up) * displace;
  52. Vector3 newPos = animalsBaseT.position + displace;
  53. #endregion
  54. GameObject animalObject = GameObject.Instantiate(animalPrefab, newPos, Quaternion.identity, animalsBaseT);
  55. animalObject.SetActive(true);
  56. Wolf wolf = animalObject.GetComponent<Wolf>();
  57. wolf.state.hp = animalStyleID == 1 ? 5 : 8;
  58. wolf.ChangeColorByType(animalStyleID);
  59. wolf.RotateByWorldY(Random.value * 360);
  60. wolf.onDie += delegate(Wolf wf) {
  61. animalCount--;
  62. animalSet.Remove(wf);
  63. };
  64. wolf.onAttack += OnAttacked;
  65. animalSet.Add(wolf);
  66. }
  67. float timeForCreateAnimal = 0;
  68. public override void Update()
  69. {
  70. base.Update();
  71. timeForCreateAnimal += Time.deltaTime;
  72. if ((int)(timeForCreateAnimal / 6) >= animalCreateIndex) {
  73. CreateAnimal();
  74. }
  75. }
  76. public int hp = 20;
  77. public int hpMax = 20;
  78. public void OnAttacked(Wolf wolf, int hurtValue) {
  79. if (hp <= 0) return;
  80. AudioMgr.ins.PlayAnimalEffect("man_injured", AudioMgr.GetAudioSource(this.gameMgr.gameObject));
  81. Vector3 p1 = wolf.transform.position;
  82. Vector3 p2 = hunterT.position;
  83. p1.y = p2.y = 0;
  84. float distance = Vector3.Distance(p1, p2);
  85. if (distance > 3) return;
  86. hp -= hurtValue;
  87. DoTweenHurt();
  88. if (hp <= 0) {
  89. hp = 0;
  90. Sequence cb = DOTween.Sequence();
  91. cb.AppendInterval(1f);
  92. cb.AppendCallback(AnnounceGameOver);
  93. }
  94. }
  95. Sequence hurtSeq = null;
  96. float iterations;
  97. public void DoTweenHurt() {
  98. iterations = 0;
  99. Blur blur = BowCamera.ins.transform.GetComponent<Blur>();
  100. blur.iterations = 0;
  101. blur.enabled = true;
  102. if (hurtSeq != null) {
  103. if (hurtSeq.IsActive()) {
  104. hurtSeq.Complete();
  105. }
  106. hurtSeq = null;
  107. }
  108. hurtSeq = DOTween.Sequence();
  109. Tween toMax = DOTween.To(() => iterations, value => {
  110. iterations = value;
  111. blur.iterations = Mathf.CeilToInt(iterations);
  112. }, 3f, 0.6f);
  113. hurtSeq.Append(toMax);
  114. Tween toMin = DOTween.To(() => iterations, value => {
  115. iterations = value;
  116. blur.iterations = Mathf.CeilToInt(iterations);
  117. }, 0f, 0.6f);
  118. hurtSeq.Append(toMin);
  119. hurtSeq.AppendCallback(delegate() {
  120. iterations = 0;
  121. blur.iterations = 0;
  122. blur.enabled = false;
  123. });
  124. hurtSeq.SetUpdate(true);
  125. }
  126. }