コード例 #1
0
ファイル: Json.cs プロジェクト: levi1994/LitDev
        public void Write(string filename)
        {
            JsonWorld world = new JsonWorld();

            world.positionIterations = Engine.positionIterations;
            world.velocityIterations = Engine.velocityIterations;
            world.stepsPerSecond     = (int)(1.0f / Engine.timeStep);
            Engine.GetWorld().SetJson(world);

            FileStream stream1             = new FileStream(filename, FileMode.Create);
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(JsonWorld));

            ser.WriteObject(stream1, world);
            stream1.Close();

            string content = File.ReadAllText(filename);

            File.WriteAllText(filename, FormatJson(content));
        }
コード例 #2
0
ファイル: Json.cs プロジェクト: levi1994/LitDev
        public JsonWorld Read(string filename)
        {
            //Preparse json
            string tempFile = Path.GetTempFileName();

            //string tempFile = filename+"X";
            string[]      content    = File.ReadAllLines(filename);
            List <string> newContent = new List <string>();

            foreach (string line in content)
            {
                string newLine = line;
                while (newLine.Contains("  "))
                {
                    newLine = newLine.Replace("  ", " ");
                }
                while (newLine.IndexOf("//") >= 0)
                {
                    newLine = newLine.Substring(0, newLine.IndexOf("//"));
                }
                foreach (string vector in vectors)
                {
                    string check = "\"" + vector + "\" : 0,";
                    //Microsoft.SmallBasic.Library.TextWindow.WriteLine(check);
                    string replace = "\"" + vector + "\" : {\"x\": 0,\"y\": 0},";
                    newLine = newLine.Replace(check, replace);
                }
                newContent.Add(newLine);
            }
            File.WriteAllLines(tempFile, newContent);

            FileStream stream1             = new FileStream(tempFile, FileMode.Open);
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(JsonWorld));
            JsonWorld world = (JsonWorld)ser.ReadObject(stream1);

            stream1.Close();
            File.Delete(tempFile);

            return(world);
        }
コード例 #3
0
ファイル: Json.cs プロジェクト: litdev1/LitDev
        public void Write(string filename)
        {
            JsonWorld world = new JsonWorld();
            world.positionIterations = Engine.positionIterations;
            world.velocityIterations = Engine.velocityIterations;
            world.stepsPerSecond = (int)(1.0f/Engine.timeStep);
            Engine.GetWorld().SetJson(world);

            FileStream stream1 = new FileStream(filename, FileMode.Create);
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(JsonWorld));
            ser.WriteObject(stream1, world);
            stream1.Close();

            string content = File.ReadAllText(filename);
            File.WriteAllText(filename,FormatJson(content));
        }
コード例 #4
0
ファイル: World.cs プロジェクト: litdev1/LitDev
        // STEVE
        public void SetJson(JsonWorld jsWorld)
        {
            jsWorld.gravity = new JsonVector(_gravity);
            jsWorld.allowSleep = _allowSleep;

            Sprite sprite;
            Dictionary<Body, int> bodies = new Dictionary<Body, int>();
            jsWorld.body = new List<JsonBody>();
            jsWorld.image = new List<JsonImage>();
            jsWorld.joint = new List<JsonJoint>();
            int iBody = 0;
            int iFixture = 0;
            int iJoint = 0;
            Body body = _bodyList;
            while (null != body._next) body = body._next;
            for (; body != null; body = body._prev)
            //for (Body body = _bodyList; body != null; body = body._next)
            {
                if (body._shapeCount == 0) continue;

                JsonBody jsBody = new JsonBody();
                jsWorld.body.Add(jsBody);

                bodies[body] = iBody;
                jsBody.name = "Body" + (iBody + 1);
                if (null != body.GetUserData() && body.GetUserData().GetType() == typeof(Sprite))
                {
                    sprite = (Sprite)body.GetUserData();
                    jsBody.name = sprite.name;
                }
                jsBody.type = body.Type == Body.BodyType.Static ? 0 : 2;
                jsBody.angle = body.GetAngle();
                jsBody.angularDamping = body._angularDamping;
                jsBody.angularVelocity = body.GetAngularVelocity();
                jsBody.awake = (body.Flags & Body.BodyFlags.Sleep) != Body.BodyFlags.Sleep;
                jsBody.bullet = (body.Flags & Body.BodyFlags.Bullet) == Body.BodyFlags.Bullet;
                jsBody.fixedRotation = (body.Flags & Body.BodyFlags.FixedRotation) == Body.BodyFlags.FixedRotation;
                jsBody.linearDamping = body._linearDamping;
                jsBody.linearVelocity = new JsonVector(body.GetLinearVelocity());
                jsBody.massData_mass = body.GetMass();
                jsBody.massData_center = new JsonVector(body.GetLocalCenter());
                jsBody.massData_I = body.GetInertia();
                jsBody.position = new JsonVector(body.GetPosition());

                jsBody.fixture = new List<JsonFixture>();
                int iFixtureCount = 0;
                for (Shape shape = body.GetShapeList(); shape != null; shape = shape.GetNext(), iFixtureCount++)
                {
                    JsonFixture jsFixture = new JsonFixture();
                    jsBody.fixture.Add(jsFixture);

                    jsFixture.name = iFixtureCount == 0 ? jsBody.name : "Fixture" + ++iFixture;
                    jsFixture.density = shape.Density;
                    jsFixture.filter_categoryBits = shape.FilterData.CategoryBits;
                    jsFixture.filter_maskBits = shape.FilterData.MaskBits;
                    jsFixture.filter_groupIndex = shape.FilterData.GroupIndex;
                    jsFixture.friction = shape.Friction;
                    jsFixture.restitution = shape.Restitution;
                    jsFixture.sensor = shape.IsSensor;
                    float angle = body.GetAngle();
                    if (shape.GetType() == ShapeType.CircleShape)
                    {
                        CircleShape circle = (CircleShape)shape;
                        Vec2 position = new Vec2(0, 0);
                        position.X += circle.GetLocalPosition().X * (float)System.Math.Cos(angle) - circle.GetLocalPosition().Y * (float)System.Math.Sin(angle);
                        position.Y += circle.GetLocalPosition().X * (float)System.Math.Sin(angle) + circle.GetLocalPosition().Y * (float)System.Math.Cos(angle);
                        jsFixture.circle = new JsonCircle(new JsonVector(position), circle.GetRadius());
                    }
                    else if (shape.GetType() == ShapeType.PolygonShape)
                    {
                        PolygonShape polygon = (PolygonShape)shape;
                        List<float> x = new List<float>();
                        List<float> y = new List<float>();
                        for (int i = 0; i < ((PolygonShape)shape).VertexCount; i++)
                        {
                            Vec2 position = new Vec2(0, 0);
                            position.X += polygon.GetVertices()[i].X * (float)System.Math.Cos(angle) - polygon.GetVertices()[i].Y * (float)System.Math.Sin(angle);
                            position.Y += polygon.GetVertices()[i].X * (float)System.Math.Sin(angle) + polygon.GetVertices()[i].Y * (float)System.Math.Cos(angle);
                            x.Add(position.X);
                            y.Add(position.Y);
                        }
                        jsFixture.polygon = new JsonPolygon(new JsonVectorArray(x, y));
                    }

                    if (null == shape.UserData || shape.UserData.GetType() != typeof(Sprite)) continue;
                    sprite = (Sprite)shape.UserData;
                    jsFixture.name = sprite.name;
                    if (!sprite.name.StartsWith("Image")) continue;

                    JsonImage image = new JsonImage();
                    jsWorld.image.Add(image);

                    image.name = sprite.name;
                    image.body = iBody;

                    Type GraphicsWindowType = typeof(GraphicsWindow);
                    Dictionary<string, UIElement> _objectsMap = (Dictionary<string, UIElement>)GraphicsWindowType.GetField("_objectsMap", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.IgnoreCase).GetValue(null);
                    UIElement obj;
                    if (!_objectsMap.TryGetValue(sprite.name, out obj)) continue;
                    if (obj.GetType() != typeof(Image)) continue;

                    InvokeHelper ret = new InvokeHelper(delegate
                    {
                        Image imageObj = (Image)obj;
                        BitmapSource img = (BitmapSource)imageObj.Source;
                        System.Drawing.Bitmap dImg = FastPixel.GetBitmap(img);
                        string fileName = Program.Directory + "\\" + sprite.name + ".png";
                        dImg.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
                        image.file = fileName;
                    });
                    FastThread.Invoke(ret);
                }
                iBody++;
                //jsBody.fixture.Reverse();
            }
            //Joint joint = _jointList;
            //while (null != joint._next) joint = joint._next;
            //for (; joint != null; joint = joint._prev)
            for (Joint joint = _jointList; joint != null; joint = joint._next)
            {
                JsonJoint jsJoint = new JsonJoint();

                switch (joint.GetType())
                {
                    case JointType.RevoluteJoint:
                        {
                            RevoluteJoint _joint = (RevoluteJoint)joint;
                            jsJoint.type = "revolute";
                            jsJoint.anchorA = new JsonVector(_joint.Anchor2);
                            jsJoint.anchorB = new JsonVector(_joint.Anchor1);
                            jsJoint.bodyA = bodies[_joint.GetBody2()];
                            jsJoint.bodyB = bodies[_joint.GetBody1()];
                            jsJoint.collideConnected = _joint._collideConnected;
                            jsJoint.enableLimit = _joint._enableLimit;
                            jsJoint.enableMotor = _joint._enableMotor;
                            jsJoint.jointSpeed = _joint.JointSpeed;
                            jsJoint.lowerLimit = _joint.LowerLimit;
                            jsJoint.maxMotorTorque = _joint.MotorTorque;
                            jsJoint.motorSpeed = _joint.MotorSpeed;
                            jsJoint.refAngle = _joint.JointAngle;
                            jsJoint.upperLimit = _joint.UpperLimit;
                            jsJoint.name = "Joint" + ++iJoint;
                            jsWorld.joint.Add(jsJoint);
                        }
                        break;
                    case JointType.DistanceJoint:
                        {
                            DistanceJoint _joint = (DistanceJoint)joint;
                            jsJoint.type = "distance";
                            jsJoint.anchorA = new JsonVector(_joint.Anchor2);
                            jsJoint.anchorB = new JsonVector(_joint.Anchor1);
                            jsJoint.bodyA = bodies[_joint.GetBody2()];
                            jsJoint.bodyB = bodies[_joint.GetBody1()];
                            jsJoint.collideConnected = _joint._collideConnected;
                            jsJoint.dampingRatio = _joint._dampingRatio;
                            jsJoint.frequency = _joint._frequencyHz;
                            jsJoint.length = _joint._length;
                            jsJoint.name = "Joint" + ++iJoint;
                            jsWorld.joint.Add(jsJoint);
                        }
                        break;
                    case JointType.PrismaticJoint:
                        {
                            PrismaticJoint _joint = (PrismaticJoint)joint;
                            jsJoint.type = "prismatic";
                            jsJoint.anchorA = new JsonVector(_joint.Anchor2);
                            jsJoint.anchorB = new JsonVector(_joint.Anchor1);
                            jsJoint.bodyA = bodies[_joint.GetBody2()];
                            jsJoint.bodyB = bodies[_joint.GetBody1()];
                            jsJoint.collideConnected = _joint._collideConnected;
                            jsJoint.enableLimit = _joint._enableLimit;
                            jsJoint.enableMotor = _joint._enableMotor;
                            jsJoint.localAxisA = new JsonVector(_joint._axis);
                            jsJoint.jointSpeed = _joint.JointSpeed;
                            jsJoint.lowerLimit = _joint.LowerLimit;
                            jsJoint.maxMotorForce = _joint.MotorForce;
                            jsJoint.motorSpeed = _joint.MotorSpeed;
                            jsJoint.refAngle = _joint._refAngle;
                            jsJoint.upperLimit = _joint.UpperLimit;
                            jsJoint.name = "Joint" + ++iJoint;
                            jsWorld.joint.Add(jsJoint);
                        }
                        break;
                    case JointType.GearJoint:
                        {
                            TextWindow.WriteLine("Gear joint is not implemented or the conversion is ambiguous");
                        }
                        break;
                    case JointType.LineJoint:
                        {
                            TextWindow.WriteLine("Line joint is not implemented or the conversion is ambiguous");
                        }
                        break;
                    case JointType.MouseJoint:
                        {
                            TextWindow.WriteLine("Mouse joint is not implemented or the conversion is ambiguous");
                        }
                        break;
                    case JointType.PulleyJoint:
                        {
                            TextWindow.WriteLine("Pulley joint is not implemented or the conversion is ambiguous");
                        }
                        break;
                }
            }
        }