Map LoadMap(MapEditor editor, GameControl gc, string map) { if (maps[map].map != null) { maps[map].map.GameControl = gc; return maps[map].map; } string mapdir = maps[map].fulldir; Map m; BinaryFormatter bf = new BinaryFormatter(); // Load the floor using (StreamReader floortiles = new StreamReader(Path.Combine(mapdir, "floor.tiles"))) { using (FileStream floormap = new FileStream(Path.Combine(mapdir, "floor.map"), FileMode.Open)) { int[,] floor = (int[,])bf.Deserialize(floormap); int w = floor.GetUpperBound(0) + 1; int h = floor.GetUpperBound(1) + 1; m = new Map(gc, w, h); Dictionary<int, string> tilenumToName = new Dictionary<int, string>(); while (!floortiles.EndOfStream) { string line = floortiles.ReadLine(); string[] tokens = line.Split(':'); tilenumToName[int.Parse(tokens[0])] = tokens[1]; } for (int xx = 0; xx < w; xx++) { for (int yy = 0; yy < h; yy++) { m.SetFloor((MapFloor)Activator.CreateInstance(floortypes[tilenumToName[floor[xx, yy]]]), xx, yy); } } } } Regex objectnamelabel = new Regex(@"^\[(?<objectname>[a-z][a-z|0-9]*)\]", RegexOptions.Compiled); // Load the objects that are on this map using (StreamReader objectlist = new StreamReader(Path.Combine(mapdir, "objects.list"))) { string line = objectlist.ReadLine(); Match match = objectnamelabel.Match(line); while (!objectlist.EndOfStream) { string instancename = match.Groups["objectname"].ToString(); ICharacter mo = null; // For each instance block while (!objectlist.EndOfStream) { line = objectlist.ReadLine(); if (line.StartsWith("type:")) { string type = line.Substring(5); mo = (ICharacter)Activator.CreateInstance(objecttypes[type]); } else if (line.StartsWith("location:")) { string[] tokens = line.Substring(9).Split(','); m.SetCharacter(mo, int.Parse(tokens[0]), int.Parse(tokens[1])); } else if (line.StartsWith("use:")) { } match = objectnamelabel.Match(line); if (match.Success) break; } } } return m; }
public void OpenMapEditor(string map) { if (!IsMapEditorRegistered(map)) { MapEditor editor = new MapEditor(tilebank, this, map); MapEditorInfo mi = new MapEditorInfo(); mi.mapname = map; mi.map = editor; openMaps[map] = mi; Map m = LoadMap(editor, editor.pictureBox1, map); editor.pictureBox1.Scene = new Scene(m); dm.DockWindow(editor, DockStyle.Fill); editor.Text = "map: " + map; editor.Show(); editor.Map = m; editor.SceneHasBeenSet(); } }