public WorldEditor(string path, bool alternate = false) : this() { try { RoomsFile rf = JsonConvert.DeserializeObject <RoomsFile>(File.ReadAllText(path)); customPortals = rf.customPortals; startingRoom = rf.startingRoom; roomsData = new Dictionary <char, Room>(rf.rooms); rooms.Clear(); if (!alternate) { foreach (KeyValuePair <char, Room> room in roomsData) { List <List <char> > roomsTable = new List <List <char> >(); foreach (string row in room.Value.layout.Trim('\n').Split('\n').Select(r => r.Trim('\t', '\r'))) { roomsTable.Add(row.ToList()); } rooms[room.Key] = roomsTable; } currentRoomId = '\0'; SelectRoom(startingRoom); } } catch (Exception e) { Console.WriteLineFormatted("Cannot read \"{0}\" + (" + e.GetType().Name + ")", Color.Green, Color.DarkRed, path); } }
private void Save() { try { if (saveWorldDialog.ShowDialog() == DialogResult.OK) { SelectRoom('\0'); RoomsFile roomsFile = new RoomsFile(); roomsData = roomsData.Where(d => rooms.ContainsKey(d.Key)).ToDictionary(kvp => kvp.Key, kvp => kvp.Value); roomsFile.customPortals = customPortals; roomsFile.startingRoom = startingRoom; int maxSize = 0; foreach (KeyValuePair <char, List <List <char> > > room in rooms) { if (!roomsData.ContainsKey(room.Key)) { roomsData[room.Key] = new Room(); } // Sanitize the room layout to be uniform width and height int highestColumnCount = 0; room.Value.ForEach(row => highestColumnCount = Math.Max(highestColumnCount, row.Count)); room.Value.ForEach(row => { while (row.Count < highestColumnCount) { row.Add(' '); } }); // Find the biggest room dimension maxSize = Math.Max(maxSize, Math.Max(room.Value.Count, highestColumnCount)); roomsData[room.Key].layout = "\n"; room.Value.ForEach(row => roomsData[room.Key].layout += string.Join("", row) + "\n"); roomsData[room.Key].layout = roomsData[room.Key].layout.TrimEnd('\n', '\r'); } roomsFile.roomSize = maxSize; roomsFile.rooms = new SortedDictionary <char, Room>(roomsData); File.WriteAllText(saveWorldDialog.FileName, JsonConvert.SerializeObject(roomsFile, Formatting.Indented).Replace("\\n", "\n")); SelectRoom(startingRoom); } } catch (Exception e) { Console.WriteLineFormatted("Cannot write \"{0}\" + (" + e.GetType().Name + ")", Color.Green, Color.DarkRed, "file"); } }
private void Awake() { instance = this; // Make a group for all tiles GameObject anchor = new GameObject("tileAnchor"); tileAnchor = anchor.transform; // Read the JSON file RoomsFile customRooms = null; if (!string.IsNullOrEmpty(loadPath)) { try { customRooms = JsonConvert.DeserializeObject <RoomsFile>(File.ReadAllText(loadPath)); } catch (System.Exception e) { Debug.LogError("Could not load custom rooms file due to a " + e.GetType().Name); Debug.LogError(e.StackTrace); } } if (customRooms == null) { if (loadOriginal) { roomsData = JsonConvert.DeserializeObject <RoomsFile>(originalRoomsFile.text); } else { roomsData = JsonConvert.DeserializeObject <RoomsFile>(roomsFile.text); } } else { roomsData = customRooms; } roomId = roomsData.startingRoom; }
public char GetRoomId(RoomsFile rooms) { return(rooms.rooms.Where(kvp => kvp.Value == this).Single().Key); }