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
151 lines (141 loc) · 4.25 KB
/
FirstPersonControls.js
File metadata and controls
151 lines (141 loc) · 4.25 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
// 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.gameState = "menu";
this.listen();
$("#commandx").blur();
$(".command").hide();
}
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) {
//Kliknięcie
_this.keys[z.keyCode] = true;
//Klawisz Enter
if (z.keyCode === 13 && _this.gameState === "chat") {
_this.socket.emit("command", commandx.value);
commandx.value = "";
}
//Klawisz T lub /
if ((z.keyCode === 84 || z.keyCode === 191) && _this.gameState === "gameLock") {
if (z.keyCode === 191) {
commandx.value = "/";
}
_this._Chat();
z.preventDefault();
}
//Klawisz `
if (z.keyCode === 192) {
$("#commandx").blur();
$(".command").hide();
z.preventDefault();
if ((_this.gameState === "menu") || (_this.gameState === "chat")) {
_this._Game();
} else {
_this._Menu();
}
}
if (z.keyCode === 27 && _this.gameState === "chat") {
$("#commandx").blur();
$(".command").hide();
_this._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);
}
});
$(document).keyup(function(z) {
//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);
}
});
$(".gameOn").click(function() {
_this._Game();
});
lockChangeAlert = function() {
if (document.pointerLockElement === _this.canvas || document.mozPointerLockElement === _this.canvas) {
//Lock
if (_this.gameState === "game") {
$("#commandx").blur();
$(".command").hide();
_this.state("gameLock");
$(".gameMenu").css("display", "none");
}
} else {
//Unlock
if ((_this.gameState === "menu") || (_this.gameState === "gameLock")) {
$("#commandx").blur();
$(".command").hide();
_this._Menu();
}
}
};
document.addEventListener('pointerlockchange', lockChangeAlert, false);
document.addEventListener('mozpointerlockchange', lockChangeAlert, false);
document.addEventListener("mousemove", function(e) {
return _this.updatePosition(e);
}, false);
return this;
}
state(state) {
this.gameState = state;
return console.log("Game state: " + state);
}
_Game() {
this.state("game");
return this.canvas.requestPointerLock();
}
_Menu() {
this.state("menu");
$(".gameMenu").css("display", "block");
document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock;
return document.exitPointerLock();
}
_Chat() {
if (this.gameState === "gameLock") {
this.state("chat");
$(".gameMenu").css("display", "none");
document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock;
document.exitPointerLock();
$(".command").show();
return $("#commandx").focus();
}
}
};
export {
FirstPersonControls
};