observable world optimization
This commit is contained in:
parent
dabd120f10
commit
9217220505
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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<World>): 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 {
|
||||
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;
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue