Esempio n. 1
0
    // Use this for initialization
    void Start()
    {
        //初始化地图信息和格子位置
        Cell[] cells = GetComponentsInChildren <Cell>();
        for (var i = 0; i < cells.Length; i++)
        {
            Cell    _cell  = cells[i];
            var     column = i % NUM_PER_ROW;
            var     row    = i / NUM_PER_ROW;
            _Vector pos    = new _Vector(column, row);
            //Debug.Log(_cell.name + ",位置:" + pos);
            _cell.Position = pos;   //设置格子的位置
            _cells.Add(pos, _cell); //添加到地图信息
        }

        /* 测试代码
         * _Vector start = new _Vector(1, 1);
         * _Vector end = new _Vector(2, 2);
         * PrintFath(FindPath(start, end));
         */
        int index = 2;

        int[] ars = { 1, 2, 3, 4, 5, 6, 0, 0, 0 };

        Array.Copy(ars, index, ars, index + 1, 4 - index);//1,2,3,3,4,5,6,0,0,

        StringBuilder sb = new StringBuilder();

        foreach (var a in ars)
        {
            sb.Append(a + ",");
        }
        Debug.Log(sb.ToString());
    }
Esempio n. 2
0
    //A*算法的自动寻路
    public Stack <Cell> FindPath(_Vector start, _Vector end)
    {
        BNode    first = new BNode(start);    //角色为起始点
        NodeList nodes = new NodeList(first); //添加起始点到open列表中

        while (nodes.OpenCount > 0)
        {
            BNode current = nodes.PopToClosed();
            //Debug.Log("取点:" + current.position);
            //获取四周相邻节点
            foreach (var d in _Vector.Directions)
            {
                var  tmp = current.position + d;
                Cell tmpI;
                //tmp是在界内,并且是可通过的(不格挡)
                if (_cells.TryGetValue(tmp, out tmpI) && !tmpI.IsBlock)
                {
                    BNode n = new BNode(tmp)
                    {
                        G      = current.G + 1,
                        H      = tmp.Distance(end),
                        parent = current
                    };
                    if (tmp.Equals(end))  //找到目的地
                    {
                        return(GetResult(n));
                    }
                    //插入列表中,Insert中还有过滤功能
                    nodes.Insert(n);
                }
            }
        }
        Debug.LogFormat("{0}到{1}找不到出路", start, end);
        return(null);
    }
Esempio n. 3
0
 public override bool Equals(object obj)
 {
     if (obj is _Vector)
     {
         _Vector equalTo = obj as _Vector;
         if (row == equalTo.row && column == equalTo.column)
         {
             return(true);
         }
     }
     return(false);
 }
Esempio n. 4
0
    //是否能扩散(上下可以向三个方向扩散,其余只能扩散左右两个方向)
    //param: center中心点,dir方向
    public bool Spread(_Vector dir, _Vector center)
    {
        _Vector diff = this - center;

        //上下可以向三个方向扩散
        if (diff.column == 0)
        {
            // > 0 表示同向 , = 0 表示左右
            return(diff.row * dir.row >= 0);
        }
        //左右只能往同向方向移动
        return(diff.column * dir.column > 0);
    }
Esempio n. 5
0
 /*
  * 计算两点之间的二进制距离
  * 例如: (0,0), (2,3)
  *      距离d = 5, 二进制距离bd = 10000b (16d)
  */
 public int BDistance(_Vector target)
 {
     return(ToBinary(Distance(target)));
 }
Esempio n. 6
0
 //计算两点之间的距离
 public int Distance(_Vector target)
 {
     return(Math.Abs(row - target.row) + Math.Abs(column - target.column));
 }
Esempio n. 7
0
 //判断src与dst的距离,是否在攻击范围scope之内
 public static bool InScope(this _Vector src, _Vector dst, int scope)
 {
     return((src.BDistance(dst) & scope) > 0);
 }
Esempio n. 8
0
 public BNode(_Vector position)
 {
     this.position = position;
 }