blockly3d/src/state/gameState.ts

61 lines
1.5 KiB
TypeScript

import { makeAutoObservable } from "mobx";
import type { WorldState } from "./worldState";
import type { RunningGameState, Scene } from "../types";
import { clone } from "../utils";
import { state } from "./rootState";
import type { Pos3 } from "../types/3d";
import type { CameraProps } from "@react-three/fiber";
export class GameState {
private readonly world: WorldState;
constructor(world: WorldState) {
this.world = world;
makeAutoObservable(this);
}
public get state(): RunningGameState {
return this.world.data.state as RunningGameState;
}
public get isPaused(): boolean {
return this.state.paused;
}
public get scene(): Scene {
return this.state.scene;
}
public get camera(): Pos3 {
return this.scene.character;
}
public get cameraAsThree(): CameraProps {
const cam = this.camera;
return {
position: cam.position,
fov: 50,
rotation: cam.look,
}
}
public resume(): void {
const state = clone(this.world.data.state) as RunningGameState;
state.paused = false;
this.world.data.state = state;
}
public pause(): void {
const state = clone(this.world.data.state) as RunningGameState;
state.paused = true;
this.world.data.state = state;
}
public stop(): void {
this.world.data.state = { playing: false };
}
public tick(deltaTime: number): void {
//TODO
}
}