Ejemplo n.º 1
0
 public WorldNode(string name, WorldData data)
     : base(name, 0)
 {
     Data = data;
 }
Ejemplo n.º 2
0
        private void Main_Load(object sender, EventArgs e)
        {
            simulationThread = new Thread(SimulationLoop);
            simulationThread.Start();
            {
                var d = new WorldData();
                d.Gravity = new Vec2(0, 10);
                var node = new WorldNode("World", d);
                treeView1.Nodes.Add(node);

                node.Expand();
            }

            imageList1.Images.Add(Properties.Resources.world);
            imageList1.Images.Add(Properties.Resources.circle);
            imageList1.Images.Add(Properties.Resources.polygon);
            imageList1.Images.Add(Properties.Resources.body);

            treeView1.ImageList = imageList1;
        }
Ejemplo n.º 3
0
        public WorldData Deserialize(Stream stream)
        {
            XMLFragmentElement root = XMLFragmentParser.LoadFromStream(stream);

            if (root.Name.ToLower() != "world")
                throw new Exception();

            WorldData data = new WorldData();

            if (root.Attributes.Count == 0)
                throw new Exception("No version");
            else if (int.Parse(root.Attributes[0].Value) != WorldXmlSerializer.XmlVersion)
                throw new Exception("Wrong version XML file");

            data.Version = int.Parse(root.Attributes[0].Value);

            foreach (var main in root.Elements)
            {
                switch (main.Name.ToLower())
                {
                case "gravity":
                    {
                        data.Gravity = ReadVector(main);
                    }
                    break;

                case "shapes":
                    {
                        foreach (var n in main.Elements)
                        {
                            if (n.Name.ToLower() != "shape")
                                throw new Exception();

                            ShapeType type = (ShapeType)Enum.Parse(typeof(ShapeType), n.Attributes[0].Value, true);
                            string name = "";

                            switch (type)
                            {
                            case ShapeType.Circle:
                                {
                                    CircleShape shape = new CircleShape();

                                    foreach (var sn in n.Elements)
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "name":
                                            name = sn.Value;
                                            break;
                                        case "radius":
                                            shape.Radius = float.Parse(sn.Value);
                                            break;
                                        case "position":
                                            shape.Position = ReadVector(sn);
                                            break;
                                        default:
                                            throw new Exception();
                                        }
                                    }

                                    _shapes.Add(new ShapeSerialized(shape, name));
                                }
                                break;
                            case ShapeType.Polygon:
                                {
                                    PolygonShape shape = new PolygonShape();

                                    foreach (var sn in n.Elements)
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "name":
                                            name = sn.Value;
                                            break;
                                        case "vertices":
                                            {
                                                List<Vec2> verts = new List<Vec2>();

                                                foreach (var vert in sn.Elements)
                                                    verts.Add(ReadVector(vert));

                                                shape.Vertices = verts.ToArray();
                                            }
                                            break;
                                        case "centroid":
                                            shape.Centroid = ReadVector(sn);
                                            break;
                                        }
                                    }

                                    _shapes.Add(new ShapeSerialized(shape, name));
                                }
                                break;
                            }
                        }
                    }
                    break;
                case "fixtures":
                    {
                        foreach (var n in main.Elements)
                        {
                            FixtureDef fixture = new FixtureDef();

                            if (n.Name.ToLower() != "fixture")
                                throw new Exception();

                            string name = "";
                            int id = 0;

                            foreach (var sn in n.Elements)
                            {
                                switch (sn.Name.ToLower())
                                {
                                case "name":
                                    name = sn.Value;
                                    break;
                                case "shape":
                                    id = int.Parse(sn.Value);
                                    break;
                                case "density":
                                    fixture.Density = float.Parse(sn.Value);
                                    break;
                                case "filterdata":
                                    fixture.Filter = (FilterData)ReadSimpleType(sn, typeof(FilterData), true);
                                    break;
                                case "friction":
                                    fixture.Friction = float.Parse(sn.Value);
                                    break;
                                case "issensor":
                                    fixture.IsSensor = bool.Parse(sn.Value);
                                    break;
                                case "restitution":
                                    fixture.Restitution = float.Parse(sn.Value);
                                    break;
                                case "userdata":
                                    fixture.UserData = ReadSimpleType(sn, null, false);
                                    break;
                                }
                            }

                            fixture.Shape = _shapes[id].Shape;

                            _fixtures.Add(new FixtureDefSerialized(fixture, id, name));
                        }
                    }
                    break;
                case "bodies":
                    {
                        foreach (var n in main.Elements)
                        {
                            BodyDef body = new BodyDef();

                            if (n.Name.ToLower() != "body")
                                throw new Exception();

                            body.BodyType = (BodyType)Enum.Parse(typeof(BodyType), n.Attributes[0].Value, true);
                            List<int> fixtures = new List<int>();
                            string name = "";

                            foreach (var sn in n.Elements)
                            {
                                switch (sn.Name.ToLower())
                                {
                                case "name":
                                    name = sn.Value;
                                    break;
                                case "active":
                                    body.Active = bool.Parse(sn.Value);
                                    break;
                                case "allowsleep":
                                    body.AllowSleep = bool.Parse(sn.Value);
                                    break;
                                case "angle":
                                    body.Angle = float.Parse(sn.Value);
                                    break;
                                case "angulardamping":
                                    body.AngularDamping = float.Parse(sn.Value);
                                    break;
                                case "angularvelocity":
                                    body.AngularVelocity = float.Parse(sn.Value);
                                    break;
                                case "awake":
                                    body.Awake = bool.Parse(sn.Value);
                                    break;
                                case "bullet":
                                    body.Bullet = bool.Parse(sn.Value);
                                    break;
                                case "fixedrotation":
                                    body.FixedRotation = bool.Parse(sn.Value);
                                    break;
                                case "inertiascale":
                                    body.InertiaScale = float.Parse(sn.Value);
                                    break;
                                case "lineardamping":
                                    body.LinearDamping = float.Parse(sn.Value);
                                    break;
                                case "linearvelocity":
                                    body.LinearVelocity = ReadVector(sn);
                                    break;
                                case "position":
                                    body.Position = ReadVector(sn);
                                    break;
                                case "userdata":
                                    body.UserData = ReadSimpleType(sn, null, false);
                                    break;
                                case "fixtures":
                                    {
                                        foreach (var v in sn.Elements)
                                            fixtures.Add(int.Parse(v.Value));
                                        break;
                                    }
                                }
                            }

                            _bodies.Add(new BodyDefSerialized(null, body, fixtures, name));
                        }
                    }
                    break;
                case "joints":
                    {
                        foreach (var n in main.Elements)
                        {
                            JointDef mainDef = null;

                            if (n.Name.ToLower() != "joint")
                                throw new Exception();

                            JointType type = (JointType)Enum.Parse(typeof(JointType), n.Attributes[0].Value, true);

                            int bodyA = -1, bodyB = -1;
                            bool collideConnected = false;
                            object userData = null;
                            string name = "";

                            switch (type)
                            {
                            case JointType.Distance:
                                mainDef = new DistanceJointDef();
                                break;
                            case JointType.Friction:
                                mainDef = new FrictionJointDef();
                                break;
                            case JointType.Line:
                                mainDef = new LineJointDef();
                                break;
                            case JointType.Prismatic:
                                mainDef = new PrismaticJointDef();
                                break;
                            case JointType.Pulley:
                                mainDef = new PulleyJointDef();
                                break;
                            case JointType.Revolute:
                                mainDef = new RevoluteJointDef();
                                break;
                            case JointType.Weld:
                                mainDef = new WeldJointDef();
                                break;
                            default:
                                throw new Exception("Invalid or unsupported joint");
                            }

                            foreach (var sn in n.Elements)
                            {
                                // check for specific nodes
                                switch (type)
                                {
                                case JointType.Distance:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "dampingratio":
                                            ((DistanceJointDef)mainDef).DampingRatio = float.Parse(sn.Value);
                                            break;
                                        case "frequencyhz":
                                            ((DistanceJointDef)mainDef).FrequencyHz = float.Parse(sn.Value);
                                            break;
                                        case "length":
                                            ((DistanceJointDef)mainDef).Length = float.Parse(sn.Value);
                                            break;
                                        case "localanchora":
                                            ((DistanceJointDef)mainDef).LocalAnchorA = ReadVector(sn);
                                            break;
                                        case "localanchorb":
                                            ((DistanceJointDef)mainDef).LocalAnchorB = ReadVector(sn);
                                            break;
                                        }
                                    }
                                    break;
                                case JointType.Friction:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "localanchora":
                                            ((FrictionJointDef)mainDef).LocalAnchorA = ReadVector(sn);
                                            break;
                                        case "localanchorb":
                                            ((FrictionJointDef)mainDef).LocalAnchorB = ReadVector(sn);
                                            break;
                                        case "maxforce":
                                            ((FrictionJointDef)mainDef).MaxForce = float.Parse(sn.Value);
                                            break;
                                        case "maxtorque":
                                            ((FrictionJointDef)mainDef).MaxTorque = float.Parse(sn.Value);
                                            break;
                                        }
                                    }
                                    break;
                                case JointType.Line:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "enablelimit":
                                            ((LineJointDef)mainDef).EnableLimit = bool.Parse(sn.Value);
                                            break;
                                        case "enablemotor":
                                            ((LineJointDef)mainDef).EnableMotor = bool.Parse(sn.Value);
                                            break;
                                        case "localanchora":
                                            ((LineJointDef)mainDef).LocalAnchorA = ReadVector(sn);
                                            break;
                                        case "localanchorb":
                                            ((LineJointDef)mainDef).LocalAnchorB = ReadVector(sn);
                                            break;
                                        case "localaxisa":
                                            ((LineJointDef)mainDef).LocalAxisA = ReadVector(sn);
                                            break;
                                        case "maxmotorforce":
                                            ((LineJointDef)mainDef).MaxMotorForce = float.Parse(sn.Value);
                                            break;
                                        case "motorspeed":
                                            ((LineJointDef)mainDef).MotorSpeed = float.Parse(sn.Value);
                                            break;
                                        case "lowertranslation":
                                            ((LineJointDef)mainDef).LowerTranslation = float.Parse(sn.Value);
                                            break;
                                        case "uppertranslation":
                                            ((LineJointDef)mainDef).UpperTranslation = float.Parse(sn.Value);
                                            break;
                                        }
                                    }
                                    break;
                                case JointType.Prismatic:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "enablelimit":
                                            ((PrismaticJointDef)mainDef).EnableLimit = bool.Parse(sn.Value);
                                            break;
                                        case "enablemotor":
                                            ((PrismaticJointDef)mainDef).EnableMotor = bool.Parse(sn.Value);
                                            break;
                                        case "localanchora":
                                            ((PrismaticJointDef)mainDef).LocalAnchorA = ReadVector(sn);
                                            break;
                                        case "localanchorb":
                                            ((PrismaticJointDef)mainDef).LocalAnchorB = ReadVector(sn);
                                            break;
                                        case "localaxisa":
                                            ((PrismaticJointDef)mainDef).LocalAxis = ReadVector(sn);
                                            break;
                                        case "maxmotorforce":
                                            ((PrismaticJointDef)mainDef).MaxMotorForce = float.Parse(sn.Value);
                                            break;
                                        case "motorspeed":
                                            ((PrismaticJointDef)mainDef).MotorSpeed = float.Parse(sn.Value);
                                            break;
                                        case "lowertranslation":
                                            ((PrismaticJointDef)mainDef).LowerTranslation = float.Parse(sn.Value);
                                            break;
                                        case "uppertranslation":
                                            ((PrismaticJointDef)mainDef).UpperTranslation = float.Parse(sn.Value);
                                            break;
                                        case "referenceangle":
                                            ((PrismaticJointDef)mainDef).ReferenceAngle = float.Parse(sn.Value);
                                            break;
                                        }
                                    }
                                    break;
                                case JointType.Pulley:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "groundanchora":
                                            ((PulleyJointDef)mainDef).GroundAnchorA = ReadVector(sn);
                                            break;
                                        case "groundanchorb":
                                            ((PulleyJointDef)mainDef).GroundAnchorB = ReadVector(sn);
                                            break;
                                        case "lengtha":
                                            ((PulleyJointDef)mainDef).LengthA = float.Parse(sn.Value);
                                            break;
                                        case "lengthb":
                                            ((PulleyJointDef)mainDef).LengthB = float.Parse(sn.Value);
                                            break;
                                        case "localanchora":
                                            ((PulleyJointDef)mainDef).LocalAnchorA = ReadVector(sn);
                                            break;
                                        case "localanchorb":
                                            ((PulleyJointDef)mainDef).LocalAnchorB = ReadVector(sn);
                                            break;
                                        case "maxlengtha":
                                            ((PulleyJointDef)mainDef).MaxLengthA = float.Parse(sn.Value);
                                            break;
                                        case "maxlengthb":
                                            ((PulleyJointDef)mainDef).MaxLengthB = float.Parse(sn.Value);
                                            break;
                                        case "ratio":
                                            ((PulleyJointDef)mainDef).Ratio = float.Parse(sn.Value);
                                            break;
                                        }
                                    }
                                    break;
                                case JointType.Revolute:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "enablelimit":
                                            ((RevoluteJointDef)mainDef).EnableLimit = bool.Parse(sn.Value);
                                            break;
                                        case "enablemotor":
                                            ((RevoluteJointDef)mainDef).EnableMotor = bool.Parse(sn.Value);
                                            break;
                                        case "localanchora":
                                            ((RevoluteJointDef)mainDef).LocalAnchorA = ReadVector(sn);
                                            break;
                                        case "localanchorb":
                                            ((RevoluteJointDef)mainDef).LocalAnchorB = ReadVector(sn);
                                            break;
                                        case "maxmotortorque":
                                            ((RevoluteJointDef)mainDef).MaxMotorTorque = float.Parse(sn.Value);
                                            break;
                                        case "motorspeed":
                                            ((RevoluteJointDef)mainDef).MotorSpeed = float.Parse(sn.Value);
                                            break;
                                        case "lowerangle":
                                            ((RevoluteJointDef)mainDef).LowerAngle = float.Parse(sn.Value);
                                            break;
                                        case "upperangle":
                                            ((RevoluteJointDef)mainDef).UpperAngle = float.Parse(sn.Value);
                                            break;
                                        case "referenceangle":
                                            ((RevoluteJointDef)mainDef).ReferenceAngle = float.Parse(sn.Value);
                                            break;
                                        }
                                    }
                                    break;
                                case JointType.Weld:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "localanchora":
                                            ((WeldJointDef)mainDef).LocalAnchorA = ReadVector(sn);
                                            break;
                                        case "localanchorb":
                                            ((WeldJointDef)mainDef).LocalAnchorB = ReadVector(sn);
                                            break;
                                        }
                                    }
                                    break;
                                case JointType.Gear:
                                    throw new Exception("Gear joint is unsupported");
                                }

                                switch (sn.Name.ToLower())
                                {
                                case "name":
                                    name = sn.Value;
                                    break;
                                case "bodya":
                                    bodyA = int.Parse(sn.Value);
                                    break;
                                case "bodyb":
                                    bodyB = int.Parse(sn.Value);
                                    break;
                                case "collideconnected":
                                    collideConnected = bool.Parse(sn.Value);
                                    break;
                                case "userdata":
                                    userData = ReadSimpleType(sn, null, false);
                                    break;
                                }
                            }

                            mainDef.CollideConnected = collideConnected;
                            mainDef.UserData = userData;
                            _joints.Add(new JointDefSerialized(mainDef, bodyA, bodyB, name));
                        }
                    }
                    break;
                }
            }

            return data;
        }