public static Map FromImage( Image image ) { Bitmap bitmap = new Bitmap(image); Map map = new Map(); Dictionary<Color, Room> RoomIDMap = new Dictionary<Color, Room>(); Surface wall = new Surface(); wall.Texture = "default_wall"; int wallID = 0; map.Surfaces.Add(wallID,wall); Surface floor = new Surface(); floor.Texture = "default_floor"; int floorID = 1; map.Surfaces.Add(floorID,floor); for (int y = 0; y < bitmap.Height; y++) { for (int x = 0; x < bitmap.Width; x++) { if (PosIsTile(x, y, bitmap)) { Color c = bitmap.GetPixel(x, bitmap.Height - y- 1); Room room = null; if (RoomIDMap.ContainsKey(c)) room = RoomIDMap[c]; else { room = new Room(); RoomIDMap.Add(c, room); } Tile tile = new Tile(); tile.Location = new Point(x,y); if (IsDoor(tile.Location, bitmap)) tile.Type = Tile.TileType.ClosedDoor; else tile.Type = Tile.TileType.Open; tile.Floor = floorID; foreach (Direction dir in FindWallDirections(tile.Location, bitmap)) tile.Walls.Add(dir, wallID); room.Tiles.Add(tile); } } } bitmap.Dispose(); foreach (KeyValuePair<Color, Room> i in RoomIDMap) map.Rooms.Add(i.Value); return map; }
private void DrawTile( Graphics graphics, Brush brush, Tile tile ) { graphics.FillRectangle(brush, tile.Location.X * GridSize, tile.Location.Y * GridSize, GridSize, GridSize); graphics.DrawRectangle(Pens.White, tile.Location.X * GridSize, tile.Location.Y * GridSize, GridSize, GridSize); if (tile.Walls.ContainsKey(Direction.North)) graphics.DrawLine(Pens.Black, tile.Location.X * GridSize, tile.Location.Y * GridSize + GridSize, tile.Location.X * GridSize + GridSize, tile.Location.Y * GridSize + GridSize); if (tile.Walls.ContainsKey(Direction.South)) graphics.DrawLine(Pens.Black, tile.Location.X * GridSize, tile.Location.Y * GridSize, tile.Location.X * GridSize + GridSize, tile.Location.Y * GridSize); if (tile.Walls.ContainsKey(Direction.West)) graphics.DrawLine(Pens.Black, tile.Location.X * GridSize, tile.Location.Y * GridSize, tile.Location.X * GridSize, tile.Location.Y * GridSize + GridSize); if (tile.Walls.ContainsKey(Direction.East)) graphics.DrawLine(Pens.Black, tile.Location.X * GridSize + GridSize, tile.Location.Y * GridSize, tile.Location.X * GridSize + GridSize, tile.Location.Y * GridSize + GridSize); Pen p = Pens.Transparent; switch (tile.Type) { case Tile.TileType.ClosedDoor: p = Pens.Brown; break; case Tile.TileType.OpenDoor: p = Pens.BurlyWood; break; case Tile.TileType.Blocked: p = Pens.Red; break; case Tile.TileType.Unknown: p = Pens.WhiteSmoke; break; } if (p != Pens.Transparent) graphics.DrawRectangle(p, tile.Location.X * GridSize + GridSize * 0.25f, tile.Location.Y * GridSize + GridSize * 0.25f, GridSize * 0.5f, GridSize * 0.5f); }