64 lines
No EOL
1.6 KiB
JavaScript
64 lines
No EOL
1.6 KiB
JavaScript
import GameBasic from "./common/game_basic.js";
|
|
import NPC from "./common/npc.js";
|
|
|
|
class Game extends GameBasic {
|
|
constructor() {
|
|
super();
|
|
this.ws = [];
|
|
}
|
|
sync(full = false) {
|
|
let { entities } = this;
|
|
let entList = [];
|
|
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);
|
|
}
|
|
|
|
if (entList.length == 0) return;
|
|
|
|
for (let client of this.ws) {
|
|
if (!client.active) continue;
|
|
|
|
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) {
|
|
entity.r = 1;
|
|
}
|
|
}
|
|
init() {
|
|
super.init();
|
|
|
|
let that = this;
|
|
that.entities = [];
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
that.entities.push(new NPC())
|
|
}
|
|
|
|
setInterval(function () { that.sync(false) }, 1000 / 10);
|
|
setInterval(function () { that.sync(true) }, 1000);
|
|
}
|
|
}
|
|
|
|
export default Game; |