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
169 lines (158 loc) · 4.64 KB
/
FirstPersonControls.coffee
File metadata and controls
169 lines (158 loc) · 4.64 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import * as THREE from 'three'
class FirstPersonControls
constructor: (game)->
@game=game
@kc={
87:"forward"
65:"right"
83:"back"
68:"left"
32:"jump"
16:"sneak"
82:"sprint"
}
@keys={}
@setState "menu"
@listen()
updatePosition: (e)->
#Updatowanie kursora
if @gameState is "gameLock"
@game.camera.rotation.x -= THREE.MathUtils.degToRad e.movementY / 10
@game.camera.rotation.y -= THREE.MathUtils.degToRad e.movementX / 10
if THREE.MathUtils.radToDeg( @game.camera.rotation.x ) < -90
@game.camera.rotation.x = THREE.MathUtils.degToRad -90
if THREE.MathUtils.radToDeg( @game.camera.rotation.x ) > 90
@game.camera.rotation.x = THREE.MathUtils.degToRad 90
@game.socket.emit "rotate", [@game.camera.rotation.y,@game.camera.rotation.x]
return
listen: ->
_this=@
$(document).keydown (z) ->
#Kliknięcie
_this.keys[z.keyCode] = true
#Klawisz Escape
if z.keyCode is 27 and _this.gameState is "inventory"
_this.setState "menu"
#Strzałki
if z.keyCode is 38 and _this.gameState is "chat"
_this.game.chat.chatGoBack()
if z.keyCode is 40 and _this.gameState is "chat"
_this.game.chat.chatGoForward()
#Klawisz Enter
if z.keyCode is 13 and _this.gameState is "chat"
_this.game.chat.command $(".com_i").val()
$(".com_i").val("")
#Klawisz E
if (z.keyCode is 69) and (_this.gameState isnt "chat") and (_this.gameState isnt "menu")
_this.setState "inventory"
#Klawisz T lub /
if (z.keyCode is 84 or z.keyCode is 191) and _this.gameState is "gameLock"
if z.keyCode is 191
$(".com_i").val("/")
_this.setState "chat"
z.preventDefault()
#Klawisz `
if z.keyCode is 192
z.preventDefault()
if (_this.gameState is "menu") or (_this.gameState is "chat") or (_this.gameState is "inventory")
_this.setState "game"
else
_this.setState "menu"
if z.keyCode is 27 and _this.gameState is "chat"
_this.setState "menu"
#Wysyłanie state'u do serwera
if _this.kc[z.keyCode] isnt undefined and _this.gameState is "gameLock"
_this.game.socket.emit "move",_this.kc[z.keyCode],true
if _this.kc[z.keyCode] is "sprint"
to={fov:_this.game.fov+10}
new _this.game.TWEEN.Tween _this.game.camera
.to to, 200
.easing _this.game.TWEEN.Easing.Quadratic.Out
.onUpdate ()->
_this.game.camera.updateProjectionMatrix()
.start()
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.game.socket.emit "move",_this.kc[z.keyCode],false
if _this.kc[z.keyCode] is "sprint"
to={fov:_this.game.fov}
new _this.game.TWEEN.Tween _this.game.camera
.to to, 200
.easing _this.game.TWEEN.Easing.Quadratic.Out
.onUpdate ()->
_this.game.camera.updateProjectionMatrix()
.start()
return
$(".gameOn").click ->
_this.setState "game"
return
lockChangeAlert=()->
if document.pointerLockElement is _this.game.canvas or document.mozPointerLockElement is _this.game.canvas
#Lock
if _this.gameState is "game"
_this.setState "gameLock"
else
#Unlock
if (_this.gameState is "gameLock") and (_this.gameState isnt "inventory")
_this.setState "menu"
return
document.addEventListener 'pointerlockchange', lockChangeAlert, false
document.addEventListener 'mozpointerlockchange', lockChangeAlert, false
document.addEventListener "mousemove", (e)->
_this.updatePosition(e)
, false
return @
reqLock:()->
@game.canvas.requestPointerLock()
unLock:()->
document.exitPointerLock = document.exitPointerLock or document.mozExitPointerLock
document.exitPointerLock()
state:(state)->
@gameState=state
if state is "inventory"
@game.pii.show()
else
@game.pii.hide()
# console.log "Game state: "+state
resetState:()->
$(".chat").removeClass("focus")
$(".chat").addClass("blur")
$(".com_i").blur()
$(".com").hide()
$(".inv_window").hide()
setState:(state)->
@resetState()
switch state
when "game"
@state "game"
@reqLock()
when "gameLock"
@state "gameLock"
$(".gameMenu").hide()
when "menu"
@state "menu"
$(".gameMenu").show()
@unLock()
when "chat"
if @gameState is "gameLock"
$(".chat").addClass("focus")
$(".chat").removeClass("blur")
$(".gameMenu").hide()
@state "chat"
@unLock()
$(".com").show()
$(".com_i").focus()
when "inventory"
if @gameState isnt "menu"
$(".gameMenu").hide()
if @gameState isnt "inventory"
@state "inventory"
$(".inv_window").show()
@unLock()
else
@setState "game"
export {FirstPersonControls}