YejiHuntGameMode.cs 3.7 KB

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