scripts architecture work
Some checks failed
studiorailgun/Renderer/pipeline/head There was a failure building this commit

This commit is contained in:
austin 2024-07-16 14:29:20 -04:00
parent f2877edf37
commit 6791ce2f3b
6 changed files with 72 additions and 57 deletions

View File

@ -1,4 +1,3 @@
import { TrackedScene } from "/Scripts/engine/scene/scene-loader";
import { Scene } from "/Scripts/types/scene";
/**
@ -6,9 +5,31 @@ import { Scene } from "/Scripts/types/scene";
*/
const TestScene1: Scene = {
/**
* Called when the scene is created
* @param instanceId The scene instanceId
*/
onCreate: (instanceId: number) => {
console.log('Hello from the scene! My ID is ' + instanceId)
}
console.log(Object.keys(this))
},
/**
* All hooks for the scene
*/
hooks: [
/**
* Equip item hook
*/
{
signal: "equipItem",
callback: () => {
console.log("Item equipped")
}
},
],
}

View File

@ -10,21 +10,21 @@ export interface Engine {
/**
* The host's view of the scripting engine
*/
classes: any,
readonly classes: any,
/**
* The singletons available to the script engine
*/
singletons: any,
readonly singletons: any,
/**
* Manages all script-defined hooks in the engine
*/
hookManager: HookManager,
readonly hookManager: HookManager,
/**
* Tracks and loads scenes
*/
sceneLoader: SceneLoader,
readonly sceneLoader: SceneLoader,
}

View File

@ -1,4 +1,3 @@
import { TrackedScene } from "/Scripts/engine/scene/scene-loader"
import { Hook } from "/Scripts/types/hook"
import { Scene } from "/Scripts/types/scene"
@ -21,12 +20,12 @@ export interface TrackedHook extends Hook {
/**
* The scope that this hook was defined at
*/
scope: HookScope,
readonly scope: HookScope,
/**
* The scene that added the hook
*/
scene?: TrackedScene,
readonly scene?: Scene,
}
@ -38,34 +37,36 @@ export class HookManager {
/**
* The list of all hooks currently tracked by this manager
*/
hooks: Array<TrackedHook> = []
readonly hooks: 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>> = { }
readonly signalHookMap: Record<string,Array<TrackedHook>> = { }
/**
* The list of all scenes tracked by the manager
*/
trackedScenes: Array<TrackedScene> = []
readonly trackedScenes: Array<Scene> = []
/**
* Registers a hook
* @param scene The scene introducing the hook
* @param hook The hook
*/
registerHook(scene: TrackedScene, hook: Hook, isServerScene: boolean){
registerHook(scene: Scene, hook: Hook, isServerScene: boolean){
const trackedHook: TrackedHook = {
...hook,
scope: isServerScene ? HookScope.SCENE_SERVER : HookScope.SCENE_CLIENT,
scene: scene,
}
//add to flat array
console.log('push 1')
this.hooks.push(trackedHook)
//add to signal array
const hookSignal: string = hook.signal
const signalArray: Array<TrackedHook> = this.signalHookMap?.[hookSignal] ? this.signalHookMap?.[hookSignal] : []
console.log('push 2')
signalArray.push(trackedHook)
this.signalHookMap[hookSignal] = signalArray
//
@ -77,6 +78,7 @@ export class HookManager {
this.trackedScenes.push(scene)
}
//add to scene tracking structures
console.log('push 3')
scene.sceneHooks.push(trackedHook)
const sceneSignalArray: Array<TrackedHook> = scene.signalHookMap?.[hookSignal] ? scene.signalHookMap?.[hookSignal] : []
sceneSignalArray.push(trackedHook)

View File

@ -1,36 +1,9 @@
import { engine } from "/Scripts/engine/engine-init";
import { HookManager, TrackedHook } from "/Scripts/engine/hooks/hook-manager";
import { HookManager } 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
*/
instanceId: 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
@ -47,12 +20,12 @@ export class SceneLoader {
/**
* The list of loaded scenes
*/
loadedScenes: TrackedScene[] = [ ]
loadedScenes: Scene[] = [ ]
/**
* A record of tracked scene id to tracked scene object
*/
sceneIdMap: Record<number,TrackedScene> = { }
sceneIdMap: Record<number,Scene> = { }
/**
* A scene
@ -65,15 +38,21 @@ export class SceneLoader {
* @returns The id assigned to the instance of the scene
*/
loadScene(sceneName: string): number {
//load and instantiate scene
//@ts-ignore
const scene: Scene = require(sceneName).default
const sourceScene: Scene = require(sceneName).default
const sceneInstance: Scene = {
...sourceScene,
sceneHooks: [ ],
signalHookMap: { },
}
//creates an instance of the scene
let sceneInstanceId: number = this.createInstance(scene,true)
let sceneInstanceId: number = this.createInstance(sceneInstance,true)
//call on init for scene
if(scene.onCreate){
scene.onCreate(sceneInstanceId)
if(sceneInstance.onCreate){
sceneInstance.onCreate(sceneInstanceId)
}
return sceneInstanceId
@ -86,18 +65,16 @@ export class SceneLoader {
*/
createInstance(scene: Scene, isServerScene: boolean): number{
//add to the list of tracked scenes
const trackedScene: TrackedScene = {
const trackedScene: Scene = {
instanceId: this.sceneIdIncrementer++,
scene: scene,
sceneHooks: [],
signalHookMap: { },
...scene,
}
this.loadedScenes.push(trackedScene)
this.sceneIdMap[trackedScene.instanceId] = trackedScene
//load all hooks from the scene
scene?.hooks?.forEach((hook: Hook) => {
engine.hookManager.registerHook(trackedScene,hook,isServerScene)
this.hookManager.registerHook(trackedScene,hook,isServerScene)
})
return trackedScene.instanceId

View File

@ -8,11 +8,11 @@ export interface Hook {
/**
* The signal that triggers this hook in particular
*/
signal: string,
readonly signal: string,
/**
* The function to call when the signal is fired
*/
callback: Function,
readonly callback: Function,
}

View File

@ -1,4 +1,4 @@
import { TrackedScene } from "/Scripts/engine/scene/scene-loader";
import { TrackedHook } from "/Scripts/engine/hooks/hook-manager";
import { Hook } from "/Scripts/types/hook";
import { Namespace } from "/Scripts/types/namespace";
@ -17,6 +17,11 @@ export function SynchronizedType() {
*/
export interface Scene extends Namespace {
/**
* The instance id of this scene
*/
readonly instanceId?: number
/**
* Values that are synchronized between the client and server. They are also stored to disk when the scene saves
*/
@ -25,11 +30,21 @@ export interface Scene extends Namespace {
/**
* The hooks that are provided by this scene
*/
hooks?: Array<Hook>
readonly hooks?: Array<Hook>
/**
* Invoked when the scene is created
*/
onCreate?: (instanceId: number) => void
readonly onCreate?: (instanceId: number) => void
/**
* Internal use
*/
readonly sceneHooks?: Array<TrackedHook>
/**
* Internal use
*/
readonly signalHookMap?: Record<string,any>
}