Beispiel #1
0
        private static void AddRectangleCloneAtXY(MapLayer layer, float tileDimension, AxisAlignedRectangle rectangle, List <uint> tiles, long tilesetTileGid, int x, int y,
                                                  TileCollisions.TileShapeCollection collectionForThisName)
        {
            var i = y * layer.width + x;

            var strippedId = tiles[i] & 0x0fffffff;

            if (strippedId == tilesetTileGid)
            {
                float xIndex = i % layer.width;
                // intentional int division
                float yIndex = i / layer.width;

                var cloned = rectangle.Clone();

                // Offset by .5 since polygons are positioned by their center, tiles by top left
                //cloned.X = (xIndex + .5f) * tileDimension;
                //cloned.Y = -(yIndex + .5f) * tileDimension;
                // Actually use the X and Y to get the top left, then use the actual rectangle's X and Y values so that
                // its offset applies:
                cloned.X = rectangle.X + (xIndex) * tileDimension;
                cloned.Y = rectangle.Y - (yIndex) * tileDimension;

                collectionForThisName.Rectangles.Add(cloned);
            }
        }
Beispiel #2
0
        private static Polygon AddPolygonCloneAtXY(MapLayer layer, float tileDimension, Polygon polygon, List <uint> tiles, long tilesetTileGid, int index,
                                                   TileCollisions.TileShapeCollection collectionForThisName)
        {
            int xIndex = index % layer.width;
            // intentional int division
            int yIndex = index / layer.width;

            var cloned = polygon.Clone();

            cloned.X = (xIndex + .5f) * tileDimension;
            cloned.Y = -(yIndex + .5f) * tileDimension;

            collectionForThisName.Polygons.Add(cloned);
            return(cloned);
        }
Beispiel #3
0
        private static TileCollisions.TileShapeCollection GetOrAddTileShapeCollection(string name, Dictionary <string, TileCollisions.TileShapeCollection> collisionDictionary)
        {
            if (collisionDictionary.ContainsKey(name))
            {
                return(collisionDictionary[name]);
            }
            else
            {
                var collection = new TileCollisions.TileShapeCollection();
                collection.Name = name;

                collisionDictionary[name] = collection;

                return(collection);
            }
        }
Beispiel #4
0
        private static void AddPolygonCloneAtXY(MapLayer layer, float tileDimension, TileCollisions.TileShapeCollection toReturn, Polygon polygon, List <uint> tiles, long tilesetTileGid, int x, int y)
        {
            var i = y * layer.width + x;

            if (tiles[i] == tilesetTileGid)
            {
                int xIndex = i % layer.width;
                // intentional int division
                int yIndex = i / layer.width;

                var cloned = polygon.Clone();

                cloned.X = (xIndex + .5f) * tileDimension;
                cloned.Y = -(yIndex + .5f) * tileDimension;

                toReturn.Polygons.Add(cloned);
            }
        }
Beispiel #5
0
        private static void AddTileShapeCollections(LayeredTileMap layeredTileMap, TiledMapSave tms)
        {
            var allTilesets = tms.Tilesets;

            Dictionary <TMXGlueLib.Tileset, bool> hasShapesDictionary = new Dictionary <TMXGlueLib.Tileset, bool>();

            for (int i = 0; i < tms.Layers.Count; i++)
            {
                var layer = tms.Layers[i];
                // Currently we only support 1 tileset per layer, so we'll find the tileset for this layer
                var firstNonZero = layer.data[0].tiles.FirstOrDefault(item => item != 0);

                if (firstNonZero != 0)
                {
                    var tileset = tms.GetTilesetForGid(firstNonZero);

                    if (tileset != null)
                    {
                        bool hasShapes;
                        if (hasShapesDictionary.ContainsKey(tileset))
                        {
                            hasShapes = hasShapesDictionary[tileset];
                        }
                        else
                        {
                            // We don't know if this tileset has shapes yet, so let's figure it out:
                            hasShapes = tileset.Tiles.Any(item => item.Objects?.@object?.Length > 0);

                            hasShapesDictionary[tileset] = hasShapes;
                        }

                        if (hasShapes)
                        {
                            TileCollisions.TileShapeCollection tileShapeCollection = GetTileShapeCollectionForLayer(layer, tileset, tms.tilewidth, i);

                            layeredTileMap.Collisions.Add(tileShapeCollection);
                        }
                    }
                }
            }
        }
Beispiel #6
0
        private static void AddRectangleCloneAtXY(MapLayer layer, float tileDimension, TileCollisions.TileShapeCollection toReturn, AxisAlignedRectangle rectangle, List <uint> tiles, long tilesetTileGid, int x, int y)
        {
            var i = y * layer.width + x;

            if (tiles[i] == tilesetTileGid)
            {
                float xIndex = i % layer.width;
                // intentional int division
                float yIndex = i / layer.width;

                var cloned = rectangle.Clone();

                // Offset by .5 since polygons are positioned by their center, tiles by top left
                cloned.X = (xIndex + .5f) * tileDimension;
                cloned.Y = -(yIndex + .5f) * tileDimension;

                toReturn.Rectangles.Add(cloned);
            }
        }
Beispiel #7
0
        private static TileCollisions.TileShapeCollection GetTileShapeCollectionForLayer(TMXGlueLib.MapLayer layer, TMXGlueLib.Tileset tileset, float tileDimension, float z)
        {
            TileCollisions.TileShapeCollection toReturn = new TileCollisions.TileShapeCollection();

            toReturn.Name = layer.Name;

            Math.Geometry.AxisAlignedRectangle rectangle = null;
            Math.Geometry.Polygon polygon = null;
            Circle circle;

            var tiles = layer.data[0].tiles;


            bool sortOnY = layer.height > layer.width;


            foreach (var tilesetTile in tileset.Tiles.Where(item => item.Objects?.@object?.Length > 0))
            {
                var tilesetTileGid = tilesetTile.id + tileset.Firstgid;
                foreach (var tilesetObject in tilesetTile.Objects.@object)
                {
                    TiledMapToShapeCollectionConverter.ConvertTiledObjectToFrbShape(tilesetObject, out polygon, out rectangle, out circle);
                    if (rectangle != null)
                    {
                        rectangle.Z = z;
                        if (sortOnY)
                        {
                            for (int y = 0; y < layer.height; y++)
                            {
                                for (int x = 0; x < layer.width; x++)
                                {
                                    AddRectangleCloneAtXY(layer, tileDimension, toReturn, rectangle, tiles, tilesetTileGid, x, y);
                                }
                            }
                        }
                        else
                        {
                            for (int x = 0; x < layer.width; x++)
                            {
                                for (int y = 0; y < layer.height; y++)
                                {
                                    AddRectangleCloneAtXY(layer, tileDimension, toReturn, rectangle, tiles, tilesetTileGid, x, y);
                                }
                            }
                        }
                    }
                    else if (polygon != null)
                    {
                        // For tile polygons we want them to be centered on the tile.
                        // To do this, we shift all points by its position:
                        for (int i = 0; i < polygon.Points.Count; i++)
                        {
                            var point = polygon.Points[i];
                            point.X += polygon.Position.X - tileDimension / 2.0f;
                            point.Y += polygon.Position.Y + tileDimension / 2.0f;

                            polygon.SetPoint(i, point);
                        }

                        polygon.Z = z;

                        if (sortOnY)
                        {
                            for (int y = 0; y < layer.height; y++)
                            {
                                for (int x = 0; x < layer.width; x++)
                                {
                                    AddPolygonCloneAtXY(layer, tileDimension, toReturn, polygon, tiles, tilesetTileGid, x, y);
                                }
                            }
                        }
                        else
                        {
                            for (int x = 0; x < layer.width; x++)
                            {
                                for (int y = 0; y < layer.height; y++)
                                {
                                    AddPolygonCloneAtXY(layer, tileDimension, toReturn, polygon, tiles, tilesetTileGid, x, y);
                                }
                            }
                        }
                    }
                    else if (circle != null)
                    {
                        throw new NotImplementedException("Need to handle circles...");
                    }
                }
            }

            // The values are inserted sorted above for speed, now we set the XAxis - this will also sort but will be fast:
            if (sortOnY)
            {
                toReturn.SortAxis = Math.Axis.Y;
            }
            else
            {
                toReturn.SortAxis = Math.Axis.X;
            }

            toReturn.RefreshAllRepositionDirections();

            return(toReturn);
        }
Beispiel #8
0
        private static void AddRectangleCloneAtXY(MapLayer layer, float tileDimension, AxisAlignedRectangle rectangle, List <uint> tiles, long tilesetTileGid, int x, int y,
                                                  Dictionary <string, TileCollisions.TileShapeCollection> dictionary, ref TileCollisions.TileShapeCollection collectionForThisName)
        {
            var i = y * layer.width + x;

            var stripedId = tiles[i] & 0x0fffffff;

            if (stripedId == tilesetTileGid)
            {
                if (collectionForThisName == null)
                {
                    collectionForThisName = GetOrAddTileShapeCollection(rectangle.Name ?? layer.Name, dictionary);
                }

                float xIndex = i % layer.width;
                // intentional int division
                float yIndex = i / layer.width;

                var cloned = rectangle.Clone();

                // Offset by .5 since polygons are positioned by their center, tiles by top left
                cloned.X = (xIndex + .5f) * tileDimension;
                cloned.Y = -(yIndex + .5f) * tileDimension;

                collectionForThisName.Rectangles.Add(cloned);
            }
        }
Beispiel #9
0
        private static void AddPolygonCloneAtXY(MapLayer layer, float tileDimension, Polygon polygon, List <uint> tiles, long tilesetTileGid, int x, int y,
                                                Dictionary <string, TileCollisions.TileShapeCollection> dictionary, ref TileCollisions.TileShapeCollection collectionForThisName)
        {
            var i = y * layer.width + x;

            if (tiles[i] == tilesetTileGid)
            {
                if (collectionForThisName == null)
                {
                    collectionForThisName = GetOrAddTileShapeCollection(polygon.Name ?? layer.Name, dictionary);
                }
                int xIndex = i % layer.width;
                // intentional int division
                int yIndex = i / layer.width;

                var cloned = polygon.Clone();

                cloned.X = (xIndex + .5f) * tileDimension;
                cloned.Y = -(yIndex + .5f) * tileDimension;

                collectionForThisName.Polygons.Add(cloned);
            }
        }
Beispiel #10
0
        private static void AddTileShapeCollectionForLayer(TMXGlueLib.MapLayer layer, Dictionary <string, TileCollisions.TileShapeCollection> collisionDictionary, TMXGlueLib.Tileset tileset, float tileDimension, float z)
        {
            Math.Geometry.AxisAlignedRectangle rectangle = null;
            Math.Geometry.Polygon polygon = null;
            Circle circle;

            var tiles = layer.data[0].tiles;


            bool sortOnY = layer.height > layer.width;


            foreach (var tilesetTile in tileset.Tiles.Where(item => item.Objects?.@object?.Length > 0))
            {
                var tilesetTileGid = tilesetTile.id + tileset.Firstgid;
                foreach (var tilesetObject in tilesetTile.Objects.@object)
                {
                    TiledMapToShapeCollectionConverter.ConvertTiledObjectToFrbShape(tilesetObject, out polygon, out rectangle, out circle);
                    if (rectangle != null)
                    {
                        TileCollisions.TileShapeCollection collection = null;
                        rectangle.Z = z;
                        if (sortOnY)
                        {
                            for (int y = 0; y < layer.height; y++)
                            {
                                for (int x = 0; x < layer.width; x++)
                                {
                                    AddRectangleCloneAtXY(layer, tileDimension, rectangle, tiles, tilesetTileGid, x, y, collisionDictionary, ref collection);
                                }
                            }
                        }
                        else
                        {
                            for (int x = 0; x < layer.width; x++)
                            {
                                for (int y = 0; y < layer.height; y++)
                                {
                                    AddRectangleCloneAtXY(layer, tileDimension, rectangle, tiles, tilesetTileGid, x, y, collisionDictionary, ref collection);
                                }
                            }
                        }
                    }
                    else if (polygon != null)
                    {
                        TileCollisions.TileShapeCollection collection = null;

                        // For tile polygons we want them to be centered on the tile.
                        // To do this, we shift all points by its position:
                        for (int i = 0; i < polygon.Points.Count; i++)
                        {
                            var point = polygon.Points[i];
                            point.X += polygon.Position.X - tileDimension / 2.0f;
                            point.Y += polygon.Position.Y + tileDimension / 2.0f;

                            polygon.SetPoint(i, point);
                        }

                        polygon.Z = z;

                        if (sortOnY)
                        {
                            for (int y = 0; y < layer.height; y++)
                            {
                                for (int x = 0; x < layer.width; x++)
                                {
                                    AddPolygonCloneAtXY(layer, tileDimension, polygon, tiles, tilesetTileGid, x, y, collisionDictionary, ref collection);
                                }
                            }
                        }
                        else
                        {
                            for (int x = 0; x < layer.width; x++)
                            {
                                for (int y = 0; y < layer.height; y++)
                                {
                                    AddPolygonCloneAtXY(layer, tileDimension, polygon, tiles, tilesetTileGid, x, y, collisionDictionary, ref collection);
                                }
                            }
                        }
                    }
                    else if (circle != null)
                    {
                        throw new NotImplementedException("Need to handle circles...");
                    }
                }
            }
        }