From 7d526eef93f8071121dfcfcd4a039b9e51dc3d67 Mon Sep 17 00:00:00 2001
From: wangguan <wangguan@kt007.com>
Date: Mon, 07 Dec 2020 18:06:09 +0800
Subject: [PATCH] 修复了新手引导多次点击开宝箱页面BUG 所有塔不论上不上阵均受缩放影响 冰冻效果只显示1次 结算页面动效修改 修改地面

---
 Assets/Scripts/TowerDefense/Towers/Placement/TowerPlacementGridEndless.cs |  297 +++++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 236 insertions(+), 61 deletions(-)

diff --git a/Assets/Scripts/TowerDefense/Towers/Placement/TowerPlacementGridEndless.cs b/Assets/Scripts/TowerDefense/Towers/Placement/TowerPlacementGridEndless.cs
index 5b6a54b..6a90795 100644
--- a/Assets/Scripts/TowerDefense/Towers/Placement/TowerPlacementGridEndless.cs
+++ b/Assets/Scripts/TowerDefense/Towers/Placement/TowerPlacementGridEndless.cs
@@ -5,7 +5,6 @@
 using TowerDefense.Level;
 using TowerDefense.UI.HUD;
 using UnityEngine;
-using UnityEngine.UI;
 
 namespace TowerDefense.Towers.Placement
 {
@@ -14,9 +13,9 @@
     /// Its origin is centered in the middle of the lower-right cell. It can be oriented in any direction
     /// </summary>
     [RequireComponent(typeof(BoxCollider))]
-    public class TowerPlacementGridEndless : MonoBehaviour, IPlacementArea
+    public class TowerPlacementGridEndless : Singleton<TowerPlacementGridEndless>, IPlacementArea
     {
-        public static float GRID_OPENCASH = 100;
+        public static float GRID_OPENCASH = 200;
 
         /// <summary>
         /// Prefab used to visualise the grid.
@@ -37,7 +36,7 @@
         /// <summary>
         /// 等待购买开启对应按钮.
         /// </summary>
-        public Button waitBuyBtnPrefab;
+        public GameObject waitBuyBtnPrefab;
 
         /// <summary>
         /// 最后一行格子与前一行格子之间的空位长度.
@@ -45,6 +44,10 @@
         public float gridFreePos;
 
         public float gridFreePos2;
+
+        public float gridFreePos3;
+
+        public float gridFreePos4;
 
         /// <summary>
         /// The dimensions of the grid 
@@ -55,7 +58,7 @@
         /// Size of the edge of a cell
         /// </summary>
         [Tooltip("The size of the edge of one grid cell for area. Should match the physical grid size of towers")]
-        public float gridSize = 1;
+        public float gridSize = 1;//1.109
 
         /// <summary>
         /// Inverted grid size, to multiply with
@@ -82,8 +85,15 @@
 
         private float[,] m_arrCoinGenTime;
 
+        public GameObject GridContainer;
+
         /// <summary>
-        /// 每一个Tile格子的中心店的世界坐标
+        /// 所有格子对应的世界坐标
+        /// </summary>
+        private Vector3[,] GridWorldPosArr;
+
+        /// <summary>
+        /// 每一个Tile格子的中心店的屏幕坐标
         /// </summary>
         private Vector2[,] arrGridCentPos;
 
@@ -117,12 +127,19 @@
 
         private EnergyUICtl[,] arrTowerEnergyUi;
 
-        private ParticleSystem[,] arrTowerEnergyEffect;
+        private FreezeBreath[,] arrTowerFreezeBreathUi;
+
+        private GameObject[,] arrTowerEnergyEffect;
+
+        /// <summary>
+        /// 水精灵充能满特效
+        /// </summary>
+        private GameObject[,] arrTowerFreezeBreathEffect;
 
         /// <summary>
         /// 充能特效对应的Prefab.
         /// </summary>
-        public ParticleSystem energyEffectPrefab;
+        public GameObject energyEffectPrefab;
 
         /// <summary>
         /// 此位置上塔对应的子弹充能Prefab.塔放置到当前的塔位后,如果是对应的塔防类型,需要把
@@ -134,6 +151,16 @@
         /// 充能条对应的界面
         /// </summary>
         public GameObject towerEnergyUIPrefab;
+
+        /// <summary>
+        /// 水精灵塔技能(冷冻气息)充能条
+        /// </summary>
+        public GameObject FreezeBreathChargePrefab;
+
+        /// <summary>
+        /// 水精灵塔充能条特效
+        /// </summary>
+        public GameObject FreezeBreathChargeEffect;
 
         /// <summary>
         /// Converts a location in world space into local grid coordinates.
@@ -192,6 +219,16 @@
             return arrTowerEnergyUi[x, 3 - y];
         }
 
+        /// <summary>
+        /// 获取水精灵对应位置的充能条
+        /// </summary>
+        /// <param name="x"></param>
+        /// <param name="y"></param>
+        /// <returns></returns>
+        public FreezeBreath GetFreezeBreath(int x, int y)
+        {
+            return arrTowerFreezeBreathUi[x, 3 - y];
+        }
 
         /// <summary>
         /// 获取一个可以放置塔防的位置.
@@ -289,8 +326,6 @@
 
             return false;
         }
-
-
 
         /// <summary>
         /// 是否是等待购买的攻击塔位.
@@ -416,6 +451,14 @@
                 freePos = gridFreePos;
             else if (gridPosition.y == 2)
                 freePos = gridFreePos2;
+            else if (gridPosition.y == 1)
+            {
+                freePos = gridFreePos3;
+            }
+            else if (gridPosition.y == 0)
+            {
+                freePos = gridFreePos4;
+            }
             // Calculate scaled local position
             Vector3 localPos = new Vector3(gridPosition.x + (sizeOffset.x * 0.5f), 0, gridPosition.y + (sizeOffset.y * 0.5f) + freePos) *
                                gridSize;
@@ -530,8 +573,9 @@
         /// <summary>
         /// Initialize values
         /// </summary>
-        protected virtual void Awake()
+        protected override void Awake()
         {
+            base.Awake();
             ResizeCollider();
 
             // Initialize empty bool array (defaults are false, which is what we want)
@@ -547,15 +591,59 @@
         {
             // 初始化塔位类型.
             initTileGridType();
+            InitGridWorldPos();
 
             // Precalculate inverted grid size, to save a division every time we translate coords
             m_InvGridSize = 1 / gridSize;
-
             SetUpGrid();
-
             // 初始化格子对应的屏幕坐标数据 延迟执行
             Invoke("preCalculateGridUIPos", 0.3f);
-            //preCalculateGridUIPos();
+            EventCenter.Ins.Add<int>((int)KTGMGemClient.EventType.EndlessCritBulletNumChange, OnCritBulletNumChange);
+            EventCenter.Ins.Add((int)KTGMGemClient.EventType.OpenAllTowerGrid, OpenAllTowerGrid);
+        }
+
+        /// <summary>
+        /// 初始化所有格子的世界坐标(中心)
+        /// </summary>
+        private void InitGridWorldPos()
+        {
+            GridWorldPosArr = new Vector3[dimensions.x, dimensions.y];
+
+            for (int i = 0; i < dimensions.x; ++i)
+            {
+                for (int j = 0; j < dimensions.y; ++j)
+                {
+                    GridWorldPosArr[i, j] = GridToWorld(new IntVector2(i, j), new IntVector2(1, 1));
+                }
+            }
+        }
+
+        /// <summary>
+        /// 根据坐标获取格子世界坐标(中心)
+        /// </summary>
+        /// <param name="x"></param>
+        /// <param name="y"></param>
+        /// <returns></returns>
+        public Vector3 GetGridWorldPos(int x, int y)
+        {
+            if (x < 0 || x >= dimensions.x || y < 0 || y >= dimensions.y) return new Vector3();
+
+            return GridWorldPosArr[x, y];
+        }
+
+        /// <summary>
+        /// 木属性暴击子弹数量改变
+        /// </summary>
+        /// <param name="count"></param>
+        private void OnCritBulletNumChange(int count)
+        {
+            for (int i = 0; i < dimensions.x; ++i)
+            {
+                for (int j = 0; j < AttackRowNumbers; ++j)
+                {
+                    arrTowerBulletUi[i, j].CritBulletNum = count;
+                }
+            }
         }
 
         /// <summary>
@@ -567,7 +655,7 @@
         {
             arrGridCentPos = new Vector2[dimensions.x, dimensions.y];
 
-            Vector3 targetPos = GridToWorld(new IntVector2(0, 0), new IntVector2(1, 1));
+            Vector3 targetPos = GetGridWorldPos(0, 0);
 
             if (!ViewPortAdj.instance.bAdjViewPort)
                 ViewPortAdj.instance.adjViewportRect();
@@ -586,33 +674,43 @@
             // 血条位置的设定
             PreCalculateTowerBloodUi();
 
-            float[] gapArr = { 0.2f, 0.7f };
+            float[] gapArr = { 0.5f, 1f };
             m_arrTGO = new EndlessTowerGridOpen[dimensions.x, dimensions.y];
 
+            GameObject container = GameObject.Find("BuyButtonContainer");
+            TowerPlacementGridEndless.GRID_OPENCASH = 200;
+            //Debug.Log("修改了数值:" + TowerPlacementGridEndless.GRID_OPENCASH);
+            //float[] xup = { -0.6f, -0.3f, 0, 0.3f, 0.6f };
             for (int x = 0; x < dimensions.x; ++x)
             {
                 for (int y = dimensions.y - AttackRowNumbers; y < dimensions.y; ++y)
                 {
                     if (m_arrGridType[x, y] != PlacementGridType.EGridWaitBuy) continue;
 
-                    GameObject container = GameObject.Find("BuyButtonContainer");
-                    Button buyButton = Instantiate(waitBuyBtnPrefab);
-                    buyButton.transform.SetParent(container.transform);
+                    GameObject buyButton = Instantiate(waitBuyBtnPrefab);
+                    buyButton.transform.SetParent(container.transform, false);
 
                     Vector3 pos = buyButton.transform.position;
+                    //pos.x = arrGridCentPos[x, y].x + (x - 2) * gapArr[3 - y] + xup[x];
                     pos.x = arrGridCentPos[x, y].x + (x - 2) * gapArr[3 - y];
                     pos.z = arrGridCentPos[x, y].y + 6f;
+                    // if (y == 2)
+                    // {
+                    //     pos.z = arrGridCentPos[x, y].y + 9f;
+                    // }
+                    // else if (y == 3)
+                    // {
+                    //     pos.z = arrGridCentPos[x, y].y + 7f;
+                    // }
                     pos.y = 30;
                     buyButton.transform.position = pos;
-                    buyButton.transform.localRotation = Quaternion.identity;
-                    buyButton.transform.localScale = Vector3.one;
 
                     // 设置按钮对应的点击功能
                     EndlessTowerGridOpen tgo = buyButton.GetComponent<EndlessTowerGridOpen>();
                     if (tgo)
                     {
                         tgo.SetBuyBtnInfo(x, y, this);
-                        tgo.cashText.SetText(TowerPlacementGrid.GRID_OPENCASH_SELF.ToString());
+                        tgo.cashText.SetText(TowerPlacementGridEndless.GRID_OPENCASH.ToString());
                         m_arrTGO[x, y] = tgo;
                     }
                 }
@@ -660,7 +758,9 @@
             m_arrTowerBulletUIPos = new Vector2[dimensions.x, AttackRowNumbers];
             arrTowerBulletUi = new BulletUICtl[dimensions.x, AttackRowNumbers];
             arrTowerEnergyUi = new EnergyUICtl[dimensions.x, AttackRowNumbers];
-            arrTowerEnergyEffect = new ParticleSystem[dimensions.x, AttackRowNumbers];
+            arrTowerFreezeBreathUi = new FreezeBreath[dimensions.x, AttackRowNumbers];
+            arrTowerEnergyEffect = new GameObject[dimensions.x, AttackRowNumbers];
+            arrTowerFreezeBreathEffect = new GameObject[dimensions.x, AttackRowNumbers];
 
             float[] gapArr = { 0.4f, 0.6f };
 
@@ -724,14 +824,31 @@
 
                     arrTowerEnergyEffect[x, y] = Instantiate(energyEffectPrefab);
                     arrTowerEnergyEffect[x, y].transform.position = vpos;
-                    arrTowerEnergyEffect[x, y].Stop();
+
+                    // 创建水精灵充能条
+                    img = Instantiate(FreezeBreathChargePrefab);
+                    img.transform.SetParent(container.transform);
+                    tpos = img.transform.position;
+                    tpos.x = m_arrTowerBulletUIPos[x, y].x + 4.4f + (x - 2) * gapArr[y];
+                    tpos.z = m_arrTowerBulletUIPos[x, y].y + 5f;
+                    tpos.y = 30f;
+                    img.transform.position = tpos;
+                    img.transform.localScale = Vector3.one;
+                    img.transform.localRotation = Quaternion.identity;
+
+                    FreezeBreath freezeBreath = img.GetComponent<FreezeBreath>();
+                    arrTowerFreezeBreathUi[x, y] = freezeBreath;
+                    freezeBreath.gameObject.SetActive(false);
+
+                    arrTowerFreezeBreathEffect[x, y] = Instantiate(FreezeBreathChargeEffect);
+                    arrTowerFreezeBreathEffect[x, y].transform.position = vpos;
                 }
             }
         }
 
         public void updateGridOpenCoin(int ix, int iy)
         {
-            GRID_OPENCASH = Mathf.Floor(GRID_OPENCASH * 1.2f);
+            GRID_OPENCASH = Mathf.Floor(GRID_OPENCASH * 1.25f);
 
             for (int x = 0; x < dimensions.x; x++)
             {
@@ -784,6 +901,30 @@
         }
 
         /// <summary>
+        /// 免费开启所有塔位
+        /// </summary>
+        public void OpenAllTowerGrid()
+        {
+            int sy = dimensions.y - 1;
+
+            for (int tx = 0; tx < dimensions.x; tx++)
+            {
+                if (m_arrGridType[tx, sy - 1] == PlacementGridType.EGridWaitBuy)
+                {
+                    BuyTowerGrid(tx, sy - 1);
+                    Destroy(m_arrTGO[tx, sy - 1].gameObject);
+                    ++GameConfig.EndlessOpenAttackTowerCount;
+                }
+                if (m_arrGridType[tx, sy] == PlacementGridType.EGridWaitBuy)
+                {
+                    BuyTowerGrid(tx, sy);
+                    Destroy(m_arrTGO[tx, sy].gameObject);
+                    ++GameConfig.EndlessOpenAttackTowerCount;
+                }
+            }
+        }
+
+        /// <summary>
         /// 购买对应的待购攻击塔位.
         /// </summary>
         /// <param name="x"></param>
@@ -809,11 +950,9 @@
 
             for (int tx = 0; tx < dimensions.x; tx++)
             {
-                m_arrGridType[tx, sy] = PlacementGridType.EGridWaitBuy;
                 m_arrGridType[tx, sy - 1] = PlacementGridType.EGridWaitBuy;
+                m_arrGridType[tx, sy] = PlacementGridType.EGridOpen;
             }
-            // 设置塔位默认开启,后面需要根据配置来
-            m_arrGridType[2, 3] = PlacementGridType.EGridOpen;
             ++GameConfig.EndlessOpenAttackTowerCount;
         }
 
@@ -829,17 +968,46 @@
 
             if (!arrTowerEnergyEffect[x, dy]) return;
 
+            ParticleSystem ps = arrTowerEnergyEffect[x, dy].GetComponent<ParticleSystem>();
+
+            if (ps == null)
+                ps = arrTowerEnergyEffect[x, dy].transform.GetChild(0).GetComponent<ParticleSystem>();
+
             if (play)
             {
                 if (!arrTowerEnergyEffect[x, dy].gameObject.activeSelf)
                     arrTowerEnergyEffect[x, dy].gameObject.SetActive(true);
-                arrTowerEnergyEffect[x, dy].Play();
+                ps.Play();
             }
             else
             {
 
-                arrTowerEnergyEffect[x, dy].Stop();
+                ps.Stop();
                 arrTowerEnergyEffect[x, dy].gameObject.SetActive(false);
+            }
+        }
+
+        public void PlayFreezeBreathEffect(int x, int y, bool play = true)
+        {
+            int dy = dimensions.y - 1 - y;
+
+            if (!arrTowerFreezeBreathEffect[x, dy]) return;
+
+            ParticleSystem ps = arrTowerFreezeBreathEffect[x, dy].GetComponent<ParticleSystem>();
+
+            if (ps == null)
+                ps = arrTowerFreezeBreathEffect[x, dy].transform.GetChild(0).GetComponent<ParticleSystem>();
+
+            if (play)
+            {
+                if (!arrTowerFreezeBreathEffect[x, dy].gameObject.activeSelf)
+                    arrTowerFreezeBreathEffect[x, dy].gameObject.SetActive(true);
+                ps.Play();
+            }
+            else
+            {
+                ps.Stop();
+                arrTowerFreezeBreathEffect[x, dy].gameObject.SetActive(false);
             }
         }
 
@@ -883,7 +1051,7 @@
                 {
                     for (int x = 0; x < dimensions.x; x++)
                     {
-                        Vector3 targetPos = GridToWorld(new IntVector2(x, y), new IntVector2(1, 1));
+                        Vector3 targetPos = GetGridWorldPos(x, y);
                         targetPos.z -= 1.0f;
                         PlacementTile newTile = Instantiate(tileToUse);
                         newTile.transform.parent = tilesParent.transform;
@@ -892,7 +1060,7 @@
 
                         m_Tiles[x, y] = newTile;
                         newTile.SetTileType(m_arrGridType[x, y]);
-                        newTile.CheckCanPlace(false);//初始化不显示
+                        newTile.SetRender(false, "");//初始化不显示
                     }
                 }
             }
@@ -916,15 +1084,18 @@
                     if (m_arrGridType[ix, y] == PlacementGridType.EGridOpen)
                     {
                         canPlace = true;
-                        for (int i = 0; i < allTowerP.Count; i++)
+                        if (allTowerP != null)
                         {
-                            if (allTowerP[i].x == ix && allTowerP[i].y == y)
+                            for (int i = 0; i < allTowerP.Count; i++)
                             {
-                                canPlace = false;
-                                break;
+                                if (allTowerP[i].x == ix && allTowerP[i].y == y)
+                                {
+                                    canPlace = false;
+                                    break;
+                                }
                             }
                         }
-                        m_Tiles[ix, y].CheckCanPlace(canPlace);
+                        //m_Tiles[ix, y].CheckCanPlace(canPlace);
                     }
                 }
             }
@@ -937,27 +1108,27 @@
         /// <param name="allTowerP"></param>
         public void PlayPS(List<IntVector2> allTowerP)
         {
-            for (int i = 0; i < allTowerP.Count; i++)
-            {
-                m_Tiles[allTowerP[i].x, allTowerP[i].y].SetParticleSystem(true);
-            }
+            // for (int i = 0; i < allTowerP.Count; i++)
+            // {
+            //     m_Tiles[allTowerP[i].x, allTowerP[i].y].SetParticleSystem(true);
+            // }
         }
         /// <summary>
         /// /// 停止所有升级动画
         /// </summary>
         public void StopPS()
         {
-            int iy = dimensions.y - 1;//3
-            for (int ix = 0; ix < dimensions.x; ix++)
-            {
-                for (int y = iy; y >= dimensions.y - AttackRowNumbers; --y)
-                {
-                    if (m_arrGridType[ix, y] == PlacementGridType.EGridOpen)
-                    {
-                        m_Tiles[ix, y].SetParticleSystem(false);
-                    }
-                }
-            }
+            // int iy = dimensions.y - 1;//3
+            // for (int ix = 0; ix < dimensions.x; ix++)
+            // {
+            //     for (int y = iy; y >= dimensions.y - AttackRowNumbers; --y)
+            //     {
+            //         if (m_arrGridType[ix, y] == PlacementGridType.EGridOpen)
+            //         {
+            //             m_Tiles[ix, y].SetParticleSystem(false);
+            //         }
+            //     }
+            // }
         }
 
         /// <summary>
@@ -969,11 +1140,11 @@
         {
             CloseCanPlace();
             currentCanPlace = m_Tiles[x, y];
-            currentCanPlace.SetSelect(true);
-            if (isEmpty)
-            {
-                currentCanPlace.SetTowerVirtualshadow(towerName);
-            }
+            currentCanPlace?.SetRender(true, isEmpty ? towerName : "");
+            // if (isEmpty)
+            // {
+            //     currentCanPlace.SetTowerVirtualshadow(towerName);
+            // }
         }
 
         /// <summary>
@@ -983,7 +1154,7 @@
         {
             if (currentCanPlace != null)
             {
-                currentCanPlace.SetSelect(false);
+                currentCanPlace.SetRender(false, "");
                 currentCanPlace = null;
             }
         }
@@ -1000,11 +1171,12 @@
                 {
                     if (m_arrGridType[ix, y] == PlacementGridType.EGridOpen)
                     {
-                        m_Tiles[ix, y].CheckCanPlace(false);
+                        //m_Tiles[ix, y].CheckCanPlace(false);
+                        m_Tiles[ix, y].SetRender(false, "");
                     }
                 }
             }
-            StopPS();
+            //StopPS();
         }
 
 #if UNITY_EDITOR
@@ -1055,7 +1227,10 @@
                     freePos = gridFreePos;
                 else if (y > 0 && y == dimensions.y - 2)
                     freePos = gridFreePos2;
-
+                else if (y > 0 && y == dimensions.y - 3)
+                    freePos = gridFreePos3;
+                else if (y == dimensions.y - 4)
+                    freePos = gridFreePos4;
                 for (int x = 0; x < dimensions.x; x++)
                 {
                     var position = new Vector3((x + 0.5f) * gridSize, 0, (y + 0.5f) * gridSize + freePos);

--
Gitblit v1.9.1