better JSON serializer
This commit is contained in:
parent
53193fbb60
commit
6ffbc44ab0
2 changed files with 32 additions and 15 deletions
36
game.js
36
game.js
|
@ -19,7 +19,12 @@ class Game extends GameBasic {
|
|||
for (let entity of entities) {
|
||||
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);
|
||||
|
||||
let props = entity.serverProps;
|
||||
let basic = props.map(prop => entity[prop]);
|
||||
let str = JSON.stringify(basic);
|
||||
|
||||
entList.push({ entity, str, basic });
|
||||
}
|
||||
|
||||
if (entList.length == 0) return;
|
||||
|
@ -28,28 +33,37 @@ class Game extends GameBasic {
|
|||
if (!client.active) continue;
|
||||
|
||||
let wsEnt = client.ent;
|
||||
wsEnt.isYou = true;
|
||||
|
||||
if (!wsEnt) continue;
|
||||
|
||||
wsEnt.isYou = true;
|
||||
|
||||
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
|
||||
filtered = entList.filter(({ entity: ent }) => {
|
||||
return 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]);
|
||||
});
|
||||
let na = [];
|
||||
for (let i in filtered) {
|
||||
let fi = filtered[i]
|
||||
|
||||
filtered.push([this.width,this.height])
|
||||
na.push(fi.str)
|
||||
if (fi.entity.isYou) {
|
||||
let narr = [... fi.basic];
|
||||
narr[fi.entity.serverProps.indexOf('isYou')] = 'true';
|
||||
na[na.length - 1] = JSON.stringify(narr);
|
||||
}
|
||||
}
|
||||
|
||||
client.send(JSON.stringify(filtered));
|
||||
na.push(JSON.stringify([this.width, this.height]))
|
||||
|
||||
client.send(`[${na.join(',')}]`);
|
||||
|
||||
wsEnt.isYou = false;
|
||||
}
|
||||
|
|
|
@ -147,13 +147,17 @@ class Game extends GameBasic {
|
|||
let that = this;
|
||||
|
||||
let entList = JSON.parse(data);
|
||||
entList = entList.map((y, i) => {
|
||||
entList = entList.map((j, i) => {
|
||||
if (i == entList.length - 1) {
|
||||
that.width = y[0];
|
||||
that.height = y[1];
|
||||
that.width = j[0];
|
||||
that.height = j[1];
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (!j) return undefined;
|
||||
|
||||
let y = j; //(typeof j == 'string') ? JSON.parse(j) : j;
|
||||
|
||||
let type = (Object.keys(legalTypes).indexOf(y[0]) == -1) ? Player : legalTypes[y[0]];
|
||||
|
||||
let props = new type().serverProps;
|
||||
|
@ -186,7 +190,6 @@ class Game extends GameBasic {
|
|||
|
||||
let matchingPlayer = entList.filter(x => x && x.you == you)
|
||||
|
||||
|
||||
if (matchingPlayer.length == 0) {
|
||||
matchingPlayer = entList.filter(x => x && x.isYou);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue