faster game state

This commit is contained in:
azykov@mail.ru
2026-06-13 18:34:54 +03:00
parent 004ec827d3
commit 6a16d39d88
3 changed files with 66 additions and 37 deletions
+27 -11
View File
@@ -1,4 +1,4 @@
import { makeAutoObservable, reaction, toJS } from "mobx";
import { autorun, makeAutoObservable, toJS } from "mobx";
import { WorldFactory } from "../model/worldFactory";
import { type ObjectInstance, type ObjectType, type World, type VoxelType, type RuntimeWorld } from "../types";
import { state } from "./rootState";
@@ -10,8 +10,32 @@ import { populateRuntimeWorld } from "../utils/runtime";
export class WorldState {
public data: RuntimeWorld = WorldFactory.create();
private _buildSave(): World {
const d = this.data;
return {
objectTypes: toJS(d.objectTypes),
voxelTypes: toJS(d.voxelTypes),
editorCamera: toJS(d.editorCamera),
gameRules: toJS(d.gameRules),
initialScene: {
playerObjectId: d.initialScene.playerObjectId,
objects: Object.fromEntries(
Object.entries(d.initialScene.objects).map(([id, obj]) => [id, {
id: obj.id,
typeId: obj.typeId,
physics: obj.physics,
gravityScale: obj.gravityScale,
position: toJS(obj.position),
rotation: toJS(obj.rotation),
scale: toJS(obj.scale),
} satisfies ObjectInstance])
),
},
};
}
private startAutoSave() {
return reaction(() => toJS(this.data), (data) => this.saveData(data), { delay: 500 });
return autorun(() => WorldFactory.save(this._buildSave()), { delay: 500 });
}
private withoutAutoSave(fn: () => void) {
@@ -103,16 +127,8 @@ export class WorldState {
state.worldEditor.resetSelection();
}
private saveData(data: World): void {
console.log('Saving world...');
const stack = new Error('Saving world...').stack!.split('\n').slice(1);
const { voxelTypes: _vts, initialScene: _is, ...debug } = toJS(data);
console.dir({ stack, debug });
WorldFactory.save(toJS(this.data));
}
public save(): void {
this.saveData(this.data);
WorldFactory.save(this._buildSave());
}
public getObjectTypeById(id: string): ObjectType | undefined {