unibutton/game.js

76 lines
No EOL
2 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 onScreen = this.entities.filter(x => x && x.health > 0).length;
onScreen += 4;
this.width = (this.width * 0.99) + (Math.sqrt(onScreen) * 16);
this.height = (this.height * 0.99) + (Math.sqrt(onScreen) * 16);
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;
wsEnt.isYou = true;
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]);
});
filtered.push([this.width,this.height])
client.send(JSON.stringify(filtered));
wsEnt.isYou = false;
}
for (let entity of entities) {
entity.r = 1;
}
}
init() {
super.init();
let that = this;
that.entities = [];
for (let i = 0; i < 20; i++) {
that.entities.push(new NPC())
}
setInterval(function () { that.sync(false) }, 1000 / 10);
setInterval(function () { that.sync(true) }, 1000);
}
}
export default Game;