Exemple #1
0
    public void CopyFromMap(IMap fromMap, int sourceX = 0, int sourceY = 0, int destX = 0, int destY = 0, int?width = null, int?height = null)
    {
        var sourceMap = (fromMap as TiledMooseMap)?.Map;

        if (sourceMap == null)
        {
            throw new Exception("Can only copy another TiledMap");
        }

        foreach (var tileset in sourceMap.Tilesets)
        {
            if (!Map.Tilesets.Contains(tileset))
            {
                Map.AddTileset(tileset, sourceMap.GetTilesetFirstGlobalIdentifier(tileset));
            }
        }

        for (var layerIndex = 0; layerIndex < sourceMap.Layers.Count; layerIndex++)
        {
            var layer = sourceMap.Layers[layerIndex];

            switch (layer)
            {
            case TiledMapObjectLayer objectLayer:
                // TODO: Convert tiled objects to game objects
                Map.AddLayer(new TiledMapObjectLayer(objectLayer.Name, objectLayer.Objects.Clone() as TiledMapObject[]));
                break;

            case TiledMapTileLayer sourceLayer:
                if (layerIndex >= Map.TileLayers.Count)
                {
                    Map.AddLayer(new TiledMapTileLayer(sourceLayer.Name, Map.Width, Map.Height, Map.TileWidth, Map.TileHeight));
                }
                var destLayer = (Map.Layers[layerIndex] as TiledMapTileLayer) !;
                for (ushort x = 0; x < (width ?? sourceMap.Width); x++)
                {
                    for (ushort y = 0; y < (height ?? sourceMap.Height); y++)
                    {
                        destLayer.SetTile
                            ((ushort)(x + destX), (ushort)(y + destY),
                            (uint)sourceLayer.GetTile((ushort)(x + sourceX), (ushort)(y + sourceY)).GlobalIdentifier);
                    }
                }
                break;
            }
        }

        BuildLayerCache();
    }