Exemplo n.º 1
0
        private static void createMapObjects(Scene scene, float mapScale, bool flipVertically, bool flipHorizontally)
        {
            float layernum  = 1;
            float mapWidth  = tilemap.MapWidth * tilemap.TileWidth * mapScale;
            float mapHeight = tilemap.MapHeight * tilemap.TileHeight * mapScale;

            foreach (var layer in tilemap.Layers)
            {
                for (uint y = 0; y < tilemap.MapHeight; y++)
                {
                    for (uint x = 0; x < tilemap.MapWidth; x++)
                    {
                        uint dataIndex = y * tilemap.MapWidth + x;
                        uint tileIndex = layer.Data[dataIndex];
                        if (tileIndex != 0 && tileIndex < tilemap.Tiles.Length)
                        {
                            Vector position = new Vector(x * (tilemap.TileWidth), y * (tilemap.TileHeight)) * mapScale;
                            Vector scale    = new Vector(tilemap.Tiles[tileIndex].Width, tilemap.Tiles[tileIndex].Height) * mapScale;

                            if (flipVertically)
                            {
                                position.X += ((mapWidth / 2) - position.X) * 2;
                            }

                            if (flipHorizontally)
                            {
                                position.Y += ((mapWidth / 2) - position.Y) * 2;
                            }

                            Tile tile = tilemap.Tiles[tileIndex];
                            if (tile.BoxColliders.Count == 0)
                            {
                                MapObject mapObject = scene.CreateEntity <MapObject>();
                                setObjectPosition(mapObject, position, scale, tile, layernum, "MapTileSet"); // Change to not manual string
                                mapObjects.Add(mapObject);
                            }
                            else
                            {
                                KeyValuePair <string, List <BoxCol> > boxCollider = tile.BoxColliders.First();
                                MapObjectCollision mapObject = scene.CreateEntity <MapObjectCollision>();
                                setObjectPosition(mapObject, position, scale, tile, layernum, "MapTileSet"); // Change to not manual string
                                setCollision(mapObject, boxCollider.Value[0]);
                                mapObjects.Add(mapObject);
                            }
                        }
                    }
                }
                layernum -= 1f / (float)(tilemap.Layers.Length);
            }
        }
Exemplo n.º 2
0
        public static bool CreateObjectFromTileSet(string name, string tileset, Vector position, Vector scale, float layer,
                                                   out MapObject mapObject, out MapObjectCollision mapObjectCol)
        {
            Tileset tilesetContent = Content.Load <Tileset>("Tilesets\\" + tileset);

            mapObject    = null;
            mapObjectCol = null;

            foreach (Tile tile in tilesetContent.tiles)
            {
                if (tile.Properties.ContainsKey("Name") && tile.Properties["Name"] == name)
                {
                    // Add light
                    if (tile.Properties.ContainsKey("IsLight") && tile.Properties["IsLight"] == "true")
                    {
                        MapLight maplight = SceneManager.GetCurrentScene().CreateEntity <MapLight>();
                        Light    light    = maplight.GetComponent <Light>();

                        light.Position = position + (scale / 2);
                    }

                    // Add collision object
                    if (tile.BoxColliders.Count > 0)
                    {
                        mapObjectCol = SceneManager.GetCurrentScene().CreateEntity <MapObjectCollision>();
                        mapObjectCol.GetComponent <Componets.Transform>().Position = position;
                        mapObjectCol.GetComponent <Componets.Transform>().Scale    = scale;
                        mapObjectCol.GetComponent <Sprite>().ContentName           = tileset;
                        mapObjectCol.GetComponent <Sprite>().SpriteLocation        = tile.Source;
                        mapObjectCol.GetComponent <Sprite>().Layer = layer;

                        BoxCol       collider = tile.BoxColliders.First().Value[0];
                        BoxCollision col      = mapObjectCol.GetComponent <BoxCollision>();

                        BoxData box = new BoxData();
                        box.Position    = new Vector(collider.Rectangle.X / (float)tile.Width, collider.Rectangle.Y / (float)tile.Height);
                        box.Scale       = new Vector(collider.Rectangle.Width / (float)tile.Width, collider.Rectangle.Height / (float)tile.Height);
                        box.TriggerOnly = collider.TriggerOnly;

                        IBox ibox = col.World.Create(position.X + box.Position.X * scale.X,
                                                     position.Y + box.Position.Y * scale.Y,
                                                     box.Scale.X * scale.X,
                                                     box.Scale.Y * scale.Y);

                        ibox.Data = box;
                        col.Boxes.Add(ibox);

                        return(true);
                    }

                    // Add object
                    else
                    {
                        mapObject = SceneManager.GetCurrentScene().CreateEntity <MapObject>();
                        mapObject.GetComponent <Componets.Transform>().Position = position;
                        mapObject.GetComponent <Componets.Transform>().Scale    = scale;
                        mapObject.GetComponent <Sprite>().ContentName           = tileset;
                        mapObject.GetComponent <Sprite>().SpriteLocation        = tile.Source;
                        mapObject.GetComponent <Sprite>().Layer = layer;

                        return(true);
                    }
                }
            }

            return(false);
        }