ccmny

LevelLoader

Aug 26th, 2011
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using Microsoft.Xna.Framework;
  8. using FarseerPhysics.Dynamics;
  9. using System.IO;
  10.  
  11. namespace Platform.Levels
  12. {
  13.     class LevelLoader
  14.     {
  15.         delegate GameComponent AddFunction(Game1 game, SpriteBatch spriteBatch, World world, int x, int y);
  16.  
  17.         private static Dictionary<int, AddFunction> Environment= new Dictionary<int, AddFunction>
  18.         {
  19.             {255, (g, sB, w, x, y) => {return new Block(g, sB, w, x * 32, y * 32, BlockType.STONE);}},
  20.             {205, (g, sB, w, x, y) => {return new Block(g, sB, w, x * 32, y * 32, BlockType.BOX);}}
  21.         };
  22.  
  23.         public static Level LoadLevel(string name, Game1 game, SpriteBatch spriteBatch)
  24.         {
  25.             Level level = new Level(game, spriteBatch);
  26.             Bitmap levelBmp = (Bitmap)Bitmap.FromFile(Directory.GetCurrentDirectory() + "\\" + name + ".png");
  27.  
  28.             for (int y = 0; y < levelBmp.Height; y++)
  29.             {
  30.                 for (int x = 0; x < levelBmp.Width; x++)
  31.                 {
  32.                     var pixel = levelBmp.GetPixel(x, y);
  33.  
  34.                     if (pixel.R > 0)
  35.                     {
  36.                        
  37.                     }
  38.                     else if (pixel.G > 0)
  39.                     {
  40.                         try
  41.                         {
  42.                             level.AddComponent(Environment[pixel.G](game, spriteBatch, level.world, x, y));
  43.                         }
  44.                         catch (Exception)
  45.                         {
  46.                            
  47.                         }
  48.                     }
  49.                     else if (pixel.B > 0)
  50.                     {
  51.                        
  52.                     }
  53.                 }
  54.             }
  55.             return level;
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment