Skip to content

Commit 391e233

Browse files
committed
Event handler class
1 parent 9c02f36 commit 391e233

File tree

7 files changed

+86
-103
lines changed

7 files changed

+86
-103
lines changed

src/client/scripts/Chat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var Chat = class Chat {
3232
window.addEventListener(
3333
"wheel",
3434
function (e) {
35-
if (_this.game.FPC.gameState !== "chat") {
35+
if (_this.game.eh.gameState !== "chat") {
3636
e.preventDefault();
3737
}
3838
},
Lines changed: 76 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import * as THREE from "three";
21
import TWEEN from "@tweenjs/tween.js";
3-
var FirstPersonControls = class FirstPersonControls {
2+
import * as THREE from "three";
3+
var modulo = function (a, b) {
4+
return ((+a % (b = +b)) + b) % b;
5+
};
6+
class EventHandler {
47
constructor(game) {
8+
var _this = this;
59
this.game = game;
6-
this.kc = {
10+
this.controls = {
711
KeyW: "forward",
812
KeyD: "right",
913
KeyS: "back",
@@ -13,38 +17,26 @@ var FirstPersonControls = class FirstPersonControls {
1317
KeyR: "sprint",
1418
};
1519
this.keys = {};
20+
this.gameState = null;
1621
this.setState("menu");
17-
this.listen();
18-
}
19-
20-
updatePosition(e) {
21-
//Updatowanie kursora
22-
if (this.gameState === "gameLock") {
23-
this.game.camera.rotation.x -= THREE.MathUtils.degToRad(
24-
e.movementY / 10
25-
);
26-
this.game.camera.rotation.y -= THREE.MathUtils.degToRad(
27-
e.movementX / 10
28-
);
29-
if (THREE.MathUtils.radToDeg(this.game.camera.rotation.x) < -90) {
30-
this.game.camera.rotation.x = THREE.MathUtils.degToRad(-90);
31-
}
32-
if (THREE.MathUtils.radToDeg(this.game.camera.rotation.x) > 90) {
33-
this.game.camera.rotation.x = THREE.MathUtils.degToRad(90);
22+
document.exitPointerLock =
23+
document.exitPointerLock || document.mozExitPointerLock;
24+
//Mouse wheel change inventory
25+
var focus = 0;
26+
this.game.inv_bar.setFocus(focus);
27+
$(window).on("wheel", function (e) {
28+
if (_this.gameState === "gameLock") {
29+
if (e.originalEvent.deltaY > 0) {
30+
focus++;
31+
} else {
32+
focus--;
33+
}
34+
focus = modulo(focus, 9);
35+
_this.game.inv_bar.setFocus(focus);
3436
}
35-
this.game.socket.emit("rotate", [
36-
this.game.camera.rotation.y,
37-
this.game.camera.rotation.x,
38-
]);
39-
}
40-
}
41-
42-
listen() {
43-
var _this, lockChangeAlert;
44-
_this = this;
45-
$(document).keydown(function (z) {
46-
var to;
47-
//Kliknięcie
37+
});
38+
//Keydown
39+
$(document).on("keydown", function (z) {
4840
_this.keys[z.code] = true;
4941
//Klawisz Escape
5042
if (z.code === "Escape" && _this.gameState === "inventory") {
@@ -104,10 +96,13 @@ var FirstPersonControls = class FirstPersonControls {
10496
_this.game.socket.emit("fly", _this.game.flying);
10597
}
10698
//Wysyłanie state'u do serwera
107-
if (_this.kc[z.code] !== void 0 && _this.gameState === "gameLock") {
108-
_this.game.socket.emit("move", _this.kc[z.code], true);
109-
if (_this.kc[z.code] === "sprint") {
110-
to = {
99+
if (
100+
_this.controls[z.code] !== undefined &&
101+
_this.gameState === "gameLock"
102+
) {
103+
_this.game.socket.emit("move", _this.controls[z.code], true);
104+
if (_this.controls[z.code] === "sprint") {
105+
var to = {
111106
fov: _this.game.fov + 10,
112107
};
113108
new TWEEN.Tween(_this.game.camera)
@@ -120,15 +115,13 @@ var FirstPersonControls = class FirstPersonControls {
120115
}
121116
}
122117
});
123-
$(document).keyup(function (z) {
124-
var to;
125-
//Odkliknięcie
118+
//Keyup
119+
$(document).on("keyup", function (z) {
126120
delete _this.keys[z.code];
127-
//Wysyłanie state'u do serwera
128-
if (_this.kc[z.code] !== void 0) {
129-
_this.game.socket.emit("move", _this.kc[z.code], false);
130-
if (_this.kc[z.code] === "sprint") {
131-
to = {
121+
if (_this.controls[z.code] !== undefined) {
122+
_this.game.socket.emit("move", _this.controls[z.code], false);
123+
if (_this.controls[z.code] === "sprint") {
124+
var to = {
132125
fov: _this.game.fov,
133126
};
134127
new TWEEN.Tween(_this.game.camera)
@@ -141,15 +134,18 @@ var FirstPersonControls = class FirstPersonControls {
141134
}
142135
}
143136
});
144-
$(".gameOn").click(function () {
137+
//Play game button
138+
$(".gameOn").on("click", function () {
145139
_this.setState("game");
146140
});
141+
//Window onblur
147142
window.onblur = function () {
148-
Object.keys(_this.kc).forEach(function (el) {
149-
_this.game.socket.emit("move", _this.kc[el], false);
143+
Object.keys(_this.controls).forEach(function (el) {
144+
_this.game.socket.emit("move", _this.controls[el], false);
150145
});
151146
};
152-
lockChangeAlert = function () {
147+
//Pointerlock
148+
var lockChangeAlert = function () {
153149
if (
154150
document.pointerLockElement === _this.game.canvas ||
155151
document.mozPointerLockElement === _this.game.canvas
@@ -181,19 +177,27 @@ var FirstPersonControls = class FirstPersonControls {
181177
},
182178
false
183179
);
184-
return this;
185180
}
186-
187-
reqLock() {
188-
return this.game.canvas.requestPointerLock();
189-
}
190-
191-
unLock() {
192-
document.exitPointerLock =
193-
document.exitPointerLock || document.mozExitPointerLock;
194-
return document.exitPointerLock();
181+
updatePosition(e) {
182+
if (this.gameState === "gameLock") {
183+
this.game.camera.rotation.x -= THREE.MathUtils.degToRad(
184+
e.movementY / 10
185+
);
186+
this.game.camera.rotation.y -= THREE.MathUtils.degToRad(
187+
e.movementX / 10
188+
);
189+
if (THREE.MathUtils.radToDeg(this.game.camera.rotation.x) < -90) {
190+
this.game.camera.rotation.x = THREE.MathUtils.degToRad(-90);
191+
}
192+
if (THREE.MathUtils.radToDeg(this.game.camera.rotation.x) > 90) {
193+
this.game.camera.rotation.x = THREE.MathUtils.degToRad(90);
194+
}
195+
this.game.socket.emit("rotate", [
196+
this.game.camera.rotation.y,
197+
this.game.camera.rotation.x,
198+
]);
199+
}
195200
}
196-
197201
state(state) {
198202
this.gameState = state;
199203
if (state === "inventory") {
@@ -202,36 +206,36 @@ var FirstPersonControls = class FirstPersonControls {
202206
return this.game.pii.hide();
203207
}
204208
}
205-
206-
// console.log "Game state: "+state
207209
resetState() {
208210
$(".chat").removeClass("focus");
209211
$(".chat").addClass("blur");
210212
$(".com_i").blur();
211213
$(".com").hide();
212214
return $(".inv_window").hide();
213215
}
214-
215216
setState(state) {
216217
this.resetState();
217218
switch (state) {
218219
case "game":
219220
this.state("game");
220-
return this.reqLock();
221+
this.game.canvas.requestPointerLock();
222+
break;
221223
case "gameLock":
222224
this.state("gameLock");
223-
return $(".gameMenu").hide();
225+
$(".gameMenu").hide();
226+
break;
224227
case "menu":
225228
this.state("menu");
226229
$(".gameMenu").show();
227-
return this.unLock();
230+
document.exitPointerLock();
231+
break;
228232
case "chat":
229233
if (this.gameState === "gameLock") {
230234
$(".chat").addClass("focus");
231235
$(".chat").removeClass("blur");
232236
$(".gameMenu").hide();
233237
this.state("chat");
234-
this.unLock();
238+
document.exitPointerLock();
235239
$(".com").show();
236240
return $(".com_i").focus();
237241
}
@@ -242,13 +246,13 @@ var FirstPersonControls = class FirstPersonControls {
242246
if (this.gameState !== "inventory") {
243247
this.state("inventory");
244248
$(".inv_window").show();
245-
return this.unLock();
249+
document.exitPointerLock();
246250
} else {
247-
return this.setState("game");
251+
this.setState("game");
248252
}
249253
}
254+
break;
250255
}
251256
}
252-
};
253-
254-
export { FirstPersonControls };
257+
}
258+
export { EventHandler };

src/client/scripts/InventoryBar.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
var modulo = function (a, b) {
2-
return ((+a % (b = +b)) + b) % b;
3-
};
4-
51
var InventoryBar = class InventoryBar {
62
constructor(game) {
73
this.game = game;
@@ -16,7 +12,6 @@ var InventoryBar = class InventoryBar {
1612
"<span class='inv_box item' data-texture=''></span> "
1713
);
1814
}
19-
this.listen();
2015
}
2116

2217
setHp(points) {
@@ -112,23 +107,6 @@ var InventoryBar = class InventoryBar {
112107
}
113108
}
114109

115-
listen() {
116-
var _this = this;
117-
var focus = 0;
118-
this.setFocus(focus);
119-
$(window).on("wheel", function (e) {
120-
if (_this.game.FPC.gameState === "gameLock") {
121-
if (e.originalEvent.deltaY > 0) {
122-
focus++;
123-
} else {
124-
focus--;
125-
}
126-
focus = modulo(focus, 9);
127-
return _this.setFocus(focus);
128-
}
129-
});
130-
}
131-
132110
tick() {
133111
var list = $(".item");
134112
for (var i = 0; i < list.length; i++) {

src/client/scripts/World/World.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ var World = class World {
5858
blocksDef: this.blocksDef,
5959
},
6060
});
61-
this.lastPlayerChunk = "";
61+
this.lastPlayerChunk = null;
6262
this.blocksUpdate = false;
6363
}
6464
/**
@@ -161,7 +161,9 @@ var World = class World {
161161
_this.cellMesh[cellId].onAfterRender = function () {};
162162
};
163163
this.game.scene.add(this.cellMesh[cellId]);
164-
this.updateRenderOrder(JSON.parse(this.lastPlayerChunk));
164+
if (this.lastPlayerChunk !== null) {
165+
this.updateRenderOrder(JSON.parse(this.lastPlayerChunk));
166+
}
165167
} else {
166168
this.cellMesh[cellId].geometry = geometry;
167169
}

src/client/scripts/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import * as dat from "dat.gui";
44
import io from "socket.io-client";
55
import TWEEN from "@tweenjs/tween.js";
66
import { World } from "./World/World.js";
7-
import { FirstPersonControls } from "./FirstPersonControls.js";
87
import { gpuInfo } from "./gpuInfo.js";
98
import { AssetLoader } from "./AssetLoader.js";
109
import { InventoryBar } from "./InventoryBar.js";
@@ -15,6 +14,7 @@ import { PlayerInInventory } from "./PlayerInInventory.js";
1514
import { BlockBreak } from "./BlockBreak.js";
1615
import { BlockPlace } from "./BlockPlace.js";
1716
import { DistanceBasedFog } from "./DistanceBasedFog.js";
17+
import { EventHandler } from "./EventHandler.js";
1818

1919
class Game {
2020
constructor() {
@@ -82,7 +82,7 @@ class Game {
8282
this.ent = new Entities(this);
8383
this.chat = new Chat(this);
8484
this.inv_bar = new InventoryBar(this);
85-
this.FPC = new FirstPersonControls(this);
85+
this.eh = new EventHandler(this);
8686
this.distanceBasedFog.addShaderToMaterial(this.world.material);
8787
this.socket.on("connect", function () {
8888
console.log("Connected to server!");
@@ -185,7 +185,7 @@ class Game {
185185
$(document).on("mousedown", function (e) {
186186
if (e.which === 1) {
187187
_this.mouse = true;
188-
if (_this.FPC.gameState === "gameLock") {
188+
if (_this.eh.gameState === "gameLock") {
189189
_this.bb.digRequest();
190190
}
191191
} else if (e.which === 3) {
@@ -235,7 +235,7 @@ class Game {
235235
this.world.updateCellsAroundPlayer(this.params.chunkdist);
236236
TWEEN.update();
237237
this.drawcalls.update(this.renderer.info.render.calls, 100);
238-
if (this.FPC.gameState === "inventory") {
238+
if (this.eh.gameState === "inventory") {
239239
this.pii.render();
240240
}
241241
this.inv_bar.tick();

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ io.sockets.on("connection", function (socket) {
121121
}
122122
}
123123
socket.emit("entities", entities);
124-
}, 10);
124+
}, 100);
125125
bot.once("spawn", function () {
126126
socket.on("fly", function (toggle) {
127127
if (toggle) {

src/postinstall.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ extract(
33
`${__dirname}/assets/pack.zip`,
44
{ dir: `${__dirname}/assets/pack` },
55
function (err) {
6-
// handle err
76
console.log(err);
87
}
98
);

0 commit comments

Comments
 (0)