| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- 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<ArmBow>().transform;
- lastCreateDistance = GetMeasureVec().magnitude;
- this.gameMgr.transform.Find("RabbitHuntGameSelectLevelView").gameObject.SetActive(true);
- }
- HashSet<Rabbit> rabbits = new HashSet<Rabbit>();
- 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>();
- 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;
- }
- }
|