Пример #1
0
        public void UpdateAdjacentTile(Tile adjacentTile)
        {
            if (adjacentTile.IsVisited) return;
            if (adjacentTile.Type == TileType.Wall) return;

            // Compute adjacente tile distance to exit
            double distance = this.DistanceToExit + adjacentTile.Type.Weight;
            if (distance < adjacentTile.DistanceToExit)
            {
                // Udpate distance
                adjacentTile.DistanceToExit = distance;

                // Reallocate path
                if (adjacentTile.Next != null)
                    adjacentTile.Next.Ancestors.Remove(adjacentTile);

                adjacentTile.Next = this;
                this.Ancestors.Add(adjacentTile);
            }
        }
Пример #2
0
        private void ResetTile(Tile targetTile)
        {
            targetTile.Next = null;
            targetTile.DistanceToExit = double.MaxValue;

            foreach (Tile tile in targetTile.GetNeighbours(this))
            {
                if (tile.Type != TileType.Wall)
                    tile.IsVisited = false;
            }

            targetTile.Ancestors.ForEach(
                tile => this.ResetTile(tile)
            );

            targetTile.Ancestors.Clear();
        }
Пример #3
0
        private LinkedList<Tile> GetPathForTile(Tile tile)
        {
            LinkedList<Tile> newPath = new LinkedList<Tile>();

            while (tile != null)
            {
                newPath.AddLast(tile);
                tile = tile.Next;
            }

            return newPath;
        }
Пример #4
0
        private void InitializeTiles()
        {
            this.Tiles = new Tile[this.Width * this.Height];

            for (int h = 0; h < this.Height; h++)
            {
                for (int w = 0; w < this.Width; w++)
                {
                    Tile tile = new Tile()
                    {
                        Position = new Vector2(w * tileTexture.Width, h * tileTexture.Height),
                        Type = TileType.Free,
                        MapX = w,
                        MapY = h
                    };

                    this.Tiles[h * this.Width + w] = tile;
                }
            }
        }
Пример #5
0
        private void BackwardUpdate(Tile source, Tile target)
        {
            // Check if we should use the source as new path
            if(source.DistanceToExit + target.Type.Weight < target.DistanceToExit) {
                if(target.Next != null) target.Next.Ancestors.Remove(target);
                target.Next = source;
                source.Ancestors.Add(target);
                target.DistanceToExit = source.DistanceToExit + target.Type.Weight;

                foreach (Tile tile in target.GetNeighbours(this))
                {
                    if(tile.Type == TileType.Wall) continue;
                    if(tile == target.Next) continue;

                    this.BackwardUpdate(target, tile);
                }
            }
        }