zoukankan      html  css  js  c++  java
  • 【自走棋】地图格子高亮效果

    <1>效果:

    <2>思路:

    需求把地图划分成均匀网格,放置玩家到任意一个网格,显示玩家可攻击范围所有网格高亮(放置)

    1.如何划分网格?

    定义行和列最大数量(自走棋row=8 col=8)

    定义起始位置Vector3 startPos 从起始点 向X轴正方向延伸列 向Z轴正方向延伸行

    为什么单独实例化每个格子?方便做高亮效果,开销也不大,替换材质球,dc不高

        //行
        private int row = 8;
        //列
        private int col = 8;
        //缓存obj
        private List<GameObject> cells = new List<GameObject>();
        //起始位置
        private Vector3 startPos = Vector3.zero;
        //初始化地图格子 从起始点 向X轴正方向延伸列 向Z轴正方向延伸行
        void InitCells()
        {
            int num = row * col;
            for (int i = 0; i < num; i++)
            {
                GameObject go = GameObject.Instantiate(tempCell) as GameObject;
                cells.Add(go);
                int index = i + 1;
                go.name = index.ToString();
                int grow = getRow(index);
                int gcol = getCol(index);
                go.transform.position = new Vector3(gcol, 0, grow);
                Color color = (index + grow) % 2 == 0 ? Color.blue : Color.green;
                int state = (index + grow) % 2 == 0 ? 1 : 2;
                setMaterial(go, color, state);
            }
        }
    

      

    2.如何确定攻击范围?

    这个问题需要与策划商量,一种方式是给每个棋子攻击范围公式,二是策划配置攻击范围格子(因为有些攻击范围很奇葩)

    如果使用第二种方式,配置一张表,最大和最小攻击格子都在其中,15*15=225

    策划需要配置什么形状的攻击范围,都由策划配置在每个棋子的攻击范围列表中

    例如3种攻击范围:索引到下面的map

        private List<int> atkLst1 = new List<int>() { 0, 2, 3, 4, 5, 6, 7, 8, 9 };//上缺1
        private List<int> atkLst2 = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8 };//九宫格
        private List<int> atkLst3 = new List<int>() { 0, 1, 2, 3, 4 };//十字形
    

      

        private Dictionary<int, int[]> map = new Dictionary<int, int[]>();
        //攻击范围格子索引 配置一张表 225个元素 15*15
        void init()
        {
            map.Add(0, new int[2] { 0, 0 });
            map.Add(1, new int[2] { 1, 0 });
            map.Add(2, new int[2] { -1, 0 });
            map.Add(3, new int[2] { 0, 1 });
            map.Add(4, new int[2] { 0, -1 });
            map.Add(5, new int[2] { -1, 1 });
            map.Add(6, new int[2] { -1, -1 });
            map.Add(7, new int[2] { 1, 1 });
            map.Add(8, new int[2] { 1, -1 });
            map.Add(9, new int[2] { 2, 0 });
        }
    

      

    <3>怎么显示攻击范围高亮?

    1.需要取到攻击范围格子

    取到放置格子的row,col,遍历攻击范围格子索引,求出每个攻击格子的相对放置格子索引,验证求得的格子是否合法

    回退之前的高亮格子,高亮当前攻击范围格子

    2.高亮替换格子材质(dc并不高,3中材质互换)

        //index = 棋子放置所在格子  attackLst = 玩家攻击范围格子索引列表
    //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);测试鼠标选中格子高亮范围
    private void showVaildCells(int index, List<int> attackLst) { int srow = getRow(index); int scol = getCol(index); List<int> list = new List<int>(); int[] delta = new int[2]; for (int i = 0; i < attackLst.Count; i++) { int[] xy = map[attackLst[i]]; delta[0] = xy[0] + srow - 1; delta[1] = xy[1] + scol; if (isVaildIndex(delta)) { list.Add(delta[0] * row + delta[1]); } } showLst(list); } private bool isVaildIndex(int[] xy) { return xy[0] >= 0 && xy[0] < row && xy[1] > 0 && xy[1] <= col; }

      

  • 相关阅读:
    centos安装libreoffice
    世界,你好!
    4.闭包函数
    3.回调函数
    1内存地址
    2.函数递归
    1.字典
    nonlocal可以修改外层函数变量
    单例模式(Singleton)示例源码
    大家来说说自己的WEB开发工具好吗?
  • 原文地址:https://www.cnblogs.com/cocotang/p/10825340.html
Copyright © 2011-2022 走看看