@@ -63,6 +63,10 @@ export class Engine {
63
63
fullscreen : true ,
64
64
container : 'body' ,
65
65
framerate : null ,
66
+ hooks : {
67
+ beforeStep : async ( ) => { } ,
68
+ afterStep : async ( ) => { } ,
69
+ } ,
66
70
...options ,
67
71
}
68
72
this . _container = document . querySelector ( this . _options . container as string ) as HTMLElement
@@ -98,6 +102,17 @@ export class Engine {
98
102
* to fix the timestep for fixed update loops (useful for physics and user interactions).
99
103
*/
100
104
protected step ( now : number ) : void {
105
+ // Call beforeStep hook
106
+ try {
107
+ if ( ! this . options . hooks || ! this . options . hooks . beforeStep ) {
108
+ throw new EngineError ( this , 'ENGINE:FAILURE' , 'No beforeStep hook given to engine.' )
109
+ }
110
+ this . options . hooks . beforeStep ( )
111
+ }
112
+ catch ( err ) {
113
+ console . error ( err )
114
+ }
115
+
101
116
if ( ! this . rootNode ) {
102
117
throw new EngineError ( this , 'ENGINE:FAILURE' , 'No root node given to engine, cannot run.' )
103
118
}
@@ -118,6 +133,17 @@ export class Engine {
118
133
system . step ( this . time . delta )
119
134
} )
120
135
this . rootNode . step ( this . time . delta )
136
+ // Call afterStep hook
137
+ try {
138
+ if ( ! this . options . hooks || ! this . options . hooks . afterStep ) {
139
+ throw new EngineError ( this , 'ENGINE:FAILURE' , 'No afterStep hook given to engine.' )
140
+ }
141
+ this . options . hooks . afterStep ( )
142
+ }
143
+ catch ( err ) {
144
+ console . error ( err )
145
+ }
146
+
121
147
// Request next step
122
148
requestAnimationFrame ( this . step . bind ( this ) )
123
149
}
0 commit comments