コード例 #1
0
ファイル: Tile.cs プロジェクト: kulhajs/dungeon
        public bool IsNextTo(Tile tile)
        {
            if (((this.Position.X + TileMap.TILE_SIZE == tile.Position.X) || (this.Position.X - TileMap.TILE_SIZE == tile.Position.X) || (this.Position.X == tile.Position.X))
                && ((this.Position.Y + TileMap.TILE_SIZE == tile.Position.Y) || (this.Position.Y - TileMap.TILE_SIZE == tile.Position.Y) || (this.Position.Y == tile.Position.Y)))
                return true;

            return false;
        }
コード例 #2
0
ファイル: PathFinder.cs プロジェクト: kulhajs/dungeon
        public static List<Tile> FindPath(Tile start, Tile destination, List<Tile> tileMap)
        {
            List<Tile> walkableTiles = tileMap;
            List<Tile> neighbours = new List<Tile>();
            Tile currentTile;

            List<Tile> openTiles = new List<Tile>();
            List<Tile> closedTiles = new List<Tile>();

            start.G = 0;
            openTiles.Add(start);

            while(openTiles.Count > 0)
            {
                openTiles = openTiles.OrderBy(tile => tile.F).ToList();
                currentTile = openTiles[0];

                closedTiles.Add(currentTile);
                openTiles.Remove(currentTile);

                if(currentTile.IsNextTo(destination))
                {
                    destination.ParentTile = currentTile;
                    return GetPath(destination, start, destination);
                }

                neighbours = walkableTiles.Where(tile => tile.IsNextTo(currentTile) && !closedTiles.Contains(tile)).ToList();
                foreach(Tile tile in neighbours)
                {
                    if(openTiles.Contains(tile))
                    {
                        int g = currentTile.G + tile.X == currentTile.X || tile.Y == currentTile.Y ? HORIZONTAL_COST : DIAGONAL_COST;
                        if (g < tile.G) tile.ParentTile = currentTile;
                        else continue;
                    }
                    tile.H = Heuristic(tile, destination);
                    tile.G = currentTile.G + tile.X == currentTile.X || tile.Y == currentTile.Y ? HORIZONTAL_COST : DIAGONAL_COST;
                    tile.F = tile.G + tile.H;

                    tile.ParentTile = currentTile;

                    openTiles.Add(tile);
                }
            }

            return new List<Tile>();
        }
コード例 #3
0
ファイル: PathFinder.cs プロジェクト: kulhajs/dungeon
        private static List<Tile> GetPath(Tile currentTile, Tile start, Tile destination)
        {
            List<Tile> path = new List<Tile>();
            path.Add(currentTile);

            while(currentTile.Position != start.Position)
            {
                currentTile.Color = Color.Red;
                path.Add(currentTile.ParentTile);
                currentTile = currentTile.ParentTile;
            }

            path.Reverse();
            path.Add(destination);

            return path;
        }
コード例 #4
0
ファイル: PathFinder.cs プロジェクト: kulhajs/dungeon
        private static int Heuristic(Tile tile, Tile destinationTile)
        {
            Vector2 tilePosition = tile.Position;

            int steps = 0;

            if(tilePosition.X < destinationTile.X)
            {
                while(tilePosition.X < destinationTile.X)
                {
                    tilePosition.X += TileMap.TILE_SIZE;
                    steps++;
                }
            }
            else if(tilePosition.X > destinationTile.X)
            {
                while(tilePosition.X > destinationTile.X)
                {
                    tilePosition.X -= TileMap.TILE_SIZE;
                    steps++;
                }
            }

            if (tilePosition.Y < destinationTile.Y)
            {
                while (tilePosition.Y < destinationTile.Y)
                {
                    tilePosition.Y += TileMap.TILE_SIZE;
                    steps++;
                }
            }
            else if (tilePosition.Y > destinationTile.Y)
            {
                while (tilePosition.Y > destinationTile.Y)
                {
                    tilePosition.Y -= TileMap.TILE_SIZE;
                    steps++;
                }
            }

            return steps * 10;
        }