using System.Collections; using System.Collections.Generic; using UnityEngine; public class YejiHuntGameMode : ChallengeGameMode { //飞行平台的高度(距离Terrain地面的高度) float flyPlaneHeight; //关键树的记录器 TreeAreaRecorder treeAreaRecorder; public YejiHuntGameMode(GameMgr gameMgr) : base(gameMgr) { this.animalTypeID = 1; animalPrefab = animalsBaseT.Find("Yeji").gameObject; this.flyPlaneHeight = gameMgr.transform.Find("FlyPlane").localPosition.y; treeAreaRecorder = gameMgr.transform.GetComponentInChildren().InitAndGet(); BanBowReady(); } public override void Start() { UnbanBowReady(); if (nextLevel != null) { SetLevel(int.Parse(nextLevel)); AddHuntGameView(); } else { AddSelectLevelView(); } // SetLevel(1); // gameMgr.transform.Find("HunterGameView").gameObject.SetActive(true); } //表示能创建的最大动物数量 public int createCountMax = 2; //动物创建索引值,目前用于记录生成到第几只动物 int createAnimalIndex = 0; //设置关卡级别 public override void SetLevel(int level) { currentlevel = level; createCountMax = level + 1; animalCountMax = animalCount = createCountMax; arrowCountMax = arrowCount = animalCountMax * 3 * 2; time = arrowCountMax * 10; CreateAnimal(); } //野鸡的基础飞行高度 const float baseFlyHeightMin = 2; float baseFlyHeight = 1.5f; //野鸡将在哪棵树附近生成 int createNearTreeIndex = 0; //能够在前几棵树徘徊 int canFlyTreeCount = 4; void CreateAnimal() { if (createAnimalIndex >= createCountMax) return; //计算初始生成位置,后面的兔子生成越来越远,在标准方向的±30°内生成 #region if (createAnimalIndex < 2) { createNearTreeIndex = Random.Range(0, 2); canFlyTreeCount = 2; } else if (createAnimalIndex < 4) { createNearTreeIndex = Random.Range(1, 3); canFlyTreeCount = 3; } else { createNearTreeIndex = Random.Range(2, 4); canFlyTreeCount = 4; } baseFlyHeight = baseFlyHeightMin + 0.5f * createAnimalIndex; TreeAreaRecorder.AreaInfo areaInfo = treeAreaRecorder.getValidAreaInfo(createNearTreeIndex); Vector3 newPos = CalculateNewPosByTreePos(animalsBaseT, flyPlaneHeight, areaInfo); #endregion GameObject animalObject = GameObject.Instantiate(animalPrefab, newPos, Quaternion.identity, animalsBaseT); animalObject.SetActive(true); Yeji yeji = animalObject.GetComponent(); yeji.canFlyTreeCount = canFlyTreeCount; yeji.treeAreaRecorder = treeAreaRecorder; yeji.flyPlaneHeight = flyPlaneHeight; yeji.SetFlyHeight(baseFlyHeight); yeji.RotateByWorldY(Random.value * 360); yeji.onDie += delegate(Yeji yeji) { animalCount--; animalSet.Remove(yeji); CreateAnimal(); }; animalSet.Add(yeji); createAnimalIndex++; if (animalSet.Count < maxAnimalCountAtTheSameTime) { CreateAnimal(); } } public static Vector3 CalculateNewPosByTreePos(Transform animalsBaseT, float flyPlaneHeight, TreeAreaRecorder.AreaInfo areaInfo) { Vector3 animalsBasePos = animalsBaseT.position; Vector3 treePos = areaInfo.transform.position; treePos.y = animalsBasePos.y; Vector3 vecAnimalsBaseToTree = treePos - animalsBasePos; float distanceH = vecAnimalsBaseToTree.magnitude; float newDistanceH = distanceH - areaInfo.radius - 1f; Vector3 displace = vecAnimalsBaseToTree.normalized * newDistanceH; displace.y += flyPlaneHeight; Vector3 newPos = animalsBaseT.position + displace; return newPos; } }