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
86 lines (80 loc) · 2.55 KB
/
FirstPersonControls.js
File metadata and controls
86 lines (80 loc) · 2.55 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
// 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"
};
this.keys = {};
this.canvas = options.canvas;
this.camera = options.camera;
this.micromove = options.micromove;
this.socket = options.socket;
this.gameState = "menu";
this.listen();
}
updatePosition(e) {
if (this.gameState === "game") {
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("playerRotate", [this.camera.rotation.y, this.camera.rotation.x]);
}
}
listen() {
var _this, lockChangeAlert;
_this = this;
$(window).keydown(function(z) {
_this.keys[z.keyCode] = true;
//If click escape
if (z.keyCode === 27) {
if (_this.gameState === "menu") {
_this.canvas.requestPointerLock();
} else {
document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock;
document.exitPointerLock();
}
}
if (_this.kc[z.keyCode] !== void 0) {
_this.socket.emit("move", _this.kc[z.keyCode], true);
}
});
$(document).keyup(function(z) {
if (_this.kc[z.keyCode] !== void 0) {
_this.socket.emit("move", _this.kc[z.keyCode], false);
}
delete _this.keys[z.keyCode];
});
$(".gameOn").click(function() {
_this.canvas.requestPointerLock();
});
lockChangeAlert = function() {
if (document.pointerLockElement === _this.canvas || document.mozPointerLockElement === _this.canvas) {
_this.gameState = "game";
$(".gameMenu").css("display", "none");
} else {
_this.gameState = "menu";
$(".gameMenu").css("display", "block");
}
};
document.addEventListener('pointerlockchange', lockChangeAlert, false);
document.addEventListener('mozpointerlockchange', lockChangeAlert, false);
document.addEventListener("mousemove", function(e) {
return _this.updatePosition(e);
}, false);
return this;
}
};
export {
FirstPersonControls
};