public override TiledMap Import(Context context, IFile file) { var reader = new TiledReader(); using (var stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.Read)) { var doc = XDocument.Load(stream); return(reader.Read(doc)); } }
static void Init() { string source = AssetDatabase.GetAssetPath(Selection.activeObject); if ((string.IsNullOrEmpty(source)) || ((source.Remove(0, source.IndexOf(".")) != ".tmx") && (source.Remove(0, source.IndexOf(".")) != ".xml"))) { EditorUtility.DisplayDialog("Select one Tiled File", "You Must Choose a .TMX Tiled file first!", "Ok"); return; } TiledReader window = (TiledReader)EditorWindow.GetWindowWithRect(typeof(TiledReader), new Rect(0, 0, 320, 160)); source = source.Remove(0, 6); window.TiledTMXFile = source; }
public static void Load(string leveldir, string filename) { string fullname = Main.startdir + "\\Levels\\" + leveldir + "\\" + filename; if (File.Exists(fullname)) { //Load the map using NTiled XDocument document = XDocument.Load(fullname); TiledMap map = new TiledReader().Read(document); //Tilesets foreach (TiledTileset tileset in map.Tilesets) { if (tileset.Image != null && File.Exists(Main.startdir + "\\Levels\\" + leveldir + "\\" + tileset.Image.Source)) { Program.game.Content.RootDirectory = "Levels"; Tileset ts = new Tileset(Program.game.Content.Load <Texture2D>(leveldir + "\\" + new FileInfo(tileset.Image.Source).Name)); //Generate all the tile rectangles within the tileset. int i = 0; for (int y = 0; y < tileset.Image.Height; y += 16) { for (int x = 0; x < tileset.Image.Width; x += 16) { ts.tilesetparts.Add(new Rectangle(x, y, 16, 16)); AssignProperties(tileset, i, ts); i++; } } tilesets.Add(ts); } } //Layers foreach (TiledLayer layer in map.Layers) { TiledTileLayer tlayer = layer as TiledTileLayer; TiledObjectGroup olayer = layer as TiledObjectGroup; if (tlayer != null) { int i = 0, tilesetid = 0; for (int y = 0; y < layer.Height * 16; y += 16) { for (int x = 0; x < layer.Width * 16; x += 16) { if (tlayer.Tiles[i] != 0) { //TODO: Find the correct tileset for each tile. //OLD CODE: //if (!tilesets[tilesetid].tilesetparts.Count > !tilesets[tilesetid].tileids.Contains(tlayer.Tiles[i])) //{ // Console.WriteLine(tlayer.Tiles[i]); // //tilesetid = -1; // for (int tsi = 0; tsi < tilesets.Count; tsi++) // { // if (tilesets[tsi].tileids.Contains(tlayer.Tiles[i])) { tilesetid = tsi; break; } // } //} //else { Console.WriteLine(tlayer.Tiles[i]); } //Spawn all the tiles if (tilesetid != -1) { tiles.Add(new Tile(tilesetid, tlayer.Tiles[i] - 1, tilesets[tilesetid].tilesetparts[tlayer.Tiles[i] - 1], new Vector2(x, y))); } } i++; } } } if (olayer != null) { foreach (TiledObject obj in olayer.Objects) { switch (obj.Type) { case "Camera H Border Lock": objects.Add(new CameraHBorder(new Rectangle((int)obj.X, (int)obj.Y, (int)obj.Width, (int)obj.Height), obj.Properties.ContainsKey("Stops Player") ? (obj.Properties["Stops Player"] == "1" || obj.Properties["Stops Player"].ToUpper() == "TRUE") : false)); break; case "Camera V Border Lock": objects.Add(new CameraVBorder(new Rectangle((int)obj.X, (int)obj.Y, (int)obj.Width, (int)obj.Height), obj.Properties.ContainsKey("Stops Player") ? (obj.Properties["Stops Player"] == "1" || obj.Properties["Stops Player"].ToUpper() == "TRUE") : false)); break; case "Death Trigger": objects.Add(new DeathTrigger(new Rectangle((int)obj.X, (int)obj.Y, (int)obj.Width, (int)obj.Height))); break; case "Ring": objects.Add(new Ring((float)obj.X, (float)obj.Y)); break; } } } } //Assign Playerstarts if (map.Properties["Sonic Player Start"] != null) { playerstarts[0] = new Vector2(Convert.ToSingle(map.Properties["Sonic Player Start"].Split(',')[0]), Convert.ToSingle(map.Properties["Sonic Player Start"].Split(',')[1])); } if (map.Properties["Tails Player Start"] != null) { playerstarts[1] = new Vector2(Convert.ToSingle(map.Properties["Tails Player Start"].Split(',')[0]), Convert.ToSingle(map.Properties["Tails Player Start"].Split(',')[1])); } if (map.Properties["Knuckles Player Start"] != null) { playerstarts[2] = new Vector2(Convert.ToSingle(map.Properties["Knuckles Player Start"].Split(',')[0]), Convert.ToSingle(map.Properties["Knuckles Player Start"].Split(',')[1])); } //Assign Camerastarts if (map.Properties["Sonic Camera Start"] != null) { camerastarts[0] = new Vector2(Convert.ToSingle(map.Properties["Sonic Camera Start"].Split(',')[0]), Convert.ToSingle(map.Properties["Sonic Camera Start"].Split(',')[1])); } if (map.Properties["Tails Camera Start"] != null) { camerastarts[1] = new Vector2(Convert.ToSingle(map.Properties["Tails Camera Start"].Split(',')[0]), Convert.ToSingle(map.Properties["Tails Camera Start"].Split(',')[1])); } if (map.Properties["Knuckles Camera Start"] != null) { camerastarts[2] = new Vector2(Convert.ToSingle(map.Properties["Knuckles Camera Start"].Split(',')[0]), Convert.ToSingle(map.Properties["Knuckles Camera Start"].Split(',')[1])); } //TODO: Fix the camerastart assignments. //Move players to their correct start positions foreach (Player plr in Main.players) { if (plr.GetType() == typeof(Sonic)) { plr.pos = playerstarts[0]; } else if (plr.GetType() == typeof(Tails)) { plr.pos = playerstarts[1]; } else if (plr.GetType() == typeof(Knuckles)) { plr.pos = playerstarts[2]; } if (plr.GetType() == typeof(Sonic)) { Camera.pos = camerastarts[0] * Main.scalemodifier; } else if (plr.GetType() == typeof(Tails)) { Camera.pos = camerastarts[1] * Main.scalemodifier; } else if (plr.GetType() == typeof(Knuckles)) { Camera.pos = camerastarts[2] * Main.scalemodifier; } plr.active = true; } Program.game.Content.RootDirectory = "Content"; } else { MessageBox.Show("ERROR: The given level (\"" + filename + "\") does not exist!", "SoniC#", MessageBoxButtons.OK, MessageBoxIcon.Error); } }