forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
213 lines (207 loc) · 7.01 KB
/
index.js
File metadata and controls
213 lines (207 loc) · 7.01 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import * as THREE from "three";
import TWEEN from "@tweenjs/tween.js";
import swal from "sweetalert";
import { AssetLoader } from "./AssetLoader.js";
import { Setup } from "./Setup.js";
class Game {
constructor() {
this.production = PRODUCTION;
if (this.production) {
console.log("Running in production mode");
} else {
console.log("Running in development mode");
}
this.servers = {
production: ["185.228.139.97", "25564"],
development: ["localhost", "25565"],
};
this.fov = {
normal: 70,
sprint: 80,
};
this.speed = 1.3;
this.al = new AssetLoader();
this.toxelSize = 27;
this.dimension = null;
this.flying = false;
this.playerPos = [0, 0, 0];
this.dimBg = {
"minecraft:overworld": [165 / 255, 192 / 255, 254 / 255],
"minecraft:the_end": [1 / 255, 20 / 255, 51 / 255],
"minecraft:the_nether": [133 / 255, 40 / 255, 15 / 255],
"minecraft:end": [1 / 255, 20 / 255, 51 / 255],
"minecraft:nether": [133 / 255, 40 / 255, 15 / 255],
};
this.headHeight = 17;
this.gamemode = null;
}
async init() {
await this.al.init();
await Setup(this);
this.socket.on("alreadyPlaying", () => {
swal({
title: "Player already is in server",
text: "Try later...",
icon: "error",
button: "Rejoin",
}).then(function () {
document.location.reload();
});
});
this.socket.on("connect", () => {
console.log("Connected to server!");
$(".loadingText").text(`Connecting to ${this.server}`);
console.log(`User nick: ${this.nick}`);
console.log(`Server ip: ${this.server}:${this.serverPort}`);
this.socket.emit("initClient", {
nick: this.nick,
});
});
this.socket.on("blockUpdate", (block) => {
this.world.setBlock(block[0], block[1] + 16, block[2], block[3]);
});
this.socket.on("spawn", (yaw, pitch) => {
console.log("Player joined the game!");
$(".initLoading").css("display", "none");
this.camera.rotation.y = yaw;
this.camera.rotation.x = pitch;
});
this.socket.on("dimension", (dim) => {
this.dimension = dim;
console.log(`Player dimension has been changed: ${dim}`);
this.world.resetWorld();
if (this.dimBg[dim] == undefined) {
dim = "minecraft:overworld";
}
var bg = this.dimBg[dim];
this.scene.background = new THREE.Color(...bg);
this.distanceBasedFog.color.x = bg[0];
this.distanceBasedFog.color.y = bg[1];
this.distanceBasedFog.color.z = bg[2];
this.distanceBasedFog.color.w = 1;
});
this.socket.on("mapChunk", (sections, x, z) => {
this.world.computeSections(sections, x, z);
});
this.socket.on("game", (gameData) => {
this.inv_bar.updateGamemode(gameData.gameMode);
});
this.socket.on("hp", (points) => {
this.inv_bar.setHp(points);
});
this.socket.on("inventory", (inv) => {
this.inv_bar.updateInv(inv);
});
this.socket.on("food", (points) => {
this.inv_bar.setFood(points);
});
this.socket.on("msg", (msg) => {
this.chat.log(msg);
});
this.socket.on("kicked", (reason) => {
document.exitPointerLock();
console.log(reason);
reason = JSON.parse(reason);
swal({
title: "You've been kicked!",
text:
reason.extra !== undefined
? reason.extra[0].text
: reason.text,
icon: "error",
button: "Rejoin",
}).then(function () {
document.location.reload();
});
});
this.socket.on("xp", (xp) => {
this.inv_bar.setXp(xp.level, xp.progress);
});
this.socket.on("move", (pos) => {
this.playerPos = [pos.x - 0.5, pos.y, pos.z - 0.5];
var to = {
x: pos.x - 0.5,
y: pos.y + this.headHeight,
z: pos.z - 0.5,
};
new TWEEN.Tween(this.camera.position)
.to(to, 100)
.easing(TWEEN.Easing.Quadratic.Out)
.start();
});
this.socket.on("entities", (entities) => {
this.ent.update(entities);
});
this.socket.on("diggingCompleted", () => {
this.bb.done = true;
console.warn("SERVER-DONE");
});
this.socket.on("diggingAborted", () => {
console.warn("SERVER-ABORT");
});
this.socket.on("digTime", (time) => {
console.warn("SERVER-START");
this.bb.startDigging(time);
});
this.mouse = false;
$(document).on("mousedown", (e) => {
if (e.which === 1) {
this.mouse = true;
if (this.eh.gameState === "gameLock") {
this.bb.digRequest();
}
} else if (e.which === 3) {
this.bp.placeBlock();
}
});
$(document).on("mouseup", (e) => {
if (e.which === 1) {
this.mouse = false;
return this.bb.stopDigging();
}
});
return this.animate();
}
animate() {
if (this.stats !== null) {
this.stats.begin();
this.render();
this.stats.end();
}
requestAnimationFrame(() => {
this.animate();
});
}
render() {
var width = window.innerWidth;
var height = window.innerHeight;
if (this.canvas.width !== width || this.canvas.height !== height) {
this.canvas.width = width;
this.canvas.height = height;
this.renderer.setSize(width, height, false);
this.camera.aspect = width / height;
this.camera.updateProjectionMatrix();
}
this.bb.updatePos(() => {
if (this.bb.isDigging) {
this.bb.stopDigging();
}
if (this.mouse && this.bb.done) {
return this.bb.digRequest();
}
});
this.world.updateChunksAroundPlayer(this.params.chunkdist);
TWEEN.update();
this.drawcalls.update(this.renderer.info.render.calls, 100);
if (this.eh.gameState === "inventory") {
this.pii.render();
}
this.inv_bar.updateItems();
this.distanceBasedFog.update();
this.renderer.render(this.scene, this.camera);
}
}
window.onload = () => {
let game = new Game();
game.init();
};