using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; /* 野鸡关卡-单人模式 */ 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(); Yeji.InitPreHeights(); if (nextLevel != null) { SetLevel(int.Parse(nextLevel)); AddHuntGameView(); } else { AddSelectLevelView(); // SetLevel(5); // AddHuntGameView(); // GameObject.FindObjectOfType().enabled = false; // GameObject.Find("ArmBow").SetActive(false); // GameObject.Find("CrossHair").SetActive(false); } } //表示能创建的最大动物数量 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(); } public bool banCreateAnimal = false; void CreateAnimal() { if (banCreateAnimal) return; if (createAnimalIndex >= createCountMax) return; #region //野鸡将在哪棵树附近生成 int createNearTreeIndex = 0; // if (createAnimalIndex < 2) { // createNearTreeIndex = createAnimalIndex; // } else if (createAnimalIndex < 4) { // createNearTreeIndex = 2; // } else if (createAnimalIndex < 5) { // createNearTreeIndex = 1; // } else { // createNearTreeIndex = 0; // } createNearTreeIndex = createAnimalIndex % 2; 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.onlineHandler.uid = createAnimalIndex + 1; yeji.treeAreaRecorder = treeAreaRecorder; yeji.flyPlaneHeight = flyPlaneHeight; float baseFlyHeight = Yeji.RandomOnePreHeight(yeji); yeji.SetFlyHeight(baseFlyHeight); yeji.RotateByWorldY(Random.value * 360); yeji.onDie += delegate(Yeji yeji) { animalCount--; animalSet.Remove(yeji); //加一个延迟,避免新生成的鸟跟刚死的鸟出现同一位置时形成重叠 Sequence seq = DOTween.Sequence(); seq.AppendInterval(1.0f); seq.AppendCallback(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; } }