using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.ImageEffects; using DG.Tweening; using UnityEngine.UI; /* 野狼关卡-单人模式 */ public class WolfHuntGameMode : ChallengeGameMode { public WolfHuntGameMode(GameMgr gameMgr) : base(gameMgr) { this.animalTypeID = 2; animalPrefab = animalsBaseT.Find("Wolf").gameObject; this.time = 18000; this.arrowCount = 10000; // this.SetLevel(5); // gameMgr.transform.Find("HunterGameView").gameObject.SetActive(true); BanBowReady(); } public override void Start() { UnbanBowReady(); GameMgr.ins.transform.Find("WolfActGrid").gameObject.SetActive(true); if (nextLevel != null) { SetLevel(int.Parse(nextLevel)); AddHuntGameView(); } else { AddSelectLevelView(); } } //动物的创建列表(数组元素表示动物颜色样式) int[] animalCreateList; //动物创建列表的当前索引值 int animalCreateIndex = 0; //设置关卡级别 public override void SetLevel(int level) { currentlevel = 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) animalCountMax = animalCount = animalCreateList.Length; canStartCreateAniaml = true; } float baseCreateDistance = 25; // float baseCreateDistance = 15; float plusCreateDistance = 0; public bool banCreateAnimal = false; void CreateAnimal() { if (banCreateAnimal) return; 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(Random.Range(-14f, 14f), 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(); WolfActGrid.ins.areaMatrix.occupyPos(newPos, wolf); wolf.transform.position = newPos; wolf.hp = animalStyleID == 1 ? 5 : 8; wolf.onlineHandler.uid = animalCreateIndex + 1; wolf.ChangeColorByType(animalStyleID); wolf.RotateByWorldY(Random.value * 360); wolf.onDie += delegate(Wolf wf) { animalCount--; animalSet.Remove(wf); CreateAnimalIgnoreAndClearCountDown(); }; wolf.onAttack += OnAttacked; animalSet.Add(wolf); } bool canStartCreateAniaml = false; //等选完关卡才能开始倒计时 float createAnimalCountDown = 0; //生产动物的倒计时 float createAnimalCountDownDefault = 30; public override void Update() { base.Update(); CheckAutoCreateAnimalByUpdate(Time.deltaTime); } public void CheckAutoCreateAnimalByUpdate(float dt) { //固定时间间隔生成一头狼 if (!gameMgr.gameOver && canStartCreateAniaml) { createAnimalCountDown -= dt; if (createAnimalCountDown <= 0) { createAnimalCountDown = createAnimalCountDownDefault; CreateAnimal(); } } } //当动物死亡后,如果场上没有活着的动物,就立即生成一头狼,并清空生产动物的倒计时 void CreateAnimalIgnoreAndClearCountDown() { if (animalSet.Count == 0) { createAnimalCountDown = createAnimalCountDownDefault; CreateAnimal(); } } public int hp = 20; public int hpMax = 20; public System.Action onHpZero; public System.Action onHurtEffect; public void OnAttacked(Wolf wolf, int hurtValue) { if (hp <= 0) return; //看下是否满足受伤距离 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; AudioMgr.ins.PlayAnimalEffect("man_injured", AudioMgr.GetAudioSource(this.gameMgr.gameObject)); DoTweenHurt(); onHurtEffect?.Invoke(); if (hp <= 0) { hp = 0; if (onHpZero == null) { Sequence cb = DOTween.Sequence(); cb.AppendInterval(1f); cb.AppendCallback(AnnounceGameOver); } else { onHpZero.Invoke(); } } } Sequence hurtSeq = null; float hurtTweenV1 = 0; public void DoTweenHurt() { GameObject screenInjuredObject = this.gameMgr.transform.Find("ScreenInjured").gameObject; Image screenInjuredImage = screenInjuredObject.GetComponentInChildren(); Color screenInjuredColor = new Color(1, 75f/255f, 75f/255f, 0); Blur blur = BowCamera.ins.transform.GetComponent(); if (hurtSeq != null) { if (hurtSeq.IsActive()) { hurtSeq.Complete(); } hurtSeq = null; } hurtSeq = DOTween.Sequence(); hurtSeq.AppendCallback(delegate() { hurtTweenV1 = 0; screenInjuredObject.SetActive(true); screenInjuredImage.color = screenInjuredColor; blur.enabled = true; blur.iterations = 0; }); Tween toMax = DOTween.To(() => hurtTweenV1, value => { hurtTweenV1 = value; blur.iterations = Mathf.CeilToInt(hurtTweenV1 * 3f); screenInjuredColor.a = hurtTweenV1; screenInjuredImage.color = screenInjuredColor; }, 1f, 0.6f); hurtSeq.Append(toMax); Tween toMin = DOTween.To(() => hurtTweenV1, value => { hurtTweenV1 = value; blur.iterations = Mathf.CeilToInt(hurtTweenV1 * 3f); screenInjuredColor.a = hurtTweenV1; screenInjuredImage.color = screenInjuredColor; }, 0f, 0.6f); hurtSeq.Append(toMin); hurtSeq.AppendCallback(delegate() { hurtTweenV1 = 0; screenInjuredObject.SetActive(false); blur.enabled = false; }); hurtSeq.SetUpdate(true); } public void QuicklyCreateAnimalForDebug() { baseCreateDistance = 15; createAnimalCountDownDefault = 10; } }