diff --git a/src/state/gameState.ts b/src/state/gameState.ts index 7cdf4a8..73bc046 100644 --- a/src/state/gameState.ts +++ b/src/state/gameState.ts @@ -1,7 +1,6 @@ import { makeAutoObservable } from "mobx"; import type { WorldState } from "./worldState"; import type { RunningGameState, Scene } from "../types"; -import { clone } from "../utils"; import type { Pos3 } from "../types/3d"; import type { CameraProps } from "@react-three/fiber"; @@ -39,15 +38,11 @@ export class GameState { } public resume(): void { - const state = clone(this.world.data.state) as RunningGameState; - state.paused = false; - this.world.data.state = state; + this.state.paused = false; } public pause(): void { - const state = clone(this.world.data.state) as RunningGameState; - state.paused = true; - this.world.data.state = state; + this.state.paused = true; } public stop(): void { diff --git a/src/state/worldEditorState.ts b/src/state/worldEditorState.ts index 089ec51..9eefcee 100644 --- a/src/state/worldEditorState.ts +++ b/src/state/worldEditorState.ts @@ -1,6 +1,6 @@ -import { makeAutoObservable } from "mobx"; +import { makeAutoObservable, runInAction } from "mobx"; import type { WorldState } from "./worldState"; -import type { ObjectInstance, Scene, World } from "../types"; +import type { ObjectInstance, Scene } from "../types"; import { createObjectInstance } from "../utils/object"; import { randomId } from "../utils"; import type { Pos3, R3, V3 } from "../types/3d"; @@ -67,49 +67,15 @@ export class WorldEditorState { return this.world.data.editorCamera; } - public mutateWorld(mutation: Partial): void { - this.world.data = { - ...this.world.data, - ...mutation, - } - } - - public mutateScene(mutation: Partial): void { - this.mutateWorld({ - initialScene: { - ...this.world.data.initialScene, - ...mutation, - } - }); - } - - public mutateObject(mutation: Partial & Pick): 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 { + public setObjectTransform(id: string, position: V3, rotation: R3, scale: V3): void { const obj = this.scene.objects[id]; if (!obj) return; - this.mutateObject({ - ...obj, - position, - rotation, - scale, + runInAction(() => { // all in one go + obj.position = position; + obj.rotation = rotation; + obj.scale = scale; }); } diff --git a/src/state/worldState.ts b/src/state/worldState.ts index 06dd023..b5fbe46 100644 --- a/src/state/worldState.ts +++ b/src/state/worldState.ts @@ -1,4 +1,4 @@ -import { makeAutoObservable } from "mobx"; +import { makeAutoObservable, toJS } from "mobx"; import { WorldFactory } from "../model/worldFactory"; import type { ObjectType, World } from "../types"; import type { VoxelType } from "../types/voxel"; @@ -68,7 +68,7 @@ export class WorldState { } public save(): void { - WorldFactory.save(this.data); + WorldFactory.save(toJS(this.data)); } public getObjectTypeById(id: string): ObjectType | undefined {