格子游戏类型实践(一)框架

发表于2018-10-11
评论0 873浏览
本篇给大家分享下开发格子游戏中的经验,这篇侧重的是其中的框架部分。其实这类游戏的模式都是在维护一个二维数组。所以我们要做一类这种游戏。所以存在一些公共的数据,这里我们新建一个类命名为MainData,这个类存这类游戏所有数据。

具体代码如下:
      public int[,] AllInts;//为0时表示当前位置没有,为1表示已经存在数据。
      public GameObject[,] AllTetrisObjects;//为空 表示没有游戏物体,不为空 表示当前位置存在数据
      public ColorType[,] CurColorTypes;
      public List<OneTetrisData> CurTetrisDatas;
后面的意义都一样,我都不仔细列举。我个人觉得这个类应该只提供一些简单的方法,例如往数组中添加一些数据,然后删除一些数据,还有一个重置和初始化。

所以为了后面重构,个人觉得这些字段都应该把它设置成private的类型,而不是public类型。这里留给你你们想想,因为游戏不是你一个人写嘛,如果改成public的,现在有3个程序员写整个游戏,如果有一个不小心直接MainData.AllInts[i,j]=1,然后另外一个程序员也这样改,现在出错了你知道错误出在哪儿么,如果我们把改变数组的值写出方法的话。出来问题的话我们直接查找引用就能轻松的搞定错误,这里重构的思想就是字段尽量保持私有,属性尽量公有。说了一点没用的。接着说。这个类应该只有一些最基本的方法,这也满足类的功能单一化的原则。

所以方法如下:
   public void IntilizedData(List<GameObject> curlevelOriginObjes)
   public bool AddToGrid(Transform parentTransform, int[,] onetypetetris, GameObject[,] objetetris, int startRow, int startCloum)
   public void RemoveFromGrid(Point pos)
  代码我就直接贴出来吧:
 public class MainData 
{
    public int[,] AllInts;
    public GameObject[,] AllTetrisObjects;
    public ColorType[,] CurColorTypes;
    public List<OneTetrisData> CurTetrisDatas;
    public MainData(List<GameObject> curlevelOriginObjes)
    {
         IntilizedData(curlevelOriginObjes);
    }
    public void IntilizedData(List<GameObject> curlevelOriginObjes)
    {
        CurTetrisDatas = new List<OneTetrisData>();
        CurColorTypes = new ColorType[MathHelper.BoundRowIndex + 2, MathHelper.BoundLineIndex];
        AllInts = new int[MathHelper.BoundRowIndex + 2, MathHelper.BoundLineIndex];
        AllTetrisObjects = new GameObject[MathHelper.BoundRowIndex + 2, MathHelper.BoundLineIndex];
        for (int i = 0; i < MathHelper.BoundRowIndex + 2; i++)
        {
            for (int j = 0; j < MathHelper.BoundLineIndex; j++)
            {
                CurColorTypes[i, j] = ColorType.None;
            }
        }
        if (curlevelOriginObjes != null)
        {
            for (int i = 0; i < curlevelOriginObjes.Count; i++)
            {
                var tem = curlevelOriginObjes[i];
                var data = tem.GetComponent<OneTetrisData>();
                AllInts[data.Pos.X, data.Pos.Y] = 1;
                AllTetrisObjects[data.Pos.X, data.Pos.Y] = tem;
                CurColorTypes[data.Pos.X, data.Pos.Y] = data.CurColorType;
                CurTetrisDatas.Add(data);
            }
        }
    }
    public bool AddToGrid(Transform parentTransform, int[,] onetypetetris, GameObject[,] objetetris, int startRow, int startCloum)
    {
        int row = onetypetetris.GetLength(0);
        int cloum = onetypetetris.GetLength(1);
        GameObject ob=null;
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < cloum; j++)
            {
                int currow = startRow - i;
                int curcloum = startCloum + j;
                if (currow >= MathHelper.BoundRowIndex)
                    return false;
                if (onetypetetris[i, j] != 0)
                {
                    if (ob == null)
                        ob = objetetris[i, j].transform.parent.gameObject;
                    AllInts[currow, curcloum] = onetypetetris[i, j];
                    AllTetrisObjects[currow, curcloum] = objetetris[i, j];
                    objetetris[i, j].transform.SetParent(parentTransform);
                    var curTetrisData = objetetris[i, j].GetComponent<OneTetrisData>();
                    curTetrisData.Pos = new Point(currow, curcloum);
                    CurTetrisDatas.Add(curTetrisData);
                    CurColorTypes[currow, curcloum] = curTetrisData.CurColorType;
                }
            }
        }
        Object.Destroy(ob);
        return true;
    }
    public void RemoveFromGrid(Point pos)
    {
        if (AllInts != null && AllInts[pos.X, pos.Y] != 0)
        {
            AllInts[pos.X, pos.Y] = 0;
            var obj = AllTetrisObjects[pos.X, pos.Y];
            var tetrisdata = obj.GetComponent<OneTetrisData>();
            CurTetrisDatas.Remove(tetrisdata);
            CurColorTypes[pos.X,pos.Y]=ColorType.None;
            Object.Destroy(obj);
            AllTetrisObjects[pos.X, pos.Y] = null;
        }
    }
}
这里还有一些基本的引用没有添加进来大家粘贴完之后可以直接添加引用。
下节我们继续讲怎么写具体的一个游戏的实例。
来自:https://blog.csdn.net/u012565990/article/details/51727411

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引