observable world optimization

This commit is contained in:
azykov@mail.ru 2026-06-02 18:36:13 +03:00
parent dabd120f10
commit 9217220505
No known key found for this signature in database
3 changed files with 11 additions and 50 deletions

View File

@ -1,7 +1,6 @@
import { makeAutoObservable } from "mobx"; import { makeAutoObservable } from "mobx";
import type { WorldState } from "./worldState"; import type { WorldState } from "./worldState";
import type { RunningGameState, Scene } from "../types"; import type { RunningGameState, Scene } from "../types";
import { clone } from "../utils";
import type { Pos3 } from "../types/3d"; import type { Pos3 } from "../types/3d";
import type { CameraProps } from "@react-three/fiber"; import type { CameraProps } from "@react-three/fiber";
@ -39,15 +38,11 @@ export class GameState {
} }
public resume(): void { public resume(): void {
const state = clone(this.world.data.state) as RunningGameState; this.state.paused = false;
state.paused = false;
this.world.data.state = state;
} }
public pause(): void { public pause(): void {
const state = clone(this.world.data.state) as RunningGameState; this.state.paused = true;
state.paused = true;
this.world.data.state = state;
} }
public stop(): void { public stop(): void {

View File

@ -1,6 +1,6 @@
import { makeAutoObservable } from "mobx"; import { makeAutoObservable, runInAction } from "mobx";
import type { WorldState } from "./worldState"; import type { WorldState } from "./worldState";
import type { ObjectInstance, Scene, World } from "../types"; import type { ObjectInstance, Scene } from "../types";
import { createObjectInstance } from "../utils/object"; import { createObjectInstance } from "../utils/object";
import { randomId } from "../utils"; import { randomId } from "../utils";
import type { Pos3, R3, V3 } from "../types/3d"; import type { Pos3, R3, V3 } from "../types/3d";
@ -67,49 +67,15 @@ export class WorldEditorState {
return this.world.data.editorCamera; return this.world.data.editorCamera;
} }
public mutateWorld(mutation: Partial<World>): void { public setObjectTransform(id: string, position: V3, rotation: R3, scale: V3): void {
this.world.data = {
...this.world.data,
...mutation,
}
}
public mutateScene(mutation: Partial<Scene>): void {
this.mutateWorld({
initialScene: {
...this.world.data.initialScene,
...mutation,
}
});
}
public mutateObject(mutation: Partial<ObjectInstance> & Pick<ObjectInstance, 'id'>): void {
this.mutateScene({
objects: {
...this.world.data.initialScene.objects,
[mutation.id]: {
...this.world.data.initialScene.objects[mutation.id],
...mutation,
},
},
});
}
public setObjectTransform(
id: string,
position: V3,
rotation: R3,
scale: V3,
): void {
const obj = this.scene.objects[id]; const obj = this.scene.objects[id];
if (!obj) if (!obj)
return; return;
this.mutateObject({ runInAction(() => { // all in one go
...obj, obj.position = position;
position, obj.rotation = rotation;
rotation, obj.scale = scale;
scale,
}); });
} }

View File

@ -1,4 +1,4 @@
import { makeAutoObservable } from "mobx"; import { makeAutoObservable, toJS } from "mobx";
import { WorldFactory } from "../model/worldFactory"; import { WorldFactory } from "../model/worldFactory";
import type { ObjectType, World } from "../types"; import type { ObjectType, World } from "../types";
import type { VoxelType } from "../types/voxel"; import type { VoxelType } from "../types/voxel";
@ -68,7 +68,7 @@ export class WorldState {
} }
public save(): void { public save(): void {
WorldFactory.save(this.data); WorldFactory.save(toJS(this.data));
} }
public getObjectTypeById(id: string): ObjectType | undefined { public getObjectTypeById(id: string): ObjectType | undefined {