@@ -9,6 +9,7 @@ import Client from "#concepts/client";
9
9
import Room from "#concepts/room" ;
10
10
11
11
import PlayerEntity from "#entities/player" ;
12
+ import { isDeepStrictEqual } from 'util' ;
12
13
13
14
14
15
export type ColliderType = 'polygon' | 'circle' | 'box' ;
@@ -30,6 +31,7 @@ export type SerializedEntity = {
30
31
y : number ,
31
32
xs : number ,
32
33
ys : number ,
34
+ a : number ,
33
35
spd ?: Point ,
34
36
st : number , // state
35
37
@@ -64,7 +66,7 @@ class Entity extends EventEmitter {
64
66
angle :number = 0 ;
65
67
66
68
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)
68
70
69
71
base_size :Point = { x : 64 , y : 64 } ;
70
72
scale :Point = { x : 1 , y : 1 } ;
@@ -153,10 +155,10 @@ class Entity extends EventEmitter {
153
155
this . emit ( 'update' ) ;
154
156
155
157
// 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
160
162
}
161
163
}
162
164
@@ -228,10 +230,6 @@ class Entity extends EventEmitter {
228
230
this . regenerateCollider ( x , y ) ;
229
231
}
230
232
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
- // }
235
233
this . collider . setAngle ( this . angle ) ;
236
234
this . collider . setPosition ( x , y ) ;
237
235
this . tree . updateBody ( this . collider ) ;
@@ -319,6 +317,7 @@ class Entity extends EventEmitter {
319
317
y : this . roundedPos ( this . y ) ,
320
318
xs : this . xscale ,
321
319
ys : this . yscale ,
320
+ a : this . angle ,
322
321
spd : this . spd ,
323
322
p : this . props , // uses a getter for props
324
323
st : this . state
@@ -329,8 +328,14 @@ class Entity extends EventEmitter {
329
328
return this . serialize ( ) ;
330
329
}
331
330
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
+
334
339
this . room . bundle . push ( data ) ;
335
340
}
336
341
@@ -340,11 +345,6 @@ class Entity extends EventEmitter {
340
345
let w :number = this . width , h :number = this . height ;
341
346
342
347
return {
343
- // left: x - w/2,
344
- // top: y - h/2,
345
- // right: x + w/2,
346
- // bottom: y + h/2,
347
-
348
348
left : x ,
349
349
top : y ,
350
350
right : x + w ,
0 commit comments