Пример #1
0
 internal Level LoadLevelFromFile(string filename)
 {
     var level = new Level();
     using (var sr = new StreamReader(filename))
     {
         while (!sr.EndOfStream)
         {
             string s = sr.ReadLine();
             if (s != null && (s.StartsWith("//") || s == ""))
                 continue;
             if (s != null)
             {
                 string[] split = s.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                 if (split[2] == "startzone")
                 {
                     Vector2 sp = new Vector2(float.Parse(split[0]), float.Parse(split[1]));
                     level.StartPosition = sp;
                 }
                 else if (split[2] == "finishzone")
                 {
                     Vector2 fp = new Vector2(float.Parse(split[0]), float.Parse(split[1]));
                     level.FinishZone = new Rectangle((int)fp.X, (int)fp.Y, 32, 32);
                 }
                 else if (split[2] == "enemy")
                 {
                     Texture2D sprite = enemyTexture;
                     Enemy enemy = new Enemy(new Vector2(float.Parse(split[0]), float.Parse(split[1])), Vector2.Zero,
                                             sprite);
                     level.GameObjects.Add(enemy);
                 }
                 else
                 {
                     var sprite = Content.Load<Texture2D>("objects/" + split[2]);
                     sprite.Name = split[2];
                     var gameObj = new GameObject(
                         new Vector2(float.Parse(split[0]), float.Parse(split[1])),
                         new Vector2(0, 0), sprite, GameObject.ObjectType.Block);
                     level.GameObjects.Add(gameObj);
                 }
             }
         }
     }
     return level;
     //first line = X
     //second line = Y
     //third line = spritename
 }
Пример #2
0
 //parse the .txt file and create a new Level object that contains all of the objects and positions
 //of the new level. returns the new Level object.
 internal Level LoadLevelFromFile(string filename)
 {
     var level = new Level();
     using (var sr = new StreamReader(filename))
     {
         while (!sr.EndOfStream)
         {
             string s = sr.ReadLine();
             if (s != null && (s.StartsWith("//") || s == ""))
                 continue;
             if (s != null)
             {
                 string[] split = s.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                 if (split[2] == "startzone")
                 {
                     Vector2 sp = new Vector2(float.Parse(split[0]), float.Parse(split[1]));
                     level.StartPosition = sp;
                 }
                 else if (split[2] == "finishzone")
                 {
                     Vector2 fp = new Vector2(float.Parse(split[0]), float.Parse(split[1]));
                     level.FinishZone = new Rectangle((int)fp.X, (int)fp.Y, 32, 32);
                 }
                 else if (split[2] == "enemy")
                 {
                     Texture2D sprite = enemyTexture;
                     Enemy enemy = new Enemy(new Vector2(float.Parse(split[0]), float.Parse(split[1])), Vector2.Zero,
                                             sprite);
                     level.GameObjects.Add(enemy);
                 }
                 else if (split[2] == "itembox")
                 {
                     Texture2D sprite1 = Content.Load<Texture2D>("objects/itembox");
                     Texture2D sprite2 = Content.Load<Texture2D>("objects/itembox_hit");
                     Item newItem = new Item(new Vector2(float.Parse(split[4]),
                                                                     float.Parse(split[5])),
                                                         ItemTextures[int.Parse(split[3])],
                                                         int.Parse(split[3]));
                     newItem.alive = false;
                     ItemBox itemBoxObj = new ItemBox(new Vector2(float.Parse(split[0]),
                                                                  float.Parse(split[1])),
                                                      Vector2.Zero,
                                                      sprite1,
                                                      sprite2,
                                                      GameObject.ObjectType.ItemBox,
                                                      newItem);
                     level.GameObjects.Add(itemBoxObj);
                     level.GameObjects.Add(newItem);
                 }
                 else
                 {
                     Texture2D sprite = Content.Load<Texture2D>("objects/" + split[2]);
                     GameObject gameObj = new GameObject(
                         new Vector2(float.Parse(split[0]), float.Parse(split[1])),
                         Vector2.Zero, sprite, GameObject.ObjectType.Block);
                     level.GameObjects.Add(gameObj);
                 }
             }
         }
     }
     return level;
 }