significant optimzations

This commit is contained in:
biglyderv 2024-11-25 14:14:20 -05:00
parent 3ce6ded308
commit 37b516faee
5 changed files with 71 additions and 20 deletions

32
game.js
View file

@ -6,19 +6,40 @@ class Game extends GameBasic {
super();
this.ws = [];
}
sync() {
sync(full = false) {
let { entities } = this;
let entList = [];
for (let entity of entities) {
let { pos, vel, rot, dir, health, headCount, you, camera, ticks, type, isMenu, r, playing } = entity;
entList.push({ pos, vel, rot, dir, health, headCount, you, camera, ticks, type, isMenu, r, playing });
entity.pos = { x: Math.round(entity.pos.x), y: Math.round(entity.pos.y) };
entity.vel = { x: Math.round(entity.vel.x), y: Math.round(entity.vel.y) };
entList.push(entity);
}
if (entList.length == 0) return;
for (let client of this.ws) {
if (!client.active) continue;
client.send(JSON.stringify(entList));
let wsEnt = client.ent;
if (!wsEnt) continue;
let filtered;
if (!full) {
filtered = entList.filter((ent) =>
(Math.sqrt(((ent.pos.x - wsEnt.pos.x) ** 2) + ((ent.pos.y - wsEnt.pos.y) ** 2))) < 777
);
} else {
filtered = entList;
}
filtered= filtered.map(x => {
let props = x.serverProps;
return props.map(prop => x[prop]);
});
client.send(JSON.stringify(filtered));
}
for (let entity of entities) {
@ -35,7 +56,8 @@ class Game extends GameBasic {
that.entities.push(new NPC())
}
setInterval(function () { that.sync() }, 1000 / 5);
setInterval(function () { that.sync(false) }, 1000 / 10);
setInterval(function () { that.sync(true) }, 1000);
}
}