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"; import { Scene } from "/Scripts/types/scene";
/** /**
@ -6,9 +5,31 @@ import { Scene } from "/Scripts/types/scene";
*/ */
const TestScene1: Scene = { const TestScene1: Scene = {
/**
* Called when the scene is created
* @param instanceId The scene instanceId
*/
onCreate: (instanceId: number) => { onCreate: (instanceId: number) => {
console.log('Hello from the scene! My ID is ' + instanceId) 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 * The host's view of the scripting engine
*/ */
classes: any, readonly classes: any,
/** /**
* The singletons available to the script engine * The singletons available to the script engine
*/ */
singletons: any, readonly singletons: any,
/** /**
* Manages all script-defined hooks in the engine * Manages all script-defined hooks in the engine
*/ */
hookManager: HookManager, readonly hookManager: HookManager,
/** /**
* Tracks and loads scenes * 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 { Hook } from "/Scripts/types/hook"
import { Scene } from "/Scripts/types/scene" import { Scene } from "/Scripts/types/scene"
@ -21,12 +20,12 @@ export interface TrackedHook extends Hook {
/** /**
* The scope that this hook was defined at * The scope that this hook was defined at
*/ */
scope: HookScope, readonly scope: HookScope,
/** /**
* The scene that added the hook * 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 * 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 * 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 * The list of all scenes tracked by the manager
*/ */
trackedScenes: Array<TrackedScene> = [] readonly trackedScenes: Array<Scene> = []
/** /**
* Registers a hook * Registers a hook
* @param scene The scene introducing the hook * @param scene The scene introducing the hook
* @param hook The hook * @param hook The hook
*/ */
registerHook(scene: TrackedScene, hook: Hook, isServerScene: boolean){ registerHook(scene: Scene, hook: Hook, isServerScene: boolean){
const trackedHook: TrackedHook = { const trackedHook: TrackedHook = {
...hook, ...hook,
scope: isServerScene ? HookScope.SCENE_SERVER : HookScope.SCENE_CLIENT, scope: isServerScene ? HookScope.SCENE_SERVER : HookScope.SCENE_CLIENT,
scene: scene, scene: scene,
} }
//add to flat array //add to flat array
console.log('push 1')
this.hooks.push(trackedHook) this.hooks.push(trackedHook)
//add to signal array //add to signal array
const hookSignal: string = hook.signal const hookSignal: string = hook.signal
const signalArray: Array<TrackedHook> = this.signalHookMap?.[hookSignal] ? this.signalHookMap?.[hookSignal] : [] const signalArray: Array<TrackedHook> = this.signalHookMap?.[hookSignal] ? this.signalHookMap?.[hookSignal] : []
console.log('push 2')
signalArray.push(trackedHook) signalArray.push(trackedHook)
this.signalHookMap[hookSignal] = signalArray this.signalHookMap[hookSignal] = signalArray
// //
@ -77,6 +78,7 @@ export class HookManager {
this.trackedScenes.push(scene) this.trackedScenes.push(scene)
} }
//add to scene tracking structures //add to scene tracking structures
console.log('push 3')
scene.sceneHooks.push(trackedHook) scene.sceneHooks.push(trackedHook)
const sceneSignalArray: Array<TrackedHook> = scene.signalHookMap?.[hookSignal] ? scene.signalHookMap?.[hookSignal] : [] const sceneSignalArray: Array<TrackedHook> = scene.signalHookMap?.[hookSignal] ? scene.signalHookMap?.[hookSignal] : []
sceneSignalArray.push(trackedHook) sceneSignalArray.push(trackedHook)

View File

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

View File

@ -8,11 +8,11 @@ export interface Hook {
/** /**
* The signal that triggers this hook in particular * The signal that triggers this hook in particular
*/ */
signal: string, readonly signal: string,
/** /**
* The function to call when the signal is fired * 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 { Hook } from "/Scripts/types/hook";
import { Namespace } from "/Scripts/types/namespace"; import { Namespace } from "/Scripts/types/namespace";
@ -17,6 +17,11 @@ export function SynchronizedType() {
*/ */
export interface Scene extends Namespace { 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 * 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 * The hooks that are provided by this scene
*/ */
hooks?: Array<Hook> readonly hooks?: Array<Hook>
/** /**
* Invoked when the scene is created * 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>
} }