Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup serializer #274

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/collision/shape/BoxShape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { PolygonShape } from './PolygonShape';
* A rectangle polygon which extend PolygonShape.
*/
export class BoxShape extends PolygonShape {
// note that box is serialized/deserialized as polygon
static TYPE = 'polygon' as const;

constructor(hx: number, hy: number, center?: Vec2Value, angle?: number) {
Expand Down
4 changes: 2 additions & 2 deletions src/dynamics/joint/DistanceJoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ export interface DistanceJointDef extends JointDef, DistanceJointOpt {
*/
localAnchorB: Vec2Value;

/** @internal */ anchorA: Vec2Value;
/** @internal */ anchorB: Vec2Value;
/** @internal */ anchorA?: Vec2Value;
/** @internal */ anchorB?: Vec2Value;
}

/** @internal */ const DEFAULTS = {
Expand Down
4 changes: 2 additions & 2 deletions src/dynamics/joint/FrictionJoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ export interface FrictionJointDef extends JointDef, FrictionJointOpt {
*/
localAnchorB: Vec2Value;

/** @internal */ anchorA: Vec2Value;
/** @internal */ anchorB: Vec2Value;
/** @internal */ anchorA?: Vec2Value;
/** @internal */ anchorB?: Vec2Value;
}

/** @internal */ const DEFAULTS = {
Expand Down
4 changes: 2 additions & 2 deletions src/dynamics/joint/PrismaticJoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ export interface PrismaticJointDef extends JointDef, PrismaticJointOpt {
*/
referenceAngle: number;

/** @internal */ anchorA: Vec2Value;
/** @internal */ anchorB: Vec2Value;
/** @internal */ anchorA?: Vec2Value;
/** @internal */ anchorB?: Vec2Value;
}

/** @internal */ const DEFAULTS = {
Expand Down
4 changes: 2 additions & 2 deletions src/dynamics/joint/RevoluteJoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ export interface RevoluteJointDef extends JointDef, RevoluteJointOpt {
*/
referenceAngle: number;

/** @internal */ anchorA: Vec2Value;
/** @internal */ anchorB: Vec2Value;
/** @internal */ anchorA?: Vec2Value;
/** @internal */ anchorB?: Vec2Value;
}

/** @internal */ const DEFAULTS = {
Expand Down
4 changes: 2 additions & 2 deletions src/dynamics/joint/WeldJoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ export interface WeldJointDef extends JointDef, WeldJointOpt {
*/
localAnchorB: Vec2Value;

/** @internal */ anchorA: Vec2Value;
/** @internal */ anchorB: Vec2Value;
/** @internal */ anchorA?: Vec2Value;
/** @internal */ anchorB?: Vec2Value;
}

/** @internal */ const DEFAULTS = {
Expand Down
4 changes: 2 additions & 2 deletions src/dynamics/joint/WheelJoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ export interface WheelJointDef extends JointDef, WheelJointOpt {
/** @internal renamed to localAxisA */
localAxis?: Vec2Value;

/** @internal */ anchorA: Vec2Value;
/** @internal */ anchorB: Vec2Value;
/** @internal */ anchorA?: Vec2Value;
/** @internal */ anchorB?: Vec2Value;
}

/** @internal */ const DEFAULTS = {
Expand Down
29 changes: 14 additions & 15 deletions src/serializer/__test__/Serialize.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import { util } from 'util';
// import util from 'util';
import { describe, it, expect } from 'vitest';

import { Vec2 } from '../../common/Vec2';
Expand All @@ -7,24 +7,24 @@ import { BoxShape } from '../../collision/shape/BoxShape';
import { DistanceJoint } from '../../dynamics/joint/DistanceJoint';
import { World } from '../../dynamics/World';

import { Serializer } from '../../serializer';
import { Serializer } from '../../serializer/index';

describe('Serializer', function(): void {
it('saves and loads to JSON', function(): void {

var world = new World();
const world = new World();

var circle = new CircleShape(1);
var box = new BoxShape(1, 1);
const circle = new CircleShape(1);
const box = new BoxShape(1, 1);

var b1 = world.createBody({
const b1 = world.createBody({
position : new Vec2(0, 0),
type : 'dynamic'
});

b1.createFixture(circle);

var b2 = world.createBody({
const b2 = world.createBody({
position : new Vec2(2, 0),
type : 'dynamic'
});
Expand All @@ -37,16 +37,15 @@ describe('Serializer', function(): void {
localAnchorB: new Vec2(0, -1)
}));

var json = Serializer.toJson(world);
var text = JSON.stringify(json, null, ' ');
// console.log(util.inspect(json, false, null, true));
const json1 = Serializer.toJson(world);
const text1 = JSON.stringify(json1, null, ' ');

world = Serializer.fromJson(json);
var json2 = Serializer.toJson(world);
const world2 = Serializer.fromJson(json1);
const json2 = Serializer.toJson(world2);

var text2 = JSON.stringify(json, null, ' ');
const text2 = JSON.stringify(json2, null, ' ');

expect(json).to.deep.equal(json2);
expect(text.split('\n')).to.deep.equal(text2.split('\n'));
expect(json1).to.deep.equal(json2);
expect(text1.split('\n')).to.deep.equal(text2.split('\n'));
});
});
Loading
Loading