Skip to content

Commit

Permalink
don't serialize JSON every tick LMAO
Browse files Browse the repository at this point in the history
  • Loading branch information
evolutionleo committed Oct 8, 2024
1 parent 4eaf6cd commit cd1fb89
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
3 changes: 1 addition & 2 deletions Client/objects/oEntityManager/Step_0.gml
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,9 @@ for(i = 0; i < l; i++) {
// 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)
inst.image_xscale = entity.xs
inst.image_yscale = entity.ys
inst.image_angle = entity.a
inst.x = entity.x
inst.y = entity.y
inst.image_angle = entity.a
inst.a = entity.a // set for interpolation

inst.state = state

Expand Down
3 changes: 1 addition & 2 deletions TypescriptServer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
"development": "node . --env=dev",
"prod": "node . --env=prod",
"production": "node . --env=prod",
"profile": "node --prof --env=dev",
"postprofile": "node --profile",
"profile": "node out/server.js --prof --env=dev",
"build": "npx tsc"
},
"dependencies": {
Expand Down
32 changes: 16 additions & 16 deletions TypescriptServer/src/concepts/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Client from "#concepts/client";
import Room from "#concepts/room";

import PlayerEntity from "#entities/player";
import { isDeepStrictEqual } from 'util';


export type ColliderType = 'polygon' | 'circle' | 'box';
Expand All @@ -30,6 +31,7 @@ export type SerializedEntity = {
y: number,
xs: number,
ys: number,
a: number,
spd?: Point,
st: number, // state

Expand Down Expand Up @@ -64,7 +66,7 @@ class Entity extends EventEmitter {
angle:number = 0;

prev_pos:Point;
prev_serialized:string; // json of serialized entity
serialized:SerializedEntity; // save the last serialized version of this entity (to compare changes)

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

// if something changed - send again (add to the room's bundle)
const serialized = JSON.stringify(this.serialize());
if (serialized != this.prev_serialized || this.sendEveryTick) {
this.prev_serialized = serialized;
this.send(); // add to the bundle
const new_serialized = this.serialize();
if (this.sendEveryTick || !isDeepStrictEqual(new_serialized, this.serialized)) {
this.serialized = new_serialized;
this.send(true); // add to the bundle
}
}

Expand Down Expand Up @@ -228,10 +230,6 @@ class Entity extends EventEmitter {
this.regenerateCollider(x, y);
}

// if (this.size.x != this.prev_size.x || this.size.y != this.prev_size.y) {
// // change the collider scale by the same ratio as the entity scale
// this.collider.setScale(this.collider.scaleX * this.size.x / this.prev_size.x, this.collider.scaleY * this.size.y / this.prev_size.y);
// }
this.collider.setAngle(this.angle);
this.collider.setPosition(x, y);
this.tree.updateBody(this.collider);
Expand Down Expand Up @@ -319,6 +317,7 @@ class Entity extends EventEmitter {
y: this.roundedPos(this.y),
xs: this.xscale,
ys: this.yscale,
a: this.angle,
spd: this.spd,
p: this.props, // uses a getter for props
st: this.state
Expand All @@ -329,8 +328,14 @@ class Entity extends EventEmitter {
return this.serialize();
}

public send() {
const data = this.bundle();
public send(cached = false) {
let data: SerializedEntity;

if (!cached)
data = this.bundle();
else
data = this.serialized;

this.room.bundle.push(data);
}

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

return {
// left: x - w/2,
// top: y - h/2,
// right: x + w/2,
// bottom: y + h/2,

left: x,
top: y,
right: x + w,
Expand Down

0 comments on commit cd1fb89

Please sign in to comment.