11import * as THREE from ' ./build/three.module.js'
22
33class FirstPersonControls
4- constructor : (options )->
4+ constructor : (game )->
5+ @game = game
56 @kc = {
67 87 : " forward"
78 65 : " right"
@@ -11,25 +12,19 @@ class FirstPersonControls
1112 16 : " sneak"
1213 82 : " sprint"
1314 }
14- @pii = options .pii
15- @fov = options .fov
1615 @keys = {}
17- @canvas = options .canvas
18- @camera = options .camera
19- @socket = options .socket
20- @TWEEN = options .TWEEN
2116 @ setState " menu"
2217 @ listen ()
2318 updatePosition : (e )->
2419 # Updatowanie kursora
2520 if @gameState is " gameLock"
26- @camera .rotation .x -= THREE .MathUtils .degToRad e .movementY / 10
27- @camera .rotation .y -= THREE .MathUtils .degToRad e .movementX / 10
28- if THREE .MathUtils .radToDeg ( @camera .rotation .x ) < - 90
29- @camera .rotation .x = THREE .MathUtils .degToRad - 90
30- if THREE .MathUtils .radToDeg ( @camera .rotation .x ) > 90
31- @camera .rotation .x = THREE .MathUtils .degToRad 90
32- @socket .emit " rotate" , [@camera .rotation .y ,@camera .rotation .x ]
21+ @game . camera .rotation .x -= THREE .MathUtils .degToRad e .movementY / 10
22+ @game . camera .rotation .y -= THREE .MathUtils .degToRad e .movementX / 10
23+ if THREE .MathUtils .radToDeg ( @game . camera .rotation .x ) < - 90
24+ @game . camera .rotation .x = THREE .MathUtils .degToRad - 90
25+ if THREE .MathUtils .radToDeg ( @game . camera .rotation .x ) > 90
26+ @game . camera .rotation .x = THREE .MathUtils .degToRad 90
27+ @game . socket .emit " rotate" , [@game . camera .rotation .y ,@game . camera .rotation .x ]
3328 return
3429 listen : ->
3530 _this = @
@@ -43,7 +38,7 @@ class FirstPersonControls
4338
4439 # Klawisz Enter
4540 if z .keyCode is 13 and _this .gameState is " chat"
46- _this .socket . emit " command" , $ (" .com_i" ).val ()
41+ _this .game . chat . command $ (" .com_i" ).val ()
4742 $ (" .com_i" ).val (" " )
4843
4944 # Klawisz E
@@ -69,14 +64,14 @@ class FirstPersonControls
6964
7065 # Wysyłanie state'u do serwera
7166 if _this .kc [z .keyCode ] isnt undefined and _this .gameState is " gameLock"
72- _this .socket .emit " move" ,_this .kc [z .keyCode ],true
67+ _this .game . socket .emit " move" ,_this .kc [z .keyCode ],true
7368 if _this .kc [z .keyCode ] is " sprint"
74- to = {fov : _this .fov + 10 }
75- new _this.TWEEN.Tween _this .camera
69+ to = {fov : _this .game . fov + 10 }
70+ new _this.game. TWEEN.Tween _this . game .camera
7671 .to to, 200
77- .easing _this .TWEEN .Easing .Quadratic .Out
72+ .easing _this .game . TWEEN .Easing .Quadratic .Out
7873 .onUpdate ()->
79- _this .camera .updateProjectionMatrix ()
74+ _this .game . camera .updateProjectionMatrix ()
8075 .start ()
8176 return
8277 $ (document ).keyup (z) ->
@@ -85,22 +80,22 @@ class FirstPersonControls
8580
8681 # Wysyłanie state'u do serwera
8782 if _this .kc [z .keyCode ] isnt undefined
88- _this .socket .emit " move" ,_this .kc [z .keyCode ],false
83+ _this .game . socket .emit " move" ,_this .kc [z .keyCode ],false
8984 if _this .kc [z .keyCode ] is " sprint"
90- to = {fov : _this .fov }
91- new _this.TWEEN.Tween _this .camera
85+ to = {fov : _this .game . fov }
86+ new _this.game. TWEEN.Tween _this . game .camera
9287 .to to, 200
93- .easing _this .TWEEN .Easing .Quadratic .Out
88+ .easing _this .game . TWEEN .Easing .Quadratic .Out
9489 .onUpdate ()->
95- _this .camera .updateProjectionMatrix ()
90+ _this .game . camera .updateProjectionMatrix ()
9691 .start ()
9792
9893 return
9994 $ (" .gameOn" ).click ->
10095 _this .setState " game"
10196 return
10297 lockChangeAlert = ()->
103- if document .pointerLockElement is _this .canvas or document .mozPointerLockElement is _this .canvas
98+ if document .pointerLockElement is _this .game . canvas or document .mozPointerLockElement is _this . game .canvas
10499 # Lock
105100 if _this .gameState is " game"
106101 _this .setState " gameLock"
@@ -116,16 +111,16 @@ class FirstPersonControls
116111 , false
117112 return @
118113 reqLock : ()->
119- @canvas .requestPointerLock ()
114+ @game . canvas .requestPointerLock ()
120115 unLock : ()->
121116 document .exitPointerLock = document .exitPointerLock or document .mozExitPointerLock
122117 document .exitPointerLock ()
123118 state : (state )->
124119 @gameState = state
125120 if state is " inventory"
126- @pii .show ()
121+ @game . pii .show ()
127122 else
128- @pii .hide ()
123+ @game . pii .hide ()
129124 # console.log "Game state: "+state
130125 resetState : ()->
131126 $ (" .chat" ).removeClass (" focus" )
0 commit comments