/
app.ts
333 lines (279 loc) · 12.1 KB
/
app.ts
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import "@babylonjs/core/Debug/debugLayer";
import "@babylonjs/inspector";
import "@babylonjs/loaders/glTF";
import { Engine, Scene, ArcRotateCamera, Vector3, HemisphericLight, Mesh, MeshBuilder, FreeCamera, Color4, StandardMaterial, Color3, PointLight, ShadowGenerator, Quaternion, Matrix } from "@babylonjs/core";
import { AdvancedDynamicTexture, Button, Control } from "@babylonjs/gui";
import { Environment } from "./environment";
import { Player } from "./characterController";
import { PlayerInput } from "./inputController";
enum State { START = 0, GAME = 1, LOSE = 2, CUTSCENE = 3 }
class App {
// General Entire Application
private _scene: Scene;
private _canvas: HTMLCanvasElement;
private _engine: Engine;
//Game State Related
public assets;
private _input: PlayerInput;
private _environment;
private _player: Player;
//Scene - related
private _state: number = 0;
private _gamescene: Scene;
private _cutScene: Scene;
constructor() {
this._canvas = this._createCanvas();
// initialize babylon scene and engine
this._engine = new Engine(this._canvas, true);
this._scene = new Scene(this._engine);
// hide/show the Inspector
window.addEventListener("keydown", (ev) => {
// Shift+Ctrl+Alt+I
if (ev.shiftKey && ev.ctrlKey && ev.altKey && ev.keyCode === 73) {
if (this._scene.debugLayer.isVisible()) {
this._scene.debugLayer.hide();
} else {
this._scene.debugLayer.show();
}
}
});
// run the main render loop
this._main();
}
private _createCanvas(): HTMLCanvasElement {
//Commented out for development
// document.documentElement.style["overflow"] = "hidden";
// document.documentElement.style.overflow = "hidden";
// document.documentElement.style.width = "100%";
// document.documentElement.style.height = "100%";
// document.documentElement.style.margin = "0";
// document.documentElement.style.padding = "0";
// document.body.style.overflow = "hidden";
// document.body.style.width = "100%";
// document.body.style.height = "100%";
// document.body.style.margin = "0";
// document.body.style.padding = "0";
//create the canvas html element and attach it to the webpage
this._canvas = document.createElement("canvas");
this._canvas.style.width = "100%";
this._canvas.style.height = "100%";
this._canvas.id = "gameCanvas";
document.body.appendChild(this._canvas);
return this._canvas;
}
private async _main(): Promise<void> {
await this._goToStart();
// Register a render loop to repeatedly render the scene
this._engine.runRenderLoop(() => {
switch (this._state) {
case State.START:
this._scene.render();
break;
case State.CUTSCENE:
this._scene.render();
break;
case State.GAME:
this._scene.render();
break;
case State.LOSE:
this._scene.render();
break;
default: break;
}
});
//resize if the screen is resized/rotated
window.addEventListener('resize', () => {
this._engine.resize();
});
}
private async _goToStart(){
this._engine.displayLoadingUI();
this._scene.detachControl();
let scene = new Scene(this._engine);
scene.clearColor = new Color4(0,0,0,1);
let camera = new FreeCamera("camera1", new Vector3(0, 0, 0), scene);
camera.setTarget(Vector3.Zero());
//create a fullscreen ui for all of our GUI elements
const guiMenu = AdvancedDynamicTexture.CreateFullscreenUI("UI");
guiMenu.idealHeight = 720; //fit our fullscreen ui to this height
//create a simple button
const startBtn = Button.CreateSimpleButton("start", "PLAY");
startBtn.width = 0.2
startBtn.height = "40px";
startBtn.color = "white";
startBtn.top = "-14px";
startBtn.thickness = 0;
startBtn.verticalAlignment = Control.VERTICAL_ALIGNMENT_BOTTOM;
guiMenu.addControl(startBtn);
//this handles interactions with the start button attached to the scene
startBtn.onPointerDownObservable.add(() => {
this._goToCutScene();
scene.detachControl(); //observables disabled
});
//--SCENE FINISHED LOADING--
await scene.whenReadyAsync();
this._engine.hideLoadingUI();
//lastly set the current state to the start state and set the scene to the start scene
this._scene.dispose();
this._scene = scene;
this._state = State.START;
}
private async _goToCutScene(): Promise<void> {
this._engine.displayLoadingUI();
//--SETUP SCENE--
//dont detect any inputs from this ui while the game is loading
this._scene.detachControl();
this._cutScene = new Scene(this._engine);
let camera = new FreeCamera("camera1", new Vector3(0, 0, 0), this._cutScene);
camera.setTarget(Vector3.Zero());
this._cutScene.clearColor = new Color4(0, 0, 0, 1);
//--GUI--
const cutScene = AdvancedDynamicTexture.CreateFullscreenUI("cutscene");
//--PROGRESS DIALOGUE--
const next = Button.CreateSimpleButton("next", "NEXT");
next.color = "white";
next.thickness = 0;
next.verticalAlignment = Control.VERTICAL_ALIGNMENT_BOTTOM;
next.horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_RIGHT;
next.width = "64px";
next.height = "64px";
next.top = "-3%";
next.left = "-12%";
cutScene.addControl(next);
next.onPointerUpObservable.add(() => {
this._goToGame();
})
//--WHEN SCENE IS FINISHED LOADING--
await this._cutScene.whenReadyAsync();
this._engine.hideLoadingUI();
this._scene.dispose();
this._state = State.CUTSCENE;
this._scene = this._cutScene;
//--START LOADING AND SETTING UP THE GAME DURING THIS SCENE--
var finishedLoading = false;
await this._setUpGame().then(res =>{
finishedLoading = true;
});
}
private async _setUpGame() {
let scene = new Scene(this._engine);
this._gamescene = scene;
//--CREATE ENVIRONMENT--
const environment = new Environment(scene);
this._environment = environment;
await this._environment.load(); //environment
await this._loadCharacterAssets(scene);
}
private async _loadCharacterAssets(scene){
async function loadCharacter(){
//collision mesh
const outer = MeshBuilder.CreateBox("outer", { width: 2, depth: 1, height: 3 }, scene);
outer.isVisible = false;
outer.isPickable = false;
outer.checkCollisions = true;
//move origin of box collider to the bottom of the mesh (to match player mesh)
outer.bakeTransformIntoVertices(Matrix.Translation(0, 1.5, 0))
//for collisions
// outer.ellipsoid = new Vector3(1, 1.5, 1);
// outer.ellipsoidOffset = new Vector3(0, 1.5, 0);
outer.rotationQuaternion = new Quaternion(0, 1, 0, 0); // rotate the player mesh 180 since we want to see the back of the player
var box = MeshBuilder.CreateBox("Small1", { width: 0.5, depth: 0.5, height: 0.25, faceColors: [new Color4(0,0,0,1), new Color4(0,0,0,1), new Color4(0,0,0,1), new Color4(0,0,0,1),new Color4(0,0,0,1), new Color4(0,0,0,1)] }, scene);
box.position.y = 1.5;
box.position.z = 1;
var body = Mesh.CreateCylinder("body", 3, 2,2,0,0,scene);
var bodymtl = new StandardMaterial("red",scene);
bodymtl.diffuseColor = new Color3(.8,.5,.5);
body.material = bodymtl;
body.isPickable = false;
body.bakeTransformIntoVertices(Matrix.Translation(0, 1.5, 0)); // simulates the imported mesh's origin
//parent the meshes
box.parent = body;
body.parent = outer;
return {
mesh: outer as Mesh
}
}
return loadCharacter().then(assets=> {
this.assets = assets;
})
}
private async _initializeGameAsync(scene): Promise<void> {
//temporary light to light the entire scene
var light0 = new HemisphericLight("HemiLight", new Vector3(0, 1, 0), scene);
const light = new PointLight("sparklight", new Vector3(0, 0, 0), scene);
light.diffuse = new Color3(0.08627450980392157, 0.10980392156862745, 0.15294117647058825);
light.intensity = 35;
light.radius = 1;
const shadowGenerator = new ShadowGenerator(1024, light);
shadowGenerator.darkness = 0.4;
//Create the player
this._player = new Player(this.assets, scene, shadowGenerator, this._input);
const camera = this._player.activatePlayerCamera();
}
private async _goToGame(){
//--SETUP SCENE--
this._scene.detachControl();
let scene = this._gamescene;
scene.clearColor = new Color4(0.01568627450980392, 0.01568627450980392, 0.20392156862745098); // a color that fit the overall color scheme better
//--GUI--
const playerUI = AdvancedDynamicTexture.CreateFullscreenUI("UI");
//dont detect any inputs from this ui while the game is loading
scene.detachControl();
//create a simple button
const loseBtn = Button.CreateSimpleButton("lose", "LOSE");
loseBtn.width = 0.2
loseBtn.height = "40px";
loseBtn.color = "white";
loseBtn.top = "-14px";
loseBtn.thickness = 0;
loseBtn.verticalAlignment = Control.VERTICAL_ALIGNMENT_BOTTOM;
playerUI.addControl(loseBtn);
//this handles interactions with the start button attached to the scene
loseBtn.onPointerDownObservable.add(() => {
this._goToLose();
scene.detachControl(); //observables disabled
});
//--INPUT--
this._input = new PlayerInput(scene); //detect keyboard/mobile inputs
//primitive character and setting
await this._initializeGameAsync(scene);
//--WHEN SCENE FINISHED LOADING--
await scene.whenReadyAsync();
scene.getMeshByName("outer").position = new Vector3(0,3,0);
//get rid of start scene, switch to gamescene and change states
this._scene.dispose();
this._state = State.GAME;
this._scene = scene;
this._engine.hideLoadingUI();
//the game is ready, attach control back
this._scene.attachControl();
}
private async _goToLose(): Promise<void> {
this._engine.displayLoadingUI();
//--SCENE SETUP--
this._scene.detachControl();
let scene = new Scene(this._engine);
scene.clearColor = new Color4(0, 0, 0, 1);
let camera = new FreeCamera("camera1", new Vector3(0, 0, 0), scene);
camera.setTarget(Vector3.Zero());
//--GUI--
const guiMenu = AdvancedDynamicTexture.CreateFullscreenUI("UI");
const mainBtn = Button.CreateSimpleButton("mainmenu", "MAIN MENU");
mainBtn.width = 0.2;
mainBtn.height = "40px";
mainBtn.color = "white";
guiMenu.addControl(mainBtn);
//this handles interactions with the start button attached to the scene
mainBtn.onPointerUpObservable.add(() => {
this._goToStart();
});
//--SCENE FINISHED LOADING--
await scene.whenReadyAsync();
this._engine.hideLoadingUI(); //when the scene is ready, hide loading
//lastly set the current state to the lose state and set the scene to the lose scene
this._scene.dispose();
this._scene = scene;
this._state = State.LOSE;
}
}
new App();