using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class RabbitHuntGameMode : GameMode { GameObject rabbitPrefab; Transform hunter; Transform animalsBaseT; public RabbitHuntGameMode(GameMgr gameMgr) : base(gameMgr) { animalsBaseT = GameObject.Find("Animals").transform; rabbitPrefab = animalsBaseT.Find("Rabbit").gameObject; hunter = this.gameMgr.GetComponentInChildren().transform; lastCreateDistance = GetMeasureVec().magnitude; this.gameMgr.transform.Find("RabbitHuntGameSelectLevelView").gameObject.SetActive(true); } HashSet rabbits = new HashSet(); float lastCreateDistance = 0; float time = 60; Vector3 GetMeasureVec() { Vector3 hunterPos = hunter.position; Vector3 animalsBasePos = animalsBaseT.position; hunterPos.y = animalsBasePos.y; return animalsBasePos - hunterPos; } void CreateRabbit() { if (animalTypeCreateIndex >= animalTypeCreateList.Length) return; int animalType = animalTypeCreateList[animalTypeCreateIndex++]; Vector3 measureVec = GetMeasureVec(); Vector3 displace = measureVec.normalized * lastCreateDistance; lastCreateDistance += 1.5f; Vector3 newPos = hunter.position + Quaternion.AngleAxis(30 - Random.value * 60, Vector3.up) * displace; GameObject o = GameObject.Instantiate(rabbitPrefab, newPos, Quaternion.identity, animalsBaseT); o.SetActive(true); Rabbit rabbit = o.GetComponent(); rabbit.hp = animalType; rabbit.ChangeColorByType(animalType); rabbit.RandomRotate(Random.value * 360); rabbit.onDie += delegate(Rabbit rb) { killCount++; rabbits.Remove(rb); CreateRabbit(); }; rabbits.Add(rabbit); if (rabbits.Count < maxAnimalCountAtTheSameTime) { CreateRabbit(); } } int killCount = 0; int maxAnimalCountAtTheSameTime = 2; int[] animalTypeCreateList; int animalTypeCreateIndex = 0; //设置关卡级别 public void SetLevel(int level) { if (level == 1) { animalTypeCreateList = new int[]{1, 1}; } else if (level == 2) { animalTypeCreateList = new int[]{2, 2}; } else if (level == 3) { animalTypeCreateList = new int[]{3, 3}; } else if (level == 4) { animalTypeCreateList = new int[]{1, 1, 2, 2}; } else if (level == 5) { animalTypeCreateList = new int[]{1, 1, 1, 3, 3, 3}; } CreateRabbit(); } public override bool DoNextShoot() { if (GameMgr.ins.gameOver) return false; if (animalTypeCreateIndex >= animalTypeCreateList.Length && rabbits.Count == 0) { gameMgr.gameOver = true; gameMgr.StopGame(); this.gameMgr.transform.Find("HunterGameSettleView").gameObject.SetActive(true); return false; } return true; } public override object[] Settle() { return new object[]{GetSurplusAnimalCount() <= 0 ? "胜利" : "失败"}; } public override void Update() { if (gameMgr.gameOver || pauseTimeCounting) return; if (this.time > 0) { this.time -= Time.deltaTime; } else { this.time = 0; gameMgr.gameOver = true; gameMgr.StopGame(); //添加结算界面 this.gameMgr.transform.Find("HunterGameSettleView").gameObject.SetActive(true); } } public string GetTimeStr() { int seconds = (int) Mathf.Ceil(this.time); string str = ""; int m = seconds / 60; if (m < 10) { str += 0; } str += m; str += " : "; int s = seconds % 60; if (s < 10) { str += 0; } str += s; return str; } public int GetSurplusAnimalCount() { return this.animalTypeCreateList.Length - killCount; } }