-
Notifications
You must be signed in to change notification settings - Fork 61
/
environment.ts
105 lines (90 loc) · 4.39 KB
/
environment.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
import { Scene, Mesh, Vector3, SceneLoader, TransformNode, PBRMetallicRoughnessMaterial, ExecuteCodeAction, ActionManager, Texture, Color3 } from "@babylonjs/core";
import { Lantern } from "./lantern";
import { Player } from "./characterController";
export class Environment {
private _scene: Scene;
//Meshes
private _lanternObjs: Array<Lantern>; //array of lanterns that need to be lit
private _lightmtl: PBRMetallicRoughnessMaterial; // emissive texture for when lanterns are lit
constructor(scene: Scene) {
this._scene = scene;
this._lanternObjs = [];
//create emissive material for when lantern is lit
const lightmtl = new PBRMetallicRoughnessMaterial("lantern mesh light", this._scene);
lightmtl.emissiveTexture = new Texture("/textures/litLantern.png", this._scene, true, false);
lightmtl.emissiveColor = new Color3(0.8784313725490196, 0.7568627450980392, 0.6235294117647059);
this._lightmtl = lightmtl;
}
public async load() {
// var ground = Mesh.CreateBox("ground", 24, this._scene);
// ground.scaling = new Vector3(1,.02,1);
const assets = await this._loadAsset();
//Loop through all environment meshes that were imported
assets.allMeshes.forEach(m => {
m.receiveShadows = true;
m.checkCollisions = true;
});
//--LANTERNS--
assets.lantern.isVisible = false; //original mesh is not visible
//transform node to hold all lanterns
const lanternHolder = new TransformNode("lanternHolder", this._scene);
for (let i = 0; i < 22; i++) {
//Mesh Cloning
let lanternInstance = assets.lantern.clone("lantern" + i); //bring in imported lantern mesh & make clones
lanternInstance.isVisible = true;
lanternInstance.setParent(lanternHolder);
//Create the new lantern object
let newLantern = new Lantern(this._lightmtl, lanternInstance, this._scene, assets.env.getChildTransformNodes(false).find(m => m.name === "lantern " + i).getAbsolutePosition());
this._lanternObjs.push(newLantern);
}
//dispose of original mesh and animation group that were cloned
assets.lantern.dispose();
}
//Load all necessary meshes for the environment
public async _loadAsset() {
const result = await SceneLoader.ImportMeshAsync(null, "./models/", "envSetting.glb", this._scene);
let env = result.meshes[0];
let allMeshes = env.getChildMeshes();
//loads lantern mesh
const res = await SceneLoader.ImportMeshAsync("", "./models/", "lantern.glb", this._scene);
//extract the actual lantern mesh from the root of the mesh that's imported, dispose of the root
let lantern = res.meshes[0].getChildren()[0];
lantern.parent = null;
res.meshes[0].dispose();
return {
env: env, //reference to our entire imported glb (meshes and transform nodes)
allMeshes: allMeshes, // all of the meshes that are in the environment
lantern: lantern as Mesh
}
}
public checkLanterns(player: Player) {
if (!this._lanternObjs[0].isLit) {
this._lanternObjs[0].setEmissiveTexture();
}
this._lanternObjs.forEach(lantern => {
player.mesh.actionManager.registerAction(
new ExecuteCodeAction(
{
trigger: ActionManager.OnIntersectionEnterTrigger,
parameter: lantern.mesh
},
() => {
//if the lantern is not lit, light it up & reset sparkler timer
if (!lantern.isLit && player.sparkLit) {
player.lanternsLit += 1; //increment the lantern count
lantern.setEmissiveTexture(); //"light up" the lantern
//reset the sparkler
player.sparkReset = true;
player.sparkLit = true;
}
//if the lantern is lit already, reset the sparkler
else if (lantern.isLit) {
player.sparkReset = true;
player.sparkLit = true;
}
}
)
);
});
}
}