YejiHuntGameMode.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class YejiHuntGameMode : ChallengeGameMode
  5. {
  6. //飞行平台的高度(距离Terrain地面的高度)
  7. float flyPlaneHeight;
  8. //关键树的记录器
  9. TreeAreaRecorder treeAreaRecorder;
  10. public YejiHuntGameMode(GameMgr gameMgr) : base(gameMgr) {
  11. this.animalTypeID = 1;
  12. animalPrefab = animalsBaseT.Find("Yeji").gameObject;
  13. this.flyPlaneHeight = gameMgr.transform.Find("FlyPlane").localPosition.y;
  14. treeAreaRecorder = gameMgr.transform.GetComponentInChildren<TreeAreaRecorder>().InitAndGet();
  15. BanBowReady();
  16. }
  17. public override void Start() {
  18. UnbanBowReady();
  19. Yeji.InitPreHeights();
  20. Yeji.landLocked = false;
  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. //野鸡的基础飞行高度
  47. const float baseFlyHeightMin = 2;
  48. float baseFlyHeight = 1.5f;
  49. //野鸡将在哪棵树附近生成
  50. int createNearTreeIndex = 0;
  51. //能够在前几棵树徘徊
  52. int canFlyTreeCount = 4;
  53. void CreateAnimal() {
  54. if (createAnimalIndex >= createCountMax) return;
  55. //计算初始生成位置,后面的兔子生成越来越远,在标准方向的±30°内生成
  56. #region
  57. if (createAnimalIndex < 2) {
  58. createNearTreeIndex = createAnimalIndex;
  59. canFlyTreeCount = 2;
  60. } else if (createAnimalIndex < 4) {
  61. createNearTreeIndex = Random.Range(1, 3);
  62. canFlyTreeCount = 3;
  63. } else {
  64. createNearTreeIndex = Random.Range(2, 4);
  65. canFlyTreeCount = 4;
  66. }
  67. baseFlyHeight = Yeji.RandomOnePreHeight();
  68. TreeAreaRecorder.AreaInfo areaInfo = treeAreaRecorder.getValidAreaInfo(createNearTreeIndex);
  69. Vector3 newPos = CalculateNewPosByTreePos(animalsBaseT, flyPlaneHeight, areaInfo);
  70. #endregion
  71. GameObject animalObject = GameObject.Instantiate(animalPrefab, newPos, Quaternion.identity, animalsBaseT);
  72. animalObject.SetActive(true);
  73. Yeji yeji = animalObject.GetComponent<Yeji>();
  74. yeji.treeAreaRecorder = treeAreaRecorder;
  75. yeji.canFlyTreeCount = canFlyTreeCount;
  76. yeji.flyPlaneHeight = flyPlaneHeight;
  77. yeji.SetFlyHeight(baseFlyHeight);
  78. yeji.RotateByWorldY(Random.value * 360);
  79. yeji.onDie += delegate(Yeji yeji) {
  80. animalCount--;
  81. animalSet.Remove(yeji);
  82. 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. }