@@ -9,6 +9,7 @@ import Client from "#concepts/client";
99import Room from "#concepts/room" ;
1010
1111import PlayerEntity from "#entities/player" ;
12+ import { isDeepStrictEqual } from 'util' ;
1213
1314
1415export 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