From e785622cb685fcebca2902ded86613e1dbc3da66 Mon Sep 17 00:00:00 2001 From: wangguan <wangguan@kt007.com> Date: Wed, 23 Dec 2020 17:16:53 +0800 Subject: [PATCH] 使用倒计时特效 --- Assets/Scripts/TowerDefense/UI/HUD/EndlessRandomTower.cs | 321 +++++++++++++++++++++++++++++++++-------------------- 1 files changed, 199 insertions(+), 122 deletions(-) diff --git a/Assets/Scripts/TowerDefense/UI/HUD/EndlessRandomTower.cs b/Assets/Scripts/TowerDefense/UI/HUD/EndlessRandomTower.cs index 35e5645..8a7dd22 100644 --- a/Assets/Scripts/TowerDefense/UI/HUD/EndlessRandomTower.cs +++ b/Assets/Scripts/TowerDefense/UI/HUD/EndlessRandomTower.cs @@ -8,6 +8,8 @@ using UnityEngine.UI; using TMPro; using KTGMGemClient; +using DG.Tweening; +using System.Collections; public class EndlessRandomTower : Singleton<EndlessRandomTower> { @@ -15,19 +17,12 @@ public Button randomBtn; /// <summary> - /// 倒计时对应的背景图片. - /// </summary> - public Image cdTimeBg; - - /// <summary> - /// 倒计划对应的文字. - /// </summary> - public TextMeshProUGUI cdTimeText; - - /// <summary> /// 购买二级宝石的按钮贴图. /// </summary> public Sprite buyBtnLevelUp; + + [SerializeField] + private TextMeshProUGUI cashText; protected bool bSetBuyLvlUp; @@ -42,15 +37,10 @@ protected int maxTower = 0; - private bool firstDeploy = false; - public static readonly int MAX_TOWERDIS = 5; // 开始出现技能塔的时间: public static float SKILL_TOWER_TIME = 30.0f; - - // 购买之后直接出现2级宝石的时间. - public static float LEVELUP_TOWER_TIME = 10.0f; protected System.Random mRandom; @@ -62,12 +52,38 @@ // 所有宝石位置权重列表 private List<int> weightList; - // 是否初始化过出战数据 - private bool isInitFightData = false; + public Text NormalDesc; + + public Text SkillDesc; + + public ParticleSystem btnPS; + + IEnumerator ReadJson() + { + while (!GameConfig.JsonReadDone) + { + yield return 10; + } + + countDownLimit = JsonDataCenter.GetById<battle>(23).value; + Debug.Log("设置了倒计时:" + countDownLimit); + yield break; + } // Start is called before the first frame update void Start() { +#if UNITY_ANDROID + +#endif +#if UNITY_IPHONE + +#endif +#if UNITY_EDITOR + +#endif + StartCoroutine(ReadJson()); + if (!EndlessLevelManager.instanceExists) Debug.LogError("[UI] No level manager for tower list"); @@ -88,9 +104,32 @@ bSetBuyLvlUp = false; bCdTimeStart = false; + randomBtn.onClick.AddListener(delegate () { onClick(EFeatureTower.NULL); }); + NormalDesc.text = $"购买{0 + 1}级精灵"; + Invoke("UpdateDescDisplay", 1.0f); + //UpdateDescDisplay(); + } - cdTimeBg.gameObject.SetActive(false); - cdTimeText.text = ""; + public void UpdateDescDisplay() + { + int minLevel = GameConfig.IsUpgradeTowerLevel ? 1 : 0; + + NormalDesc.text = $"购买{minLevel + 1}级精灵"; + } + + public void ChangeBtnClickNormal() + { + randomBtn.onClick.RemoveAllListeners(); + randomBtn.onClick.AddListener(delegate () { onClick(EFeatureTower.NULL); }); + } + public void ChangeBtnClick() + { + randomBtn.onClick.RemoveAllListeners(); + randomBtn.onClick.AddListener(delegate () { onClick(EFeatureTower.NULL); }); + randomBtn.onClick.AddListener(() => + { + AudioSourceManager.Ins.Play(AudioEnum.UIDisable); + }); } /// <summary> @@ -99,37 +138,6 @@ public List<Tower> towerList { get { return this.towerArray; } - } - - /// <summary> - /// 处理按钮升级相关. - /// </summary> - private void Update() - { - float gstime = EndlessUIStart.instance.GameStartTime; - if (gstime <= 0) return; - - if ((!bCdTimeStart) && (LEVELUP_TOWER_TIME - gstime) <= JsonDataCenter.TOWERLVLUP_CDTIME) - { - bCdTimeStart = true; - btnLvlUpCdTime = LEVELUP_TOWER_TIME - gstime; - cdTimeBg.gameObject.SetActive(true); - } - if ((!bSetBuyLvlUp) && (gstime > LEVELUP_TOWER_TIME)) - { - bSetBuyLvlUp = true; - this.randomBtn.GetComponent<Image>().sprite = buyBtnLevelUp; - cdTimeBg.gameObject.SetActive(false); - cdTimeText.text = ""; - } - - // 更新倒计时: - if ((!bSetBuyLvlUp) && bCdTimeStart) - { - btnLvlUpCdTime -= Time.deltaTime; - string distr = ((int)Math.Ceiling(btnLvlUpCdTime)).ToString() + "s"; - cdTimeText.text = distr; - } } public void actionTest() @@ -178,7 +186,7 @@ if (name == this.towerArray[ti].towerName) return towerArray[ti]; - return GetRandomTower(false); + return GetRandomTower(EFeatureTower.NULL); } /// <summary> @@ -206,106 +214,175 @@ /// <summary> /// 根据规则生成一个随机的塔 /// </summary> - /// <param name="onlySpawnElf">是否仅产生精灵宝石,而不产生技能宝石</param> /// <returns></returns> - public Tower GetRandomTower(bool onlySpawnElf = true) + public Tower GetRandomTower(EFeatureTower towerType, bool isRandom = false) { - if (!isInitFightData) + int[] indexArr = { 0, 1, 2, 3, 4 }; + + if (!isRandom) { - isInitFightData = true; - weightList = new List<int>(); - List<posWeight> posWeight = JsonDataCenter.GetList<posWeight>(); - randomTotalWeight = 0; - - for (int i = 0; i < posWeight.Count; ++i) + if (towerType == EFeatureTower.NULL) { - weightList.Add(posWeight[i].weight); - randomTotalWeight += posWeight[i].weight; + // 只获得普通宝石 + indexArr = new int[] { 0, 1, 2 }; } - - // 游戏开始之前先把出站组乱序 - GameUtils.Shuffle(towerArray); + else + { + // 只获得技能宝石 + indexArr = new int[] { 3, 4 }; + } } - Tower ret = null; + int random = UnityEngine.Random.Range(0, indexArr.Length); - // 还没到技能时间,忽略掉技能宝石 - if (onlySpawnElf) - { - // 精灵宝石位置索引列表 - List<int> elfIndexList = new List<int>(); - - for (int i = 0; i < towerArray.Count; ++i) - { - if (towerArray[i].towerFeature == EFeatureTower.NULL) - elfIndexList.Add(i); - } - - // 没有上阵精灵宝石 - if (elfIndexList.Count == 0) - { - Debug.LogError("--------------------- 没有上阵精灵宝石 ---------------------"); - return null; - } - - // 计算所有精灵宝石位置的总权重 - int elfTotalWeight = 0; - // 精灵宝石位置权重列表 - List<int> elfWeightList = new List<int>(); - - for (int i = 0; i < elfIndexList.Count; ++i) - { - elfTotalWeight += weightList[elfIndexList[i]]; - elfWeightList.Add(weightList[elfIndexList[i]]); - } - - // 所有精灵宝石位置的权重都为0,直接等概率出一个精灵宝石 - int index = elfTotalWeight == 0 ? mRandom.Next(0, elfIndexList.Count) : GetRandomIndex(elfWeightList, elfTotalWeight); - ret = towerArray[elfIndexList[index]]; - } - // 可以自由产生 - else - { - int index = randomTotalWeight == 0 ? mRandom.Next(0, towerArray.Count) : GetRandomIndex(weightList, randomTotalWeight); - ret = towerArray[index]; - } - - // 生成宝石成功,需要调整宝石位置,把宝石放到队尾 - towerArray.Remove(ret); - towerArray.Add(ret); - - return ret; + return towerArray[indexArr[random]]; } /// <summary> /// 随机购买Tower的入口,如果购买成功,则需要实时更新价格 /// </summary> - public void onClick() + public void onClick(EFeatureTower towerType) { - // 还没到技能时间,忽略掉技能宝石 - Tower newTower = GetRandomTower(EndlessUIStart.instance.GameStartTime <= SKILL_TOWER_TIME); + if (GameConfig.IsNewbie) + { + AudioSourceManager.Ins.Play(AudioEnum.UI); + btnPS?.Play(); + return; + } + + if (EndlessGameUI.instance.tdBuyDisable) + { + cashText.color = new Color(218f / 255f, 32f / 255f, 32f / 255f); + DOTween.To(() => cashText.color, (Color v) => cashText.color = v, cashText.color, 0.2f) + .OnComplete(CheckCurrencyEnough); + return; + } + + AudioSourceManager.Ins.Play(AudioEnum.UI); + + Tower newTower = GetRandomTower(towerType, false); if (!newTower) throw new Exception("未能成功产生Tower"); - RandomPlaceTower(newTower); + RandomPlaceTower(newTower, -1, -1, -1, -1, true); + + btnPS?.Play(); + //重置倒计时 + + if (!isCountStartDown) + { + needClickPS.Stop(); + isCountStartDown = true; + } + + countDownTime = 0f; } + + + private bool isCountStartDown = false;//是否开始倒计时 + public void SetCountDown(bool isOn, bool isReset = false) + { + isCountStartDown = isOn; + if (isReset) + { + countDownTime = 0; + if (needClickPS.isPlaying) + needClickPS.Stop(); + } + } + + public void SetPS(bool isOn) + { + if (needClickPS.gameObject.activeSelf != isOn) + { + needClickPS.gameObject.SetActive(isOn); + } + + } + float countDownTime = 0f; + float countDownLimit = 5f; + public ParticleSystem needClickPS;//提示点击按钮 + /// <summary> + /// This function is called every fixed framerate frame, if the MonoBehaviour is enabled. + /// </summary> + void FixedUpdate() + { + if (isCountStartDown) + { + countDownTime += Time.deltaTime; + if (countDownTime > countDownLimit) + { + + CheckTower(); + countDownTime = 0; + } + } + } + + /// <summary> + /// 查看是否满足播放按钮提示 + /// </summary> + private void CheckTower() + { + //条件 金币是否满足,合成区是否有位置 + int result; + int.TryParse(cashText.text.ToString(), out result); + int current = EndlessLevelManager.instance.Currency.currentCurrency; + int num = EndlessGameUI.instance.GetTowerNum(); + + if (current >= result && num < 10)//自己的钱多 + { + //Debug.Log($"当前金币 :{current} 下一次购买需要金币:{result} 塔的数量:{num}"); + isCountStartDown = false; + needClickPS.Play(); + } + } + + public void CheckMoney() + { + int result; + int.TryParse(cashText.text.ToString(), out result); + int current = EndlessLevelManager.instance.Currency.currentCurrency; + if (current < result && needClickPS.isPlaying)//自己的钱多 + { + needClickPS.Stop(); + isCountStartDown = true; + countDownTime = 0; + } + } + + private void CheckCurrencyEnough() + { + int result; + int.TryParse(cashText.text.ToString(), out result); + int current = EndlessLevelManager.instance.Currency.currentCurrency; + + if (current >= result) + EndlessGameUI.instance.enableRandomTowerBtn(); + else + EndlessGameUI.instance.disableRandomTowerBtn(); + } + + private List<string> towerNameLis = new List<string>();//用来判断是否是首次购买宝石 /// <summary> /// 随机找一个空白位置放置塔防 /// </summary> /// <param name="tower"></param> - public bool RandomPlaceTower(Tower tower, int level = -1, int cost = -1) + public bool RandomPlaceTower(Tower tower, int level = -1, int cost = -1, int posx = -1, int posy = -1, bool isFirstAppear = false) { EndlessGameUI gameUI = EndlessGameUI.instance; if (gameUI.isBuilding) gameUI.CancelGhostPlacement(); - if (EndlessUIStart.instance.GameStartTime >= LEVELUP_TOWER_TIME) - return gameUI.RandomPlaceTower(tower, -1, -1, level == -1 ? 1 : level, cost); - else - return gameUI.RandomPlaceTower(tower, -1, -1, level == -1 ? 0 : level, cost); + if (level == -1) + { + level = GameConfig.IsUpgradeTowerLevel ? 1 : 0; + } + + return gameUI.RandomPlaceTower(tower, posx, posy, level, cost, false, isFirstAppear); } /// <summary> @@ -344,6 +421,6 @@ targetTower = getTowerByName(tname); // River: 随机找一个空白位置放置塔防。 - EndlessGameUI.instance.RandomPlaceTower(targetTower, x, y, lvl); + EndlessGameUI.instance.RandomPlaceTower(targetTower, x, y, lvl, -1, true); } } -- Gitblit v1.9.1