forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstPersonControls.js
More file actions
199 lines (188 loc) · 5.71 KB
/
FirstPersonControls.js
File metadata and controls
199 lines (188 loc) · 5.71 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Generated by CoffeeScript 2.5.1
var FirstPersonControls;
import * as THREE from './build/three.module.js';
FirstPersonControls = class FirstPersonControls {
constructor(options) {
this.kc = {
87: "forward",
65: "right",
83: "back",
68: "left",
32: "jump",
16: "sneak",
82: "sprint"
};
this.keys = {};
this.canvas = options.canvas;
this.camera = options.camera;
this.socket = options.socket;
this.TWEEN = options.TWEEN;
this.setState("menu");
this.listen();
}
updatePosition(e) {
//Updatowanie kursora
if (this.gameState === "gameLock") {
this.camera.rotation.x -= THREE.MathUtils.degToRad(e.movementY / 10);
this.camera.rotation.y -= THREE.MathUtils.degToRad(e.movementX / 10);
if (THREE.MathUtils.radToDeg(this.camera.rotation.x) < -90) {
this.camera.rotation.x = THREE.MathUtils.degToRad(-90);
}
if (THREE.MathUtils.radToDeg(this.camera.rotation.x) > 90) {
this.camera.rotation.x = THREE.MathUtils.degToRad(90);
}
this.socket.emit("rotate", [this.camera.rotation.y, this.camera.rotation.x]);
}
}
listen() {
var _this, lockChangeAlert;
_this = this;
$(document).keydown(function(z) {
var to;
//Kliknięcie
_this.keys[z.keyCode] = true;
//Klawisz Escape
if (z.keyCode === 27 && _this.gameState === "inventory") {
_this.setState("menu");
}
//Klawisz Enter
if (z.keyCode === 13 && _this.gameState === "chat") {
_this.socket.emit("command", $(".com_i").val());
$(".com_i").val("");
}
//Klawisz E
if ((z.keyCode === 69) && (_this.gameState !== "chat") && (_this.gameState !== "menu")) {
_this.setState("inventory");
}
//Klawisz T lub /
if ((z.keyCode === 84 || z.keyCode === 191) && _this.gameState === "gameLock") {
if (z.keyCode === 191) {
$(".com_i").val("/");
}
_this.setState("chat");
z.preventDefault();
}
//Klawisz `
if (z.keyCode === 192) {
z.preventDefault();
if ((_this.gameState === "menu") || (_this.gameState === "chat") || (_this.gameState === "inventory")) {
_this.setState("game");
} else {
_this.setState("menu");
}
}
if (z.keyCode === 27 && _this.gameState === "chat") {
_this.setState("menu");
}
//Wysyłanie state'u do serwera
if (_this.kc[z.keyCode] !== void 0 && _this.gameState === "gameLock") {
_this.socket.emit("move", _this.kc[z.keyCode], true);
if (_this.kc[z.keyCode] === "sprint") {
to = {
fov: 105
};
new _this.TWEEN.Tween(_this.camera).to(to, 200).easing(_this.TWEEN.Easing.Quadratic.Out).onUpdate(function() {
return _this.camera.updateProjectionMatrix();
}).start();
}
}
});
$(document).keyup(function(z) {
var to;
//Odkliknięcie
delete _this.keys[z.keyCode];
//Wysyłanie state'u do serwera
if (_this.kc[z.keyCode] !== void 0) {
_this.socket.emit("move", _this.kc[z.keyCode], false);
if (_this.kc[z.keyCode] === "sprint") {
to = {
fov: 95
};
new _this.TWEEN.Tween(_this.camera).to(to, 200).easing(_this.TWEEN.Easing.Quadratic.Out).onUpdate(function() {
return _this.camera.updateProjectionMatrix();
}).start();
}
}
});
$(".gameOn").click(function() {
_this.setState("game");
});
lockChangeAlert = function() {
if (document.pointerLockElement === _this.canvas || document.mozPointerLockElement === _this.canvas) {
//Lock
if (_this.gameState === "game") {
_this.setState("gameLock");
}
} else {
//Unlock
if ((_this.gameState === "gameLock") && (_this.gameState !== "inventory")) {
_this.setState("menu");
}
}
};
document.addEventListener('pointerlockchange', lockChangeAlert, false);
document.addEventListener('mozpointerlockchange', lockChangeAlert, false);
document.addEventListener("mousemove", function(e) {
return _this.updatePosition(e);
}, false);
return this;
}
reqLock() {
return this.canvas.requestPointerLock();
}
unLock() {
document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock;
return document.exitPointerLock();
}
state(state) {
this.gameState = state;
return console.log("Game state: " + state);
}
resetState() {
$(".chat").removeClass("focus");
$(".chat").addClass("blur");
$(".com_i").blur();
$(".com").hide();
return $(".inv_window").hide();
}
setState(state) {
this.resetState();
switch (state) {
case "game":
this.state("game");
return this.reqLock();
case "gameLock":
this.state("gameLock");
return $(".gameMenu").hide();
case "menu":
this.state("menu");
$(".gameMenu").show();
return this.unLock();
case "chat":
if (this.gameState === "gameLock") {
$(".chat").addClass("focus");
$(".chat").removeClass("blur");
$(".gameMenu").hide();
this.state("chat");
this.unLock();
$(".com").show();
return $(".com_i").focus();
}
break;
case "inventory":
if (this.gameState !== "menu") {
$(".gameMenu").hide();
if (this.gameState !== "inventory") {
this.state("inventory");
$(".inv_window").show();
return this.unLock();
} else {
return this.setState("game");
}
}
}
}
};
export {
FirstPersonControls
};