Ejemplo n.º 1
0
        private static Player MakeWorldAndPlayer(params Rectangle[] immovables)
        {
            var world       = new World();
            var map         = new TiledMap("TestMap", Room.NumTilesWidth, Room.NumTilesHeight, Room.TileWidth, Room.TileHeight, TiledMapTileDrawOrder.RightDown, TiledMapOrientation.Orthogonal);
            var bottomLayer = new TiledMapTileLayer(Dungeon.BottomLayerName, Room.NumTilesWidth, Room.NumTilesHeight, Room.TileWidth, Room.TileHeight);

            for (int y = 0; y < bottomLayer.Height; ++y)
            {
                for (int x = 0; x < bottomLayer.Width; ++x)
                {
                    bottomLayer.SetTile((ushort)x, (ushort)y, 1);
                }
            }
            map.AddLayer(bottomLayer);
            var collidables = new TiledMapRectangleObject[immovables.Length];
            int i           = 0;

            foreach (Rectangle immovable in immovables)
            {
                // Note: Object IDs for Tiled maps start at 1, not 0.
                collidables[i] = new TiledMapRectangleObject(i + 1, "collidable" + (i + 1), new Size2(immovable.Width, immovable.Height), new Vector2(immovable.X, immovable.Y));
                ++i;
            }
            var collisionLayer = new TiledMapObjectLayer(Dungeon.CollisionLayerName, collidables);

            map.AddLayer(collisionLayer);
            var dungeon = new Dungeon(world, map, null);

            world.Dungeons.Add(dungeon);
            var player = new Player(dungeon, new Point(Room.Width / 2, Room.Height / 2), Direction.Down);

            world.Player = player;
            return(player);
        }
Ejemplo n.º 2
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();
    }