Beispiel #1
0
        public void SetupTiles()
        {
            if (Core.Scene == null || enabled)
            {
                return;
            }
            //enabled = true;
            TmxMap map    = Core.Scene.Content.LoadTiledMap("Assets/map.tmx");
            Entity entity = Core.Scene.CreateEntity("tiled-map");

            TiledMapRenderer tmr = entity.AddComponent(new TiledMapRenderer(map, "Collision", true));
            TmxLayer         CustomCollisionLayer = (TmxLayer)map.GetLayer("CustomCollision");

            foreach (TmxLayerTile tile in CustomCollisionLayer.Tiles)
            {
                if (tile != null && tile.TilesetTile != null)
                {
                    TmxList <TmxObjectGroup> objgl = tile.TilesetTile.ObjectGroups;

                    if (objgl != null && objgl.Count > 0)
                    {
                        TmxObjectGroup objg = objgl[0];
                        if (objg.Objects != null && objg.Objects.Count > 0)
                        {
                            TmxObject     obj  = objg.Objects[0];
                            TmxObjectType type = obj.ObjectType;

                            if (type == TmxObjectType.Ellipse)
                            {
                                //Draw Ellipse as collision
                                Core.Scene.CreateEntity(obj.Name, new Vector2(tile.Position.X * tile.Tileset.TileWidth + obj.Width / 2, tile.Position.Y * tile.Tileset.TileHeight + obj.Height / 2))
                                .AddComponent(new FSCollisionEllipse(obj.Width / 2, obj.Height / 2))
                                .AddComponent(new CircleCollider((obj.Width + obj.Height) / 4));     // have to get an average of sides, hence / 4
                            }
                            else if (type == TmxObjectType.Polygon)
                            {
                                Vector2[] points = obj.Points;

                                Core.Scene.CreateEntity(obj.Name, new Vector2(tile.Tileset.TileWidth * tile.Position.X + obj.X, tile.Tileset.TileHeight * tile.Position.Y + obj.Y))
                                .AddComponent(new FSCollisionPolygon(points))
                                .AddComponent(new PolygonCollider(points));
                            }
                            //basic is rectangle
                            else if (type == TmxObjectType.Basic)
                            {
                                Core.Scene.CreateEntity(obj.Name, new Vector2(tile.Position.X * tile.Tileset.TileWidth + obj.Width / 2, tile.Position.Y * tile.Tileset.TileHeight + obj.Height / 2))
                                .AddComponent(new FSCollisionBox(obj.Width, obj.Height))
                                .AddComponent(new BoxCollider(obj.Width, obj.Height));
                            }
                        }
                    }
                }
            }
            tmr.SetLayersToRender(new string[] { });
        }
Beispiel #2
0
        public TmxObject(TmxMap map, XElement xObject)
        {
            Id       = (int?)xObject.Attribute("id") ?? 0;
            Name     = (string)xObject.Attribute("name") ?? string.Empty;
            X        = (float)xObject.Attribute("x");
            Y        = (float)xObject.Attribute("y");
            Width    = (float?)xObject.Attribute("width") ?? 0.0f;
            Height   = (float?)xObject.Attribute("height") ?? 0.0f;
            Type     = (string)xObject.Attribute("type") ?? string.Empty;
            Visible  = (bool?)xObject.Attribute("visible") ?? true;
            Rotation = (float?)xObject.Attribute("rotation") ?? 0.0f;

            // Assess object type and assign appropriate content
            var xGid      = xObject.Attribute("gid");
            var xEllipse  = xObject.Element("ellipse");
            var xPolygon  = xObject.Element("polygon");
            var xPolyline = xObject.Element("polyline");
            var xText     = xObject.Element("text");
            var xPoint    = xObject.Element("point");

            if (xGid != null)
            {
                Tile       = new TmxLayerTile(map, (uint)xGid, Convert.ToInt32(Math.Round(X)), Convert.ToInt32(Math.Round(X)));
                ObjectType = TmxObjectType.Tile;
            }
            else if (xEllipse != null)
            {
                ObjectType = TmxObjectType.Ellipse;
            }
            else if (xPolygon != null)
            {
                Points     = ParsePoints(xPolygon);
                ObjectType = TmxObjectType.Polygon;
            }
            else if (xPolyline != null)
            {
                Points     = ParsePoints(xPolyline);
                ObjectType = TmxObjectType.Polyline;
            }
            else if (xText != null)
            {
                Text       = new TmxText(xText);
                ObjectType = TmxObjectType.Text;
            }
            else if (xPoint != null)
            {
                ObjectType = TmxObjectType.Point;
            }
            else
            {
                ObjectType = TmxObjectType.Basic;
            }

            Properties = PropertyDict.ParsePropertyDict(xObject.Element("properties"));
        }
Beispiel #3
0
        private static void DrawTileLayerColliders(Graphics g, TmxMap tmxMap, TmxLayer tileLayer, float scale)
        {
            // Bail if this layer is ignoring collision
            if (tileLayer.Ignore == TmxLayerNode.IgnoreSettings.Collision)
            {
                return;
            }

            foreach (TmxLayer collisionLayer in tileLayer.CollisionLayers)
            {
                TmxObjectType type = tmxMap.ObjectTypes.GetValueOrDefault(collisionLayer.Name);

                Color lineColor = type.Color;
                Color polyColor = Color.FromArgb(128, lineColor);
                DrawCollisionLayer(g, collisionLayer, polyColor, lineColor, scale);
            }
        }
Beispiel #4
0
        private static void DrawTileLayerColliders(SKCanvas canvas, TmxMap tmxMap, TmxLayer tileLayer)
        {
            // Bail if this layer is ignoring collision
            if (tileLayer.Ignore == TmxLayerNode.IgnoreSettings.Collision)
            {
                return;
            }

            foreach (TmxLayer collisionLayer in tileLayer.CollisionLayers)
            {
                TmxObjectType type = tmxMap.ObjectTypes.GetValueOrDefault(collisionLayer.Name);

                SKColor lineColor = new SKColor(type.Color.R, type.Color.B, type.Color.B);
                SKColor polyColor = new SKColor(type.Color.R, type.Color.B, type.Color.B, 128);
                DrawCollisionLayer(canvas, collisionLayer, polyColor, lineColor);
            }
        }
Beispiel #5
0
        private void SetupCustomCollision()
        {
            TmxLayer CustomCollisionLayer = (TmxLayer)TileMap.GetLayer("CustomCollision");

            foreach (TmxLayerTile tile in CustomCollisionLayer.Tiles)
            {
                if (tile != null && tile.TilesetTile != null)
                {
                    TmxList <TmxObjectGroup> objgl = tile.TilesetTile.ObjectGroups;

                    if (objgl != null && objgl.Count > 0)
                    {
                        TmxObjectGroup objg = objgl[0];
                        if (objg.Objects != null && objg.Objects.Count > 0)
                        {
                            TmxObject     obj  = objg.Objects[0];
                            TmxObjectType type = obj.ObjectType;

                            if (type == TmxObjectType.Ellipse)
                            {
                                //Draw Ellipse as collision
                                Entity.Scene.CreateEntity(obj.Name, new Vector2(Entity.Position.X + tile.Position.X * tile.Tileset.TileWidth + obj.Width / 2, Entity.Position.Y + tile.Position.Y * tile.Tileset.TileHeight + obj.Height / 2))
                                .AddComponent(new FSCollisionEllipse(obj.Width / 2, obj.Height / 2))
                                .AddComponent(new CircleCollider((obj.Width + obj.Height) / 4));     // have to get an average of sides, hence / 4
                            }
                            else if (type == TmxObjectType.Polygon)
                            {
                                Vector2[] points = obj.Points;

                                Entity.Scene.CreateEntity(obj.Name, new Vector2(Entity.Position.X + tile.Tileset.TileWidth * tile.Position.X + obj.X, Entity.Position.Y + tile.Tileset.TileHeight * tile.Position.Y + obj.Y))
                                .AddComponent(new FSCollisionPolygon(points))
                                .AddComponent(new PolygonCollider(points));
                            }
                            //basic is rectangle
                            else if (type == TmxObjectType.Basic)
                            {
                                Entity.Scene.CreateEntity(obj.Name, new Vector2(Entity.Position.X + tile.Position.X * tile.Tileset.TileWidth + obj.Width / 2, Entity.Position.Y + tile.Position.Y * tile.Tileset.TileHeight + obj.Height / 2))
                                .AddComponent(new FSCollisionBox(obj.Width, obj.Height))
                                .AddComponent(new BoxCollider(obj.Width, obj.Height));
                                Entity.AddComponent(new FSCollisionBox(obj.Width, obj.Height)).AddComponent(new BoxCollider(obj.Width, obj.Height));
                            }
                        }
                    }
                }
            }
        }
Beispiel #6
0
        private static void DrawColliders(Graphics g, TmxMap tmxMap, float scale)
        {
            for (int l = 0; l < tmxMap.Layers.Count; ++l)
            {
                TmxLayer layer = tmxMap.Layers[l];

                if (layer.Visible == true && layer.Ignore != TmxLayer.IgnoreSettings.Collision)
                {
                    foreach (TmxLayer collisionLayer in layer.CollisionLayers)
                    {
                        TmxObjectType type = tmxMap.ObjectTypes.GetValueOrDefault(collisionLayer.Name);

                        Color lineColor = type.Color;
                        Color polyColor = Color.FromArgb(128, lineColor);
                        DrawLayerColliders(g, collisionLayer, polyColor, lineColor, scale);
                    }
                }
            }
        }
Beispiel #7
0
        private static void DrawObjectCollider(SKCanvas canvas, TmxMap tmxMap, TmxObject tmxObject, SKColor color)
        {
            using (new SKAutoCanvasRestore(canvas))
                using (SKPaint paint = new SKPaint())
                {
                    PointF xfPosition = TmxMath.ObjectPointFToMapSpace(tmxMap, tmxObject.Position);
                    canvas.Translate(xfPosition.ToSKPoint());
                    canvas.RotateDegrees(tmxObject.Rotation);

                    if (tmxObject.GetType() == typeof(TmxObjectPolygon))
                    {
                        DrawPolygon(canvas, color, tmxMap, tmxObject as TmxObjectPolygon);
                    }
                    else if (tmxObject.GetType() == typeof(TmxObjectRectangle))
                    {
                        if (tmxMap.Orientation == TmxMap.MapOrientation.Isometric)
                        {
                            TmxObjectPolygon tmxIsometricRectangle = TmxObjectPolygon.FromRectangle(tmxMap, tmxObject as TmxObjectRectangle);
                            DrawPolygon(canvas, color, tmxMap, tmxIsometricRectangle);
                        }
                        else
                        {
                            // Rectangles are polygons
                            DrawPolygon(canvas, color, tmxMap, tmxObject as TmxObjectPolygon);
                        }
                    }
                    else if (tmxObject.GetType() == typeof(TmxObjectEllipse))
                    {
                        DrawEllipse(canvas, color, tmxMap, tmxObject as TmxObjectEllipse);
                    }
                    else if (tmxObject.GetType() == typeof(TmxObjectPolyline))
                    {
                        DrawPolyline(canvas, color, tmxMap, tmxObject as TmxObjectPolyline);
                    }
                    else if (tmxObject.GetType() == typeof(TmxObjectTile))
                    {
                        using (new SKAutoCanvasRestore(canvas))
                        {
                            TmxObjectTile tmxObjectTile = tmxObject as TmxObjectTile;
                            PrepareTransformForTileObject(canvas, tmxMap, tmxObjectTile);

                            // Draw the collisions
                            // Temporarily set orienation to Orthogonal for tile colliders
                            TmxMap.MapOrientation restoreOrientation = tmxMap.Orientation;
                            tmxMap.Orientation = TmxMap.MapOrientation.Orthogonal;
                            {
                                // Make up for the fact that the bottom-left corner is the origin
                                canvas.Translate(0, -tmxObjectTile.Tile.TileSize.Height);
                                foreach (var obj in tmxObjectTile.Tile.ObjectGroup.Objects)
                                {
                                    TmxObjectType type = tmxMap.ObjectTypes.GetValueOrDefault(obj.Type);
                                    DrawObjectCollider(canvas, tmxMap, obj, type.Color.ToSKColor());
                                }
                            }
                            tmxMap.Orientation = restoreOrientation;
                        }
                    }
                    else
                    {
                        Logger.WriteWarning("Unhandled object: {0}", tmxObject.GetNonEmptyName());
                    }
                }
        }