Beispiel #1
0
 public void AddLayer(MapLayer newLayer)
 {
     if (newLayer == null)
     { throw new ArgumentNullException(); }
     if (layers.Contains(newLayer))
     { throw new ArgumentException("this layer is already present"); }
     if (activeLayer == null)
     { activeLayer = newLayer; }
     layers.Add(newLayer);
 }
Beispiel #2
0
 public BigMap()
 {
     layers = new List<MapLayer>();
     layersRO = layers.AsReadOnly();
     activeLayer = new MapLayer();
     activeLayer.Visible = true;
     activeLayer.Name = "Base Layer";
     layers.Add(activeLayer);
     MapLayer walls = new MapLayer();
     walls.Name = "Walls";
     walls.IsWall = true;
     walls.Visible = true;
     layers.Add(walls);
 }
Beispiel #3
0
 public void ClearLayers()
 {
     layers.Clear();
     activeLayer = null;
 }
        private static void DoSerialize(BigMap map, TileSet tileset, bool IncludeTileset, Stream destination)
        {
            SerializedMap result = new SerializedMap();
            Dictionary<Tile, int> tileLookup = new Dictionary<Tile, int>();
            if (tileset != null)
            {
                result.TileHeight = tileset.TileHeight;
                result.TileWidth = tileset.TileWidth;

                foreach (Tile tile in tileset.Tiles)
                {
                    if (IncludeTileset)
                    {
                        result.Tiles.Add(SaveImage(tile.FullImage));
                    }
                    tileLookup.Add(tile, tileLookup.Count);
                }
            }
            else
            { result.Tiles = null; }
            foreach (var layer in map.Layers)
            {
                Rectangle r = layer.ImageBounds;
                MapChunk chunk = new MapChunk();
                chunk.X = r.Left;
                chunk.Y = r.Top; ;
                chunk.Width = r.Width;
                chunk.Height = r.Height;
                for (int x = r.Left; x < r.Right; x++)
                {
                    for (int y = r.Top; y < r.Bottom; y++)
                    {
                        Tile tile = layer[x, y];
                        int index = -1;
                        if (tile != null)
                        {
                            if (!tileLookup.TryGetValue(tile, out index))
                            {
                                index = tileLookup.Count;
                                tileLookup.Add(tile, index);
                            }
                        }
                        chunk.Data.Add(index);
                    }
                }
                MapLayer serializedLayer = new MapLayer();
                serializedLayer.Visible = layer.Visible;
                serializedLayer.IsWall = layer.IsWall;
                serializedLayer.Chunks.Add(chunk);
                result.Layers.Add(serializedLayer);
            }
            Serializer.Serialize(destination, result);
        }
        private void DrawLayer(Graphics g, MapLayer layer, Rectangle clip, bool DrawGrid)
        {
            int tw = tileset.TileWidth;
            int th = tileset.TileHeight;
            Rectangle clip2 = (layer.IsWall ? new Rectangle(clip.Left, clip.Top * 2, clip.Width, clip.Height * 2) : clip);

            layer.GetAllTilesInRectangle(clip2, (tile, x, y) =>
            {
                if (layer.IsWall)
                {
                    int wallWidth = 6;
                    int halfWall = wallWidth / 2;
                    if (tile == null) { return; }
                    Image wallImage = tile[x, y];
                    if (wallImage == null) { return; }
                    int x1 = (x - xOffset) * tw;
                    int y1 = ((y / 2) - yOffset) * th;
                    int x2 = x1 + tw;
                    int y2 = y1 + th;
                    Rectangle clipTile = Rectangle.Empty;
                    if (y % 2 == 0)
                    {
                        clipTile = new Rectangle(x1 - halfWall, y1, wallWidth, th);
                    }
                    else
                    {
                        clipTile = new Rectangle(x1, y1 - halfWall, tw, wallWidth);
                    }
                    try
                    {
                        g.DrawImage(tile[x, y], clipTile, 0, 0, tw, th, GraphicsUnit.Pixel);
                    }
                    catch (Exception ignored)
                    {
                        g.DrawLine(Pens.Red, x1, y1, x2, y2);
                        g.DrawLine(Pens.Red, x1, y2, x2, y1);
                        g.DrawRectangle(Pens.Red, x1, y1, x2 - x1, y2 - y1);
                    }
                }
                else
                {
                    int x1 = (x - xOffset) * tw;
                    int y1 = (y - yOffset) * th;
                    int x2 = x1 + tw;
                    int y2 = y1 + th;
                    if (tile == null)
                    {
                        if (DrawGrid)
                        {
                            g.DrawLine(Pens.Black, x1, y1, x2, y2);
                            g.DrawLine(Pens.Black, x1, y2, x2, y1);
                            g.DrawRectangle(Pens.Black, x1, y1, x2 - x1, y2 - y1);
                        }
                    }
                    else
                    {
                        try
                        {
                            g.DrawImage(tile[x, y], x1, y1, tw, th);
                        }
                        catch (Exception ignored)
                        {
                            g.DrawLine(Pens.Red, x1, y1, x2, y2);
                            g.DrawLine(Pens.Red, x1, y2, x2, y1);
                            g.DrawRectangle(Pens.Red, x1, y1, x2 - x1, y2 - y1);
                        }
                    }
                }
            });
        }