using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.ImageEffects; using DG.Tweening; public class WolfHuntGameMode : ChallengeGameMode { public WolfHuntGameMode(GameMgr gameMgr) : base(gameMgr) { this.animalTypeID = 2; animalPrefab = animalsBaseT.Find("Wolf").gameObject; // AddSelectLevelView(); this.time = 1800; this.arrowCount = 100; this.SetLevel(5); gameMgr.transform.Find("HunterGameView").gameObject.SetActive(true); } //动物的创建列表(数组元素表示动物颜色样式) int[] animalCreateList; //动物创建列表的当前索引值 int animalCreateIndex = 0; //设置关卡级别 public override void SetLevel(int level) { if (level == 1) { animalCreateList = new int[]{1, 1}; } else if (level == 2) { animalCreateList = new int[]{1, 1, 1, 1}; } else if (level == 3) { animalCreateList = new int[]{2, 2}; } else if (level == 4) { animalCreateList = new int[]{1, 1, 2, 2}; } else if (level == 5) { animalCreateList = new int[]{1, 1, 1, 1, 2, 2, 2, 2}; } if (animalCreateList != null) animalCount = animalCreateList.Length; } float baseCreateDistance = 25; float plusCreateDistance = 0; void CreateAnimal() { if (animalCreateIndex >= animalCreateList.Length) return; int animalStyleID = animalCreateList[animalCreateIndex++]; //计算初始生成位置,后面的兔子生成越来越远,在标准方向的±30°内生成 #region float createDistance = baseCreateDistance; baseCreateDistance += plusCreateDistance; Vector3 standardPointer = animalsBaseT.forward; Vector3 displace = standardPointer * createDistance; displace = Quaternion.AngleAxis(15 - Random.value * 30, Vector3.up) * displace; Vector3 newPos = animalsBaseT.position + displace; #endregion GameObject animalObject = GameObject.Instantiate(animalPrefab, newPos, Quaternion.identity, animalsBaseT); animalObject.SetActive(true); Wolf wolf = animalObject.GetComponent(); wolf.state.hp = animalStyleID == 1 ? 5 : 8; wolf.ChangeColorByType(animalStyleID); wolf.RotateByWorldY(Random.value * 360); wolf.onDie += delegate(Wolf wf) { animalCount--; animalSet.Remove(wf); }; wolf.onAttack += OnAttacked; animalSet.Add(wolf); } float timeForCreateAnimal = 0; public override void Update() { base.Update(); timeForCreateAnimal += Time.deltaTime; if ((int)(timeForCreateAnimal / 6) >= animalCreateIndex) { CreateAnimal(); } } public int hp = 20; public int hpMax = 20; public void OnAttacked(Wolf wolf, int hurtValue) { if (hp <= 0) return; AudioMgr.ins.PlayAnimalEffect("man_injured", AudioMgr.GetAudioSource(this.gameMgr.gameObject)); Vector3 p1 = wolf.transform.position; Vector3 p2 = hunterT.position; p1.y = p2.y = 0; float distance = Vector3.Distance(p1, p2); if (distance > 3) return; hp -= hurtValue; DoTweenHurt(); if (hp <= 0) { hp = 0; Sequence cb = DOTween.Sequence(); cb.AppendInterval(1f); cb.AppendCallback(AnnounceGameOver); } } Sequence hurtSeq = null; float iterations; public void DoTweenHurt() { iterations = 0; Blur blur = BowCamera.ins.transform.GetComponent(); blur.iterations = 0; blur.enabled = true; if (hurtSeq != null) { if (hurtSeq.IsActive()) { hurtSeq.Complete(); } hurtSeq = null; } hurtSeq = DOTween.Sequence(); Tween toMax = DOTween.To(() => iterations, value => { iterations = value; blur.iterations = Mathf.CeilToInt(iterations); }, 3f, 0.6f); hurtSeq.Append(toMax); Tween toMin = DOTween.To(() => iterations, value => { iterations = value; blur.iterations = Mathf.CeilToInt(iterations); }, 0f, 0.6f); hurtSeq.Append(toMin); hurtSeq.AppendCallback(delegate() { iterations = 0; blur.iterations = 0; blur.enabled = false; }); hurtSeq.SetUpdate(true); } }