more rearchitecting
This commit is contained in:
parent
6791ce2f3b
commit
2c807ce4f5
@ -3,22 +3,22 @@ import { Scene } from "/Scripts/types/scene";
|
||||
/**
|
||||
* The main scene interface
|
||||
*/
|
||||
const TestScene1: Scene = {
|
||||
class TestScene1 extends 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(Object.keys(this))
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* All hooks for the scene
|
||||
*/
|
||||
hooks: [
|
||||
|
||||
hooks = [
|
||||
|
||||
/**
|
||||
* Equip item hook
|
||||
*/
|
||||
@ -28,8 +28,8 @@ const TestScene1: Scene = {
|
||||
console.log("Item equipped")
|
||||
}
|
||||
},
|
||||
|
||||
],
|
||||
|
||||
]
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -61,12 +61,10 @@ export class HookManager {
|
||||
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
|
||||
//
|
||||
@ -78,7 +76,6 @@ 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)
|
||||
|
||||
@ -40,12 +40,8 @@ export class SceneLoader {
|
||||
loadScene(sceneName: string): number {
|
||||
//load and instantiate scene
|
||||
//@ts-ignore
|
||||
const sourceScene: Scene = require(sceneName).default
|
||||
const sceneInstance: Scene = {
|
||||
...sourceScene,
|
||||
sceneHooks: [ ],
|
||||
signalHookMap: { },
|
||||
}
|
||||
const SourceSceneClass = require(sceneName).default
|
||||
const sceneInstance: Scene = new SourceSceneClass()
|
||||
|
||||
//creates an instance of the scene
|
||||
let sceneInstanceId: number = this.createInstance(sceneInstance,true)
|
||||
@ -88,20 +84,6 @@ export class SceneLoader {
|
||||
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
|
||||
|
||||
@ -15,7 +15,7 @@ export function SynchronizedType() {
|
||||
/**
|
||||
* The namespace of a given scene
|
||||
*/
|
||||
export interface Scene extends Namespace {
|
||||
export class Scene implements Namespace {
|
||||
|
||||
/**
|
||||
* The instance id of this scene
|
||||
@ -25,12 +25,12 @@ export interface Scene extends Namespace {
|
||||
/**
|
||||
* Values that are synchronized between the client and server. They are also stored to disk when the scene saves
|
||||
*/
|
||||
persistentValues?: Record<string,any>
|
||||
persistentValues: Record<string,any> = { }
|
||||
|
||||
/**
|
||||
* The hooks that are provided by this scene
|
||||
*/
|
||||
readonly hooks?: Array<Hook>
|
||||
readonly hooks: Array<Hook> = [ ]
|
||||
|
||||
/**
|
||||
* Invoked when the scene is created
|
||||
@ -38,13 +38,13 @@ export interface Scene extends Namespace {
|
||||
readonly onCreate?: (instanceId: number) => void
|
||||
|
||||
/**
|
||||
* Internal use
|
||||
* The map of signal name -> array of hooks created by this scene to fire on that signal
|
||||
*/
|
||||
readonly sceneHooks?: Array<TrackedHook>
|
||||
readonly signalHookMap: Record<string,Array<TrackedHook>> = { }
|
||||
|
||||
/**
|
||||
* Internal use
|
||||
*/
|
||||
readonly signalHookMap?: Record<string,any>
|
||||
readonly sceneHooks: Array<TrackedHook> = [ ]
|
||||
|
||||
}
|
||||
|
||||
@ -248,14 +248,16 @@ public class ScriptEngine {
|
||||
/**
|
||||
* Initializes a scene script
|
||||
* @param scenePath The scene's init script path
|
||||
* @return The id assigned to the scene instance from the script-side
|
||||
*/
|
||||
public void initScene(String scenePath){
|
||||
public int initScene(String scenePath){
|
||||
//add files to virtual filesystem in script engine
|
||||
registerFile(scenePath);
|
||||
//load scene from javascript side
|
||||
Value sceneLoader = this.engineObject.getMember("sceneLoader");
|
||||
Value loadFunc = sceneLoader.getMember("loadScene");
|
||||
loadFunc.execute(scenePath);
|
||||
Value result = loadFunc.execute(scenePath);
|
||||
return result.asInt();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user