三消游戏核心逻辑

三消控制器,判断死图那里Item没有重置回去,写的时候要注意

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using Kernel.core;
namespace Kernel.MusicGame
{
   
    public enum ThreeRemoveType
    {
   
        level1 = 0,
        level2 = 1,
        level3 = 2,
        level4 = 3,
        level5 = 4
    }
    public partial class ThreeRemoveCon : MonoBehaviour
    {
   

        public Transform ItemPar;

        public List<Sprite> splist = new List<Sprite> {
    };
        public int tableRow = 7;
        public int tableCol = 7;
        //偏移量
        public Vector2 offset = new Vector2(0, 0);
        //生成物体的宽高
        public int width, height;
        //所有的Item
        public ThreeRemoveItem[,] allItems;
        //所有的Iten坐标
        public Vector3[,] allPos;
        //相同Item列表
        public List<ThreeRemoveItem> sameList = new List<ThreeRemoveItem> {
    };
        //要清除的Item列表
        public List<ThreeRemoveItem> boomList = new List<ThreeRemoveItem> {
    };
        //是否正在操作
        bool isOperation = true;
        //是否在清除
        bool allBoom = false;

        bool isFDec = true;
        float FDecTimer = 0f;
        public Image mask;

        //摧毁的种类列表
        Dictionary<int, List<ThreeRemoveItem>> boomDic = new Dictionary<int, List<ThreeRemoveItem>> {
    };

        bool isGameStart = false;
        public Text vicScore;
        public int allScore;
        public Text score;
        public Text txtreduceTimer;
        float reduceTimer;
        public Button btnSkip;
        public Button btnRest;

        public UIThreeRemovePanel threeRemovePanel;
        KTThreeRemoveCfg cfg;

        private void Awake()
        {
   
            allItems = new ThreeRemoveItem[tableRow, tableCol];
            allPos = new Vector3[tableRow, tableCol];
            btnSkip.onClick.AddListener(ClickSkip);
            btnRest.onClick.AddListener(ResetGame);
        }


        public void StartNewGame()
        {
   
            ResetProperty();
            InitGame();
            AllBoom();
        }

        public void ResetProperty()
        {
   
            btnSkip.interactable = true;
            isGameStart = false;
            isFDec = true;
            FDecTimer = 0f;
            allScore = 0;
            mask.fillAmount = 1f;
            cfg = threeRemovePanel.curRoadCfg;
            vicScore.text = cfg.victory.ToString();
            reduceTimer = cfg.songLength;
        }

        public void InitGame()
        {
   
            for (int i = 0; i < tableRow; i++)
            {
   
                for (int j = 0; j < tableCol; j++)
                {
   
                    if (allItems[i, j] != null)
                    {
   
                        ThreeRemovePool.instance.AddPool(allItems[i, j]);
                        allItems[i, j] = null;
                    }
                }
            }


            for (int i = 0; i < tableRow; i++)
            {
   
                for (int j = 0; j < tableCol; j++)
                {
   
                    ThreeRemoveItem item = ThreeRemovePool.instance.GetPool();
                    item.transform.SetParent(ItemPar);
                    item.transform.localPosition = new Vector3(j * width, i * height, 0) + new Vector3(offset.x, offset.y, 0)+new Vector3(i*2.5f,j*2.5f,0);
                    //随机图案编号
                    int randomsp = Random.Range(0, splist.Count);
                    item.Init(i, j, splist[randomsp], (ThreeRemoveType)randomsp, Click);
                    allItems[i, j] = item;
                    allPos[i, j] = item.transform.position;
                }
            }
        }

        public void AllBoom()
        {
   
            //有消除
            bool hasBoom = false;
            foreach (var item in allItems)
            {
   
                if (item && !item.hasCheck)
                {
   
                    //检测周围的消除
                    CheckAround(item);
                    if (boomList.Count > 0)
                    {
   
                        hasBoom = true;
                        isOperation = true;
                    }
                }
            }
            if (!hasBoom)
            {
   
                isOperation = false;
            }

            FDecTimer = 0f;
        }





        public void Click(ThreeRemoveItem item)
        {
   
            if (isOperation)
                return;
            isOperation = true;
            Vector2 dir = item.GetDirection();
            //点击异常处理
            if (dir.magnitude != 1)
            {
   
                isOperation = false;
                return;
            }
            //开启协程
            StartCoroutine(ItemExchange(item, dir));
        }

        /// <summary>
        /// Item交换
        /// </summary>
        /// <returns>The exchange.</returns>
        /// <param name="dir">Dir.</param>
        IEnumerator ItemExchange(ThreeRemoveItem item, Vector2 dir)
        {
   
            //获取目标行列
            int targetRow = item.itemRow + System.Convert.ToInt32(dir.y);
            int targetColumn = item.itemColumn + System.Convert.ToInt32(dir.x);
            //检测合法
            bool isLagal = CheckRCLegal(targetRow, targetColumn);
            if (!isLagal)
            {
   
                isOperation = false;
                //不合法跳出
                yield break;
            }
            //获取目标
            ThreeRemoveItem target = allItems[targetRow, targetColumn];
            //从全局列表中获取当前item,查看是否已经被消除,被消除后不能再交换
            ThreeRemoveItem myItem = allItems[item.itemRow, item.itemColumn];
            if (!target || !myItem)
            {
   
                isOperation = false;
                //Item已经被消除
                yield break;
            }
            //相互移动
            target.ItemMove(item.itemRow, item.itemColumn, allPos[item.itemRow, item.itemColumn]);
            AddAllItems(item.itemRow, item.itemColumn, target);
            myItem.ItemMove(targetRow, targetColumn, allPos[targetRow, targetColumn]);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值