unibutton/game.js

76 lines
2 KiB
JavaScript
Raw Normal View History

2024-09-29 03:16:15 -04:00
import GameBasic from "./common/game_basic.js";
import NPC from "./common/npc.js";
2024-09-29 03:16:15 -04:00
class Game extends GameBasic {
constructor() {
super();
2024-09-29 03:55:22 -04:00
this.ws = [];
2024-09-29 03:16:15 -04:00
}
2024-10-02 04:14:56 -04:00
sync(full = false) {
2024-10-03 05:16:40 -04:00
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);
2024-09-29 03:55:22 -04:00
let { entities } = this;
let entList = [];
for (let entity of entities) {
2024-10-02 04:14:56 -04: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-09-29 03:55:22 -04:00
}
if (entList.length == 0) return;
for (let client of this.ws) {
if (!client.active) continue;
2024-10-02 04:14:56 -04:00
let wsEnt = client.ent;
2024-10-02 04:22:32 -04:00
wsEnt.isYou = true;
2024-10-02 04:14:56 -04:00
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]);
});
2024-10-03 05:16:40 -04:00
filtered.push([this.width,this.height])
2024-10-02 04:14:56 -04:00
client.send(JSON.stringify(filtered));
2024-10-02 04:22:32 -04:00
wsEnt.isYou = false;
2024-09-29 03:55:22 -04:00
}
for (let entity of entities) {
entity.r = 1;
}
2024-09-29 03:16:15 -04:00
}
init() {
super.init();
let that = this;
2024-09-29 03:55:22 -04:00
that.entities = [];
2024-09-29 03:16:15 -04:00
for (let i = 0; i < 20; i++) {
that.entities.push(new NPC())
}
2024-10-02 04:14:56 -04:00
setInterval(function () { that.sync(false) }, 1000 / 10);
setInterval(function () { that.sync(true) }, 1000);
2024-09-29 03:16:15 -04:00
}
}
export default Game;