Exemplo n.º 1
0
        void loadMap(string mapFilePath)
        {
            string[] lines = File.ReadAllLines(mapFilePath);

            string[] widthAndHeight = lines[0].Split(' ');
            width = int.Parse(widthAndHeight[0]);
            height = int.Parse(widthAndHeight[1]);

            tiles = new MapTile[height, width];

            for (int i = 1; i < height + 1; i++)
            {
                string[] rowOfTiles = lines[i].Split(' ');

                for (int s = 0; s < width; s++)
                {
                    int typeCode = int.Parse(rowOfTiles[s].Substring(0, 1));
                    int pathingCode = int.Parse(rowOfTiles[s].Substring(1, 1));
                    tiles[i - 1, s] = new MapTile(s * tileSize, (i - 1) * tileSize, tileSize, tileSize, typeCode, pathingCode);
                    if (pathingCode == 1)
                        Walls.Add(tiles[i - 1, s]);
                }
            }

            Util.SortByX(Walls);
        }
Exemplo n.º 2
0
 public PathNode(MapTile tile)
 {
     Tile = tile;
 }
Exemplo n.º 3
0
        // find centerpoint of nearest walkable tile from given vector
        public Vector2 FindNearestWalkableTile(Vector2 point)
        {
            int     y    = (int)MathHelper.Clamp(point.Y / tileSize, 0, height - 1);
            int     x    = (int)MathHelper.Clamp(point.X / tileSize, 0, width - 1);
            MapTile tile = tiles[y, x];

            if (tile.Walkable)
            {
                return(tile.CenterPoint);
            }

            MapTile neighbor;

            // find nextdoor neighbor closer to given vector
            float howFarLeft  = tile.CenterPoint.X - point.X;
            float howFarRight = point.X - tile.CenterPoint.X;
            float howFarUp    = tile.CenterPoint.Y - point.Y;
            float howFarDown  = point.Y - tile.CenterPoint.Y;

            float biggest = 0;

            if (howFarLeft > biggest)
            {
                biggest = howFarLeft;
            }
            if (howFarRight > biggest)
            {
                biggest = howFarRight;
            }
            if (howFarUp > biggest)
            {
                biggest = howFarUp;
            }
            if (howFarDown > biggest)
            {
                biggest = howFarDown;
            }

            if (howFarLeft == biggest && x - 1 >= 0)
            {
                neighbor = tiles[y, x - 1];
                if (neighbor.Walkable)
                {
                    return(neighbor.CenterPoint);
                }
            }
            else if (howFarRight == biggest && x + 1 < width)
            {
                neighbor = tiles[y, x + 1];
                if (neighbor.Walkable)
                {
                    return(neighbor.CenterPoint);
                }
            }
            else if (howFarUp == biggest && y - 1 >= 0)
            {
                neighbor = tiles[y - 1, x];
                if (neighbor.Walkable)
                {
                    return(neighbor.CenterPoint);
                }
            }
            else if (howFarDown == biggest && y + 1 < height)
            {
                neighbor = tiles[y + 1, x];
                if (neighbor.Walkable)
                {
                    return(neighbor.CenterPoint);
                }
            }

            // find next closest neighbor
            for (int i = 0; ; i++)
            {
                if (y - i >= 0)
                {
                    neighbor = tiles[y - i, x];
                    if (neighbor.Walkable)
                    {
                        return(neighbor.CenterPoint);
                    }
                }
                if (y + i < height)
                {
                    neighbor = tiles[y + i, x];
                    if (neighbor.Walkable)
                    {
                        return(neighbor.CenterPoint);
                    }
                }
                if (x - i >= 0)
                {
                    neighbor = tiles[y, x - i];
                    if (neighbor.Walkable)
                    {
                        return(neighbor.CenterPoint);
                    }
                }
                if (x + i < width)
                {
                    neighbor = tiles[y, x + i];
                    if (neighbor.Walkable)
                    {
                        return(neighbor.CenterPoint);
                    }
                }
                if (y - i >= 0 && x - i >= 0)
                {
                    neighbor = tiles[y - i, x - i];
                    if (neighbor.Walkable)
                    {
                        return(neighbor.CenterPoint);
                    }
                }
                if (y - i >= 0 && x + i < width)
                {
                    neighbor = tiles[y - i, x + i];
                    if (neighbor.Walkable)
                    {
                        return(neighbor.CenterPoint);
                    }
                }
                if (y + i < height && x - i >= 0)
                {
                    neighbor = tiles[y + i, x - i];
                    if (neighbor.Walkable)
                    {
                        return(neighbor.CenterPoint);
                    }
                }
                if (y + i < height && x + i < width)
                {
                    neighbor = tiles[y + i, x + i];
                    if (neighbor.Walkable)
                    {
                        return(neighbor.CenterPoint);
                    }
                }
            }
        }