forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstPersonControls.coffee
More file actions
124 lines (114 loc) · 3.34 KB
/
FirstPersonControls.coffee
File metadata and controls
124 lines (114 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import * as THREE from './build/three.module.js'
class FirstPersonControls
constructor: (options)->
@kc={
87:"forward"
65:"right"
83:"back"
68:"left"
32:"jump"
16:"sneak"
82:"sprint"
}
@keys={}
@canvas=options.canvas
@camera=options.camera
@socket=options.socket
@gameState="menu"
@listen()
$("#commandx").blur()
$(".command").hide()
updatePosition: (e)->
#Updatowanie kursora
if @gameState is "gameLock"
@camera.rotation.x -= THREE.MathUtils.degToRad e.movementY / 10
@camera.rotation.y -= THREE.MathUtils.degToRad e.movementX / 10
if THREE.MathUtils.radToDeg( @camera.rotation.x ) < -90
@camera.rotation.x = THREE.MathUtils.degToRad -90
if THREE.MathUtils.radToDeg( @camera.rotation.x ) > 90
@camera.rotation.x = THREE.MathUtils.degToRad 90
@socket.emit "rotate", [@camera.rotation.y,@camera.rotation.x]
return
listen: ->
_this=@
$(document).keydown (z) ->
#Kliknięcie
_this.keys[z.keyCode] = true
#Klawisz Enter
if z.keyCode is 13 and _this.gameState is "chat"
_this.socket.emit "command",commandx.value
commandx.value=""
#Klawisz T lub /
if (z.keyCode is 84 or z.keyCode is 191) and _this.gameState is "gameLock"
if z.keyCode is 191
commandx.value="/"
_this._Chat()
z.preventDefault()
#Klawisz `
if z.keyCode is 192
$("#commandx").blur()
$(".command").hide()
z.preventDefault()
if (_this.gameState is "menu") or (_this.gameState is "chat")
_this._Game()
else
_this._Menu()
if z.keyCode is 27 and _this.gameState is "chat"
$("#commandx").blur()
$(".command").hide()
_this._Menu()
#Wysyłanie state'u do serwera
if _this.kc[z.keyCode] isnt undefined and _this.gameState is "gameLock"
_this.socket.emit "move",_this.kc[z.keyCode],true
return
$(document).keyup (z) ->
#Odkliknięcie
delete _this.keys[z.keyCode]
#Wysyłanie state'u do serwera
if _this.kc[z.keyCode] isnt undefined
_this.socket.emit "move",_this.kc[z.keyCode],false
return
$(".gameOn").click ->
_this._Game()
return
lockChangeAlert=()->
if document.pointerLockElement is _this.canvas or document.mozPointerLockElement is _this.canvas
#Lock
if _this.gameState is "game"
$("#commandx").blur()
$(".command").hide()
_this.state "gameLock"
$(".gameMenu").css "display", "none"
else
#Unlock
if (_this.gameState is "menu") or (_this.gameState is "gameLock")
$("#commandx").blur()
$(".command").hide()
_this._Menu()
return
document.addEventListener 'pointerlockchange', lockChangeAlert, false
document.addEventListener 'mozpointerlockchange', lockChangeAlert, false
document.addEventListener "mousemove", (e)->
_this.updatePosition(e)
, false
return @
state:(state)->
@gameState=state
console.log "Game state: "+state
_Game:()->
@state "game"
@canvas.requestPointerLock()
_Menu:()->
@state "menu"
$(".gameMenu").css "display", "block"
document.exitPointerLock = document.exitPointerLock or document.mozExitPointerLock
document.exitPointerLock();
_Chat:()->
if @gameState is "gameLock"
@state "chat"
$(".gameMenu").css "display", "none"
document.exitPointerLock = document.exitPointerLock or document.mozExitPointerLock
document.exitPointerLock()
$(".command").show()
$("#commandx").focus()
export {FirstPersonControls}