unibutton/game.js

64 lines
1.6 KiB
JavaScript
Raw Normal View History

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