-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
211 lines (177 loc) · 6.66 KB
/
utils.js
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
// util class containing results of PossibleSight Method
class sigthInfo {
constructor(inSight, direction, vecFrom){
this.inSight = inSight;
this.direction = direction;
this.vecFrom = vecFrom;
}
}
var rayHelperC;
// method that checks if two objects are close to a certain distance and no objects are in bewteen them
function isInCloseSight(observer, lookedAt, excused, distance, adjustHeight = null){
if(adjustHeight != null){
var corPos = observer.absolutePosition.clone();
corPos.y += adjustHeight;
}else
var corPos = observer.absolutePosition.clone();
var vecFrom = lookedAt.absolutePosition.subtract(corPos);
var vLength = vecFrom.length();
var vecFrom = BABYLON.Vector3.Normalize(vecFrom);
if(observer.name == "bambina"){
observer.getChildMeshes().forEach(e =>{
excused.push(e);
});
}
var lookedAtList = [lookedAt];
if(lookedAt.name == "torchMain"){
lookedAt.getChildMeshes().forEach(e =>{
lookedAtList.push(e);
});
}
if(lookedAt.name == "chiave0"){
lookedAt.getChildMeshes().forEach(e =>{
lookedAtList.push(e);
});
}
if(vLength <= distance){
excused.push(observer);
//Ray
var ray = new BABYLON.Ray(corPos, vecFrom, distance);
var hits = scene.multiPickWithRay(ray);
if (hits){
hits = sortPickedMeshesByDistance(hits, 0);
for (var i = hits.length -1; i >= 0; i--) {
if(lookedAtList.includes(hits[i].pickedMesh)){
return true;
}
}
}
}
return false;
}
var rayHelper;
// method that checks if, given two objects and a forward direction of the observer, the lookedAt is in possible sight
function isInPossibleSight(observer, lookedAt, forward, targetCos, mesh = true){
var direction = directionFrom(forward, observer);
if (mesh){
var vecFrom = BABYLON.Vector3.Normalize(lookedAt.absolutePosition.subtract(observer.absolutePosition));
}else{
var vecFrom = BABYLON.Vector3.Normalize(lookedAt.subtract(observer.absolutePosition));
}
var cos = cosBetween(direction, vecFrom);
var inS = false;
if(cos >= targetCos){
inS = true;
}
var inSight = new sigthInfo(inS, direction, vecFrom);
return inSight;
}
function cosBetween(vector1, vector2){
var cos = BABYLON.Vector3.Dot(vector1, vector2);
return cos;
}
//calculate the actual direction vector
function directionFrom(forward, mesh){
forward = vecToLocal(forward, mesh);
var direction = forward.subtract(mesh.absolutePosition);
direction = BABYLON.Vector3.Normalize(direction);
return direction;
}
//calculate vector relative to mesh position
function vecToLocal(vector, mesh){
var m = mesh.getWorldMatrix();
var v = BABYLON.Vector3.TransformCoordinates(vector, m);
return v;
}
function removeSubstring(originalString, substringToRemove) {
return originalString.replace(substringToRemove, "");
}
// sort the picked mesh array according to distance (closest mesh first)
function sortPickedMeshesByDistance(hit, mode) {
if(mode == 0){
return hit.sort(function(a, b) { //DESC
return parseFloat(b.distance) - parseFloat(a.distance);
});
}else{
return hit.sort(function(a, b) { //ASC
return parseFloat(a.distance) - parseFloat(b.distance);
});
}
}
function sleep(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}
function updateImportedBoundingBox(parent){
let childMeshes = parent.getChildMeshes();
let min = childMeshes[0].getBoundingInfo().boundingBox.minimumWorld;
let max = childMeshes[0].getBoundingInfo().boundingBox.maximumWorld;
for(let i=0; i<childMeshes.length; i++){
let meshMin = childMeshes[i].getBoundingInfo().boundingBox.minimumWorld;
let meshMax = childMeshes[i].getBoundingInfo().boundingBox.maximumWorld;
min = BABYLON.Vector3.Minimize(min, meshMin);
max = BABYLON.Vector3.Maximize(max, meshMax);
}
parent.setBoundingInfo(new BABYLON.BoundingInfo(min, max));
return parent;
}
function setBoundingBox(mesh, height, lenght, depth){
mesh.setBoundingInfo(new BABYLON.BoundingInfo(new BABYLON.Vector3(lenght,lenght,lenght), new BABYLON.Vector3(height,height,height)));
return mesh;
}
function showMessage(text, milliseconds) {
// Metti il testo nella casella e mostra l'elemento
document.getElementById("custom-text").innerHTML = text;
document.getElementById("text-box").removeAttribute("hidden");
// Dopo un tot di tempo, nascondi di nuovo l'elemento
setTimeout(function() {
document.getElementById("text-box").setAttribute("hidden", null);
}, milliseconds);
}
function showMessageFixed(text) {
// Metti il testo nella casella e mostra l'elemento
document.getElementById("custom-text").innerHTML = text;
document.getElementById("text-box").removeAttribute("hidden");
}
function hideMessage() {
document.getElementById("text-box").setAttribute("hidden", null);
}
// Questa serve a rendere pickable/non-pickable le mesh importate, che sono sempre costituite da piu' parti
function setPickableChildren(mesh, bool) {
mesh.isPickable = bool;
let ch = mesh.getChildMeshes();
for (let i = 0; i < ch.length; i++)
ch[i].isPickable = bool;
}
// Questa serve a rendere visibili/invisibili le mesh importate, che sono sempre costituite da piu' parti
function setVisibilityChildren(mesh, value) {
mesh.visibility = value;
let ch = mesh.getChildMeshes();
for (let i = 0; i < ch.length; i++)
ch[i].visibility = value;
}
// Per aggiornare il numero di vite sullo schermo
function showLives(number) {
let s = "LIVES: ";
for (let i=0; i<number; i++)
s += " ♡"
document.getElementById("lives").innerHTML = s;
}
function showHoldingKey() {
let s = '<img width="15px" height="15px" style="position:relative; top:2px; left:5px;" src="images/key.png"/>'
document.getElementById("key_symbol").innerHTML = s;
document.getElementById("key_symbol").removeAttribute("hidden");
}
function showHoldingTorch(bool) {
if (bool) {
let s = '<img width="15px" height="15px" style="position:relative; top:2px; left:5px;" src="images/torch.png"/>'
document.getElementById("torch_symbol").innerHTML = s;
document.getElementById("torch_symbol").removeAttribute("hidden");
}
else {
document.getElementById("torch_symbol").setAttribute("hidden", null);
}
}