Exemplo n.º 1
0
        private void AddShape(Cv_ShapeData newShape, string id, Vector2 anchor,
                              string material, bool isBullet,
                              Cv_CollisionCategories collisionCategories   = null,
                              Cv_CollisionCategories collidesWith          = null,
                              Dictionary <int, string> collisionDirections = null)
        {
            newShape.IsBullet = isBullet;
            newShape.Material = material;
            newShape.ShapeID  = id;
            newShape.Anchor   = anchor;

            if (collisionCategories == null)
            {
                var categories = new Cv_CollisionCategories();
                categories.AddAllCategories();
                newShape.Categories = categories;
            }
            else
            {
                newShape.Categories = collisionCategories;
            }

            if (collidesWith == null)
            {
                var categories = new Cv_CollisionCategories();
                categories.AddAllCategories();
                newShape.CollidesWith = categories;
            }
            else
            {
                newShape.CollidesWith = collidesWith;
            }

            if (collisionDirections == null)
            {
                var directions = new Dictionary <int, string>();

                var _collidesWith = newShape.CollidesWith.GetCategoriesArray();
                foreach (var c in _collidesWith)
                {
                    directions.Add(c, "All");
                }
                newShape.CollisionDirections = directions;
            }
            else
            {
                newShape.CollisionDirections = collisionDirections;
            }

            m_Shapes.Add(newShape);

            var newEvt = new Cv_Event_NewCollisionShape(Owner.ID, newShape, this);

            Cv_EventManager.Instance.QueueEvent(newEvt, true);
        }
Exemplo n.º 2
0
 public Cv_ShapeData(Cv_ShapeData toCopy)
 {
     ShapeID             = toCopy.ShapeID;
     Type                = toCopy.Type;
     Anchor              = toCopy.Anchor;
     Radius              = toCopy.Radius;
     Dimensions          = toCopy.Dimensions;
     Points              = toCopy.Points;
     IsBullet            = toCopy.IsBullet;
     Material            = toCopy.Material;
     Categories          = toCopy.Categories;
     CollidesWith        = toCopy.CollidesWith;
     CollisionDirections = toCopy.CollisionDirections;
 }
Exemplo n.º 3
0
        public void AddPolygonShape(string id, Vector2[] points, Vector2 anchor,
                                    string material, bool isBullet,
                                    Cv_CollisionCategories collisionCategories   = null,
                                    Cv_CollisionCategories collidesWith          = null,
                                    Dictionary <int, string> collisionDirections = null)
        {
            var shapeData = new Cv_ShapeData();

            shapeData.Points = points;
            shapeData.Type   = ShapeType.Polygon;

            AddShape(shapeData, id, anchor, material, isBullet,
                     collisionCategories, collidesWith, collisionDirections);
        }
Exemplo n.º 4
0
        public void AddTriggerShape(string id, Vector2 dimensions, Vector2 anchor,
                                    string material, bool isBullet,
                                    Cv_CollisionCategories collisionCategories   = null,
                                    Cv_CollisionCategories collidesWith          = null,
                                    Dictionary <int, string> collisionDirections = null)
        {
            var shapeData = new Cv_ShapeData();

            shapeData.Dimensions = dimensions;
            shapeData.Type       = ShapeType.Trigger;

            AddShape(shapeData, id, anchor, material, isBullet,
                     collisionCategories, collidesWith, collisionDirections);
        }
Exemplo n.º 5
0
        public void AddCircleShape(string id, float radius, Vector2 anchor,
                                   string material, bool isBullet,
                                   Cv_CollisionCategories collisionCategories   = null,
                                   Cv_CollisionCategories collidesWith          = null,
                                   Dictionary <int, string> collisionDirections = null)
        {
            var shapeData = new Cv_ShapeData();

            shapeData.Radius = radius;
            shapeData.Type   = ShapeType.Circle;

            AddShape(shapeData, id, anchor, material, isBullet,
                     collisionCategories, collidesWith, collisionDirections);
        }
Exemplo n.º 6
0
        public void SetShapeMaterial(string shapeId, string materialId)
        {
            for (var i = 0; i < m_Shapes.Count; i++)
            {
                if (m_Shapes[i].ShapeID == shapeId)
                {
                    var newShape = new Cv_ShapeData(m_Shapes[i]);
                    newShape.Material = materialId;

                    m_Shapes[i] = newShape;
                }
            }

            IsDirty = true;
        }
Exemplo n.º 7
0
 public Cv_Event_NewCollisionShape(Cv_EntityID entityId, Cv_ShapeData shape, object sender, float timeStamp = 0) : base(entityId, sender, timeStamp)
 {
     ShapeData = shape;
 }
Exemplo n.º 8
0
 public abstract Cv_CollisionShape VAddTrigger(Cv_Entity gameEntity, Cv_ShapeData data);
Exemplo n.º 9
0
 public abstract Cv_CollisionShape VAddPointShape(Cv_Entity gameEntity, Cv_ShapeData data);
Exemplo n.º 10
0
        public override bool VInitialize(XmlElement componentData)
        {
            XmlElement materialNode = (XmlElement)componentData.SelectSingleNode("Material");

            if (materialNode != null)
            {
                m_sMaterial = materialNode.Attributes["material"].Value;
            }

            XmlElement physicsNode = (XmlElement)componentData.SelectSingleNode("Physics");

            if (physicsNode != null)
            {
                m_bFixedRotation   = bool.Parse(physicsNode.Attributes["fixedRotation"].Value);
                m_fGravityScale    = float.Parse(physicsNode.Attributes["gravityScale"].Value, CultureInfo.InvariantCulture);
                MaxVelocity        = float.Parse(physicsNode.Attributes["maxVelocity"].Value, CultureInfo.InvariantCulture);
                MaxAngularVelocity = float.Parse(physicsNode.Attributes["maxAngVelocity"].Value, CultureInfo.InvariantCulture);
            }

            XmlElement bodyNode = (XmlElement)componentData.SelectSingleNode("Body");

            if (bodyNode != null)
            {
                m_fLinearDamping  = float.Parse(bodyNode.Attributes["linearDamping"].Value, CultureInfo.InvariantCulture);
                m_fAngularDamping = float.Parse(bodyNode.Attributes["angularDamping"].Value, CultureInfo.InvariantCulture);
                UseEntityRotation = bool.Parse(bodyNode.Attributes["followEntityRotation"].Value);
                var bodyType = bodyNode.Attributes["type"].Value.ToLowerInvariant();

                switch (bodyType)
                {
                case "static":
                    m_RigidBodyType = Cv_BodyType.Static;
                    break;

                case "kinematic":
                    m_RigidBodyType = Cv_BodyType.Kinematic;
                    break;

                case "dynamic":
                    m_RigidBodyType = Cv_BodyType.Dynamic;
                    break;

                default:
                    Cv_Debug.Error("Invalid body type. Unable to build component.");
                    return(false);
                }
            }


            var collisionShapesNode = componentData.SelectSingleNode("CollisionShapes");

            if (collisionShapesNode != null)
            {
                m_Shapes.Clear();

                foreach (XmlElement shape in collisionShapesNode.ChildNodes)
                {
                    var shapeData = new Cv_ShapeData();

                    var shapeType = shape.Name.ToLowerInvariant();

                    switch (shapeType)
                    {
                    case "box":
                        shapeData.Type = ShapeType.Box;
                        break;

                    case "circle":
                        shapeData.Type = ShapeType.Circle;
                        break;

                    case "polygon":
                        shapeData.Type = ShapeType.Polygon;
                        break;

                    case "trigger":
                        shapeData.Type = ShapeType.Trigger;
                        break;

                    default:
                        Cv_Debug.Error("Invalid shape type. Unable to build component.");
                        return(false);
                    }

                    shapeData.ShapeID  = shape.Attributes?["id"].Value;
                    shapeData.Material = shape.Attributes?["material"].Value;

                    int x, y;

                    x = int.Parse(shape.Attributes?["anchorX"].Value, CultureInfo.InvariantCulture);
                    y = int.Parse(shape.Attributes?["anchorY"].Value, CultureInfo.InvariantCulture);
                    shapeData.Anchor = new Vector2((float)x, (float)y);

                    shapeData.IsBullet = bool.Parse(shape.Attributes?["isBullet"].Value);

                    if (shapeData.Type == ShapeType.Circle)
                    {
                        shapeData.Radius = float.Parse(shape.Attributes?["radius"].Value, CultureInfo.InvariantCulture);
                    }
                    else if (shapeData.Type == ShapeType.Box)
                    {
                        x = int.Parse(shape.Attributes?["dimensionsX"].Value, CultureInfo.InvariantCulture);
                        y = int.Parse(shape.Attributes?["dimensionsY"].Value, CultureInfo.InvariantCulture);
                        shapeData.Dimensions = new Vector2((float)x, (float)y);
                    }
                    else if (shapeData.Type == ShapeType.Trigger)
                    {
                        x = int.Parse(shape.Attributes?["dimensions"].Value, CultureInfo.InvariantCulture);
                        shapeData.Dimensions = new Vector2((float)x, (float)x);
                    }
                    else
                    {
                        var points      = new List <Vector2>();
                        var shapePoints = shape.SelectNodes("Point");
                        foreach (XmlElement point in shapePoints)
                        {
                            x = int.Parse(point.Attributes?["x"].Value, CultureInfo.InvariantCulture);
                            y = int.Parse(point.Attributes?["y"].Value, CultureInfo.InvariantCulture);
                            points.Add(new Vector2((float)x, (float)y));
                        }

                        shapeData.Points = points.ToArray();
                    }

                    var shapeCollisionCategories = shape.SelectNodes("CollisionCategory");
                    shapeData.Categories = new Cv_CollisionCategories();
                    foreach (XmlElement category in shapeCollisionCategories)
                    {
                        var id = int.Parse(category.Attributes?["id"].Value, CultureInfo.InvariantCulture);

                        if (shapeData.Categories.HasCategory(id))
                        {
                            id = 0;

                            while (shapeData.Categories.HasCategory(id))
                            {
                                id++;

                                if (id >= 32)
                                {
                                    Cv_Debug.Error("Trying to add a collision category when all have been used already.");
                                }
                            }
                        }

                        shapeData.Categories.AddCategory(id);
                    }

                    var shapeCollidesWith = shape.SelectNodes("CollidesWith");
                    shapeData.CollidesWith        = new Cv_CollisionCategories();
                    shapeData.CollisionDirections = new Dictionary <int, string>();
                    foreach (XmlElement category in shapeCollidesWith)
                    {
                        var id = int.Parse(category.Attributes?["id"].Value, CultureInfo.InvariantCulture);

                        if (shapeData.CollidesWith.HasCategory(id))
                        {
                            id = 0;

                            while (shapeData.CollidesWith.HasCategory(id))
                            {
                                id++;

                                if (id >= 32)
                                {
                                    Cv_Debug.Error("Trying to add a collision category when all have been used already.");
                                }
                            }
                        }

                        shapeData.CollidesWith.AddCategory(id);
                        shapeData.CollisionDirections.Add(id, category.Attributes["directions"].Value);
                    }

                    m_Shapes.Add(shapeData);
                }
            }

            IsDirty = true;
            return(true);
        }