1- import * as THREE from "three" ;
21import TWEEN from "@tweenjs/tween.js" ;
3- var FirstPersonControls = class FirstPersonControls {
2+ import * as THREE from "three" ;
3+ var modulo = function ( a , b ) {
4+ return ( ( + a % ( b = + b ) ) + b ) % b ;
5+ } ;
6+ class EventHandler {
47 constructor ( game ) {
8+ var _this = this ;
59 this . game = game ;
6- this . kc = {
10+ this . controls = {
711 KeyW : "forward" ,
812 KeyD : "right" ,
913 KeyS : "back" ,
@@ -13,38 +17,26 @@ var FirstPersonControls = class FirstPersonControls {
1317 KeyR : "sprint" ,
1418 } ;
1519 this . keys = { } ;
20+ this . gameState = null ;
1621 this . setState ( "menu" ) ;
17- this . listen ( ) ;
18- }
19-
20- updatePosition ( e ) {
21- //Updatowanie kursora
22- if ( this . gameState === "gameLock" ) {
23- this . game . camera . rotation . x -= THREE . MathUtils . degToRad (
24- e . movementY / 10
25- ) ;
26- this . game . camera . rotation . y -= THREE . MathUtils . degToRad (
27- e . movementX / 10
28- ) ;
29- if ( THREE . MathUtils . radToDeg ( this . game . camera . rotation . x ) < - 90 ) {
30- this . game . camera . rotation . x = THREE . MathUtils . degToRad ( - 90 ) ;
31- }
32- if ( THREE . MathUtils . radToDeg ( this . game . camera . rotation . x ) > 90 ) {
33- this . game . camera . rotation . x = THREE . MathUtils . degToRad ( 90 ) ;
22+ document . exitPointerLock =
23+ document . exitPointerLock || document . mozExitPointerLock ;
24+ //Mouse wheel change inventory
25+ var focus = 0 ;
26+ this . game . inv_bar . setFocus ( focus ) ;
27+ $ ( window ) . on ( "wheel" , function ( e ) {
28+ if ( _this . gameState === "gameLock" ) {
29+ if ( e . originalEvent . deltaY > 0 ) {
30+ focus ++ ;
31+ } else {
32+ focus -- ;
33+ }
34+ focus = modulo ( focus , 9 ) ;
35+ _this . game . inv_bar . setFocus ( focus ) ;
3436 }
35- this . game . socket . emit ( "rotate" , [
36- this . game . camera . rotation . y ,
37- this . game . camera . rotation . x ,
38- ] ) ;
39- }
40- }
41-
42- listen ( ) {
43- var _this , lockChangeAlert ;
44- _this = this ;
45- $ ( document ) . keydown ( function ( z ) {
46- var to ;
47- //Kliknięcie
37+ } ) ;
38+ //Keydown
39+ $ ( document ) . on ( "keydown" , function ( z ) {
4840 _this . keys [ z . code ] = true ;
4941 //Klawisz Escape
5042 if ( z . code === "Escape" && _this . gameState === "inventory" ) {
@@ -104,10 +96,13 @@ var FirstPersonControls = class FirstPersonControls {
10496 _this . game . socket . emit ( "fly" , _this . game . flying ) ;
10597 }
10698 //Wysyłanie state'u do serwera
107- if ( _this . kc [ z . code ] !== void 0 && _this . gameState === "gameLock" ) {
108- _this . game . socket . emit ( "move" , _this . kc [ z . code ] , true ) ;
109- if ( _this . kc [ z . code ] === "sprint" ) {
110- to = {
99+ if (
100+ _this . controls [ z . code ] !== undefined &&
101+ _this . gameState === "gameLock"
102+ ) {
103+ _this . game . socket . emit ( "move" , _this . controls [ z . code ] , true ) ;
104+ if ( _this . controls [ z . code ] === "sprint" ) {
105+ var to = {
111106 fov : _this . game . fov + 10 ,
112107 } ;
113108 new TWEEN . Tween ( _this . game . camera )
@@ -120,15 +115,13 @@ var FirstPersonControls = class FirstPersonControls {
120115 }
121116 }
122117 } ) ;
123- $ ( document ) . keyup ( function ( z ) {
124- var to ;
125- //Odkliknięcie
118+ //Keyup
119+ $ ( document ) . on ( "keyup" , function ( z ) {
126120 delete _this . keys [ z . code ] ;
127- //Wysyłanie state'u do serwera
128- if ( _this . kc [ z . code ] !== void 0 ) {
129- _this . game . socket . emit ( "move" , _this . kc [ z . code ] , false ) ;
130- if ( _this . kc [ z . code ] === "sprint" ) {
131- to = {
121+ if ( _this . controls [ z . code ] !== undefined ) {
122+ _this . game . socket . emit ( "move" , _this . controls [ z . code ] , false ) ;
123+ if ( _this . controls [ z . code ] === "sprint" ) {
124+ var to = {
132125 fov : _this . game . fov ,
133126 } ;
134127 new TWEEN . Tween ( _this . game . camera )
@@ -141,15 +134,18 @@ var FirstPersonControls = class FirstPersonControls {
141134 }
142135 }
143136 } ) ;
144- $ ( ".gameOn" ) . click ( function ( ) {
137+ //Play game button
138+ $ ( ".gameOn" ) . on ( "click" , function ( ) {
145139 _this . setState ( "game" ) ;
146140 } ) ;
141+ //Window onblur
147142 window . onblur = function ( ) {
148- Object . keys ( _this . kc ) . forEach ( function ( el ) {
149- _this . game . socket . emit ( "move" , _this . kc [ el ] , false ) ;
143+ Object . keys ( _this . controls ) . forEach ( function ( el ) {
144+ _this . game . socket . emit ( "move" , _this . controls [ el ] , false ) ;
150145 } ) ;
151146 } ;
152- lockChangeAlert = function ( ) {
147+ //Pointerlock
148+ var lockChangeAlert = function ( ) {
153149 if (
154150 document . pointerLockElement === _this . game . canvas ||
155151 document . mozPointerLockElement === _this . game . canvas
@@ -181,19 +177,27 @@ var FirstPersonControls = class FirstPersonControls {
181177 } ,
182178 false
183179 ) ;
184- return this ;
185180 }
186-
187- reqLock ( ) {
188- return this . game . canvas . requestPointerLock ( ) ;
189- }
190-
191- unLock ( ) {
192- document . exitPointerLock =
193- document . exitPointerLock || document . mozExitPointerLock ;
194- return document . exitPointerLock ( ) ;
181+ updatePosition ( e ) {
182+ if ( this . gameState === "gameLock" ) {
183+ this . game . camera . rotation . x -= THREE . MathUtils . degToRad (
184+ e . movementY / 10
185+ ) ;
186+ this . game . camera . rotation . y -= THREE . MathUtils . degToRad (
187+ e . movementX / 10
188+ ) ;
189+ if ( THREE . MathUtils . radToDeg ( this . game . camera . rotation . x ) < - 90 ) {
190+ this . game . camera . rotation . x = THREE . MathUtils . degToRad ( - 90 ) ;
191+ }
192+ if ( THREE . MathUtils . radToDeg ( this . game . camera . rotation . x ) > 90 ) {
193+ this . game . camera . rotation . x = THREE . MathUtils . degToRad ( 90 ) ;
194+ }
195+ this . game . socket . emit ( "rotate" , [
196+ this . game . camera . rotation . y ,
197+ this . game . camera . rotation . x ,
198+ ] ) ;
199+ }
195200 }
196-
197201 state ( state ) {
198202 this . gameState = state ;
199203 if ( state === "inventory" ) {
@@ -202,36 +206,36 @@ var FirstPersonControls = class FirstPersonControls {
202206 return this . game . pii . hide ( ) ;
203207 }
204208 }
205-
206- // console.log "Game state: "+state
207209 resetState ( ) {
208210 $ ( ".chat" ) . removeClass ( "focus" ) ;
209211 $ ( ".chat" ) . addClass ( "blur" ) ;
210212 $ ( ".com_i" ) . blur ( ) ;
211213 $ ( ".com" ) . hide ( ) ;
212214 return $ ( ".inv_window" ) . hide ( ) ;
213215 }
214-
215216 setState ( state ) {
216217 this . resetState ( ) ;
217218 switch ( state ) {
218219 case "game" :
219220 this . state ( "game" ) ;
220- return this . reqLock ( ) ;
221+ this . game . canvas . requestPointerLock ( ) ;
222+ break ;
221223 case "gameLock" :
222224 this . state ( "gameLock" ) ;
223- return $ ( ".gameMenu" ) . hide ( ) ;
225+ $ ( ".gameMenu" ) . hide ( ) ;
226+ break ;
224227 case "menu" :
225228 this . state ( "menu" ) ;
226229 $ ( ".gameMenu" ) . show ( ) ;
227- return this . unLock ( ) ;
230+ document . exitPointerLock ( ) ;
231+ break ;
228232 case "chat" :
229233 if ( this . gameState === "gameLock" ) {
230234 $ ( ".chat" ) . addClass ( "focus" ) ;
231235 $ ( ".chat" ) . removeClass ( "blur" ) ;
232236 $ ( ".gameMenu" ) . hide ( ) ;
233237 this . state ( "chat" ) ;
234- this . unLock ( ) ;
238+ document . exitPointerLock ( ) ;
235239 $ ( ".com" ) . show ( ) ;
236240 return $ ( ".com_i" ) . focus ( ) ;
237241 }
@@ -242,13 +246,13 @@ var FirstPersonControls = class FirstPersonControls {
242246 if ( this . gameState !== "inventory" ) {
243247 this . state ( "inventory" ) ;
244248 $ ( ".inv_window" ) . show ( ) ;
245- return this . unLock ( ) ;
249+ document . exitPointerLock ( ) ;
246250 } else {
247- return this . setState ( "game" ) ;
251+ this . setState ( "game" ) ;
248252 }
249253 }
254+ break ;
250255 }
251256 }
252- } ;
253-
254- export { FirstPersonControls } ;
257+ }
258+ export { EventHandler } ;
0 commit comments