2024-09-29 03:16:15 -04:00
|
|
|
import GameBasic from "./common/game_basic.js";
|
2024-10-01 04:03:55 -04:00
|
|
|
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) };
|
2024-10-06 09:47:52 -04:00
|
|
|
entity.camera = entity.camera ? { x: Math.round(entity.camera.x), y: Math.round(entity.camera.y) } : false;
|
2024-10-04 12:29:34 -04:00
|
|
|
|
|
|
|
let props = entity.serverProps;
|
|
|
|
let basic = props.map(prop => entity[prop]);
|
|
|
|
let str = JSON.stringify(basic);
|
|
|
|
|
|
|
|
entList.push({ entity, str, basic });
|
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-04 12:29:34 -04:00
|
|
|
|
2024-10-02 04:14:56 -04:00
|
|
|
if (!wsEnt) continue;
|
|
|
|
|
2024-10-04 12:29:34 -04:00
|
|
|
wsEnt.isYou = true;
|
|
|
|
|
2024-10-02 04:14:56 -04:00
|
|
|
let filtered;
|
|
|
|
|
|
|
|
if (!full) {
|
2024-10-04 12:29:34 -04:00
|
|
|
filtered = entList.filter(({ entity: ent }) => {
|
|
|
|
return Math.sqrt((ent.pos.x - wsEnt.pos.x) ** 2 + (ent.pos.y - wsEnt.pos.y) ** 2) < 777
|
|
|
|
}
|
2024-10-02 04:14:56 -04:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
filtered = entList;
|
|
|
|
}
|
|
|
|
|
2024-10-04 12:29:34 -04:00
|
|
|
let na = [];
|
|
|
|
for (let i in filtered) {
|
|
|
|
let fi = filtered[i]
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2024-10-02 04:14:56 -04:00
|
|
|
|
2024-10-04 12:29:34 -04:00
|
|
|
na.push(JSON.stringify([this.width, this.height]))
|
2024-10-03 05:16:40 -04:00
|
|
|
|
2024-10-04 12:29:34 -04:00
|
|
|
client.send(`[${na.join(',')}]`);
|
2024-10-02 04:22:32 -04:00
|
|
|
|
|
|
|
wsEnt.isYou = false;
|
2024-09-29 03:55:22 -04:00
|
|
|
}
|
2024-10-01 04:03:55 -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
|
|
|
|
2024-10-04 02:56:58 -04:00
|
|
|
for (let i = 0; i < 20; i++) {
|
2024-10-01 04:03:55 -04:00
|
|
|
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;
|