YejiHuntGameMode.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. if (nextLevel != null) {
  20. SetLevel(int.Parse(nextLevel));
  21. AddHuntGameView();
  22. } else {
  23. AddSelectLevelView();
  24. }
  25. // SetLevel(1);
  26. // gameMgr.transform.Find("HunterGameView").gameObject.SetActive(true);
  27. }
  28. //表示能创建的最大动物数量
  29. public int createCountMax = 2;
  30. //动物创建索引值,目前用于记录生成到第几只动物
  31. int createAnimalIndex = 0;
  32. //设置关卡级别
  33. public override void SetLevel(int level) {
  34. currentlevel = level;
  35. createCountMax = level + 1;
  36. animalCountMax = animalCount = createCountMax;
  37. arrowCountMax = arrowCount = animalCountMax * 3 * 2;
  38. time = arrowCountMax * 10;
  39. CreateAnimal();
  40. }
  41. //野鸡的基础飞行高度
  42. const float baseFlyHeightMin = 2;
  43. float baseFlyHeight = 1.5f;
  44. //野鸡将在哪棵树附近生成
  45. int createNearTreeIndex = 0;
  46. //能够在前几棵树徘徊
  47. int canFlyTreeCount = 4;
  48. void CreateAnimal() {
  49. if (createAnimalIndex >= createCountMax) return;
  50. //计算初始生成位置,后面的兔子生成越来越远,在标准方向的±30°内生成
  51. #region
  52. if (createAnimalIndex < 2) {
  53. createNearTreeIndex = Random.Range(0, 2);
  54. canFlyTreeCount = 2;
  55. } else if (createAnimalIndex < 4) {
  56. createNearTreeIndex = Random.Range(1, 3);
  57. canFlyTreeCount = 3;
  58. } else {
  59. createNearTreeIndex = Random.Range(2, 4);
  60. canFlyTreeCount = 4;
  61. }
  62. baseFlyHeight = baseFlyHeightMin + 0.5f * createAnimalIndex;
  63. TreeAreaRecorder.AreaInfo areaInfo = treeAreaRecorder.getValidAreaInfo(createNearTreeIndex);
  64. Vector3 newPos = CalculateNewPosByTreePos(animalsBaseT, flyPlaneHeight, areaInfo);
  65. #endregion
  66. GameObject animalObject = GameObject.Instantiate(animalPrefab, newPos, Quaternion.identity, animalsBaseT);
  67. animalObject.SetActive(true);
  68. Yeji yeji = animalObject.GetComponent<Yeji>();
  69. yeji.canFlyTreeCount = canFlyTreeCount;
  70. yeji.treeAreaRecorder = treeAreaRecorder;
  71. yeji.flyPlaneHeight = flyPlaneHeight;
  72. yeji.SetFlyHeight(baseFlyHeight);
  73. yeji.RotateByWorldY(Random.value * 360);
  74. yeji.onDie += delegate(Yeji yeji) {
  75. animalCount--;
  76. animalSet.Remove(yeji);
  77. CreateAnimal();
  78. };
  79. animalSet.Add(yeji);
  80. createAnimalIndex++;
  81. if (animalSet.Count < maxAnimalCountAtTheSameTime) {
  82. CreateAnimal();
  83. }
  84. }
  85. public static Vector3 CalculateNewPosByTreePos(Transform animalsBaseT, float flyPlaneHeight, TreeAreaRecorder.AreaInfo areaInfo) {
  86. Vector3 animalsBasePos = animalsBaseT.position;
  87. Vector3 treePos = areaInfo.transform.position;
  88. treePos.y = animalsBasePos.y;
  89. Vector3 vecAnimalsBaseToTree = treePos - animalsBasePos;
  90. float distanceH = vecAnimalsBaseToTree.magnitude;
  91. float newDistanceH = distanceH - areaInfo.radius - 1f;
  92. Vector3 displace = vecAnimalsBaseToTree.normalized * newDistanceH;
  93. displace.y += flyPlaneHeight;
  94. Vector3 newPos = animalsBaseT.position + displace;
  95. return newPos;
  96. }
  97. }