public LevelLayout LoadMap(string levelName) { StreamReader reader; var appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); appDataDir = Path.Combine(appDataDir, "TheScrollOfNope"); Directory.CreateDirectory(appDataDir); // to-do: handle exceptions var fileName = Path.Combine(appDataDir, levelName); if (File.Exists(fileName)) { // Open file reader = new StreamReader(fileName); // Read file string unserialisedMap = reader.ReadToEnd(); // Converts from json to LevelLayout LevelLayout map = JsonConvert.DeserializeObject <LevelLayout>(unserialisedMap); reader.Close(); return(map); } return(null); }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); GameElements.LoadContent(Content, Window); mapEditor = new LevelObjects.LevelEditor(); // Load the textures into the texture handler textures = new The_scroll_of_NOPE.Content.TextureHandler(Content); #region Jonatan, load map // FOR DEBUG PUSPOSES tmpTexture = Content.Load<Texture2D>("images/ANKA/ANKA"); try { // Load the default map levelLayout = mapEditor.LoadMap("defaultMap"); // If it could not load because there probably was no defaultMap if (levelLayout == null) { // Create new map and save it to a file levelLayout = new LevelObjects.LevelLayout(Content); mapEditor.SaveMap(levelLayout, "defaultMap"); } } // Textures probably did not load properly // TODO: Fix the deserialization and serialization of textures catch { levelLayout = new LevelObjects.LevelLayout(Content); } #endregion anka = new BaseClasses.Players.ANKA(1, Content.Load<Texture2D>("images/ANKA/SpriteTest"/*SpriteTest skall vara ANKA*/), new Vector2(50, 50), 5, 10000); testStudent = new Student1(Content.Load<Texture2D>("images/Students/PlayerTemp"), new Vector2(0, 0), 7, Content, new Vector2(5, 5)); // TODO: use this.Content to load your game content here collidables.Add(anka); collidables.Add(levelLayout); collidables.Add(testStudent); // For drawing text font = Content.Load<SpriteFont>("Text/Score"); winFont = Content.Load<SpriteFont>("Text/Win"); // for drawing whatever }
// Using streamWriter writes level object to a file public bool SaveMap(LevelLayout map, string levelName) { var appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); appDataDir = Path.Combine(appDataDir, "TheScrollOfNope"); Directory.CreateDirectory(appDataDir); // to-do: handle exceptions var fileName = Path.Combine(appDataDir, levelName); StreamWriter writer; // IF file exist write over if (File.Exists(fileName)) { writer = new StreamWriter(fileName); // Write the entire levelObject to file in json format, this removes previous map of filename // Converts to json using NwetonsoftJson and writes to file string mapJSON = JsonConvert.SerializeObject(map); writer.Write(mapJSON); writer.Close(); return(true); } // If not create new file and write over else if (levelName != "") { // Create file // Open file with stream writer = new StreamWriter(fileName); // Write to the file string mapJSON = JsonConvert.SerializeObject(map); writer.Write(mapJSON); writer.Close(); return(true); } // If something with the map or name or writing went wrong return false return(false); }