//fill the list "maps" with the maps read from the corresponding text files static void loadMaps() { for (int room = 0; room < (int)RoomsManager.Rooms.total; room++) { string line = ""; List <List <int> > tileMap = new List <List <int> >(); string[] row; try { StreamReader inputStream = new StreamReader("mapFiles\\mapRoom_" + (RoomsManager.Rooms)room + ".txt"); using (inputStream) { line = inputStream.ReadLine(); int count = 0; while (line != null) { tileMap.Add(new List <int>()); row = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (string tile in row) { tileMap[count].Add(int.Parse(tile)); } line = inputStream.ReadLine(); count++; } maps[room] = new RoomMap(tileMap.Count, tileMap[0].Count); for (int i = 0; i < tileMap.Count; i++) { for (int j = 0; j < tileMap[0].Count; j++) { if (tileMap[i][j] != 0) { maps[room].array[i, j].tileType = (Tile.TileType)tileMap[i][j]; } } } } } catch (IOException) { maps[room] = new RoomMap( (Game1.gameHeight + 2 * CameraManager.maxOffsetY + (int)(Tile.tileSize) - 1) / (int)(Tile.tileSize), (Game1.gameWidth + (int)(Tile.tileSize) - 1) / (int)(Tile.tileSize)); } catch (System.ArgumentOutOfRangeException) { maps[room] = new RoomMap( (Game1.gameHeight + 2 * CameraManager.maxOffsetY + (int)(Tile.tileSize) - 1) / (int)(Tile.tileSize), (Game1.gameWidth + (int)(Tile.tileSize) - 1) / (int)(Tile.tileSize)); } } }
//fill the list "maps" with the maps read from the corresponding text files public static void resetMap(RoomsManager.Rooms roomName) { string line = ""; List <List <int> > tileMap = new List <List <int> >(); string[] row; try { StreamReader inputStream = new StreamReader("mapFiles\\mapRoom_" + roomName + ".txt"); using (inputStream) { line = inputStream.ReadLine(); int count = 0; while (line != null) { tileMap.Add(new List <int>()); row = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (string tile in row) { tileMap[count].Add(int.Parse(tile)); } line = inputStream.ReadLine(); count++; } maps[(int)roomName] = new RoomMap(tileMap.Count, tileMap[0].Count); for (int i = 0; i < tileMap.Count; i++) { for (int j = 0; j < tileMap[0].Count; j++) { if (tileMap[i][j] != 0) { maps[(int)roomName].array[i, j].tileType = (Tile.TileType)tileMap[i][j]; } } } } } catch (IOException) { return; } catch (System.ArgumentOutOfRangeException) { return; } }
//state machine that allows to traverse the menus void MenuLoop(MouseState mouseState, MouseState previousMouseState, int tilesPerRow, int infoBoxHeighPx) { switch (menu) { case MenuState.start: if (Game1.currentKeyboard.IsKeyDown(Keys.Enter)) { menu = MenuState.none; message = ""; } break; case MenuState.none: if (Game1.currentKeyboard.IsKeyDown(Keys.M)) { menu = MenuState.main; message = "Main Menu (backspace to exit). Press:\nR-Room Options\nT-Tile options\nV-View options\nS-Save maps"; } break; case MenuState.main: if (Game1.currentKeyboard.IsKeyDown(Keys.R)) { menu = MenuState.rooms; message = "Press:\nA-Change room\nB-Change room size"; } else if (Game1.currentKeyboard.IsKeyDown(Keys.T)) { menu = MenuState.tiles; message = "Press:\nA-Change tile type\nB-Random tile mode"; } else if (Game1.currentKeyboard.IsKeyDown(Keys.V)) { menu = MenuState.view; message = "Press:\nA-Highlight solid tiles\nB-Highlight deadly tiles"; } else if (Game1.currentKeyboard.IsKeyDown(Keys.S)) { menu = MenuState.none; message = ""; saveMaps(); } else if (Game1.currentKeyboard.IsKeyDown(Keys.Back) && !Game1.previousKeyboard.IsKeyDown(Keys.Back)) { menu = MenuState.none; message = ""; } break; case MenuState.rooms: if (Game1.currentKeyboard.IsKeyDown(Keys.A)) { menu = MenuState.pickRoom; message = "Room index: "; userInputInt = 0; } else if (Game1.currentKeyboard.IsKeyDown(Keys.B)) { menu = MenuState.pickRoomSize; message = "Room size in tiles (format: width,height): "; userInputString = ""; } else if (Game1.currentKeyboard.IsKeyDown(Keys.Back) && !Game1.previousKeyboard.IsKeyDown(Keys.Back)) { menu = MenuState.main; message = "Main Menu (backspace to exit). Press:\nR-Room Options\nT-Tile options\nV-View options\nS-Save maps"; } break; case MenuState.tiles: if (Game1.currentKeyboard.IsKeyDown(Keys.A)) { menu = MenuState.pickTileIndex; message = "Tile index: "; userInputInt = 0; } else if (Game1.currentKeyboard.IsKeyDown(Keys.B)) { menu = MenuState.selectRandomTiles; message = "Enter the indexes of the tiles you want to\nrandomly select from (format: index1,index2...)\n" + "You can also click the tiles displayed on the right.\nIndexes: "; userInputString = ""; randomTiles.Clear(); } else if (Game1.currentKeyboard.IsKeyDown(Keys.Back) && !Game1.previousKeyboard.IsKeyDown(Keys.Back)) { menu = MenuState.main; message = "Main Menu (backspace to exit). Press:\nR-Room Options\nT-Tile options\nV-View options\nS-Save maps"; } break; case MenuState.view: if (Game1.currentKeyboard.IsKeyDown(Keys.A)) { menu = MenuState.none; message = ""; solidView = !solidView; } else if (Game1.currentKeyboard.IsKeyDown(Keys.B)) { menu = MenuState.none; message = ""; deadlyView = !deadlyView; } else if (Game1.currentKeyboard.IsKeyDown(Keys.Back) && !Game1.previousKeyboard.IsKeyDown(Keys.Back)) { menu = MenuState.main; message = "Main Menu (backspace to exit). Press:\nR-Room Options\nT-Tile options\nV-View options\nS-Save maps"; } break; case MenuState.pickRoom: if (Game1.currentKeyboard.IsKeyDown(Keys.Back) && !Game1.previousKeyboard.IsKeyDown(Keys.Back)) { menu = MenuState.main; message = "Main Menu (backspace to exit). Press:\nR-Room Options\nT-Tile options\nV-View options\nS-Save maps"; } else if (GetIntInput()) { message = "Room index: " + userInputInt; } else if (Game1.currentKeyboard.IsKeyDown(Keys.Enter)) { currentRoomNumber = userInputInt < (int)RoomsManager.Rooms.total ? userInputInt : ((int)RoomsManager.Rooms.total - 1); CameraManager.SwitchCamera((RoomsManager.Rooms)currentRoomNumber, 0); CameraManager.pointLocked = new Vector2(0, 0); userInputInt = 0; menu = MenuState.none; message = ""; } break; case MenuState.pickRoomSize: if (Game1.currentKeyboard.IsKeyDown(Keys.Back) && !Game1.previousKeyboard.IsKeyDown(Keys.Back)) { menu = MenuState.main; message = "Main Menu (backspace to exit). Press:\nR-Room Options\nT-Tile options\nV-View options\nS-Save maps"; } else if (GetStringInput()) { message = "Room size in tiles (format: width,height): " + userInputString; } else if (Game1.currentKeyboard.IsKeyDown(Keys.Enter)) { string[] arr = userInputString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (arr.Length != 2) { break; } widthTiles = int.Parse(arr[0]); widthTiles = MathHelper.Clamp( widthTiles, (Game1.gameWidth + (int)(Tile.tileSize) - 1) / (int)(Tile.tileSize), 500); heightTiles = int.Parse(arr[1]); heightTiles = MathHelper.Clamp( heightTiles, (Game1.gameHeight + 2 * CameraManager.maxOffsetY + (int)(Tile.tileSize) - 1) / (int)(Tile.tileSize), 500); menu = MenuState.pickResizingDirection; userInputString = "U,R"; message = "Use the arrows to select\nthe edges (top/bottom and left/right)\n" + "to shift to resize the room.\n" + userInputString; } break; case MenuState.pickResizingDirection: if (Game1.currentKeyboard.IsKeyDown(Keys.Back) && !Game1.previousKeyboard.IsKeyDown(Keys.Back)) { menu = MenuState.main; message = "Main Menu (backspace to exit). Press:\nR-Room Options\nT-Tile options\nV-View options\nS-Save maps"; userInputString = ""; heightTiles = 0; widthTiles = 0; } else if (getDirectionalInput()) { message = "Use the arrows to select\nthe edges (top/bottom and left/right)\n" + "to shift to resize the room.\n" + userInputString; } else if (Game1.currentKeyboard.IsKeyDown(Keys.Enter) && !Game1.previousKeyboard.IsKeyDown(Keys.Enter)) { RoomMap newMap = new RoomMap(heightTiles, widthTiles); int oldHeightTiles = MapsManager.maps[currentRoomNumber].roomHeightTiles; int oldWidthTiles = MapsManager.maps[currentRoomNumber].roomWidthTiles; for (int row = 0; row < Math.Min(oldHeightTiles, heightTiles); row++) { for (int col = 0; col < Math.Min(oldWidthTiles, widthTiles); col++) { if (userInputString == "D,R") { newMap.array[row, col].tileType = MapsManager.maps[currentRoomNumber].array[row, col].tileType; } else if (userInputString == "U,R") { newMap.array[heightTiles - 1 - row, col].tileType = MapsManager.maps[currentRoomNumber].array[oldHeightTiles - 1 - row, col].tileType; } else if (userInputString == "D,L") { newMap.array[row, widthTiles - 1 - col].tileType = MapsManager.maps[currentRoomNumber].array[row, oldWidthTiles - 1 - col].tileType; } else if (userInputString == "U,L") { newMap.array[heightTiles - 1 - row, widthTiles - 1 - col].tileType = MapsManager.maps[currentRoomNumber].array[oldHeightTiles - 1 - row, oldWidthTiles - 1 - col].tileType; } } } MapsManager.maps[currentRoomNumber] = newMap; CameraManager.SwitchCamera((RoomsManager.Rooms)currentRoomNumber, 0); menu = MenuState.none; message = ""; userInputString = ""; heightTiles = 0; widthTiles = 0; } break; case MenuState.pickTileIndex: if (Game1.currentKeyboard.IsKeyDown(Keys.Back) && !Game1.previousKeyboard.IsKeyDown(Keys.Back)) { menu = MenuState.main; message = "Main Menu (backspace to exit). Press:\nR-Room Options\nT-Tile options\nV-View options\nS-Save maps"; } else if (GetIntInput()) { message = "Tile index: " + userInputInt; } else if (Game1.currentKeyboard.IsKeyDown(Keys.Enter)) { currentTileType = userInputInt < (int)Tile.TileType.total ? userInputInt : ((int)Tile.TileType.total - 1); userInputInt = 0; menu = MenuState.none; message = ""; randomMode = false; PreviousTileHovered = new Point(-1, -1); } break; case MenuState.selectRandomTiles: if (Game1.currentKeyboard.IsKeyDown(Keys.Back) && !Game1.previousKeyboard.IsKeyDown(Keys.Back)) { menu = MenuState.main; message = "Main Menu (backspace to exit). Press:\nR-Room Options\nT-Tile options\nV-View options\nS-Save maps"; } else if (GetStringInput()) { message = "Enter the indexes of the tiles you want to\nrandomly select from (format: index1,index2...)\n" + "You can also click the tiles displayed on the right.\nIndexes: " + userInputString; } else if (mouseState.X >= Game1.gameWidth * Game1.scale && mouseState.X < Game1.viewportRectangle.Width && mouseState.Y >= 0 && mouseState.Y < Game1.viewportRectangle.Height && mouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton != ButtonState.Pressed) { int newTileType = (mouseState.X - Game1.gameWidth * Game1.scale) / (Tile.tileSize * Game1.scale) + (mouseState.Y / (Tile.tileSize * Game1.scale) * tilesPerRow); userInputString += ("," + newTileType + ","); message = "Enter the indexes of the tiles you want to\nrandomly select from (format: index1,index2...)\n" + "You can also click the tiles displayed on the right.\nIndexes: " + userInputString; } else if (Game1.currentKeyboard.IsKeyDown(Keys.Enter)) { randomTiles.Clear(); string[] arr = userInputString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); userInputString = ""; if (arr.Length == 0) { break; } else if (arr.Length == 1) { menu = MenuState.none; message = ""; randomMode = false; PreviousTileHovered = new Point(-1, -1); int tile = int.Parse(arr[0]); currentTileType = tile < (int)Tile.TileType.total ? tile : ((int)Tile.TileType.total - 1); break; } foreach (string s in arr) { int tile = int.Parse(s); tile = tile < (int)Tile.TileType.total ? tile : ((int)Tile.TileType.total - 1); randomTiles.Add(tile); } randomMode = true; menu = MenuState.none; message = ""; } break; default: break; } }