YejiHuntGameMode.cs 4.3 KB

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