chenxin
2020-11-17 40b918d5189a154bc0ff31627e69867742934720
Assets/Scripts/TowerDefense/Towers/Tower.cs
@@ -1,4 +1,6 @@
using System;
using KTGMGemClient;
using System.Collections;
using System;
using ActionGameFramework.Health;
using Core.Utilities;
using TowerDefense.Level;
@@ -25,6 +27,7 @@
        NULL,
        BULLET,   // 子弹塔
        ENERGY,   // 能量充能.
        FREEZE, // 水精灵充能
        END
    }
@@ -62,6 +65,8 @@
        /// </summary>
        public Material materialTowerAttackSelf;
        public Material materialTowerWaitSelf;
        public Material materialTowerAttackOppo;
        public Material materialTowerWaitOppo;
        /// <summary>
        /// 0 空状态  1 等待状态  2 攻击状态.
@@ -73,6 +78,9 @@
        /// </summary>
        public ETowerFuntion eTowerFuntion = ETowerFuntion.NULL;
        public BulletUICtl bulletCtl = null;
        public EnergyUICtl energyCtl = null;
        public FreezeBreath FreezeBreathCtrl = null;
        /// <summary>
        /// The tower levels associated with this tower
@@ -117,11 +125,26 @@
        public float attackRise { get; set; }
        /// <summary>
        /// 用于界面部分的数据位移
        /// </summary>
        protected int progressOffset = 0;
        public int FreezeBreathProgressOffset { get; set; }
        public bool PlayWaveLineFlash { get; set; } = true;
        /// <summary>
        /// 塔防数据的局内升级
        /// </summary>
        public int inSceneTowerLevel
        {
            get; set;
        }
        public int uiProOffset
        {
            get { return progressOffset; }
            set { progressOffset = value; }
        }
        /// <summary>
@@ -136,16 +159,33 @@
        {
            if (!materialTowerAttackSelf) return;
            if (attack && (this.curActionState != 2))
            if (opponentSide)
            {
                currentTowerLevel.SetTowerMonsterMat(materialTowerAttackSelf);
                this.curActionState = 2;
            }
                if (attack && (curActionState != 2))
                {
                    currentTowerLevel.SetTowerMonsterMat(materialTowerAttackOppo, true);
                    curActionState = 2;
                }
            if ((!attack) && (this.curActionState != 1))
                if ((!attack) && (curActionState != 1))
                {
                    currentTowerLevel.SetTowerMonsterMat(materialTowerWaitOppo, false);
                    curActionState = 1;
                }
            }
            else
            {
                currentTowerLevel.SetTowerMonsterMat(materialTowerWaitSelf);
                this.curActionState = 1;
                if (attack && (curActionState != 2))
                {
                    currentTowerLevel.SetTowerMonsterMat(materialTowerAttackSelf, true);
                    curActionState = 2;
                }
                if ((!attack) && (curActionState != 1))
                {
                    currentTowerLevel.SetTowerMonsterMat(materialTowerWaitSelf, false);
                    curActionState = 1;
                }
            }
        }
@@ -155,61 +195,274 @@
        public bool opponentSide { get; set; }
        /// <summary>
        /// 播放充能状态特效.
        /// </summary>
        /// <param name="play"></param>
        public void PlayEnergyEffect(bool play, bool isClose = true)
        {
            if (energyCtl && isClose)
                energyCtl.gameObject.SetActive(!play);
            else if (!isClose)
            {
            }
            if (!opponentSide)
            {
                if (GameUI.instanceExists)
                    ((TowerPlacementGrid)GameUI.instance.selfTowerPlaceArea).PlayEnergyEffect(gridPosition.x, play);
                else if (EndlessGameUI.instanceExists)
                    ((TowerPlacementGridEndless)EndlessGameUI.instance.selfTowerPlaceArea).PlayEnergyEffect(gridPosition.x, gridPosition.y, play);
            }
            else
                ((TowerPlacementGrid)OpponentMgr.instance.m_CurrentArea).PlayEnergyEffect(gridPosition.x, play);
        }
        /// <summary>
        /// 播放水精灵充能满了特效
        /// </summary>
        /// <param name="play"></param>
        /// <param name="isClose"></param>
        public void PlayFreezeBreathEffect(bool play, bool isClose = true)
        {
            if (FreezeBreathCtrl && isClose)
                FreezeBreathCtrl.gameObject.SetActive(!play);
            if (!opponentSide)
            {
                if (EndlessGameUI.instanceExists)
                    ((TowerPlacementGridEndless)EndlessGameUI.instance.selfTowerPlaceArea).PlayFreezeBreathEffect(gridPosition.x, gridPosition.y, play);
            }
        }
        /// <summary>
        /// 当前是否处于攻击模式
        /// </summary>
        public bool bInAttackMode
        {
            get { return this.m_bInAttackMode; }
            get { return m_bInAttackMode; }
            // 设置是否处于攻击状态:
            set
            {
                m_bInAttackMode = value;
                currentTowerLevel.SetAffectorState(m_bInAttackMode, gridPosition.x);
                currentTowerLevel.SetAttackState(value);
                if (value)
                {
                    if (opponentSide)
                        currentTowerLevel.SetTowerMonsterMat(materialMonsterOppo);
                    else
                    {
                        currentTowerLevel.SetTowerMonsterMat(materialMonsterSelf);
                        this.setTowerState(false);
                    }
                    if (opponentSide)
                    {
                        OpponentMgr.instance.SetTowerAttID(gridPosition.x, attributeId, this.currentLevel);
                        OpponentMgr.instance.SetTowerAttID(gridPosition.x, attributeId, currentLevel);
                    }
                    else
                    {
                        if (LevelManager.instanceExists)
                        {
                            LevelManager.instance.SetTowerAttID(gridPosition.x, attributeId, this.currentLevel);
                            LevelManager.instance.SetTowerAttID(gridPosition.x, attributeId, currentLevel);
                            WaveLineSelMgr.instance.attackTowerFixed(gridPosition.x);
                        }
                        else if (EndlessLevelManager.instanceExists)
                        {
                            EndlessLevelManager.instance.SetTowerAttID(gridPosition.x, attributeId, this.currentLevel);
                            EndlessWaveLineManager.instance.AttackTowerFixed(gridPosition.x);
                            EndlessLevelManager.instance.SetTowerAttID(gridPosition.x, attributeId, currentLevel);
                            EndlessWaveLineManager.instance.AttackTowerFixed(gridPosition.x, PlayWaveLineFlash);
                        }
                    }
                    //
                    // 根据是否是子弹塔防来决定是否显示相应的界面
                    if (this.eTowerFuntion == ETowerFuntion.BULLET)
                    {
                        BulletUICtl buc = this.placementArea.GetBulletUICtl(gridPosition.x);
                        if (buc)
                        {
                            buc.gameObject.SetActive(true);
                            this.bulletCtl = buc;
                            buc.resetToMaxBullet();
                        }
                    }
                    // 处理塔位边上的界面.
                    OnTowerUICtrl();
                }
            }
        }
        /// <summary>
        /// 去掉当前Tower对应的界面数据.
        /// </summary>
        public void DisableTowerUICtrl()
        {
            // 根据是否是子弹塔防来决定是否显示相应的界面
            BulletUICtl buc = placementArea.GetBulletUICtl(gridPosition.x, gridPosition.y);
            if ((eTowerFuntion == ETowerFuntion.BULLET) && (buc != null))
            {
                buc.gameObject.SetActive(false);
                bulletCtl = null;
            }
            EnergyUICtl euc = placementArea.GetEnergyUICtl(gridPosition.x, gridPosition.y);
            if ((eTowerFuntion == ETowerFuntion.ENERGY) && (euc != null))
            {
                // 设置数据
                euc.gameObject.SetActive(false);
                energyCtl = null;
            }
            FreezeBreath ctrl = placementArea.GetFreezeBreath(gridPosition.x, gridPosition.y);
            if (eTowerFuntion == ETowerFuntion.FREEZE && ctrl != null)
            {
                ctrl.gameObject.SetActive(false);
                ctrl = null;
            }
        }
        public int GetTowerUICtrlProgress()
        {
            // 根据是否是子弹塔防来决定是否显示相应的界面
            BulletUICtl buc = placementArea.GetBulletUICtl(gridPosition.x, gridPosition.y);
            if ((eTowerFuntion == ETowerFuntion.BULLET) && (buc != null))
                return buc.GetCtlProgress();
            EnergyUICtl euc = placementArea.GetEnergyUICtl(gridPosition.x, gridPosition.y);
            if ((eTowerFuntion == ETowerFuntion.ENERGY) && (euc != null))
                return euc.GetCtlProgress();
            FreezeBreath ctrl = placementArea.GetFreezeBreath(gridPosition.x, gridPosition.y);
            if (eTowerFuntion == ETowerFuntion.FREEZE && ctrl != null)
                return ctrl.CurrentProgress;
            return 0;
        }
        public void SetTowerUICtlProcess(int pro)
        {
            // 根据是否是子弹塔防来决定是否显示相应的界面
            BulletUICtl buc = placementArea.GetBulletUICtl(gridPosition.x, gridPosition.y);
            if ((eTowerFuntion == ETowerFuntion.BULLET) && (buc != null))
            {
                buc.SetCtlProcess(pro);
                progressOffset = pro;
                return;
            }
            EnergyUICtl euc = placementArea.GetEnergyUICtl(gridPosition.x, gridPosition.y);
            if ((eTowerFuntion == ETowerFuntion.ENERGY) && (euc != null))
            {
                euc.SetCtlProcess(pro);
                progressOffset = pro;
                if (progressOffset == 10)
                    progressOffset = 0;
                return;
            }
            FreezeBreath ctrl = placementArea.GetFreezeBreath(gridPosition.x, gridPosition.y);
            if (eTowerFuntion == ETowerFuntion.FREEZE && ctrl != null)
            {
                ctrl.SetCtrlProgress(pro);
                FreezeBreathProgressOffset = pro;
                if (FreezeBreathProgressOffset == 10)
                    FreezeBreathProgressOffset = 0;
            }
        }
        /// <summary>
        /// 充能技能相关的代码开关。包括子弹充能和时间充能
        /// </summary>
        protected void OnTowerUICtrl()
        {
            HandleBulletUICtrl();
            HandleEnergyUICtrl();
            HandleFreezeBreathCtrl();
        }
        private void HandleBulletUICtrl()
        {
            // 根据是否是子弹塔防来决定是否显示相应的界面
            BulletUICtl buc = placementArea.GetBulletUICtl(gridPosition.x, gridPosition.y);
            if (buc == null) return;
            if ((eTowerFuntion == ETowerFuntion.BULLET) && (buc != null))
            {
                // 设置数据
                buc.gameObject.SetActive(true);
                bulletCtl = buc;
                buc.ResetToMaxBullet();
            }
            else
            {
                // 清空数据
                buc.gameObject.SetActive(false);
                bulletCtl = null;
            }
        }
        private void HandleEnergyUICtrl()
        {
            // 根据是否是能量充能来决定是否显示相应的界面.
            EnergyUICtl euc = placementArea.GetEnergyUICtl(gridPosition.x, gridPosition.y);
            if ((eTowerFuntion == ETowerFuntion.ENERGY) && (euc != null))
            {
                // 设置数据
                euc.gameObject.SetActive(true);
                energyCtl = euc;
                euc.SetEnergyProgress(0);
            }
            else
            {
                // 清空数据
                euc.gameObject.SetActive(false);
                energyCtl = null;
            }
        }
        private void HandleFreezeBreathCtrl()
        {
            FreezeBreath ctrl = placementArea.GetFreezeBreath(gridPosition.x, gridPosition.y);
            if (eTowerFuntion == ETowerFuntion.FREEZE && ctrl != null)
            {
                ctrl.gameObject.SetActive(true);
                FreezeBreathCtrl = ctrl;
                ctrl.SetProgress(0);
            }
            else
            {
                ctrl.gameObject.SetActive(false);
                FreezeBreathCtrl = null;
            }
        }
        /// <summary>
        /// This function is called when the object becomes enabled and active.
        /// </summary>
        void OnEnable()
        {
            // if (bulletCtl != null)
            // {
            //     Debug.Log("打开了bulletCtl");
            //     bulletCtl.gameObject.SetActive(true);
            // }
            // if (energyCtl != null)
            // {
            //     Debug.Log("打开了energyCtl");
            //     energyCtl.gameObject.SetActive(true);
            // }
        }
        /// <summary>
        /// This function is called when the behaviour becomes disabled or inactive.
        /// </summary>
        void OnDisable()
        {
            if (bulletCtl != null)
            {
                bulletCtl.gameObject.SetActive(false);
            }
            if (energyCtl != null)
            {
                energyCtl.gameObject.SetActive(false);
            }
            if (FreezeBreathCtrl != null)
                FreezeBreathCtrl.gameObject.SetActive(false);
        }
        /// <summary>
@@ -225,8 +478,9 @@
                return;
            }
            // 设置攻击数据的加强,暂时是测试数据,后面需要读取表格数据处理:
            float damageAdd = (inSceneTowerLevel - 1) * this.INSCENE_TU_DAMAGE;
            for (int ti = 0; ti < this.levels.Length; ti++)
            float damageAdd = 0;
            // float damageAdd = (inSceneTowerLevel - 1) * INSCENE_TU_DAMAGE;
            for (int ti = 0; ti < levels.Length; ti++)
            {
                if (levels[ti].levelDamager)
                {
@@ -238,7 +492,7 @@
        protected void ResetInSceneTowerLevel()
        {
            for (int ti = 0; ti < this.levels.Length; ti++)
            for (int ti = 0; ti < levels.Length; ti++)
            {
                if (levels[ti].levelDamager)
                {
@@ -257,9 +511,9 @@
            inSceneTowerLevel++;
            // 设置攻击数据的加强,暂时是测试数据,后面需要读取表格数据处理:
            float damageAdd = inSceneTowerLevel * this.INSCENE_TU_DAMAGE;
            float damageAdd = inSceneTowerLevel * INSCENE_TU_DAMAGE;
            for (int ti = 0; ti < this.levels.Length; ti++)
            for (int ti = 0; ti < levels.Length; ti++)
            {
                if (levels[ti].levelDamager)
                {
@@ -312,6 +566,16 @@
        public Action towerDestroyed;
        /// <summary>
        /// 放置到目标位置
        /// </summary>
        /// <param name="destination"></param>
        public virtual void SetToDestination(IntVector2 destination)
        {
            gridPosition = destination;
            transform.position = placementArea.GridToWorld(destination, dimensions);
        }
        /// <summary>
        /// Provide the tower with data to initialize with
        /// </summary>
        /// <param name="targetArea">The placement area configuration</param>
@@ -337,17 +601,32 @@
                EndlessLevelManager.instance.LevelStateChanged += OnLevelStateChanged;
            // 查找Targetter:
            Targetter target = this.GetComponentInChildren<Targetter>();
            Targetter target = GetComponentInChildren<Targetter>();
            if (target)
            {
                target.bOpponent = this.opponentSide;
                target.bOpponent = opponentSide;
            }
            else
                Debug.Log("在当前的Tower中找不到Targetter.");
            // 
            // 初始化当前的局内Tower等级数据
            this.initInSceneTowerLevel(SceneTowerLvl.getInSceneTowerLvl(this.towerName));
            initInSceneTowerLevel(SceneTowerLvl.getInSceneTowerLvl(towerName));
            StartCoroutine(ResetScale());
        }
        IEnumerator ResetScale()
        {
            yield return new WaitForSeconds(0.2f);
            if (gridPosition.y > 1)
            {
                currentTowerLevel.ResetScale();
            }
            else
            {
                currentTowerLevel.NormalScale();
            }
        }
        /// <summary>
@@ -371,11 +650,11 @@
        {
            if (vis)
            {
                this.gameObject.SetActive(true);
                gameObject.SetActive(true);
            }
            else
            {
                this.gameObject.SetActive(false);
                gameObject.SetActive(false);
            }
        }