Skip to content

Commit

Permalink
clean up serializer code
Browse files Browse the repository at this point in the history
  • Loading branch information
shakiba committed Mar 12, 2024
1 parent 24c7d5f commit 20677e1
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 118 deletions.
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
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

0 comments on commit 20677e1

Please sign in to comment.