Mesh CreateHexagonMeshWithoutUVColor()
    {
        Mesh mesh = new Mesh();

        mesh.name = "Hexagon";

        List <Vector3> vertices  = new List <Vector3>();
        List <int>     triangles = new List <int>();

        for (int col = 0; col < width; ++col)
        {
            for (int row = 0; row < height; ++row)
            {
                Coord     local  = new Coord(col, row);
                Coord     world  = new Coord(col + xMin, row + yMin);
                MapTileVO tileVo = GameFacade.GetProxy <MapProxy>().GetTile(world);
                if (tileVo != null)
                {
                    AddHexagonVertices(vertices, triangles, layout.HexCenter(local));
                }
            }
        }

        mesh.vertices  = vertices.ToArray();
        mesh.triangles = triangles.ToArray();
        return(mesh);
    }
 public void InitBlocks()
 {
     for (int x = 0; x < xTileCnt; ++x)
     {
         for (int y = 0; y < yTileCnt; ++y)
         {
             Coord     c      = new Coord(x + xTileCnt * xIdx, y + yTileCnt * yIdx);
             MapTileVO tileVo = GameFacade.GetProxy <MapProxy>().GetTile(c);
             if (tileVo != null)
             {
                 if (!tileVo.IsWater())
                 {
                     SpriteRenderer block = CreateBlock(tileVo);
                     block.transform.parent = blockRoot;
                     block.gameObject.SetActive(true);
                     block.transform.localPosition = layout.HexCenter(tileVo.coord) - this.transform.localPosition;
                     blocks.Add(c, block);
                 }
             }
         }
     }
 }
Example #3
0
    public List <Coord> CalcPathImp(Coord src, Coord dst)
    {
        ResetContext();

        var sharedLayout = new MapLayout(1f / 1.732f);
        Func <int, int, float> distanceCalculator = (int x, int y) =>
        {
            return((sharedLayout.HexCenter(new Coord(x, y)) - sharedLayout.HexCenter(dst)).magnitude);
        };

        MinHeap <uint> opened = new MinHeap <uint>((uint lhr, uint rhr) =>
        {
            int lhr_x = 0, lhr_y = 0, rhr_x = 0, rhr_y = 0;
            ExtractCoord(lhr, out lhr_x, out lhr_y);
            ExtractCoord(rhr, out rhr_x, out rhr_y);
            var lhr_dis = GetFootprintLen(lhr) + distanceCalculator.Invoke(lhr_x, lhr_y);
            var rhr_dis = GetFootprintLen(rhr) + distanceCalculator.Invoke(rhr_x, rhr_y);
            return(lhr_dis < rhr_dis);
        });

        Func <int, int, int, bool> adjacentChecker = (int x, int y, int radition) =>
        {
            MapTileVO tileVO = GameFacade.GetProxy <MapProxy>().GetTile(NextX(x, y, radition, true), NextY(x, y, radition, true));
            return(tileVO != null && tileVO.type != MapTileType.Block);
        };

        Func <int, int, int, bool> leapfrogChecker = (int x, int y, int leap) =>
        {
            int p1 = (leap + 4) % 6;
            p1 = (0 == p1) ? 6 : p1;
            int p2 = (leap + 5) % 6;
            p2 = (0 == p2) ? 6 : p2;

            return(adjacentChecker.Invoke(NextX(x, y, leap, false), NextY(x, y, leap, false), 0) &&
                   adjacentChecker.Invoke(x, y, p1) &&
                   adjacentChecker.Invoke(x, y, p2));
        };

        Utils.Assert(adjacentChecker.Invoke(dst.x, dst.y, 0));
        SetActiveMask(src.x, src.y, true);
        opened.Insert(GetCellMask(src.x, src.y));
        SetOpenMask(src.x, src.y, true);
        SetFootprintLen(GetCellMask(src.x, src.y), 0f);

        while (opened.HeapLen > 0)
        {
            uint curMask = opened.ExtractMin();
            int  curX = 0, curY = 0;
            ExtractCoord(curMask, out curX, out curY);

            for (int i = 1; i <= 6; ++i)
            {
                int adjacentX = NextX(curX, curY, i, true);
                int adjacentY = NextY(curX, curY, i, true);
                if (adjacentX < 0 || adjacentX >= MapConst.MAP_WIDTH ||
                    adjacentY < 0 || adjacentY >= MapConst.MAP_HEIGHT)
                {
                    continue;
                }
                uint adjacentMask = GetCellMask(adjacentX, adjacentY);
                if (IsActive(adjacentMask) && !isOpened(adjacentMask))
                {
                    continue;
                }
                if (adjacentX == dst.x && adjacentY == dst.y)
                {
                    curMask = SetPredecessor(adjacentX, adjacentY, ReversePredecessor(i), true);
                    return(ConstructPath(adjacentX, adjacentY));
                }
                if (!adjacentChecker.Invoke(curX, curY, i))
                {
                    continue;
                }
                TouchNext(opened, curMask, GetCellMask(adjacentX, adjacentY), ReversePredecessor(i), true);
            }

            for (int i = 1; i <= 6; ++i)
            {
                int leapfrogX = NextX(curX, curY, i, false);
                int leapfrogY = NextY(curX, curY, i, false);
                if (leapfrogX < 0 || leapfrogX >= MapConst.MAP_WIDTH ||
                    leapfrogY < 0 || leapfrogY >= MapConst.MAP_HEIGHT)
                {
                    continue;
                }
                uint leapfrogMask = GetCellMask(leapfrogX, leapfrogY);
                if (IsActive(leapfrogMask) && !isOpened(leapfrogMask))
                {
                    continue;
                }
                if (leapfrogX == dst.x && leapfrogY == dst.y)
                {
                    curMask = SetPredecessor(leapfrogX, leapfrogY, ReversePredecessor(i), false);
                    return(ConstructPath(leapfrogX, leapfrogY));
                }
                if (!leapfrogChecker.Invoke(curX, curY, i))
                {
                    continue;
                }
                TouchNext(opened, curMask, GetCellMask(leapfrogX, leapfrogY), ReversePredecessor(i), false);
            }
            curMask = SetOpenMask(curMask, false);
        }

        return(null);
    }