/// <summary> /// Process the map into lines of string /// </summary> private void ProcessMap() { lines = new List <string>(); // Process game map for (int y = 0; y < map.GetLength(1); y++) { for (int x = 0; x < map.GetLength(0); x++) { if (map[y, x] == null) { continue; } GameObject obj = map[y, x]; string line = "map|" + x + "-" + y + "|" + obj.position.X + "-" + obj.position.Y + "|" + obj.sourceRect.X + "-" + obj.sourceRect.Y + "|"; lines.Add(line); } } // Process game objects for (int i = 0; i < objList.Count; i++) { if (objList[i] is SpawnObj) { SpawnObj obj = (SpawnObj)objList[i]; string line = "spawn|" + obj.TeamID + "|" + obj.TeamName + "|" + (int)obj.position.X + "|" + (int)obj.position.Y + "|"; lines.Add(line); } } }
/// <summary> /// Adds spawn object into the world /// </summary> private void AddSpawnObject() { if (mouse.X < 0 || mouse.Y < 0) { return; } SpawnForm form = new SpawnForm(this); DialogResult result = form.ShowDialog(); if (result == DialogResult.Cancel) { return; } if (objList.Count != 0 && (int)objList[objList.Count - 1].position.X == (int)mouse.X && (int)objList[objList.Count - 1].position.Y == (int)mouse.Y) { return; } SpawnObj obj = new SpawnObj(); obj.TeamName = form.teamName; obj.TeamID = spawnList.Count; obj.position = new Vector2((int)mouse.X, (int)mouse.Y); objList.Add(obj); spawnList.Add(obj); }
/// <summary> /// Loads map from file /// </summary> public void LoadMap() { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "EnemyFortress Map | *.efmap"; dialog.DefaultExt = "efmap"; DialogResult result = dialog.ShowDialog(); if (result == DialogResult.Cancel) { return; } if (result == DialogResult.OK) { map = new GameObject[100, 100]; objList.Clear(); StreamReader reader = new StreamReader(dialog.FileName); while (!reader.EndOfStream) { string[] data = reader.ReadLine().Split('|'); int posx = 0, posy = 0; switch (data[0]) { case "spawn": int teamid = int.Parse(data[1]); string teamname = data[2]; posx = int.Parse(data[3]); posy = int.Parse(data[4]); SpawnObj obj1 = new SpawnObj(); obj1.TeamID = teamid; obj1.TeamName = teamname; obj1.position = new Vector2(posx, posy); objList.Add(obj1); spawnList.Add(obj1); break; case "map": int x = 0, y = 0, srcx = 0, srcy = 0; // converts the data into integers for (int i = 1; i < data.Length; i++) { if (string.IsNullOrWhiteSpace(data[i])) { continue; } string[] xy = data[i].Split('-'); switch (i) { case 1: x = int.Parse(xy[0]); y = int.Parse(xy[1]); break; case 2: posx = int.Parse(xy[0]); posy = int.Parse(xy[1]); break; case 3: srcx = int.Parse(xy[0]); srcy = int.Parse(xy[1]); break; } } GameObject obj = new GameObject(AssetManager.Tilesheet); obj.width = 128; obj.height = 128; obj.position = new Vector2(posx, posy); obj.origin = new Vector2(128 / 2, 128 / 2); obj.sourceRect = new Rectangle(srcx, srcy, 128, 128); map[y, x] = obj; break; } } } }