Skip to content

Commit cd1fb89

Browse files
committed
don't serialize JSON every tick LMAO
1 parent 4eaf6cd commit cd1fb89

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

Client/objects/oEntityManager/Step_0.gml

+1-2
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,9 @@ for(i = 0; i < l; i++) {
118118
// the reason I'm not using a with() statement here is because for some reason it is not equivallent to this, and produces weird errors (due to this being called in an Async event)
119119
inst.image_xscale = entity.xs
120120
inst.image_yscale = entity.ys
121+
inst.image_angle = entity.a
121122
inst.x = entity.x
122123
inst.y = entity.y
123-
inst.image_angle = entity.a
124-
inst.a = entity.a // set for interpolation
125124

126125
inst.state = state
127126

TypescriptServer/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
"development": "node . --env=dev",
1515
"prod": "node . --env=prod",
1616
"production": "node . --env=prod",
17-
"profile": "node --prof --env=dev",
18-
"postprofile": "node --profile",
17+
"profile": "node out/server.js --prof --env=dev",
1918
"build": "npx tsc"
2019
},
2120
"dependencies": {

TypescriptServer/src/concepts/entity.ts

+16-16
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Client from "#concepts/client";
99
import Room from "#concepts/room";
1010

1111
import PlayerEntity from "#entities/player";
12+
import { isDeepStrictEqual } from 'util';
1213

1314

1415
export type ColliderType = 'polygon' | 'circle' | 'box';
@@ -30,6 +31,7 @@ export type SerializedEntity = {
3031
y: number,
3132
xs: number,
3233
ys: number,
34+
a: number,
3335
spd?: Point,
3436
st: number, // state
3537

@@ -64,7 +66,7 @@ class Entity extends EventEmitter {
6466
angle:number = 0;
6567

6668
prev_pos:Point;
67-
prev_serialized:string; // json of serialized entity
69+
serialized:SerializedEntity; // save the last serialized version of this entity (to compare changes)
6870

6971
base_size:Point = { x: 64, y: 64 };
7072
scale:Point = { x: 1, y: 1 };
@@ -153,10 +155,10 @@ class Entity extends EventEmitter {
153155
this.emit('update');
154156

155157
// if something changed - send again (add to the room's bundle)
156-
const serialized = JSON.stringify(this.serialize());
157-
if (serialized != this.prev_serialized || this.sendEveryTick) {
158-
this.prev_serialized = serialized;
159-
this.send(); // add to the bundle
158+
const new_serialized = this.serialize();
159+
if (this.sendEveryTick || !isDeepStrictEqual(new_serialized, this.serialized)) {
160+
this.serialized = new_serialized;
161+
this.send(true); // add to the bundle
160162
}
161163
}
162164

@@ -228,10 +230,6 @@ class Entity extends EventEmitter {
228230
this.regenerateCollider(x, y);
229231
}
230232

231-
// if (this.size.x != this.prev_size.x || this.size.y != this.prev_size.y) {
232-
// // change the collider scale by the same ratio as the entity scale
233-
// this.collider.setScale(this.collider.scaleX * this.size.x / this.prev_size.x, this.collider.scaleY * this.size.y / this.prev_size.y);
234-
// }
235233
this.collider.setAngle(this.angle);
236234
this.collider.setPosition(x, y);
237235
this.tree.updateBody(this.collider);
@@ -319,6 +317,7 @@ class Entity extends EventEmitter {
319317
y: this.roundedPos(this.y),
320318
xs: this.xscale,
321319
ys: this.yscale,
320+
a: this.angle,
322321
spd: this.spd,
323322
p: this.props, // uses a getter for props
324323
st: this.state
@@ -329,8 +328,14 @@ class Entity extends EventEmitter {
329328
return this.serialize();
330329
}
331330

332-
public send() {
333-
const data = this.bundle();
331+
public send(cached = false) {
332+
let data: SerializedEntity;
333+
334+
if (!cached)
335+
data = this.bundle();
336+
else
337+
data = this.serialized;
338+
334339
this.room.bundle.push(data);
335340
}
336341

@@ -340,11 +345,6 @@ class Entity extends EventEmitter {
340345
let w:number = this.width, h:number = this.height;
341346

342347
return {
343-
// left: x - w/2,
344-
// top: y - h/2,
345-
// right: x + w/2,
346-
// bottom: y + h/2,
347-
348348
left: x,
349349
top: y,
350350
right: x + w,

0 commit comments

Comments
 (0)