feat: save ECS
This commit is contained in:
parent
b56d95f4fa
commit
945e759942
|
@ -22,6 +22,8 @@ export default class Engine {
|
|||
|
||||
frame = 0;
|
||||
|
||||
handle;
|
||||
|
||||
last = Date.now();
|
||||
|
||||
server;
|
||||
|
@ -97,11 +99,7 @@ export default class Engine {
|
|||
System.active = true;
|
||||
}
|
||||
});
|
||||
const view = Ecs.serialize(ecs);
|
||||
await this.server.writeData(
|
||||
join('homesteads', `${id}`),
|
||||
view,
|
||||
);
|
||||
this.saveEcs(join('homesteads', `${id}`), ecs);
|
||||
}
|
||||
|
||||
async createPlayer(id) {
|
||||
|
@ -167,6 +165,19 @@ export default class Engine {
|
|||
return JSON.parse((new TextDecoder()).decode(buffer));
|
||||
}
|
||||
|
||||
async saveEcs(path, ecs) {
|
||||
const view = Ecs.serialize(ecs);
|
||||
await this.server.writeData(path, view);
|
||||
}
|
||||
|
||||
async saveEcses() {
|
||||
const promises = []
|
||||
for (const i in this.ecses) {
|
||||
promises.push(this.saveEcs(i, this.ecses[i]));
|
||||
}
|
||||
await Promise.all(promises);
|
||||
}
|
||||
|
||||
async savePlayer(id, entity) {
|
||||
const encoder = new TextEncoder();
|
||||
const buffer = encoder.encode(JSON.stringify(entity.toJSON()));
|
||||
|
@ -174,7 +185,7 @@ export default class Engine {
|
|||
}
|
||||
|
||||
start() {
|
||||
return setInterval(() => {
|
||||
this.handle = setInterval(() => {
|
||||
const elapsed = (Date.now() - this.last) / 1000;
|
||||
this.last = Date.now();
|
||||
this.tick(elapsed);
|
||||
|
@ -183,6 +194,11 @@ export default class Engine {
|
|||
}, 1000 / TPS);
|
||||
}
|
||||
|
||||
stop() {
|
||||
clearInterval(this.handle);
|
||||
this.handle = undefined;
|
||||
}
|
||||
|
||||
tick(elapsed) {
|
||||
for (const i in this.ecses) {
|
||||
this.ecses[i].setClean();
|
||||
|
|
|
@ -32,7 +32,9 @@ const engine = new Engine(WorkerServer);
|
|||
|
||||
onmessage = async (event) => {
|
||||
if (0 === event.data) {
|
||||
engine.stop();
|
||||
await engine.disconnectPlayer(0, 0);
|
||||
await engine.saveEcses();
|
||||
postMessage(0);
|
||||
return;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user