/
WorldManager.ts
98 lines (81 loc) · 3.07 KB
/
WorldManager.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
/// <reference path='../../reference.ts'/>
namespace app.game_engine {
'use strict';
export class WorldManager {
private activeLight: boolean;
private businessLight: engine.common.business.Light;
private newColor: app.models.Light;
private isRemoteChange: boolean;
public RenderLoop(scene: BABYLON.Scene): void {
if (this.IsUpdating(scene) && !this.isRemoteChange) {
this.ManageLight(scene);
}
scene.lights[1].diffuse.b = this.newColor.color.b;
scene.lights[1].diffuse.r = this.newColor.color.r;
scene.lights[1].diffuse.g = this.newColor.color.g;
scene.lights[1].setEnabled(this.newColor.state);
this.isRemoteChange = false;
scene.render();
}
public CreateScene(scene: BABYLON.Scene): void {
let that: WorldManager = this;
scene.cameras.pop();
scene.lights[1].setEnabled(false);
scene.meshes.forEach((mesh: BABYLON.Mesh) => {
mesh.isPickable = false;
// l'interrupteur de la scene
if (mesh.name === 'Box001') {
mesh.isPickable = true;
mesh.actionManager = new BABYLON.ActionManager(scene);
mesh.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnLeftPickTrigger, function(evt: BABYLON.ActionEvent): void {
scene.beginAnimation(mesh, 0, 100, false, 10, () => {
that.activeLight = !that.activeLight;
that.ManageLight(scene);
});
}));
}
});
let camera: BABYLON.TouchCamera = new BABYLON.TouchCamera('TouchCamera', new BABYLON.Vector3(-72, 103, 228), scene);
camera.rotation = new BABYLON.Vector3(0, Math.PI, 0);
scene.setActiveCameraByName('TouchCamera');
}
public constructor(newColor: app.models.Light, businessLight: engine.common.business.Light) {
let that: WorldManager = this;
this.activeLight = false;
this.businessLight = businessLight;
this.newColor = newColor;
this.isRemoteChange = false;
this.businessLight.realData('stateLight', function(data: models.Light): void {
that.isRemoteChange = true;
that.newColor.color.r = data.color.r;
that.newColor.color.g = data.color.g;
that.newColor.color.b = data.color.b;
that.newColor.lightId = data.lightId;
that.newColor.state = data.state;
that.activeLight = data.state;
})
}
private IsUpdating(scene: BABYLON.Scene): boolean {
return !(scene.lights[1].diffuse.b === this.newColor.color.b &&
scene.lights[1].diffuse.r === this.newColor.color.r &&
scene.lights[1].diffuse.g === this.newColor.color.g
&& scene.lights[1].isEnabled() === this.newColor.state
);
}
private ManageLight(scene: BABYLON.Scene): void {
let lightModel: app.models.Light = new app.models.Light(1);
lightModel.color.r = this.newColor.color.r;
lightModel.color.g = this.newColor.color.g;
lightModel.color.b = this.newColor.color.b;
if (this.activeLight) {
this.businessLight.on(lightModel).then(function(data: models.ApiResult) {
scene.lights[1].setEnabled(data.result);
});
} else {
this.businessLight.off(lightModel.lightId).then(function(data: models.ApiResult) {
scene.lights[1].setEnabled(data.result);
});
}
}
}
}