public LevelManager(Game game) { gameRef = (MainGame)game; var tutLevel = new TileMap(0, 0); tutLevel.Load(@"test_level.slf"); levels.Add(tutLevel.GUID, tutLevel); levelIds.Add("tutorial level", tutLevel.GUID); //var connect_1Level = new TileMap(0, 0); //connect_1Level.Load(@"connect_1.slf"); //levels.Add(connect_1Level.GUID, connect_1Level); //levelIds.Add("connect 1 level", connect_1Level.GUID); var obstacle_1Level = new TileMap(0, 0); obstacle_1Level.Load(@"obstacle_1.slf"); levels.Add(obstacle_1Level.GUID, obstacle_1Level); levelIds.Add("obstacle 1 level", obstacle_1Level.GUID); levels[levelIds["tutorial level"]].AddExitZone(new ExitZone(new Rectangle(tutLevel.WidthInTiles - 1, 3, 1, 2), levelIds["tutorial level"], levelIds["obstacle 1 level"])); levels[levelIds["obstacle 1 level"]].AddExitZone(new ExitZone(new Rectangle(0, 1, 1, 2), levelIds["obstacle 1 level"], levelIds["tutorial level"])); ChangeLevel(levelIds["Tutorial Level".ToLower()]); red = new DrawableRectangle(gameRef.GraphicsDevice, new Vector2(10, 10), Color.White, true).Texture; }
public Pathfinder(TileMap map) { levelWidth = map.WidthInTiles; levelHeight = map.HeightInTiles; InitializeSearchNodes(map); }
public TileSelector(MainGame game, Vector2 startPos) { gameRef = game; levelRef = game.level; selectedIndex = gameRef.SelectedTileIndex; this.startPos = startPos; if (gameRef.level.CurrentTileSet != null) loadedTileSet = gameRef.level.CurrentTileSet.Image; plainWhite = new DrawableRectangle(gameRef.GraphicsDevice, Vector2.One, Color.White, true).Texture; }
protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); var fs = File.Open(@"C:\Users\ANTHONY\Downloads\tilesets2\tileset1.png", FileMode.Open); Texture2D tilesetTexture = Texture2D.FromStream(GraphicsDevice, fs); tileSet = new TileSet(tilesetTexture, Engine.TileWidth, Engine.TileHeight); fs.Close(); fs.Dispose(); map = new TileMap(40, 40); MapLayer mapLayer = new MapLayer(map); MapLayer bushes = new MapLayer(map); for (int x = 0; x < 40; x++) for (int y = 0; y < 40; y++) mapLayer.SetTile(x, y, new Tile(0, 0)); mapLayer.Visible = false; map.AddTileSet(tileSet); map.AddLayer(mapLayer); map.AddLayer(bushes); map.CurrentLayerIndex = 1; Camera.Initialize( Vector2.Zero, new Rectangle() { Width = graphics.PreferredBackBufferWidth, Height = graphics.PreferredBackBufferHeight }, new Vector2(map.WidthInTiles * Engine.TileWidth, map.HeightInTiles * Engine.TileHeight) ); Camera.MaxClamp = new Vector2( map.WidthInTiles * Engine.TileWidth - Camera.View.Width, map.HeightInTiles * Engine.TileHeight - Camera.View.Height ); }
/// <summary> /// Checks if the cell is bordered by an open cell /// </summary> /// <param name="cell"></param> /// <param name="neighbor"></param> /// <param name="cells"></param> /// <returns></returns> public bool HasOpening(Vector2 cell, Vector2 neighbor, MazeCell[,] cells, TileMap level) { // Test each adjacent block minus the neighbor foreach (Vector2 dir in adjacent(level, cell)) if (dir != neighbor) if (!cells[(int)dir.X, (int)dir.Y].Wall) return true; // If none of the cells are adjacent, the block doesn't border a wall return false; }
/// <summary> /// Gets all adjacent cells (N, S, E, and W) /// </summary> /// <param name="cell"></param> /// <returns></returns> public List<Vector2> adjacent(TileMap level, Vector2 cell) { List<Vector2> adjacents = new List<Vector2>(); Vector2 N = new Vector2(cell.X, cell.Y - 1); Vector2 S = new Vector2(cell.X, cell.Y + 1); Vector2 W = new Vector2(cell.X - 1, cell.Y); Vector2 E = new Vector2(cell.X + 1, cell.Y); adjacents.Add(N); adjacents.Add(S); adjacents.Add(W); adjacents.Add(E); adjacents.ForEach(delegate(Vector2 dir) { if (dir.X <= 0 || dir.Y <= 0 || dir.X >= level.WidthInTiles || dir.Y >= level.HeightInTiles) adjacents.Remove(dir); }); return adjacents; }
public static MapLayer GenerateFromAlgorithm(GenerationAlgorithm algorithm, TileMap level, int selectedTileSet, int groundTile = 0, int wallTile = 0) { Random rand = new Random(); Tile[,] layerTiles = new Tile[level.HeightInTiles, level.WidthInTiles]; // TODO: Algorithm logic switch (algorithm) { case GenerationAlgorithm.Dungeon_DrunkenWalk: { List<Room> rooms = new List<Room>(); for (int x = 0; x < level.WidthInTiles; x++) for (int y = 0; y < level.HeightInTiles; y++) { layerTiles[y, x] = new Tile(wallTile, selectedTileSet); level.SetCollision(x, y, true); } int roomsMin = (int)(level.WidthInTiles * level.HeightInTiles) / 300; int roomsMax = (int)(level.WidthInTiles * level.HeightInTiles) / 150; int roomCount = 30; int widthRoot = (int)Math.Sqrt(level.WidthInPixels * 2); int heightRoot = (int)Math.Sqrt(level.HeightInPixels * 2); int minimumWidth = (int)4 * Engine.TileWidth; int maximumWidth = (int)8 * Engine.TileHeight; int minimumHeight = (int)3 * Engine.TileWidth; int maximumHeight = (int)10 * Engine.TileHeight; do { bool ok = false; Room room = new Room(); room.X = (int)Math.Round(rand.Next(0, level.WidthInPixels) / (double)Engine.TileWidth) * Engine.TileWidth; room.Y = (int)Math.Round(rand.Next(0, level.HeightInPixels) / (double)Engine.TileHeight) * Engine.TileHeight; room.Width = (int)Math.Round(rand.Next(minimumWidth, maximumWidth) / (double)Engine.TileWidth) * Engine.TileWidth; room.Height = (int)Math.Round(rand.Next(minimumHeight, maximumHeight) / (double)Engine.TileHeight) * Engine.TileHeight; if (room.X < 0 || room.X > level.WidthInPixels - room.Width || room.Y < 0 || room.Y > level.HeightInPixels - room.Height) continue; ok = true; if (rooms.Count > 0) { foreach (Room r in rooms) if (r.Bounds.Intersects(room.Bounds)) ok = false; } if (ok) rooms.Add(room); } while (rooms.Count < roomCount); rooms.Add(new Room() { X = 0, Y = 0, Width = 10 * Engine.TileWidth, Height = 10 * Engine.TileHeight }); List<Room> usableRooms = rooms; List<Cell> connectedTiles = new List<Cell>(); int connections = roomCount; int index = 0; for (int i = 0; i < connections - 1; i++) { Room room = rooms[index]; usableRooms.Remove(room); Room connectToRoom = usableRooms[rand.Next(usableRooms.Count)]; double sideStepChance = 0.4; Vector2 pointA = new Vector2(rand.Next(room.Bounds.X, room.Bounds.X + room.WidthInTiles), rand.Next(room.Bounds.Y, room.Bounds.Y + room.HeightInTiles)); Vector2 pointB = new Vector2(rand.Next(connectToRoom.Bounds.X, connectToRoom.Bounds.X + connectToRoom.WidthInTiles), rand.Next(connectToRoom.Bounds.Y, connectToRoom.Bounds.Y + connectToRoom.HeightInTiles)); while (pointB != pointA) { if (rand.NextDouble() < sideStepChance) { if (pointB.X != pointA.X) { if (pointB.X < pointA.X) pointB.X++; else pointB.X--; } } else if (pointB.Y != pointA.Y) { if (pointB.Y < pointA.Y) pointB.Y++; else pointB.Y--; } if (pointB.X < level.WidthInTiles && pointB.Y < level.HeightInTiles) { level.SetCollision((int)pointB.X, (int)pointB.Y, false); layerTiles[(int)(pointB.Y), (int)(pointB.X)] = new Tile(-1, -1); } } } foreach (Room r in rooms) { for (int x = (int)r.Position.X; x < r.Width + r.Position.X; x++) { for (int y = (int)r.Position.Y; y < r.Height + r.Position.Y; y++) { if (x / 32 == r.Position.X / 32 || x / 32 == (int)(r.WidthInTiles + (r.Position.X / 32) - 1) || y / 32 == r.Position.Y / 32 || y / 32 == (int)(r.HeightInTiles + (r.Position.Y / 32) - 1)) { } else { level.SetCollision((int)(x / Engine.TileWidth), (int)(y / Engine.TileHeight), false); layerTiles[(int)(y / Engine.TileHeight), (int)(x / Engine.TileWidth)] = new Tile(-1, -1); } } } } } break; case GenerationAlgorithm.PerlinNoise: break; } return new MapLayer(level, layerTiles); }
/// <summary> /// Initializes a new map layer /// </summary> /// <param name="width"></param> /// <param name="height"></param> public MapLayer(TileMap parent) { this.parent = parent; map = new Tile[HeightInTiles, WidthInTiles]; for (int y = 0; y < HeightInTiles; y++) { for (int x = 0; x < WidthInTiles; x++) { map[y, x] = new Tile(-1, -1); } } }
/// <summary> /// Initializes a new map layer with an existing map /// </summary> /// <param name="map"></param> public MapLayer(TileMap parent, Tile[,] map) { this.parent = parent; this.map = (Tile[,])map.Clone(); }
/// <summary> /// Initializes a new map layer with an existing map /// </summary> /// <param name="map"></param> public MapLayer(TileMap parent, Tile[,] map) { this.parent = parent; this.map = (Tile[, ])map.Clone(); }
public static MapLayer GenerateFromAlgorithm(GenerationAlgorithm algorithm, TileMap level, int selectedTileSet, int groundTile = 0, int wallTile = 0) { Random rand = new Random(); Tile[,] layerTiles = new Tile[level.HeightInTiles, level.WidthInTiles]; // TODO: Algorithm logic switch (algorithm) { case GenerationAlgorithm.Dungeon_DrunkenWalk: { List <Room> rooms = new List <Room>(); for (int x = 0; x < level.WidthInTiles; x++) { for (int y = 0; y < level.HeightInTiles; y++) { layerTiles[y, x] = new Tile(wallTile, selectedTileSet); level.SetCollision(x, y, true); } } int roomsMin = (int)(level.WidthInTiles * level.HeightInTiles) / 300; int roomsMax = (int)(level.WidthInTiles * level.HeightInTiles) / 150; int roomCount = 30; int widthRoot = (int)Math.Sqrt(level.WidthInPixels * 2); int heightRoot = (int)Math.Sqrt(level.HeightInPixels * 2); int minimumWidth = (int)4 * Engine.TileWidth; int maximumWidth = (int)8 * Engine.TileHeight; int minimumHeight = (int)3 * Engine.TileWidth; int maximumHeight = (int)10 * Engine.TileHeight; do { bool ok = false; Room room = new Room(); room.X = (int)Math.Round(rand.Next(0, level.WidthInPixels) / (double)Engine.TileWidth) * Engine.TileWidth; room.Y = (int)Math.Round(rand.Next(0, level.HeightInPixels) / (double)Engine.TileHeight) * Engine.TileHeight; room.Width = (int)Math.Round(rand.Next(minimumWidth, maximumWidth) / (double)Engine.TileWidth) * Engine.TileWidth; room.Height = (int)Math.Round(rand.Next(minimumHeight, maximumHeight) / (double)Engine.TileHeight) * Engine.TileHeight; if (room.X < 0 || room.X > level.WidthInPixels - room.Width || room.Y < 0 || room.Y > level.HeightInPixels - room.Height) { continue; } ok = true; if (rooms.Count > 0) { foreach (Room r in rooms) { if (r.Bounds.Intersects(room.Bounds)) { ok = false; } } } if (ok) { rooms.Add(room); } } while (rooms.Count < roomCount); rooms.Add(new Room() { X = 0, Y = 0, Width = 10 * Engine.TileWidth, Height = 10 * Engine.TileHeight }); List <Room> usableRooms = rooms; List <Cell> connectedTiles = new List <Cell>(); int connections = roomCount; int index = 0; for (int i = 0; i < connections - 1; i++) { Room room = rooms[index]; usableRooms.Remove(room); Room connectToRoom = usableRooms[rand.Next(usableRooms.Count)]; double sideStepChance = 0.4; Vector2 pointA = new Vector2(rand.Next(room.Bounds.X, room.Bounds.X + room.WidthInTiles), rand.Next(room.Bounds.Y, room.Bounds.Y + room.HeightInTiles)); Vector2 pointB = new Vector2(rand.Next(connectToRoom.Bounds.X, connectToRoom.Bounds.X + connectToRoom.WidthInTiles), rand.Next(connectToRoom.Bounds.Y, connectToRoom.Bounds.Y + connectToRoom.HeightInTiles)); while (pointB != pointA) { if (rand.NextDouble() < sideStepChance) { if (pointB.X != pointA.X) { if (pointB.X < pointA.X) { pointB.X++; } else { pointB.X--; } } } else if (pointB.Y != pointA.Y) { if (pointB.Y < pointA.Y) { pointB.Y++; } else { pointB.Y--; } } if (pointB.X < level.WidthInTiles && pointB.Y < level.HeightInTiles) { level.SetCollision((int)pointB.X, (int)pointB.Y, false); layerTiles[(int)(pointB.Y), (int)(pointB.X)] = new Tile(-1, -1); } } } foreach (Room r in rooms) { for (int x = (int)r.Position.X; x < r.Width + r.Position.X; x++) { for (int y = (int)r.Position.Y; y < r.Height + r.Position.Y; y++) { if (x / 32 == r.Position.X / 32 || x / 32 == (int)(r.WidthInTiles + (r.Position.X / 32) - 1) || y / 32 == r.Position.Y / 32 || y / 32 == (int)(r.HeightInTiles + (r.Position.Y / 32) - 1)) { } else { level.SetCollision((int)(x / Engine.TileWidth), (int)(y / Engine.TileHeight), false); layerTiles[(int)(y / Engine.TileHeight), (int)(x / Engine.TileWidth)] = new Tile(-1, -1); } } } } } break; case GenerationAlgorithm.PerlinNoise: break; } return(new MapLayer(level, layerTiles)); }
/// <summary> /// Initialzes a new tile map /// </summary> /// <param name="map"></param> public TileMap(TileMap map) { guid = map.guid; LoadMap(map); }
/// <summary> /// Loads a map from an existing map /// </summary> /// <param name="map"></param> private void LoadMap(TileMap map) { this.tileSets = map.tileSets; this.layers = map.layers; this.widthInTiles = map.widthInTiles; this.heightInTiles = map.heightInTiles; this.collisionMap = (bool[,])map.collisionMap.Clone(); this.playerSpawnPoint = map.playerSpawnPoint; foreach (TileSet t in tileSets) t.Initialize(t.Path); }
protected override void Initialize() { Components.Add(new InputHandler(this)); Config.Initialize(this); hud = new HUD(this); hud.Initialize(); SheetManager.Initialize(this); canvas = new Canvas(this); level = new TileMap(30, 30); level.AddTileSet(new TileSet("TileSheets\\dirt.png", 32, 32)); for (int i = 0; i < 3; i++) level.AddLayer(new MapLayer(level)); level.CurrentLayerIndex = 0; canvas.Initialize(); base.Initialize(); }
private static void InitializeSearchNodes(TileMap map) { searchNodes = new SearchNode[levelHeight, levelWidth]; for (int x = 0; x < levelWidth; x++) { for (int y = 0; y < levelHeight; y++) { SearchNode node = new SearchNode() { Position = new Point(x, y), Walkable = !map.GetIsCollision(x, y) }; if (node.Walkable) { node.Neighbors = new SearchNode[4]; searchNodes[y, x] = node; } } } for (int x = 0; x < levelWidth; x++) { for (int y = 0; y < levelHeight; y++) { SearchNode node = searchNodes[y, x]; // We only want to look at the nodes that // our enemies can walk on. if (node == null || node.Walkable == false) { continue; } // An array of all of the possible neighbors this // node could have. (We will ignore diagonals for now.) Point[] neighbors = new Point[] { new Point (x, y - 1), // The node above the current node new Point (x, y + 1), // The node below the current node. new Point (x - 1, y), // The node left of the current node. new Point (x + 1, y), // The node right of the current node }; // We loop through each of the possible neighbors for (int i = 0; i < neighbors.Length; i++) { Point position = neighbors[i]; // We need to make sure this neighbour is part of the level. if (position.X < 0 || position.X > levelWidth - 1 || position.Y < 0 || position.Y > levelHeight - 1) { continue; } SearchNode neighbor = searchNodes[position.Y, position.X]; // We will only bother keeping a reference // to the nodes that can be walked on. if (neighbor == null || neighbor.Walkable == false) { continue; } // Store a reference to the neighbor. node.Neighbors[i] = neighbor; } } } }