private static void AddEnemyRessourcesToLoadIfNecessary(Map map, AddEnemyCommand c) { //Enemy ressources to load with the map bool mustAdd = true; //Get one enemy of each type used in the map foreach (BadGuy b in map.EnemyRessourcesToLoad) { if (b.GetType() == c.Enemy.GetType()) mustAdd = false; } if (mustAdd) map.EnemyRessourcesToLoad.Add(c.Enemy); }
/// <summary> /// Read .tgpa files outside TGPA Game class /// </summary> /// <param name="file"></param> /// <returns></returns> public static Map BuildMapFromTGPAFile(String file, Vector2 screenResolution) { Map map = new Map(); StreamReader reader = new StreamReader(TitleContainer.OpenStream(file)); String line = reader.ReadLine(); while (line.Equals("") || line.StartsWith("//")) line = reader.ReadLine(); //Check version //try //{ // if (Convert.ToDouble((line.Split(' '))[1]) > Convert.ToDouble(TheGreatPaperGame.version)) // { // reader.Close(); // throw new Exception("Insupported game version."); // } //} //catch (FormatException) { throw new Exception("Invalid game version : " + line); } //Bullshit map.GameVersion = TheGreatPaperGame.Version; line = reader.ReadLine(); while (line.Equals("") || line.StartsWith("//")) line = reader.ReadLine(); //Map informations #region Map Infos try { map.Level = Convert.ToInt32((line.Split(' '))[1]); } catch (FormatException) { reader.Close(); throw new Exception("Invalid map level number : " + line); } line = reader.ReadLine(); while (line.Equals("") || line.StartsWith("//")) line = reader.ReadLine(); try { map.lastPart = Convert.ToBoolean(line.Replace("lastpart ", "")); } catch (FormatException) { reader.Close(); throw new Exception("Invalid map level lastpart : " + line); } line = reader.ReadLine(); while (line.Equals("") || line.StartsWith("//")) line = reader.ReadLine(); map.Name = LocalizedStrings.GetString(line.Replace("name ", "")); line = reader.ReadLine(); while (line.Equals("") || line.StartsWith("//")) line = reader.ReadLine(); String filedesc = line.Replace("desc ", ""); map.Description = LocalizedStrings.GetString(filedesc); #endregion line = reader.ReadLine(); while (line.Equals("") || line.StartsWith("//")) line = reader.ReadLine(); //Backgrounds #region Backgrounds for (int i = 1; i < 4; i++) { ScrollingBackground bg = null; if (i == 1) bg = map.Background1; else if (i == 2) bg = map.Background2; else if (i == 3) bg = map.Background3; string[] tokens = line.Split(' '); ScrollDirection direction = ScrollDirection.Left; try { direction = ScrollingBackground.String2ScrollDirection(tokens[1]); } catch (Exception e) { Logger.Log(LogLevel.Error, "TGPA Exception : " + e.Message); } bool inf = false; try { inf = Convert.ToBoolean(tokens[4]); } catch (FormatException) { reader.Close(); throw new Exception("Invalid boolean for Infinite scroll : " + line); } Vector2 speed = Vector2.Zero; try { speed = new Vector2(Convert.ToInt32(tokens[2]), Convert.ToInt32(tokens[3])); } catch (FormatException) { reader.Close(); throw new Exception("Invalid Vector for scroll speed : " + line); } bg = new ScrollingBackground(direction, speed, inf); //Parts while ((line = reader.ReadLine()).Split(' ')[0].Equals("bgpart")) { Logger.Log(LogLevel.Info, "BG Found " + line.Split(' ')[1]); bg.AddBackground(line.Split(' ')[1]); } if (bg.BackgroundSprites.Count == 0) { reader.Close(); throw new Exception("No BGPart found for Background " + i); } if (i == 1) map.Background1 = bg; else if (i == 2) map.Background2 = bg; else if (i == 3) map.Background3 = bg; line = reader.ReadLine(); while (line.Equals("") || line.StartsWith("//")) line = reader.ReadLine(); } #endregion //Initialization #region Init if (!line.Split(' ')[0].Equals("init")) { reader.Close(); throw new Exception("Invalid TGPA map : init section not found"); } line = reader.ReadLine(); while (line.Equals("") || line.StartsWith("//")) line = reader.ReadLine(); //level music string[] tokens2 = line.Split('\"'); if (tokens2.Length < 4) { Console.WriteLine("No music loaded"); } else { map.Music = new MySong(tokens2[0].Split(' ')[1], tokens2[1], tokens2[3]); } line = reader.ReadLine(); while (line.Equals("") || line.StartsWith("//")) line = reader.ReadLine(); //Initial datas try { map.InitialScore = Convert.ToInt32(line.Split(' ')[1]); } catch (FormatException) { reader.Close(); throw new Exception("Invalid integer for score : " + line); } line = reader.ReadLine(); while (line.Equals("") || line.StartsWith("//")) line = reader.ReadLine(); try { map.InitialLivesCount = Convert.ToInt32(line.Split(' ')[1]); } catch (FormatException) { reader.Close(); throw new Exception("Invalid integer for lives : " + line); } line = reader.ReadLine(); while (line.Equals("") || line.StartsWith("//")) line = reader.ReadLine(); string weaponName = line.Split(' ')[1]; map.InitialWeapon = Weapon.TypeToWeapon(weaponName); line = reader.ReadLine(); while (line.Equals("") || line.StartsWith("//")) line = reader.ReadLine(); //Initial loc Vector2 initialPlayerLoc = new Vector2(Convert.ToInt32(line.Split(' ')[1]), Convert.ToInt32(line.Split(' ')[2])); map.InitialPlayerLocation = initialPlayerLoc; line = reader.ReadLine(); while (line.Equals("") || line.StartsWith("//")) line = reader.ReadLine(); //Initial flip string sFlip = line.Split(' ')[1]; SpriteEffects flip = SpriteEffects.None; if (sFlip.Equals(SpriteEffects.FlipHorizontally.ToString())) { flip = SpriteEffects.FlipHorizontally; } else if (sFlip.Equals(SpriteEffects.FlipVertically.ToString())) { flip = SpriteEffects.FlipVertically; } map.InitialFlip = flip; line = reader.ReadLine(); while (line.Equals("") || line.StartsWith("//")) line = reader.ReadLine(); //End conditions Event endmap = new Event(Vector2.Zero, null); String winflag = line.Split(' ')[1]; line = reader.ReadLine(); String loseflag = line.Split(' ')[1]; endmap.AddCommand(new EndLevelCommand(winflag, loseflag)); map.Events.Add(endmap); line = reader.ReadLine(); while (line.Equals("") || line.StartsWith("//")) line = reader.ReadLine(); ScrollDirection outDirection = ScrollingBackground.String2ScrollDirection(line.Split(' ')[1]); map.OutDirection = outDirection; #endregion //Script #region Script event line = reader.ReadLine(); while (line.Equals("") || line.StartsWith("//")) line = reader.ReadLine(); if (!line.Split(' ')[0].Equals("begin")) { reader.Close(); throw new Exception("Invalid TGPA map : begin keyword not found"); } line = reader.ReadLine(); while (line.Equals("") || line.StartsWith("//")) line = reader.ReadLine(); //Read all events while (!line.Split(' ')[0].Equals("end")) { tokens2 = line.Split(' '); if (!tokens2[0].Equals("event")) { reader.Close(); throw new Exception("Invalid TGPA map : event section missing (line : " + line + ")"); } Vector2 vector = Vector2.Zero; try { vector = new Vector2((float)Convert.ToDouble(tokens2[2]), (float)Convert.ToDouble(tokens2[3])); } catch (FormatException) { reader.Close(); throw new Exception("Invalid Vector for event scroll value : " + line); } line = reader.ReadLine(); tokens2 = line.Split(' '); if (!tokens2[0].Equals("start")) { reader.Close(); throw new Exception("Invalid TGPA map : start keyword missing"); } String startFlag = null; IfCommand ifc = null; try { startFlag = line.Split(' ')[1]; if (!startFlag.Equals("")) ifc = new IfCommand(startFlag); } catch (IndexOutOfRangeException) { } Event e = new Event(vector, ifc); line = reader.ReadLine(); while (line.Equals("") || line.StartsWith("//")) line = reader.ReadLine(); List<Command> commands = new List<Command>(); //Add actions while (!line.Split(' ')[0].Equals("endevent")) { Command c = null; switch (line.Split(' ')[0]) { case "addenemy": c = new AddEnemyCommand(line, screenResolution); AddEnemyRessourcesToLoadIfNecessary(map, (AddEnemyCommand)c); break; case "addbge": c = new AddBackgroundElementCommand(line, screenResolution); AddBGERessourcesToLoadIfNecessary(map, (AddBackgroundElementCommand)c); break; case "if": c = new IfCommand(line); break; case "set": c = new SetFlagCommand(line, true); break; case "unset": c = new UnsetFlagCommand(line, true); break; case "whilenot": c = new WhileNotCommand(line); line = reader.ReadLine(); while (line.Equals("") || line.StartsWith("//")) line = reader.ReadLine(); List<String> lines = new List<String>(); while (!line.Split(' ')[0].Equals("done")) { if (line.Split(' ')[0].Equals("addenemy")) { AddEnemyRessourcesToLoadIfNecessary(map, new AddEnemyCommand(line, screenResolution)); } lines.Add(line); line = reader.ReadLine(); while (line.Equals("") || line.StartsWith("//")) line = reader.ReadLine(); } ((WhileNotCommand)c).AddCommands(lines); break; case "scrollspeedreset": c = new ResetScrollingSpeedCommand(line); break; case "scrollspeed": c = new NewScrollingSpeedCommand(line); break; case "changemusic": c = new ChangeMusicCommand(line); map.MusicRessourcesToLoad.Add(((ChangeMusicCommand)c).Song); break; case "autogen": c = new EnemyAutoGenerationCommand(line); break; case "addrandombonus": c = new AddRandomBonusCommand(line); break; case "changemusicstate": c = new ChangeMusicStateCommand(line); break; case "wait": c = new WaitCommand(line); break; case "addbomb": c = new AddBombToPlayerCommand(line); break; default: throw new Exception("Unknown TGPA script command " + line); } commands.Add(c); line = reader.ReadLine(); while (line.Equals("") || line.StartsWith("//")) line = reader.ReadLine(); } //commands e.AddCommands(commands); map.Events.Add(e); line = reader.ReadLine(); while (line.Equals("") || line.StartsWith("//")) line = reader.ReadLine(); } //events #endregion reader.Close(); return map; }