YejiHuntGameMode.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. public bool banCreateAnimal = false;
  47. void CreateAnimal() {
  48. if (banCreateAnimal) return;
  49. if (createAnimalIndex >= createCountMax) return;
  50. #region
  51. //野鸡将在哪棵树附近生成
  52. int createNearTreeIndex = 0;
  53. if (createAnimalIndex < 2) {
  54. createNearTreeIndex = createAnimalIndex;
  55. } else if (createAnimalIndex < 4) {
  56. createNearTreeIndex = 2;
  57. } else {
  58. createNearTreeIndex = 3;
  59. }
  60. TreeAreaRecorder.AreaInfo areaInfo = treeAreaRecorder.getValidAreaInfo(createNearTreeIndex);
  61. Vector3 newPos = CalculateNewPosByTreePos(animalsBaseT, flyPlaneHeight, areaInfo);
  62. #endregion
  63. GameObject animalObject = GameObject.Instantiate(animalPrefab, newPos, Quaternion.identity, animalsBaseT);
  64. animalObject.SetActive(true);
  65. Yeji yeji = animalObject.GetComponent<Yeji>();
  66. yeji.onlineHandler.uid = createAnimalIndex + 1;
  67. yeji.treeAreaRecorder = treeAreaRecorder;
  68. yeji.flyPlaneHeight = flyPlaneHeight;
  69. float baseFlyHeight = Yeji.RandomOnePreHeight(yeji);
  70. yeji.SetFlyHeight(baseFlyHeight);
  71. yeji.RotateByWorldY(Random.value * 360);
  72. yeji.onDie += delegate(Yeji yeji) {
  73. animalCount--;
  74. animalSet.Remove(yeji);
  75. //加一个延迟,避免新生成的鸟跟刚死的鸟出现同一位置时形成重叠
  76. Sequence seq = DOTween.Sequence();
  77. seq.AppendInterval(1.0f);
  78. seq.AppendCallback(CreateAnimal);
  79. };
  80. animalSet.Add(yeji);
  81. createAnimalIndex++;
  82. if (animalSet.Count < maxAnimalCountAtTheSameTime) {
  83. CreateAnimal();
  84. }
  85. }
  86. public static Vector3 CalculateNewPosByTreePos(Transform animalsBaseT, float flyPlaneHeight, TreeAreaRecorder.AreaInfo areaInfo) {
  87. Vector3 animalsBasePos = animalsBaseT.position;
  88. Vector3 treePos = areaInfo.transform.position;
  89. treePos.y = animalsBasePos.y;
  90. Vector3 vecAnimalsBaseToTree = treePos - animalsBasePos;
  91. float distanceH = vecAnimalsBaseToTree.magnitude;
  92. float newDistanceH = distanceH - areaInfo.radius - 1f;
  93. Vector3 displace = vecAnimalsBaseToTree.normalized * newDistanceH;
  94. displace.y += flyPlaneHeight;
  95. Vector3 newPos = animalsBaseT.position + displace;
  96. return newPos;
  97. }
  98. }