@@ -63,6 +63,10 @@ export class Engine {
63
63
fullscreen : true ,
64
64
container : 'body' ,
65
65
framerate : null ,
66
+ hooks : {
67
+ beforeStep : ( ) => { } ,
68
+ afterStep : ( ) => { } ,
69
+ } ,
66
70
...options ,
67
71
}
68
72
this . _container = document . querySelector ( this . _options . container as string ) as HTMLElement
@@ -98,6 +102,18 @@ 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
+ // eslint-disable-next-line unused-imports/no-unused-vars
113
+ catch ( _ ) {
114
+ throw new EngineError ( this , 'ENGINE:FAILURE' , 'Error in beforeStep hook' )
115
+ }
116
+
101
117
if ( ! this . rootNode ) {
102
118
throw new EngineError ( this , 'ENGINE:FAILURE' , 'No root node given to engine, cannot run.' )
103
119
}
@@ -118,6 +134,18 @@ export class Engine {
118
134
system . step ( this . time . delta )
119
135
} )
120
136
this . rootNode . step ( this . time . delta )
137
+ // Call afterStep hook
138
+ try {
139
+ if ( ! this . options . hooks || ! this . options . hooks . afterStep ) {
140
+ throw new EngineError ( this , 'ENGINE:FAILURE' , 'No afterStep hook given to engine.' )
141
+ }
142
+ this . options . hooks . afterStep ( )
143
+ }
144
+ // eslint-disable-next-line unused-imports/no-unused-vars
145
+ catch ( _ ) {
146
+ throw new EngineError ( this , 'ENGINE:FAILURE' , 'Error in afterStep hook' )
147
+ }
148
+
121
149
// Request next step
122
150
requestAnimationFrame ( this . step . bind ( this ) )
123
151
}
0 commit comments