YejiHuntGameMode.cs 4.1 KB

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