YejiHuntGameMode.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5. public class YejiHuntGameMode : ChallengeGameMode
  6. {
  7. //飞行平台的高度(距离Terrain地面的高度)
  8. float flyPlaneHeight;
  9. //关键树的记录器
  10. TreeAreaRecorder treeAreaRecorder;
  11. public YejiHuntGameMode(GameMgr gameMgr) : base(gameMgr) {
  12. this.animalTypeID = 1;
  13. animalPrefab = animalsBaseT.Find("Yeji").gameObject;
  14. this.flyPlaneHeight = gameMgr.transform.Find("FlyPlane").localPosition.y;
  15. treeAreaRecorder = gameMgr.transform.GetComponentInChildren<TreeAreaRecorder>().InitAndGet();
  16. BanBowReady();
  17. }
  18. public override void Start() {
  19. UnbanBowReady();
  20. Yeji.InitPreHeights();
  21. if (nextLevel != null) {
  22. SetLevel(int.Parse(nextLevel));
  23. AddHuntGameView();
  24. } else {
  25. AddSelectLevelView();
  26. // SetLevel(5);
  27. // AddHuntGameView();
  28. // GameObject.FindObjectOfType<BowCamera>().enabled = false;
  29. // GameObject.Find("ArmBow").SetActive(false);
  30. // GameObject.Find("CrossHair").SetActive(false);
  31. }
  32. }
  33. //表示能创建的最大动物数量
  34. public int createCountMax = 2;
  35. //动物创建索引值,目前用于记录生成到第几只动物
  36. int createAnimalIndex = 0;
  37. //设置关卡级别
  38. public override void SetLevel(int level) {
  39. currentlevel = level;
  40. createCountMax = level + 1;
  41. animalCountMax = animalCount = createCountMax;
  42. arrowCountMax = arrowCount = animalCountMax * 3 * 2;
  43. time = arrowCountMax * 10;
  44. CreateAnimal();
  45. }
  46. void CreateAnimal() {
  47. if (createAnimalIndex >= createCountMax) return;
  48. #region
  49. //野鸡将在哪棵树附近生成
  50. int createNearTreeIndex = 0;
  51. if (createAnimalIndex < 2) {
  52. createNearTreeIndex = createAnimalIndex;
  53. } else if (createAnimalIndex < 4) {
  54. createNearTreeIndex = 2;
  55. } else {
  56. createNearTreeIndex = 3;
  57. }
  58. float baseFlyHeight = Yeji.RandomOnePreHeight();
  59. TreeAreaRecorder.AreaInfo areaInfo = treeAreaRecorder.getValidAreaInfo(createNearTreeIndex);
  60. Vector3 newPos = CalculateNewPosByTreePos(animalsBaseT, flyPlaneHeight, areaInfo);
  61. #endregion
  62. GameObject animalObject = GameObject.Instantiate(animalPrefab, newPos, Quaternion.identity, animalsBaseT);
  63. animalObject.SetActive(true);
  64. Yeji yeji = animalObject.GetComponent<Yeji>();
  65. yeji.treeAreaRecorder = treeAreaRecorder;
  66. yeji.flyPlaneHeight = flyPlaneHeight;
  67. yeji.SetFlyHeight(baseFlyHeight);
  68. yeji.RotateByWorldY(Random.value * 360);
  69. yeji.onDie += delegate(Yeji yeji) {
  70. animalCount--;
  71. animalSet.Remove(yeji);
  72. //加一个延迟,避免新生成的鸟跟刚死的鸟出现同一位置时形成重叠
  73. Sequence seq = DOTween.Sequence();
  74. seq.AppendInterval(1.0f);
  75. seq.AppendCallback(CreateAnimal);
  76. };
  77. animalSet.Add(yeji);
  78. createAnimalIndex++;
  79. if (animalSet.Count < maxAnimalCountAtTheSameTime) {
  80. CreateAnimal();
  81. }
  82. }
  83. public static Vector3 CalculateNewPosByTreePos(Transform animalsBaseT, float flyPlaneHeight, TreeAreaRecorder.AreaInfo areaInfo) {
  84. Vector3 animalsBasePos = animalsBaseT.position;
  85. Vector3 treePos = areaInfo.transform.position;
  86. treePos.y = animalsBasePos.y;
  87. Vector3 vecAnimalsBaseToTree = treePos - animalsBasePos;
  88. float distanceH = vecAnimalsBaseToTree.magnitude;
  89. float newDistanceH = distanceH - areaInfo.radius - 1f;
  90. Vector3 displace = vecAnimalsBaseToTree.normalized * newDistanceH;
  91. displace.y += flyPlaneHeight;
  92. Vector3 newPos = animalsBaseT.position + displace;
  93. return newPos;
  94. }
  95. }