返回首页DA系统C#IDE文件同步服务屏保 今天是: 2026-05-05    "立夏"  夏季的第一个节气,表示盛夏时节的正式开始

搜索
热搜: linux 技术
Hi~登录注册
查看: 1855|回复: 0

[原创] 【原创】三消类GameManage脚本

[复制链接]
发表于 2024-1-5 16:23:16 | 显示全部楼层 |阅读模式

少侠不来段修仙之旅吗~

您需要 登录 才可以下载或查看,没有帐号?注册成为修仙之旅的少年~

x
//GameManage.cs
//备份-20240105

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 三消游戏总管理层
/// </summary>
public class GameManage : MonoBehaviour
{

    //定义小东西类型的枚举字段
    public enum PintleEnum
    {
        EMPTY,//空物体
        NORMAL,//普通小东西物体
        BARRIER,//障碍物
        ROW_CLEAR,//行清除
        COLUMN_CLEAR,//列清除
        RAINBOWPINTLE,//五消之后的特殊小东西
        COUNT//标记类型
    }

    //结构体的使用需要调用System.Serializable序列化,这个后面教程有讲解
    //定义结构体,因为字典公开访问时,无法在Unity检视面板上看到,所以需要结构体来解决
    [System.Serializable]
    public struct GetDictionaryDateStruct
    {
        public PintleEnum Pintletype;
        public GameObject PintleObj;
    }

    //定义结构体数组,涉及多个类型及小东西obj,所以需要数组保存
    public GetDictionaryDateStruct[] PintleDateStructArray;

    //定义小东西预制体的字典
    public Dictionary<PintleEnum, GameObject> AllPintleDIC;

    public GameObject Cell, BluePintlePreformed;

    //定义游戏面板原点TF位置
    public GameObject PosObj;

    private GameObject InstantiateIsCellObj;

    private int AllCellColumn, AllCellRow;

    //定义小东西在游戏地图的二维数组
    public GamePintle_script[,] PintleMapTwoArray;

    public Transform[,] PintleTransformMapTwoArray;

    public Text TmpText;

    //定义格子增加的距离,以拼成一张地图
    private float TFCountXRow, TFCountYColumn;

    private void Start()
    {
        TmpText.text = "设备宽度: " + Screen.width;
        TFCountXRow = 0;
        TFCountYColumn = 0;

        //声明游戏面板格子的行数和列数--设置游戏格子数为8x8
        AllCellColumn = 9;
        AllCellRow = 9;

        AllPintleDIC = new Dictionary<PintleEnum, GameObject>();

        //实例化小东西在游戏地图的二维数组,第一个维度是列,第二个维度是行
        PintleMapTwoArray = new GamePintle_script[AllCellColumn, AllCellRow];

        PintleTransformMapTwoArray = new Transform[AllCellColumn, AllCellRow];

        //给小东西字典赋值
        for (int i = 0; i < PintleDateStructArray.Length; i++)
        {
            //如果字典不包含结构体现在遍历中的类型,则插入字典
            //结构体的获取字段的方式:结构体.字段名
            if (!AllPintleDIC.ContainsKey(PintleDateStructArray[i].Pintletype))
            {
                AllPintleDIC.Add(PintleDateStructArray[i].Pintletype, PintleDateStructArray[i].PintleObj);
            }
        }

        CreateObjFF();
    }

    public void CreateObjFF()
    {
        //先将统计数返回至原点
        TFCountBackPosFF();

        //Cell格子缩放设置--小东西缩放设置在PintleColor_script脚本的PintleRectTransformScaleChange方法中
        ScreenWidthChangeSizeFF();


        //生成游戏面板-格子面板
        //三消游戏是从上往下掉元素的,所以最后遍历都是从上往下掉,从左到右的方向
        for (int x = 0; x < AllCellColumn - 1; x++)
        {
            for (int y = AllCellRow - 1; y > 0; y--)
            {

                /*
                //确认运行平台系统,根据安卓或者PC判断赋值不同的位置距离
                if (Application.platform == RuntimePlatform.Android)
                {
                    //套了Canvas需要计算的距离~~唉
                    InstantiateIsCellObj = GameObject.Instantiate(Cell, new Vector3(PosObj.transform.position.x + (x / 1.68f), PosObj.transform.position.y + (y / 1.72f), PosObj.transform.position.z), Quaternion.identity, this.transform);
                }
                else
                {
                    //在PosObj位置基础上,根据两个for嵌套循环,生成格子
                    //x方向需要除去1.938f,y方向需要除去2f(套了Canvas需要计算的距离~~唉)
                    InstantiateIsCellObj = GameObject.Instantiate(Cell, new Vector3(PosObj.transform.position.x + (x / 1.135f), PosObj.transform.position.y + (y / 1.2f), PosObj.transform.position.z), Quaternion.identity, this.transform);

                }
                */

                //教程测试通过方法!!
                //InstantiateIsCellObj = GameObject.Instantiate(Cell, TFChange(x, y), Quaternion.identity, this.transform);            

                TFCountXRowZJFF();


                InstantiateIsCellObj = GameObject.Instantiate(Cell, Vector3.zero, Quaternion.identity, this.transform);
                InstantiateIsCellObj.GetComponent<RectTransform>().anchoredPosition = new Vector2(TFCountXRow, TFCountYColumn);

                //将真实的位置信息赋值给二维数组
                PintleTransformMapTwoArray[x, y] = InstantiateIsCellObj.transform;
            }

            TFCountYColumnZJFF();


        }

        TFCountBackPosFF();

        //生成小东西并且赋值给游戏二维数组地图
        //三消游戏是从上往下掉元素的,所以最后遍历都是从上往下掉,从左到右的方向
        for (int x = 0; x < AllCellColumn - 1; x++)
        {
            for (int y = AllCellRow - 1; y > 0; y--)
            {

                /*
                //确认运行平台系统,根据安卓或者PC判断赋值不同的位置距离
                if (Application.platform == RuntimePlatform.Android)
                {
                    //根据枚举查找小东西字典获取obj--安卓位置
                    //套了Canvas需要计算的距离~~唉
                    PintleMapTwoArray[x, y] = GameObject.Instantiate(AllPintleDIC[PintleEnum.NORMAL], new Vector3(PosObj.transform.position.x + (x / 1.68f), PosObj.transform.position.y + (y / 1.72f), PosObj.transform.position.z), Quaternion.identity, this.transform).GetComponent<GamePintle_script>();
                }
                else
                {
                    //根据枚举查找小东西字典获取obj
                    //x方向需要除去1.938f,y方向需要除去2f(套了Canvas需要计算的距离~~唉)
                    PintleMapTwoArray[x, y] = GameObject.Instantiate(AllPintleDIC[PintleEnum.NORMAL], new Vector3(PosObj.transform.position.x + (x / 1.135f), PosObj.transform.position.y + (y / 1.2f), PosObj.transform.position.z), Quaternion.identity, this.transform).GetComponent<GamePintle_script>();
                }
                */

                //教程测试通过方法!!
                //PintleMapTwoArray[x, y] = GameObject.Instantiate(AllPintleDIC[PintleEnum.NORMAL], TFChange(x, y), Quaternion.identity, this.transform).GetComponent<GamePintle_script>();

                /*
                PintleMapTwoArray[x, y] = GameObject.Instantiate(AllPintleDIC[PintleEnum.NORMAL], Vector3.zero, Quaternion.identity, this.transform).GetComponent<GamePintle_script>();

                TFCountXRowZJFF();

                //每个小东西的位置改变为对的位置--纠正位置
                PintleMapTwoArray[x, y].GetComponent<RectTransform>().anchoredPosition = new Vector2(TFCountXRow, TFCountYColumn);

                //小东西生成后马上调用挂载在小东西脚本的初始化方法
                PintleMapTwoArray[x, y].PintleInit(x, y, PintleEnum.NORMAL, this);

                /*
                if (PintleMapTwoArray[x, y].CanYouMove() == true)
                {
                    //print(PintleMapTwoArray[x, y].name + "小东西可以移动");
                    PintleMapTwoArray[x, y].PintleMoveVar.PintleMoveFF(x, y);
                }
                */


                /*
                if (PintleMapTwoArray[x, y].CanYouChangeColor()==true)
                {
                    PintleMapTwoArray[x, y].PintleColorVar.SetColorFF((PintleColor_script.PintleColorEnum)Random.Range(0, PintleMapTwoArray[x, y].PintleColorVar.PintleColorMaxNum));
                }
                */


                //CreatePintleObjFF(x, y, PintleEnum.NORMAL);
                CreatePintleObjFF(x, y, PintleEnum.EMPTY);



            }

            TFCountYColumnZJFF();
        }
    }

    public Vector3 TFChange(int x, int y)
    {
        //确认运行平台系统,根据安卓或者PC判断赋值不同的位置距离
        if (Application.platform == RuntimePlatform.Android)
        {
            return new Vector3(PosObj.transform.position.x + (x / 1.68f), PosObj.transform.position.y + (y / 1.72f), PosObj.transform.position.z);
            //return new Vector3(PosObj.transform.localPosition.x + (x / 1.68f), PosObj.transform.localPosition.y + (y / 1.72f), PosObj.transform.localPosition.z);
            //return new Vector3(x / 1.68f, y / 1.72f, 0);
            //return new Vector3(transform.position.x - x / 7.5f + (x - 2), transform.position.y + y / 7.5f - y);
        }
        else
        {
            print(PosObj.GetComponent<RectTransform>().anchoredPosition.x);

            return new Vector3(PosObj.transform.position.x + (x / 1.135f), PosObj.transform.position.y + (y / 1.2f), PosObj.transform.position.z);
            //return new Vector3(x / 1.135f,y / 1.2f, 0);
            //return new Vector3(transform.position.x -x / 7.5f + (x-2), transform.position.y + y / 7.5f - y);
            //return new Vector2(PosObj.GetComponent<RectTransform>().anchoredPosition.x + 191, transform.position.y + y / 7.5f - y);

        }

    }

    public void ScreenWidthChangeSizeFF()
    {
        if (Screen.width == 1440)
        {
            Cell.GetComponent<RectTransform>().localScale = new Vector3(106, 106);
        }
        else
        {
            Cell.GetComponent<RectTransform>().localScale = new Vector3(80, 80);
            print(Cell.GetComponent<RectTransform>().localScale);
        }
    }

    /// <summary>
    /// X轴增加位置的方法
    /// </summary>
    public void TFCountXRowZJFF()
    {
        if (Screen.width > 1080)
        {
            TFCountXRow = TFCountXRow + 165;
        }
        else
        {
            TFCountXRow = TFCountXRow + 128;
        }
    }


    /// <summary>
    /// Y轴增加位置的方法
    /// </summary>
    public void TFCountYColumnZJFF()
    {
        //每次执行Y轴位置时,X轴先要归位,因为X要重头开始for循环
        if (Screen.width > 1080)
        {
            TFCountXRow = -239;
        }
        else
        {
            TFCountXRow = -50;
        }


        if (Screen.width > 1080)
        {
            TFCountYColumn = TFCountYColumn + 164;
        }
        else
        {
            TFCountYColumn = TFCountYColumn + 123;
        }
    }

    /// <summary>
    /// 返回原点位置的方法
    /// </summary>
    public void TFCountBackPosFF()
    {
        //定义原点-测试
        if (Screen.width > 1080)
        {
            TFCountXRow = -239;
        }
        else
        {
            TFCountXRow = -50;
        }

        TFCountYColumn = 120;
    }


    public GamePintle_script CreatePintleObjFF(int x,int y,PintleEnum PintleType)
    {
        GameObject newPintleObj = GameObject.Instantiate(AllPintleDIC[PintleType], PintleTransformMapTwoArray[x, y].position, Quaternion.identity, this.transform);

        PintleMapTwoArray[x, y] = newPintleObj.GetComponent<GamePintle_script>();
        PintleMapTwoArray[x, y].PintleInit(x, y, PintleType, this);

        if (PintleMapTwoArray[x, y].CanYouChangeColor() == true)
        {
            PintleMapTwoArray[x, y].PintleColorVar.SetColorFF((PintleColor_script.PintleColorEnum)Random.Range(0, PintleMapTwoArray[x, y].PintleColorVar.PintleColorMaxNum));
        }

        return PintleMapTwoArray[x, y];
    }

}


游客
回复
*滑块验证:

DA论坛飞机票来了~
快速回复 返回顶部 返回列表