Exemple #1
0
 public IEnumerable<Tile> GetNeighbours(TileMap map)
 {
     if (this.MapX > 0) yield return map[this.MapX - 1, this.MapY];
     if (this.MapX < map.Width - 1) yield return map[this.MapX + 1, this.MapY];
     if (this.MapY > 0) yield return map[this.MapX, this.MapY - 1];
     if (this.MapY < map.Height - 1) yield return map[this.MapX, this.MapY + 1];
 }
Exemple #2
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // Debug component
            this.Components.Add(new DebugComponent(this));

            // Controller components
            this.Components.Add(new MouseManager(this));
            this.Components.Add(new KeyboardManager(this));

            // TileMap
            this.tileMap = new TileMap(this, 60, 40);
            this.Components.Add(tileMap);

            // FPS counter
            this.Components.Add(new FrameRateCounter(this));

            base.Initialize();
        }
        public static void RandomizeTiles(TileMap tileMap)
        {
            // Randomize entry & exit
            tileMap.Entry = tileMap[random.Next(0, tileMap.Width), random.Next(0, tileMap.Height)];
            tileMap.Exit = tileMap.Entry;

            while (tileMap.Exit == tileMap.Entry)
            {
                tileMap.Exit = tileMap[random.Next(0, tileMap.Width), random.Next(0, tileMap.Height)];
            }

            tileMap.Entry.Type = TileType.Entry;
            tileMap.Exit.Type = TileType.Exit;

            AddRandomWalls(tileMap);

            tileMap.PathOrigin = tileMap.Entry;
        }
        private static void ClearWalls(TileMap tileMap)
        {
            foreach (Tile tile in tileMap.Tiles)
            {
                tile.IsVisited = false;
                tile.Ancestors.Clear();
                tile.Next = null;

                if (tile.Type == TileType.Exit)
                {
                    tile.DistanceToExit = 0;
                }
                else
                {
                    tile.DistanceToExit = double.MaxValue;
                }

                if (tile.Type == TileType.Wall)
                {
                    tile.Type = TileType.Free;
                }
            }
        }
        private static void AddRandomWalls(TileMap tileMap)
        {
            do
            {
                ClearWalls(tileMap);

                foreach (Tile tile in tileMap.Tiles)
                {
                    if (tile.Type == TileType.Entry || tile.Type == TileType.Exit)
                        continue;

                    if (random.NextDouble() < 0.3)
                        tile.Type = TileType.Wall;
                }

                tileMap.UpdateTiles();
            } while (tileMap.Entry.Next == null);

            foreach (Tile tile in tileMap.Tiles)
            {
                if (tile.Type == TileType.Free && tile.Next == null)
                    tile.Type = TileType.Wall;
            }
        }
Exemple #6
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            MouseManager mouseManager = this.Services.GetService<MouseManager>();
            KeyboardManager keyboardManager = this.Services.GetService<KeyboardManager>();

            // Allows the game to exit
            if (keyboardManager.CurrentState.IsKeyDown(Keys.Escape))
                this.Exit();

            if (keyboardManager.IsKeyPressed(Keys.R) && keyboardManager.CurrentState.IsKeyDown(Keys.LeftControl))
            {
                this.Components.Remove(this.tileMap);
                this.tileMap = new TileMap(this, 60, 40);
                this.Components.Add(this.tileMap);
            }

            if (mouseManager.CurrentState.X > 0 && mouseManager.CurrentState.X < GraphicsDevice.Viewport.Width && mouseManager.CurrentState.Y > 0 && mouseManager.CurrentState.Y < GraphicsDevice.Viewport.Height)
            {
                Tile hitTile = tileMap.GetTileAt(mouseManager.CurrentState.X, mouseManager.CurrentState.Y);
                tileMap.HoverTile = hitTile;

                if (hitTile != null)
                {
                    if (mouseManager.LeftClick)
                    {
                        if (keyboardManager.CurrentState.IsKeyDown(Keys.LeftControl))
                        {
                            if (hitTile.Type == TileType.Free)
                                this.tileMap.AddWall(hitTile.MapX, hitTile.MapY);
                            else if (hitTile.Type == TileType.Wall)
                                this.tileMap.RemoveWall(hitTile.MapX, hitTile.MapY);
                        }
                        else
                        {
                            this.tileMap.PathOrigin = hitTile;
                        }
                    }
                }
            }

            base.Update(gameTime);
        }