Skip to content

Commit eab82bf

Browse files
committed
move async code out of constructor
1 parent 1b8c7e6 commit eab82bf

File tree

3 files changed

+131
-122
lines changed

3 files changed

+131
-122
lines changed

src/client/scripts/AssetLoader.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
import * as THREE from "three";
2-
import { FBXLoader } from "three/examples/jsm/loaders/FBXLoader.js";
2+
import {FBXLoader} from "three/examples/jsm/loaders/FBXLoader.js";
33

44
class AssetLoader {
5-
constructor(init) {
5+
constructor() {
66
this.assets = {};
7-
$.get("assets/assetLoader.json", (assets) => {
8-
this.load(assets, function () {
7+
}
8+
9+
async init() {
10+
return new Promise(async (resolve) => {
11+
let assets = await $.get("assets/assetLoader.json");
12+
await this.load(assets, () => {
13+
console.log(this.assets)
914
console.log("AssetLoader: done loading!");
10-
if (init !== null) {
11-
init(this);
12-
}
15+
resolve()
1316
});
14-
});
15-
return;
17+
})
1618
}
1719

18-
load(assets, callback) {
20+
async load(assets, callback) {
21+
1922
var textureLoader = new THREE.TextureLoader();
2023
var fbxl = new FBXLoader();
2124
var assetsNumber = 0;
@@ -74,4 +77,4 @@ class AssetLoader {
7477
}
7578
}
7679

77-
export { AssetLoader };
80+
export {AssetLoader};

src/client/scripts/Setup.js

Lines changed: 82 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -3,86 +3,89 @@ import TWEEN from "@tweenjs/tween.js";
33
import Stats from "stats-js";
44
import * as dat from "dat.gui";
55
import io from "socket.io-client";
6-
import { DistanceBasedFog } from "./DistanceBasedFog.js";
7-
import { UrlParams } from "./UrlParams.js";
8-
import { gpuInfo } from "./gpuInfo.js";
9-
import { World } from "./World/World.js";
10-
import { InventoryBar } from "./InventoryBar.js";
11-
import { Chat } from "./Chat.js";
12-
import { Entities } from "./Entities.js";
13-
import { PlayerInInventory } from "./PlayerInInventory.js";
14-
import { BlockBreak } from "./BlockBreak.js";
15-
import { BlockPlace } from "./BlockPlace.js";
16-
import { EventHandler } from "./EventHandler.js";
6+
import {DistanceBasedFog} from "./DistanceBasedFog.js";
7+
import {UrlParams} from "./UrlParams.js";
8+
import {gpuInfo} from "./gpuInfo.js";
9+
import {World} from "./World/World.js";
10+
import {InventoryBar} from "./InventoryBar.js";
11+
import {Chat} from "./Chat.js";
12+
import {Entities} from "./Entities.js";
13+
import {PlayerInInventory} from "./PlayerInInventory.js";
14+
import {BlockBreak} from "./BlockBreak.js";
15+
import {BlockPlace} from "./BlockPlace.js";
16+
import {EventHandler} from "./EventHandler.js";
1717

18-
function Setup(game, cb) {
19-
game.canvas = document.querySelector("#c");
20-
game.pcanvas = document.querySelector("#c_player");
21-
game.renderer = new THREE.WebGLRenderer({
22-
canvas: game.canvas,
23-
PixelRatio: window.devicePixelRatio,
24-
});
25-
game.renderer.sortObjects = true;
26-
game.scene = new THREE.Scene();
27-
game.camera = new THREE.PerspectiveCamera(game.fov.normal, 2, 0.1, 1000);
28-
game.camera.rotation.order = "YXZ";
29-
game.camera.position.set(26, 26, 26);
30-
game.scene.add(new THREE.AmbientLight(0xdddddd));
31-
game.stats = new Stats();
32-
game.drawcalls = game.stats.addPanel(
33-
new Stats.Panel("calls", "#ff8", "#221")
34-
);
35-
game.stats.showPanel(0);
36-
document.body.appendChild(game.stats.dom);
37-
game.distanceBasedFog = new DistanceBasedFog(game);
38-
UrlParams(game, (password) => {
39-
console.warn(gpuInfo());
40-
game.socket = io({
41-
query: {
42-
nick: game.nick,
43-
server: game.server,
44-
port: game.serverPort,
45-
password,
46-
premium: game.premium,
47-
},
18+
async function Setup(game, cb) {
19+
return new Promise((resolve) => {
20+
game.canvas = document.querySelector("#c");
21+
game.pcanvas = document.querySelector("#c_player");
22+
game.renderer = new THREE.WebGLRenderer({
23+
canvas: game.canvas,
24+
PixelRatio: window.devicePixelRatio,
4825
});
49-
game.pii = new PlayerInInventory(game);
50-
game.bb = new BlockBreak(game);
51-
game.bp = new BlockPlace(game);
52-
game.world = new World(game);
53-
game.ent = new Entities(game);
54-
game.chat = new Chat(game);
55-
game.inv_bar = new InventoryBar(game);
56-
game.eh = new EventHandler(game);
57-
game.distanceBasedFog.addShaderToMaterial(game.world.material);
58-
var gui = new dat.GUI();
59-
game.params = {
60-
chunkdist: 3,
61-
};
62-
game.distanceBasedFog.farnear.x = (game.params.chunkdist - 1) * 16;
63-
game.distanceBasedFog.farnear.y = game.params.chunkdist * 16;
64-
gui.add(game.world.material, "wireframe").name("Wireframe").listen();
65-
var chunkDist = gui
66-
.add(game.params, "chunkdist", 0, 10, 1)
67-
.name("Render distance")
68-
.listen();
69-
chunkDist.onChange(function (val) {
70-
game.distanceBasedFog.farnear.x = (val - 1) * 16;
71-
game.distanceBasedFog.farnear.y = val * 16;
72-
console.log(val);
73-
});
74-
game.playerImpulse = function () {
75-
var to = {
76-
x: game.playerPos[0],
77-
y: game.playerPos[1] + game.headHeight,
78-
z: game.playerPos[2],
26+
game.renderer.sortObjects = true;
27+
game.scene = new THREE.Scene();
28+
game.camera = new THREE.PerspectiveCamera(game.fov.normal, 2, 0.1, 1000);
29+
game.camera.rotation.order = "YXZ";
30+
game.camera.position.set(26, 26, 26);
31+
game.scene.add(new THREE.AmbientLight(0xdddddd));
32+
game.stats = new Stats();
33+
game.drawcalls = game.stats.addPanel(
34+
new Stats.Panel("calls", "#ff8", "#221")
35+
);
36+
game.stats.showPanel(0);
37+
document.body.appendChild(game.stats.dom);
38+
game.distanceBasedFog = new DistanceBasedFog(game);
39+
UrlParams(game, (password) => {
40+
console.warn(gpuInfo());
41+
game.socket = io({
42+
query: {
43+
nick: game.nick,
44+
server: game.server,
45+
port: game.serverPort,
46+
password,
47+
premium: game.premium,
48+
},
49+
});
50+
game.pii = new PlayerInInventory(game);
51+
game.bb = new BlockBreak(game);
52+
game.bp = new BlockPlace(game);
53+
game.world = new World(game);
54+
game.ent = new Entities(game);
55+
game.chat = new Chat(game);
56+
game.inv_bar = new InventoryBar(game);
57+
game.eh = new EventHandler(game);
58+
game.distanceBasedFog.addShaderToMaterial(game.world.material);
59+
var gui = new dat.GUI();
60+
game.params = {
61+
chunkdist: 3,
62+
};
63+
game.distanceBasedFog.farnear.x = (game.params.chunkdist - 1) * 16;
64+
game.distanceBasedFog.farnear.y = game.params.chunkdist * 16;
65+
gui.add(game.world.material, "wireframe").name("Wireframe").listen();
66+
var chunkDist = gui
67+
.add(game.params, "chunkdist", 0, 10, 1)
68+
.name("Render distance")
69+
.listen();
70+
chunkDist.onChange(function (val) {
71+
game.distanceBasedFog.farnear.x = (val - 1) * 16;
72+
game.distanceBasedFog.farnear.y = val * 16;
73+
console.log(val);
74+
});
75+
game.playerImpulse = function () {
76+
var to = {
77+
x: game.playerPos[0],
78+
y: game.playerPos[1] + game.headHeight,
79+
z: game.playerPos[2],
80+
};
81+
new TWEEN.Tween(game.camera.position)
82+
.to(to, 100)
83+
.easing(TWEEN.Easing.Quadratic.Out)
84+
.start();
7985
};
80-
new TWEEN.Tween(game.camera.position)
81-
.to(to, 100)
82-
.easing(TWEEN.Easing.Quadratic.Out)
83-
.start();
84-
};
85-
cb();
86-
});
86+
resolve();
87+
});
88+
})
8789
}
88-
export { Setup };
90+
91+
export {Setup};

src/client/scripts/index.js

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
import * as THREE from "three";
22
import TWEEN from "@tweenjs/tween.js";
33
import swal from "sweetalert";
4-
import { AssetLoader } from "./AssetLoader.js";
5-
import { Setup } from "./Setup.js";
4+
import {AssetLoader} from "./AssetLoader.js";
5+
import {Setup} from "./Setup.js";
66

77
class Game {
88
constructor() {
9-
this.al = new AssetLoader(() => {
10-
if (PRODUCTION) {
11-
console.log("Running in production mode");
12-
} else {
13-
console.log("Running in development mode");
14-
}
15-
this.fov = {
16-
normal: 70,
17-
sprint: 80,
18-
};
19-
this.toxelSize = 27;
20-
this.dimension = null;
21-
this.flying = false;
22-
this.playerPos = [0, 0, 0];
23-
this.dimBg = {
24-
"minecraft:overworld": [165 / 255, 192 / 255, 254 / 255],
25-
"minecraft:the_end": [1 / 255, 20 / 255, 51 / 255],
26-
"minecraft:the_nether": [133 / 255, 40 / 255, 15 / 255],
27-
"minecraft:end": [1 / 255, 20 / 255, 51 / 255],
28-
"minecraft:nether": [133 / 255, 40 / 255, 15 / 255],
29-
};
30-
this.headHeight = 17;
31-
this.gamemode = null;
329

33-
Setup(this, () => {
34-
this.init();
35-
});
36-
});
37-
return;
10+
if (PRODUCTION) {
11+
console.log("Running in production mode");
12+
} else {
13+
console.log("Running in development mode");
14+
}
15+
this.fov = {
16+
normal: 70,
17+
sprint: 80,
18+
};
19+
this.al = new AssetLoader();
20+
this.toxelSize = 27;
21+
this.dimension = null;
22+
this.flying = false;
23+
this.playerPos = [0, 0, 0];
24+
this.dimBg = {
25+
"minecraft:overworld": [165 / 255, 192 / 255, 254 / 255],
26+
"minecraft:the_end": [1 / 255, 20 / 255, 51 / 255],
27+
"minecraft:the_nether": [133 / 255, 40 / 255, 15 / 255],
28+
"minecraft:end": [1 / 255, 20 / 255, 51 / 255],
29+
"minecraft:nether": [133 / 255, 40 / 255, 15 / 255],
30+
};
31+
this.headHeight = 17;
32+
this.gamemode = null;
33+
34+
3835
}
39-
init() {
36+
37+
async init() {
38+
await this.al.init();
39+
await Setup(this);
4040
this.socket.on("connect", () => {
4141
console.log("Connected to server!");
4242
$(".loadingText").text(`Connecting to ${this.server}`);
@@ -189,4 +189,7 @@ class Game {
189189
}
190190
}
191191

192-
new Game();
192+
window.onload = () => {
193+
let game = new Game();
194+
game.init();
195+
}

0 commit comments

Comments
 (0)