private void DrawTile(SpriteBatch batch, Map map, TmxLayerTile tile)
        {
            TmxTileset tileset = map.GetTilesetForTile(tile);

            if (tileset == null)
            {
                return;
            }

            Texture2D tilesetTexture = map.Textures[tileset];

            var effect   = SpriteEffects.None;
            var offset   = Vector2.Zero;
            var rotation = 0.0f;

            if (tile.DiagonalFlip)
            {
                rotation  = -MathHelper.PiOver2;
                offset.Y -= tileset.TileHeight;

                effect |= SpriteEffects.FlipHorizontally;
            }

            if (tile.HorizontalFlip)
            {
                if (tile.DiagonalFlip)
                {
                    effect ^= SpriteEffects.FlipVertically;
                }
                else
                {
                    effect ^= SpriteEffects.FlipHorizontally;
                }
            }

            if (tile.VerticalFlip)
            {
                if (!tile.DiagonalFlip)
                {
                    effect ^= SpriteEffects.FlipVertically;
                }
                else
                {
                    effect ^= SpriteEffects.FlipHorizontally;
                }
            }

            var sourceRect = map.GetSourceRectangleForTile(tileset, tile);

            batch.Draw(
                tilesetTexture,
                new Vector2(tileset.TileWidth * tile.X, tileset.TileHeight * tile.Y) - offset,
                sourceRect,
                Color.White,
                rotation,
                Vector2.Zero,
                1,
                effect,
                0);
        }
Exemplo n.º 2
0
        private void AddTileVertices(Vertex[] vertices, int verticeIndex, TmxLayerTile tileItem, int tilesetColumns)
        {
            var xIndex = (tileItem.Gid - 1) % tilesetColumns;
            var yIndex = (tileItem.Gid - 1) / tilesetColumns;

            AddTileVertices(vertices, verticeIndex, xIndex, yIndex, new Vector2f(tileItem.X, tileItem.Y));
        }
Exemplo n.º 3
0
        /// <summarY>
        /// Draws the map
        /// </summarY>
        /// <param name="map">The map to draw</param>
        /// <param name="camera">The position to draw from</param>
        /// <param name="orientation">The rotation to draw from in degrees</param>
        public void RenderMap(SpriteBatch spriteBatch, Map map, Vector2 camera, float orientation, int cellSize,
                              string wallsLayer)
        {
            TmxMap mapData     = map.Data;
            int    slices      = viewport.Width;
            float  halfFov     = FOV / 2;
            float  focalLength = slices / 2 / (float)Math.Tan(halfFov);
            float  cameraAngle = orientation * (float)(Math.PI / 180.0f);

            float sliceAngle = FOV / slices;
            float beginAngle = cameraAngle - halfFov;

            // draw ceiling and floor
            spriteBatch.Draw(blankTexture, new Rectangle(0, 0, viewport.Width, viewport.Height / 2),
                             Color.FromNonPremultiplied(23, 14, 8, 255));
            spriteBatch.Draw(blankTexture, new Rectangle(0, viewport.Height / 2, viewport.Width, viewport.Height / 2),
                             Color.DarkKhaki);

            // draw all wallslices
            for (int x = 0; x < slices; x++)
            {
                float angle = beginAngle + x * sliceAngle;

                RayCaster.HitData castData;
                if (!RayCaster.RayIntersectsGrid(camera, angle, cellSize, out castData,
                                                 map.GetIsTileOccupiedFunction(wallsLayer)))
                {
                    continue;
                }

                // get the texture slice
                int          tileIndex = (int)(castData.tileCoordinates.Y * mapData.Width + castData.tileCoordinates.X);
                TmxLayer     wallLayer = mapData.Layers[wallsLayer];
                TmxLayerTile tile      = wallLayer.Tiles[tileIndex];
                TmxTileset   tileset   = map.GetTilesetForTile(tile);
                if (tileset == null)
                {
                    continue;
                }

                // fix fisheye for distance and get slice height
                float distance    = (float)(castData.rayLength * Math.Cos(angle - cameraAngle));
                int   sliceHeight = (int)(cellSize * focalLength / distance);
                zBuffer[x] = distance;

                // get drawing rectangles
                Rectangle wallRectangle    = new Rectangle(x, viewport.Height / 2 - sliceHeight / 2, 1, sliceHeight);
                Rectangle textureRectangle = map.GetSourceRectangleForTile(tileset, tile);

                textureRectangle.X =
                    (int)(textureRectangle.X + (textureRectangle.Width * castData.cellFraction) % cellSize);
                textureRectangle.Width = 1;

                // get texture tint
                float dot          = Vector2.Dot(castData.normal, Vector2.UnitY);
                Color lightingTint = Math.Abs(dot) > 0.9f ? Color.Gray : Color.White;

                spriteBatch.Draw(map.Textures[tileset], wallRectangle, textureRectangle, lightingTint);
            }
        }
Exemplo n.º 4
0
        public static TileSetAditionalInfo GetTileSetAditionalInfo(this TmxMap map, TmxLayerTile tile,
                                                                   out TmxTileset tileSet)
        {
            tileSet = map.GetTileSet(tile);

            var tileSetColumns = tileSet.Columns.Value == 0 ? 1 : tileSet.Columns.Value;
            var tileSetWidth   = tileSetColumns > 0 ?
                                 tileSetColumns * map.TileWidth :
                                 map.TileWidth;
            var tileSetHeight = tileSetColumns > 0 ?
                                (tileSet.TileCount.Value / tileSetColumns) * map.TileWidth :
                                map.TileWidth;
            var tileSetRows = tileSetHeight / tileSet.TileHeight;

            var position = new Vector2(tile.X * map.TileWidth,
                                       ((tile.Y + 1) * map.TileHeight) - tileSet.TileHeight);

            var frame  = tile.Gid - tileSet.FirstGid;
            var column = frame <= tileSetColumns ? frame : frame % (float)tileSetColumns;
            var row    = (int)Math.Floor((float)frame / tileSetColumns);

            var source = new Vector2(column * tileSet.TileWidth,
                                     row * tileSet.TileHeight);

            return(new TileSetAditionalInfo(position, source, tileSetColumns, tileSetRows, tileSetWidth, tileSetHeight));
        }
Exemplo n.º 5
0
        GameObject getBaseGameobject(TmxLayerTile tile)
        {
            GameObject go = null;

            if (tileIdMap.ContainsKey(tile.Gid) &&
                tileIdMap[tile.Gid].Properties.ContainsKey(customProperties["use base prefab"]))
            {
                var fabs = prefabs.Where(p => p.name == tileIdMap[tile.Gid].Properties[customProperties["use base prefab"]]).ToList();
                if (fabs.Count != 0)
                {
                    go = Instantiate(fabs.First());
                }
                else
                {
                    go = new GameObject("Tile");
                }
            }
            else
            {
                go = new GameObject("Tile");
            }

            if (go.GetComponent <SpriteRenderer>() == null &&
                !(tileIdMap.ContainsKey(tile.Gid) &&
                  tileIdMap[tile.Gid].Properties.ContainsKey(customProperties["don't render"]) &&
                  (tileIdMap[tile.Gid].Properties[customProperties["don't render"]].ToLower() != "true")))
            {
                go.AddComponent <SpriteRenderer>();
            }

            return(go);
        }
Exemplo n.º 6
0
    /// <summary>
    /// Gets the acctual gid of a TilesetTile and returns the tileset that the tile is from-
    /// </summary>
    /// <param name="map">The map where the TilesetTile is from.</param>
    /// <param name="layerTile">The TilesetTile to find the correct gid from.</param>
    /// <param name="toFindTileset">The correct Tileset.</param>
    /// <returns>The gid value.</returns>
    private int GetGid(TmxMap map, TmxLayerTile layerTile, out TmxTileset toFindTileset)
    {
        int gid = layerTile.Gid;

        toFindTileset = null;

        // Empty tile, do nothing
        if (gid == 0)
        {
            return(-1);
        }

        // Get the acctual gid value
        gid--;

        // Get the correct tileset with the gid value
        for (int j = 0; j < map.Tilesets.Count; j++)
        {
            gid -= map.Tilesets[j].TileCount ?? 0;
            if (gid <= 0)
            {
                toFindTileset = map.Tilesets[j];
                break;
            }
        }

        // set gid to a positive value again
        return(gid + toFindTileset.TileCount ?? 0);
    }
Exemplo n.º 7
0
    private void SpawnTile(TmxLayerTile tile, int x, int y, TmxLayer layer, GridLayerConfig gridLayerConfig, int layerNumber)
    {
        Transform container   = GetContainer("TileLayers");
        GridTile  spawnedTile = null;

        if (gridLayerConfig != null)
        {
            if (gridLayerConfig.OverridePrefab)
            {
                spawnedTile = Instantiate(gridLayerConfig.OverridePrefab, scene.transform);
            }
            else
            {
                spawnedTile = Instantiate(config.GridTilePrefab, scene.transform);
            }
        }
        else
        {
            spawnedTile = Instantiate(config.GridTilePrefab);
        }
        Sprite sprite = GetTileSprite(tile.Gid);

        ColliderConfig conf = GetColliderConfig(tile.Gid);

        spawnedTile.Initialize(sprite, x, y, gridLayerConfig, conf, layer.Name, layerNumber);
        GridTileLayerManager.main.AddTile(spawnedTile, layer.Name, container);
    }
        private void DrawVisibleTiles(SpriteBatch batch, Map map, TmxLayer layer, Camera camera)
        {
            TmxMap    mapData      = map.Data;
            Rectangle cameraBounds = camera.GetBounds();

            // calculate how many tiles to draw
            int cameraTilesWidth  = ((int)camera.Width / mapData.TileWidth) + 2;
            int cameraTilesHeight = ((int)camera.Height / mapData.TileHeight) + 2;

            // get camera position in tiles
            int xCameraStartTile = (int)((camera.Position.X - cameraBounds.Width / 2) / mapData.TileWidth);
            int yCameraStartTile = (int)((camera.Position.Y - cameraBounds.Height / 2) / mapData.TileHeight);

            int xCameraEndTile = xCameraStartTile + cameraTilesWidth;
            int yCameraEndTile = yCameraStartTile + cameraTilesHeight;

            // clamp values
            ClampValue(ref xCameraStartTile, mapData.Width);
            ClampValue(ref xCameraEndTile, mapData.Width);
            ClampValue(ref yCameraStartTile, mapData.Height);
            ClampValue(ref yCameraEndTile, mapData.Height);

            for (int y = yCameraStartTile; y < yCameraEndTile; y++)
            {
                for (int x = xCameraStartTile; x < xCameraEndTile; x++)
                {
                    TmxLayerTile tile = layer.Tiles[y * mapData.Width + x];
                    DrawTile(batch, map, tile);
                }
            }
        }
Exemplo n.º 9
0
    /// <summary>
    /// Gets the gid values of the tiles in an array. The gid are not the acctual gid values of the map, but the gid
    /// values of the tileset. Assume tileset a has 10 tiles and tileset b has 10 tiles. If at position 1 there is
    /// the tileset tile 1 of tileset a and at position 2 the tileset tile 1 of tileset b then the gid values of the
    /// return array are the same. To put it simply, just use one tileset and one layer for this method.
    /// </summary>
    /// <param name="mapName">The name of the map to get the gid values from.</param>
    /// <param name="prefabs">A list of all prefabs and their local position.</param>
    /// <returns>The gid values of the map.</returns>
    public Fast2DArray <int> GetReplacableMap(string mapName, out PropertyDict properties, out List <PrefabLocations> prefabs)
    {
        prefabs = new List <PrefabLocations>();

        TmxMap map = LoadMap(mapName);

        properties = map.Properties;

        Fast2DArray <int> retArr = new Fast2DArray <int>(map.Width, map.Height);

        if (map.TileLayers.Count == 0)
        {
            throw new Exception("Replacable map " + mapName + " has no tile layers!");
        }
        else if (map.TileLayers.Count > 1)
        {
            Debug.LogWarning("Replacable map " + mapName + " has more than 1 tile layer. I am only reading the first layer!");
        }
        if (map.Tilesets.Count != 1)
        {
            Debug.LogWarning("Replacable map " + mapName + " has not only 1 tileset. This might cause issues!");
        }

        for (int t = 0; t < map.TileLayers[0].Tiles.Count; t++)
        {
            TmxLayerTile layerTile = map.TileLayers[0].Tiles[t];
            int          gid       = GetGid(map, layerTile, out _);
            retArr.Set(gid, layerTile.X, map.Height - layerTile.Y - 1);
        }

        //LoadAllObjects(map, atX, atY, loadedGameobjects);

        // Go through each objectlayer and spawn their objects
        for (int i = 0; i < map.ObjectGroups.Count; i++)
        {
            for (int j = 0; j < map.ObjectGroups[i].Objects.Count; j++)
            {
                // Try to get the prefab
                if (TiledDict.Instance.TryGetObject(map.ObjectGroups[i].Objects[j].Type, out GameObject obj) == false)
                {
                    continue;
                }

                // Instantiate and position the object
                Vector3 pos;
                pos.x = ((float)map.ObjectGroups[i].Objects[j].X / map.TileWidth);
                pos.y = map.Height - ((float)map.ObjectGroups[i].Objects[j].Y / map.TileHeight);
                pos.z = 0f;

                prefabs.Add(new PrefabLocations {
                    Prefab = obj, Position = pos
                });
            }
        }

        return(retArr);
    }
Exemplo n.º 10
0
 public override void Init(
     TmxLayerTile tmxLayerTile,
     TmxMap _
     )
 {
     gid            = tmxLayerTile.Gid;
     horizontalFlip = tmxLayerTile.HorizontalFlip;
     verticalFlip   = tmxLayerTile.VerticalFlip;
     diagonalFlip   = tmxLayerTile.DiagonalFlip;
 }
Exemplo n.º 11
0
        public override void Init(
            TmxLayerTile tmxLayerTile,
            TmxMap tmxMap
            )
        {
            int tileGid   = tmxLayerTile.Gid;
            int tileFrame = TmxUtil.GetTileFrame(tileGid, tmxMap);

            kind = GetKind(tileFrame);
        }
Exemplo n.º 12
0
        public override void Draw(TimeSpan gameTime)
        {
            /*
             * Der einzige Weg (von dem ich bis jetzt weiß) eine Textur zu laden ist, eine neue Entity mit der Sprite-Komponente zu erstellen und
             * diese Entity dem EntityManager hinzuzufügen.
             * Das soll aber nur ein einziges mal passieren, darum diese Abfrage.
             */

            if (!init)
            {
                EntityManager.Add(new Entity().AddComponent(new Transform2D()).AddComponent(thisSprite));
                init = true;
            }

            foreach (TmxLayer layer in tmxMap.Layers)
            {
                for (int i = 0; i < layer.Tiles.Count; i++)
                {
                    TmxLayerTile currentTile = layer.Tiles[i];

                    /*  GID = Global ID
                     *  Exkurs: GIDs in einer Tiled-Map
                     *  Eine TileSheet hat z.B das Format 5x6 (5 Zeilen und 6 Spalten).
                     *  Tiles in diesem Sheet werden der Reihe nach durchnummeriert (Also hat das erste Tile oben links die GID 1).
                     *  Die ID ist über alle Tilesheets einzigartig, das heißt, dass wenn man zwei Tilesheets hat, das erste die IDs von 1 bis 31
                     *  und das zweite Tilesheet hat die IDs 32 bis 62.
                     *  Bei welcher GID das Tilesheet anfängt ist in der "FirstGid" Eigenschaft gespeichert.
                     */

                    int gid = currentTile.Gid - tileset.FirstGid;

                    //TODO: Verstehen
                    int column = gid % ((int)tileset.Image.Width / tileset.TileWidth);
                    int row    = (int)(gid / (tileset.Image.Width / tileset.TileWidth));


                    /*
                     *  Unsere Tiles sind in einer jpg-Datei gespeichert. Wird wissen wie viel Abstand die Teils zum Rand (Spacing) und zueinander (Margin) haben.
                     *  Damit können wir berechnen, welches Rechteck auf dem Bild unser gesuchtes Tile ist.
                     */

                    int tilesheetX = tileset.Spacing + column * (tileset.TileWidth + tileset.Margin);
                    int tilesheetY = tileset.Spacing + row * (tileset.TileHeight + tileset.Margin);

                    int worldX = currentTile.X * tileset.TileWidth;
                    int worldY = currentTile.Y * tileset.TileHeight;

                    Rectangle tilesheetRectangle = new Rectangle(tilesheetX, tilesheetY, tileset.TileWidth, tileset.TileHeight);

                    this.layer.SpriteBatch.Draw(thisSprite.Texture, new Vector2(worldX, worldY),
                                                tilesheetRectangle, Color.White, 0f, new Vector2(), Vector2.One,
                                                SpriteEffects.None, 1f, AddressMode.PointClamp);
                }
            }
        }
Exemplo n.º 13
0
        private Tile GetTile(TmxLayerTile tile, TmxTileset tileset)
        {
            int       code       = int.Parse(tileset.Tiles[tile.Gid - 1].Properties["Code"]);
            TermColor foreground = TermColor.Parse(tileset.Tiles[tile.Gid - 1].Properties["Foreground"]);
            TermColor background = TermColor.Parse(tileset.Tiles[tile.Gid - 1].Properties["Background"]);

            Character character   = new Character(code, foreground, background);
            Vector2D  position    = new Vector2D(tile.X, tile.Y);
            bool      collideable = bool.Parse(tileset.Tiles[tile.Gid - 1].Properties["Collideable"]);

            return(new Tile(character, position, collideable));
        }
Exemplo n.º 14
0
        public Rectangle GetTileRectangle(TmxLayerTile tile)
        {
            if (!IsValidTile(tile))
            {
                return(new Rectangle());
            }

            return(new Rectangle(
                       (int)(RelativeTileID(tile) % _tilesAcross) * TileWidth,
                       (int)(RelativeTileID(tile) / _tilesAcross) * TileHeight,
                       TileWidth,
                       TileHeight));
        }
Exemplo n.º 15
0
        public static Rectangle GetTileSourceRect(TmxMap map, TmxLayerTile tile)
        {
            TmxTileset tileset = GetTilesetForTile(map, tile);

            if (tileset == null)
            {
                return(Rectangle.Empty);
            }
            int relativeGid = tile.Gid - tileset.FirstGid;
            int tileColumn  = relativeGid % tileset.Columns.Value;
            int tileRow     = (int)Math.Floor((double)relativeGid / tileset.Columns.Value);

            return(new Rectangle(tileColumn * tileset.TileWidth, tileRow * tileset.TileHeight, tileset.TileWidth, tileset.TileHeight));
        }
Exemplo n.º 16
0
        public static TmxTileset GetTilesetForTile(TmxMap map, TmxLayerTile tile)
        {
            TmxTileset currentTileset = null;

            foreach (var tileset in map.Tilesets.OrderByDescending(_ => _.FirstGid))
            {
                if (tile.Gid >= tileset.FirstGid)
                {
                    currentTileset = tileset;
                    return(currentTileset);
                }
            }
            return(currentTileset);
        }
Exemplo n.º 17
0
        public bool HandleReplaceTileCollision(TmxLayerTile tile, TileLookupEnum collisionTile, TileLookupEnum newTile)
        {
            if (tile.Gid != TileLookup.GetTileValue(collisionTile))
            {
                return(false);
            }

            //_detailLayer.RemoveTile(tile.X, tile.Y);
            tile.Gid = TileLookup.GetTileValue(newTile);

            _tiledMapRenderer.CollisionLayer.RemoveTile(tile.X, tile.Y);

            return(true);
        }
Exemplo n.º 18
0
        public Vector2 Move(Vector2 velocity, Entity entity, string collisionLayer = "collision")
        {
            TmxLayer layer = Data.Layers[collisionLayer];

            Vector2   newPosition = entity.position + velocity;
            Rectangle sweptBounds = new Rectangle((int)(newPosition.X - entity.Width / 2),
                                                  (int)(newPosition.Y - entity.Height / 2),
                                                  entity.Width, entity.Height);

            // create swept rectangle
            sweptBounds = Rectangle.Union(sweptBounds, entity.BoundingBox);

            int minTileX = sweptBounds.Left / Data.TileWidth;
            int minTileY = sweptBounds.Top / Data.TileHeight;

            int maxTileX = sweptBounds.Right / Data.TileWidth + 1;
            int maxTileY = sweptBounds.Bottom / Data.TileHeight + 1;

            for (int y = minTileY; y < maxTileY; y++)
            {
                for (int x = minTileX; x < maxTileX; x++)
                {
                    if (x < 0 || x >= Data.Width ||
                        y < 0 || y >= Data.Height)
                    {
                        continue;
                    }

                    TmxLayerTile tile = layer.Tiles[y * Data.Width + x];
                    if (GetTilesetForTile(tile) == null)
                    {
                        continue;
                    }

                    Rectangle tileBounds   = GetTileBounds(x, y);
                    Rectangle intersection = Rectangle.Intersect(tileBounds, sweptBounds);

                    if (intersection.Width < intersection.Height)
                    {
                        velocity.X += -Math.Sign(velocity.X) * intersection.Width;
                    }
                    else
                    {
                        velocity.Y += -Math.Sign(velocity.Y) * intersection.Height;
                    }
                }
            }

            return(velocity);
        }
Exemplo n.º 19
0
        public void Init <T>(
            TmxLayer tmxLayer,
            TmxMap tmxMap
            ) where T : Tile, new()
        {
            int nTiles = tmxLayer.Tiles.Count;

            tiles = new List <Tile>(nTiles);
            for (var i = 0; i < nTiles; i++)
            {
                TmxLayerTile tmxLayerTile = tmxLayer.Tiles[i];
                T            tile         = new T();
                tile.Init(tmxLayerTile, tmxMap);
                tiles.Add(tile);
            }
        }
Exemplo n.º 20
0
        private TmxTilesetTile GetTilesetTile(TmxLayerTile tile, TmxTileset tileset)
        {
            int id = tile.Gid - tileset.FirstGid;
            //var a = tileset.Tiles.Where(x => x.Value.Id == id).ToList();

            var tilesList = tileset.Tiles.ToList();

            var r = tilesList.Where(x => x.Value.Id == id).FirstOrDefault();

            if (r.Value == null)
            {
                Logger.GetOrCreate("BrTiledLoader").Log(Logger.LogLevel.Info, "Invalid tile at " + tile.X + ", " + tile.Y + ". Id is " + id + ", gid is " + tile.Gid);
            }

            return(r.Value);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="LayerTile" /> class.
        /// </summary>>
        /// <param name="tmxTile">The TMX parsed tile.</param>
        /// <param name="tileset">Th</param>
        public LayerTile(TmxLayerTile tmxTile, Tileset tileset)
        {
            this.X = tmxTile.X;
            this.Y = tmxTile.Y;
            this.HorizontalFlip = tmxTile.HorizontalFlip;
            this.VerticalFlip   = tmxTile.VerticalFlip;
            this.DiagonalFlip   = tmxTile.DiagonalFlip;

            if (tileset != null)
            {
                this.Tileset = tileset;
                this.Id      = tmxTile.Gid - this.Tileset.FirstGid;

                this.TilesetTile = this.Tileset.TilesTable[this.Id];
            }
        }
Exemplo n.º 22
0
        public TmxTileset GetTilesetForTile(TmxLayerTile tile)
        {
            // TODO: fix tileset choosing, might have to refactor the tiledSharp lib for it
            // we now assume that tilesets are ordered in ascending order by gid
            int tilesetCount = Data.Tilesets.Count;

            for (int i = 0; i < tilesetCount; i++)
            {
                TmxTileset tileset = Data.Tilesets[tilesetCount - 1 - i];
                if (tile.Gid >= tileset.FirstGid)
                {
                    return(tileset);
                }
            }

            return(null);
        }
Exemplo n.º 23
0
    void GenerateTile(TmxLayerTile tile)
    {
        if (tile.Gid < 0)
        {
            return;
        }
        float   x      = tile.X;
        float   z      = tile.Y;
        Vector3 normal = Vector3.up;

        vertices.Add(new Vector3(-x, 0, z));
        vertices.Add(new Vector3(-x + 1, 0, z));
        vertices.Add(new Vector3(-x + 1, 0, z - 1));
        vertices.Add(new Vector3(-x, 0, z - 1));
        normals.Add(normal);
        normals.Add(normal);
        normals.Add(normal);
        normals.Add(normal);

        triangles.Add(squareCount * 4);
        triangles.Add((squareCount * 4) + 1);
        triangles.Add((squareCount * 4) + 3);
        triangles.Add((squareCount * 4) + 1);
        triangles.Add((squareCount * 4) + 2);
        triangles.Add((squareCount * 4) + 3);

        Vector2 texture      = tiles[tile.Gid - 1];
        float   fiddleLeft   = 0.005f;
        float   fiddleRight  = 0.005f;
        float   fiddleTop    = 0.005f;
        float   fiddleBottom = 0.005f;
        float   left         = tileUnit * texture.x + tileUnit - fiddleLeft;
        float   top          = tileUnit * texture.y + fiddleTop;
        float   right        = tileUnit * texture.x + fiddleRight;
        float   bottom       = tileUnit * texture.y + tileUnit - fiddleBottom;

        uv.Add(new Vector2(left, top));
        uv.Add(new Vector2(right, top));
        uv.Add(new Vector2(right, bottom));
        uv.Add(new Vector2(left, bottom));
        squareCount++;
    }
Exemplo n.º 24
0
    void DrawMesh(int tileCountX, int tileCountZ, TmxLayer layer)
    {
        //Vector3 startingPosition = new Vector3(-tileCountX / 2 - unitSize / 2, 0f, -tileCountZ / 2 - unitSize / 2);
        Vector3   startingPosition = new Vector3(-unitSize / 2, 0f, -unitSize / 2);
        int       index            = 0;
        LayerType layerType        = (LayerType)Tools.IntParseFast(layer.Properties["Type"]);
        int       numTiles         = 0;

        for (int z = 0; z < tileCountZ; z++)
        {
            for (int x = 0; x < tileCountX; x++)
            {
                TmxLayerTile tile   = layer.Tiles[(tileCountZ - z - 1) * tileCountX + x];
                float        xPos   = startingPosition.x + x * unitSize;
                float        yPos   = startingPosition.z + z * unitSize;
                int          tileId = tile.Gid - 1;
                if (tileId == -1)
                {
                    continue;
                }

                numTiles += 1;
                Vector3 currentPosition = new Vector3(xPos, startingPosition.y, -yPos - 2f);
                if (prefab != null)
                {
                    GameObject block = Instantiate(prefab);
                    block.transform.SetParent(prefabContainer);
                    block.transform.position = new Vector3(tile.X, -tile.Y, 0f);
                }
                DrawVertex(index + 2, currentPosition);
                DrawVertex(index + 1, currentPosition, unitSize);
                DrawVertex(index, currentPosition, unitSize, unitSize);

                DrawVertex(index + 5, currentPosition);
                DrawVertex(index + 4, currentPosition, unitSize, unitSize);
                DrawVertex(index + 3, currentPosition, 0, unitSize);
                AssignUv(index, tiles[tileId], tileSize);

                index += 6;
            }
        }
    }
Exemplo n.º 25
0
        public static TmxTileset GetTileSet(this TmxMap map, TmxLayerTile tile)
        {
            TmxTileset tileSet = null;

            for (var i = 0; i < map.Tilesets.Count; i++)
            {
                var isHigher = tile.Gid >= map.Tilesets[i].FirstGid;

                if (isHigher && i >= map.Tilesets.Count - 1)
                {
                    tileSet = map.Tilesets[i];
                }
                else if (isHigher && tile.Gid < map.Tilesets[i + 1].FirstGid)
                {
                    tileSet = map.Tilesets[i];
                }
            }

            return(tileSet);
        }
Exemplo n.º 26
0
        public Rectangle GetSourceRectangleForTile(TmxTileset tileset, TmxLayerTile tile)
        {
            Rectangle source                = new Rectangle();
            int       tileWidth             = tileset.TileWidth;
            int       tileHeight            = tileset.TileHeight;
            int       tilesInHorizontalAxis = tileset.Image.Width.GetValueOrDefault() / tileWidth;

            // depending on the tile gid get the correct tile coordinates
            int tileIndex = tile.Gid - tileset.FirstGid;
            int xTilePos  = tileIndex / tilesInHorizontalAxis;
            int yTilePos  = tileIndex - xTilePos * tilesInHorizontalAxis;

            source.Width  = tileWidth;
            source.Height = tileHeight;

            source.X = yTilePos * tileWidth;
            source.Y = xTilePos * tileHeight;

            return(source);
        }
Exemplo n.º 27
0
        private TmxTileset GetTileset(TmxLayerTile tile, List <TmxTileset> tilesets)
        {
            if (tilesets.Count > 1)
            {
                for (int i = 0; i < tilesets.Count; i++)
                {
                    TmxTileset currTileset = tilesets[i];

                    if (tile.Gid >= currTileset.FirstGid && tile.Gid < currTileset.FirstGid + currTileset.TileCount && (tile.Gid - currTileset.FirstGid) < currTileset.TileCount)
                    {
                        return(currTileset);
                    }
                }
            }
            else
            {               //if the number of tilesets is only one, then that must be the tileset the tile belongs to.
                return(tilesets[0]);
            }

            return(null);
        }
Exemplo n.º 28
0
        void DrawTile(SpriteBatch spriteBatch, TmxLayerTile tile, bool indoor, bool skipVisible)
        {
            if (tile.Gid == 0)
            {
                return;          //Only not Empty tile
            }
            //Convert to world size
            float x = tile.X * _map.TileWidth;
            float y = tile.Y * _map.TileHeight;

            if (indoor && raycast(new Vector2(x, y), GameMain.mainPlayer.position, Layer.Ceiling, RaycastType.Continuous))
            {
                return;
            }
            if (skipVisible && isTileInView(new Vector2(x, y)))
            {
                return;
            }

            spriteBatch.Draw(_tileset, Tools.GetDrawRect((int)x, (int)y, tileSize), GetGidRect(tile.Gid), Color.White);
        }
Exemplo n.º 29
0
        private Direction tileDirection(TmxLayerTile tile)
        {
            var dir = Direction.Up;

            if (tile.DiagonalFlip && tile.HorizontalFlip && !tile.VerticalFlip)
            {
                dir = Direction.Right;
            }

            if (tile.DiagonalFlip && !tile.HorizontalFlip && tile.VerticalFlip)
            {
                dir = Direction.Left;
            }

            if (!tile.DiagonalFlip && tile.HorizontalFlip && tile.VerticalFlip)
            {
                dir = Direction.Down;
            }

            return(dir);
        }
Exemplo n.º 30
0
    void DrawMesh(int tileCountX, int tileCountZ, TmxLayer layer)
    {
        //Vector3 startingPosition = new Vector3(-tileCountX / 2 - unitSize / 2, 0f, -tileCountZ / 2 - unitSize / 2);
        Vector3 startingPosition = new Vector3(-unitSize / 2, 0f, -unitSize / 2);
        int     index            = 0;

        for (int z = 0; z < tileCountZ; z++)
        {
            for (int x = 0; x < tileCountX; x++)
            {
                //LayerType layerType = (LayerType)Tools.IntParseFast(layer.Properties["Type"]);
                TmxLayerTile tile   = layer.Tiles[(tileCountZ - z - 1) * tileCountX + x];
                int          tileId = tile.Gid - 1;
                if (tileId == -1)
                {
                    continue;
                }
                Vector3 currentPosition = new Vector3(startingPosition.x + x * unitSize, startingPosition.y, startingPosition.z + z * unitSize);
                DrawVertex(index + 2, currentPosition);
                DrawVertex(index + 1, currentPosition, unitSize);
                DrawVertex(index, currentPosition, unitSize, unitSize);

                DrawVertex(index + 5, currentPosition);
                DrawVertex(index + 4, currentPosition, unitSize, unitSize);
                DrawVertex(index + 3, currentPosition, 0, unitSize);
                AssignUv(index, tiles[tileId], tileSize);

                /*if (layerType == LayerType.Wall)
                 * {
                 *  SpawnWall(tileCountX, tileCountZ, tile.X, tile.Y);
                 * } else if (layerType == LayerType.Water)
                 * {
                 *  SpawnWater(tileCountX, tileCountZ, tile.X, tile.Y);
                 * }*/
                index += 6;
            }
        }
    }