@@ -3,45 +3,19 @@ import * as THREE from './build/three.module.js'
33class FirstPersonControls
44 constructor : (options )->
55 @kc = {
6- " w" : 87 ,
7- " s" : 83 ,
8- " a" : 65 ,
9- " d" : 68 ,
10- " space" : 32 ,
11- " shift" : 16
6+ 87 : " forward"
7+ 65 : " right"
8+ 83 : " back"
9+ 68 : " left"
10+ 32 : " jump"
1211 }
1312 @keys = {}
1413 @canvas = options .canvas
1514 @camera = options .camera
1615 @micromove = options .micromove
16+ @socket = options .socket
1717 @gameState = " menu"
1818 @ listen ()
19- ac : (qx , qy , qa , qf )->
20- m_x = - Math .sin (qa) * qf;
21- m_y = - Math .cos (qa) * qf;
22- r_x = qx - m_x;
23- r_y = qy - m_y;
24- return {
25- x : r_x,
26- y : r_y
27- }
28- camMicroMove : ->
29- if @keys [@kc [" w" ]]
30- @camera .position .x = @ ac (@camera .position .x , @camera .position .z , @camera .rotation .y + THREE .MathUtils .degToRad (180 ), @micromove ).x
31- @camera .position .z = @ ac (@camera .position .x , @camera .position .z , @camera .rotation .y + THREE .MathUtils .degToRad (180 ), @micromove ).y
32- if @keys [@kc [" s" ]]
33- @camera .position .x = @ ac (@camera .position .x , @camera .position .z , @camera .rotation .y , @micromove ).x
34- @camera .position .z = @ ac (@camera .position .x , @camera .position .z , @camera .rotation .y , @micromove ).y
35- if @keys [@kc [" a" ]]
36- @camera .position .x = @ ac (@camera .position .x , @camera .position .z , @camera .rotation .y - THREE .MathUtils .degToRad (90 ), @micromove ).x
37- @camera .position .z = @ ac (@camera .position .x , @camera .position .z , @camera .rotation .y - THREE .MathUtils .degToRad (90 ), @micromove ).y
38- if @keys [@kc [" d" ]]
39- @camera .position .x = @ ac (@camera .position .x , @camera .position .z , @camera .rotation .y + THREE .MathUtils .degToRad (90 ), @micromove ).x
40- @camera .position .z = @ ac (@camera .position .x , @camera .position .z , @camera .rotation .y + THREE .MathUtils .degToRad (90 ), @micromove ).y
41- if @keys [@kc [" space" ]]
42- @camera .position .y += @micromove
43- if @keys [@kc [" shift" ]]
44- @camera .position .y -= @micromove
4519 updatePosition : (e )->
4620 if @gameState is " game"
4721 @camera .rotation .x -= THREE .MathUtils .degToRad e .movementY / 10
@@ -50,6 +24,7 @@ class FirstPersonControls
5024 @camera .rotation .x = THREE .MathUtils .degToRad - 90
5125 if THREE .MathUtils .radToDeg ( @camera .rotation .x ) > 90
5226 @camera .rotation .x = THREE .MathUtils .degToRad 90
27+ @socket .emit " playerRotate" , [@camera .rotation .y ,@camera .rotation .x ]
5328 return
5429 listen : ->
5530 _this = @
@@ -62,8 +37,12 @@ class FirstPersonControls
6237 else
6338 document .exitPointerLock = document .exitPointerLock or document .mozExitPointerLock ;
6439 document .exitPointerLock ();
40+ if _this .kc [z .keyCode ] isnt undefined
41+ _this .socket .emit " move" ,_this .kc [z .keyCode ],true
6542 return
6643 $ (document ).keyup (z) ->
44+ if _this .kc [z .keyCode ] isnt undefined
45+ _this .socket .emit " move" ,_this .kc [z .keyCode ],false
6746 delete _this .keys [z .keyCode ]
6847 return
6948 $ (" .gameOn" ).click ->
0 commit comments