Skip to content

Commit

Permalink
add onTick hook
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpineda committed Nov 11, 2023
1 parent 65037d3 commit cab5174
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 71 deletions.
14 changes: 5 additions & 9 deletions src/common/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface PluginMetaData extends PluginPackage {
}
}


/**
* A plugin that executes in the main process.
*/
Expand Down Expand Up @@ -104,6 +105,10 @@ export interface NativePlugin {
* Called on a game frame
*/
onFrame?( frame: number, commands?: any[] ): void;
/**
* Called before render, every render tick
*/
onTick?( delta: number, elapsed: number ): void;
/**
* When a game has been loaded and the game loop is about to begin
*/
Expand All @@ -117,13 +122,4 @@ export interface NativePlugin {
*/
onFrameReset?(): void;

/**
* When a scene is entered and nearly initialized.
*/
onEnterScene?( prevData: any ): Promise<unknown>;

/**
* When a scene has exited. Dispose resources here.
*/
onExitScene?( currentData: any ): any;
}
2 changes: 1 addition & 1 deletion src/core/world/api-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class ApiSession {
// which is not allowed WITHIN plugins since they are 3rd party, but ok in user macros and sandbox
this.#macros.setContainer(
mix( {
game: gameTimeApi,
api: gameTimeApi,
plugins: borrow( this.#plugins.store.vars ),
settings: borrow( settings.vars ),
events: eventsProxy,
Expand Down
26 changes: 24 additions & 2 deletions src/core/world/view-controller-composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { DamageType, Explosion } from "common/enums";
import { PerspectiveCamera, Vector3 } from "three";
import { GameViewPort } from "../../camera/game-viewport";
import { World } from "./world";
import type { SceneController } from "@plugins/scene-controller";
import type { SceneController, VRSceneController } from "@plugins/scene-controller";
import { easeInCubic } from "@utils/function-utils";
import range from "common/utils/range";
import { mixer } from "@audio/main-mixer";
import { renderComposer } from "@render/index";

// frequency, duration, strength multiplier
const explosionFrequencyDuration = {
Expand Down Expand Up @@ -134,12 +135,24 @@ export const createViewControllerComposer = (
if ( sceneController?.onExitScene ) {
try {
world.events.emit( "scene-controller-exit", sceneController.name );
prevData = sceneController.onExitScene( prevData );
const _prevData = sceneController.onExitScene( prevData );
//todo: further validate prevData
if (_prevData?.target && _prevData?.position) {
prevData = _prevData;
}
} catch ( e ) {
log.error( e );
}
}

if (sceneController) {
sceneController.parent.removeFromParent();
}

if ( sceneController?.isWebXR ) {
(sceneController as VRSceneController).viewerPosition.removeFromParent();
}

sceneController = null;
gameSurface.togglePointerLock( false );

Expand All @@ -150,6 +163,15 @@ export const createViewControllerComposer = (
viewports.length = 0;
viewports.push(...createViewports(newController.viewportsCount));

if (newController.isWebXR) {
const vrController = newController as VRSceneController;
vrController.setupXR( renderComposer.glRenderer.xr );
newController.scene.add( vrController.viewerPosition );
vrController.viewerPosition.add( vrController.viewport.camera );
}

newController.scene.add( newController.parent );

await newController.onEnterScene( prevData );
sceneController = newController;

Expand Down
5 changes: 5 additions & 0 deletions src/core/world/world-composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ export const createWorldComposer = async (
);
}

apiSession.native.hook_onTick(
delta,
elapsed
)

postProcessingComposer.render( delta, elapsed );

inputsComposer.reset();
Expand Down
22 changes: 21 additions & 1 deletion src/plugins/plugin-system-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class PluginSystemNative {
throw new Error("Plugin constructor must extend PluginBase");
}

const plugin = new Plugin.default(pluginPackage, sessionContext);
const plugin = new Plugin.default(pluginPackage, sessionContext) as PluginBase;
mix(plugin, sessionContext.game);

plugin.isSceneController = pluginPackage.isSceneController;
Expand Down Expand Up @@ -235,6 +235,26 @@ export class PluginSystemNative {
}
}

#hook_onTick( delta: number, elapsed: number ) {
for (const plugin of this.#plugins) {
if (plugin.onTick && this.isRegularPluginOrActiveSceneController(plugin)) {
plugin.onTick(delta, elapsed);
}
}
}

hook_onTick( delta: number, elapsed: number ) {
if (this.useTryCatchOnHooks) {
try {
this.#hook_onTick(delta, elapsed);
} catch (e) {
log.error(withErrorMessage(e, "@plugin-system-native: onFrame"));
}
} else {
this.#hook_onTick( delta, elapsed );
}
}

activateAdditionalPlugins(
pluginPackages: PluginMetaData[],
createCompartment: (env: any) => any
Expand Down
Loading

0 comments on commit cab5174

Please sign in to comment.