Renderer/assets/Scripts/engine/scene/scene-loader.ts
2024-07-14 16:00:35 -04:00

120 lines
2.7 KiB
TypeScript

import { engine } from "/Scripts/engine/engine-init";
import { HookManager, TrackedHook } from "/Scripts/engine/hooks/hook-manager";
import { Hook } from "/Scripts/types/hook";
import { Scene } from "/Scripts/types/scene";
/**
* A scene being tracked by the manager
*/
export interface TrackedScene {
/**
* The id assigned to the scene
*/
sceneId: number,
/**
* The scene itself
*/
scene: Scene,
/**
* A map of a scene to all hooks related to the scene
*/
sceneHooks: Array<TrackedHook>
/**
* A map of engine signal to the list of hooks that should be called when that signal fires
*/
signalHookMap: Record<string,Array<TrackedHook>>
}
/**
* Loads scenes
*/
export class SceneLoader {
/**
* The hook manager
*/
hookManager: HookManager
/**
* The list of loaded scenes
*/
loadedScenes: TrackedScene[]
/**
* A record of tracked scene id to tracked scene object
*/
sceneIdMap: Record<number,TrackedScene>
/**
* A scene
*/
sceneIdIncrementer: number = 0
/**
* Registers all hooks in a scene to the hook manager
* @param scene The scene
*/
registerScene(scene: Scene, isServerScene: boolean){
const shouldRegister: boolean = !this.containsScene(scene)
if(shouldRegister){
//add to the list of tracked scenes
const trackedScene: TrackedScene = {
sceneId: this.sceneIdIncrementer++,
scene: scene,
sceneHooks: [],
signalHookMap: { },
}
this.loadedScenes.push(trackedScene)
this.sceneIdMap[trackedScene.sceneId] = trackedScene
//load all hooks from the scene
scene.hooks.forEach((hook: Hook) => {
engine.hookManager.registerHook(trackedScene,hook,isServerScene)
})
}
}
/**
* Deregisters all hooks in a scene from the hook manager
* @param scene The scene
*/
deregisterScene(scene: Scene){
throw new Error("Unsupported Operation!")
}
/**
* Checks if the manager is tracking a given scene
* @param scene The scene
* @returns true if it is being tracked already, false otherwise
*/
containsScene(scene: Scene): boolean {
this.loadedScenes.forEach(trackedScene => {
if(trackedScene.scene === scene){
return true
}
})
return false
}
/**
* Gets a tracked scene by its id
* @param sceneId The tracked scene
* @returns The tracked scene if it exists, null otherwise
*/
getScene(sceneId: number){
return this.sceneIdMap[sceneId]
}
}