forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetup.js
More file actions
104 lines (100 loc) · 3.9 KB
/
Setup.js
File metadata and controls
104 lines (100 loc) · 3.9 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
import * as THREE from "three";
import TWEEN from "@tweenjs/tween.js";
import Stats from "stats-js";
import * as dat from "dat.gui";
import { DistanceBasedFog } from "./DistanceBasedFog.js";
import { UrlParams } from "./UrlParams.js";
import { gpuInfo } from "./gpuInfo.js";
import { World } from "./World/World.js";
import { InventoryBar } from "./InventoryBar.js";
import { Chat } from "./Chat.js";
import { Entities } from "./Entities.js";
import { PlayerInInventory } from "./PlayerInInventory.js";
import { BlockBreak } from "./BlockBreak.js";
import { BlockPlace } from "./BlockPlace.js";
import { EventHandler } from "./EventHandler.js";
import { Socket } from "./Socket.js";
async function Setup(game) {
return new Promise((resolve) => {
game.canvas = document.querySelector("#c");
game.pcanvas = document.querySelector("#c_player");
game.renderer = new THREE.WebGLRenderer({
canvas: game.canvas,
PixelRatio: window.devicePixelRatio,
});
game.renderer.sortObjects = true;
game.scene = new THREE.Scene();
game.camera = new THREE.PerspectiveCamera(
game.fov.normal,
2,
0.1,
1000
);
game.camera.rotation.order = "YXZ";
game.camera.position.set(26, 26, 26);
game.scene.add(new THREE.AmbientLight(0xdddddd));
game.stats = new Stats();
game.drawcalls = game.stats.addPanel(
new Stats.Panel("calls", "#ff8", "#221")
);
game.stats.showPanel(0);
document.body.appendChild(game.stats.dom);
game.distanceBasedFog = new DistanceBasedFog(game);
UrlParams(game, (password) => {
console.warn(gpuInfo());
var prefix;
if (document.location.protocol === "https:") {
prefix = "wss";
} else {
prefix = "ws";
}
game.socket = new Socket(
game,
`${prefix}://${document.location.host}?nick=${game.nick}&server=${game.server}&port=${game.serverPort}&password=${password}&premium=${game.premium}`
);
game.pii = new PlayerInInventory(game);
game.bb = new BlockBreak(game);
game.bp = new BlockPlace(game);
game.world = new World(game);
game.ent = new Entities(game);
game.chat = new Chat(game);
game.inv_bar = new InventoryBar(game);
game.distanceBasedFog.addShaderToMaterial(game.world.material);
var gui = new dat.GUI();
game.params = {
chunkdist: 3,
};
game.distanceBasedFog.farnear.x = (game.params.chunkdist - 1) * 16;
game.distanceBasedFog.farnear.y = game.params.chunkdist * 16;
gui.add(game.world.material, "wireframe")
.name("Wireframe")
.listen();
var chunkDist = gui
.add(game.params, "chunkdist", 0, 10, 1)
.name("Render distance")
.listen();
gui.add(game, "speed", 1.3, 5).name("sprint speed").listen();
chunkDist.onChange(function (val) {
game.distanceBasedFog.farnear.x = (val - 1) * 16;
game.distanceBasedFog.farnear.y = val * 16;
console.log(val);
});
game.playerImpulse = function () {
var to = {
x: game.playerPos[0],
y: game.playerPos[1] + game.headHeight,
z: game.playerPos[2],
};
new TWEEN.Tween(game.camera.position)
.to(to, 100)
.easing(TWEEN.Easing.Quadratic.Out)
.start();
};
game.socket.ws.onopen = () => {
game.eh = new EventHandler(game);
resolve();
};
});
});
}
export { Setup };