unibutton/game.js

42 lines
1 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
}
sync() {
2024-09-29 03:55:22 -04:00
let { entities } = this;
let entList = [];
for (let entity of entities) {
let { pos, vel, rot, dir, health, headCount, you, camera, ticks, type, isMenu, r, playing } = entity;
entList.push({ pos, vel, rot, dir, health, headCount, you, camera, ticks, type, isMenu, r, playing });
2024-09-29 03:55:22 -04:00
}
if (entList.length == 0) return;
for (let client of this.ws) {
if (!client.active) continue;
client.send(JSON.stringify(entList));
}
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 < 10; i++) {
that.entities.push(new NPC())
}
2024-09-29 17:07:22 -04:00
setInterval(function () { that.sync() }, 1000 / 5);
2024-09-29 03:16:15 -04:00
}
}
export default Game;