forked from talkol/spiderman
-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.js
250 lines (219 loc) · 8.5 KB
/
map.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
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
AFRAME.registerComponent("map", {
schema: {
tilesUpdateFrequency: { type: "int", default: 5 },
skyUpdateFrequency: { type: "int", default: 59 },
tileSize: { type: "number", default: 160 },
viewDistance: { type: "int", default: 5 },
crystalRespawnTiles: { type: "int", default: 2 },
crystalHitDistance: { type: "number", default: 8 }
},
init: function() {
this.frame = 0;
this.XRStart = 0;
this.playerTile = { x: 1000, y: 1000 };
this.playerAdjacentTileIds = [];
this.tileOffset = 0.5 * this.data.tileSize;
this.existingTiles = {};
this.tileToElement = {};
this.userID = 0;
this.crystal = document.createElement("a-entity");
this.crystal.setAttribute("animation", "property: rotation; to: 0 360 0; loop: true; dur: 5000; easing: linear;");
this.crystal.setAttribute(
"sound__proximity",
"src: #sound-crystal; loop: true; autoplay: true; volume: 2; rolloffFactor: 0.1;"
);
this.crystal.setAttribute("sound__hit", "src: #sound-hit-crystal; positional: false;");
this.crystal.setAttribute("position", "1000 1000 1000");
let model = document.createElement("a-entity");
model.setAttribute("gltf-model", "#crystal");
model.setAttribute("scale", "1.5 1.5 1.5");
this.crystal.appendChild(model);
this.el.appendChild(this.crystal);
document.querySelector('a-scene').addEventListener('enter-vr', function () {
let model_name = 'unknown';
if (navigator.userAgent.indexOf('Quest 2') !== -1)
model_name = 'Quest 2';
else if (navigator.userAgent.indexOf('Quest 3') !== -1)
model_name = 'Quest 3';
else if (navigator.userAgent.indexOf("Quest Pro") !== -1)
model_name = 'Quest Pro';
else
model_name = navigator.userAgent;
gtag('event', 'enter-vr', { 'model': model_name});
if (this.xrSession.updateTargetFrameRate) {
this.xrSession.updateTargetFrameRate(model_name === 'Quest 3' ? 90 : 72);
}
if (model_name === 'Quest 3') {
this.renderer.xr.setFoveation(0);
}
fetch("https://cvs95jiqa7.execute-api.us-east-1.amazonaws.com/IncreaseUsageCounter?key=citysling_entervr", {
method: "GET",
mode: "cors"}).then((data) => {
data.json().then((json) => {
this.setAttribute("UserID", json);
})
});
this.XRStart = Date.now();
});
document.querySelector('a-scene').addEventListener('exit-vr', function () {
let d = Math.floor((Date.now() - this.XRStart)/1000);
let timeSpent = "unknown";
if (d < 30)
timeSpent = "< 30 seconds";
else if (d < 60)
timeSpent = "< 1 minute";
else if (d < 300)
timeSpent = "< 5 minutes";
else if (d < 600)
timeSpent = "< 10 minutes";
else if (d < 1200)
timeSpent = "< 20 minutes";
else
timeSpent = Math.floor(d/60) + " minutes";
gtag('event', 'exit-vr', { time_spent: timeSpent });
fetch("https://cvs95jiqa7.execute-api.us-east-1.amazonaws.com/IncreaseUsageCounter?key=citysling_leavevr", {
method: "GET",
mode: "cors"});
this.XRStart = 0;
});
this.throttledEmitComponentChanged = function emitChange () {};
},
play: function() {
this.sky = document.getElementById("sky");
this.populateMap(0, 0);
},
distanceXZ: function(vec1, vec2) {
let dx = vec1.x - vec2.x;
let dz = vec1.z - vec2.z;
return Math.sqrt(dx * dx + dz * dz);
},
playCollideSound: function() {
this.crystal.components.sound__hit.playSound();
},
updateMap: function(playerWorldPosition, playerVelocity, velocityRaycaster, webRaycasters) {
this.frame++;
// see if we hit the crystal
let hitCrystal = false;
let crystalDistance = playerWorldPosition.distanceTo(this.crystal.object3D.position);
if (crystalDistance < this.data.crystalHitDistance) {
this.crystal.object3D.position.lerp(playerWorldPosition, 1 / (1 + crystalDistance));
if (playerWorldPosition.distanceTo(this.crystal.object3D.position) < 1) {
hitCrystal = true;
gtag('event', 'hitCrystal');
this.playCollideSound();
this.moveCrystal(playerWorldPosition, playerVelocity);
}
}
// move the sky
if (this.sky && this.frame % this.data.skyUpdateFrequency == 0) {
this.sky.object3D.position.x = playerWorldPosition.x;
this.sky.object3D.position.z = playerWorldPosition.z;
}
// update the tiles
if (this.frame % this.data.tilesUpdateFrequency == 0) {
// do we need to spawn a new crystal
let crystalXZDistance = this.distanceXZ(playerWorldPosition, this.crystal.object3D.position);
if (crystalXZDistance > this.data.crystalRespawnTiles * this.data.tileSize * 1.5) {
this.moveCrystal(playerWorldPosition, playerVelocity);
}
// was there a change in player tile x,y
let x = Math.floor(playerWorldPosition.x / this.data.tileSize);
let y = Math.floor(playerWorldPosition.z / this.data.tileSize);
if (x == this.playerTile.x && y == this.playerTile.y) return hitCrystal;
this.playerTile.x = x;
this.playerTile.y = y;
// player tile changed
this.populateMap(this.playerTile.x, this.playerTile.y);
// update the velocity raycaster to collide inside current tile only
velocityRaycaster.data.objects = `#${this.getTileId(this.playerTile.x, this.playerTile.y)} > .collidable`;
velocityRaycaster.refreshObjects();
// update the web raycasters
let adjacentSelector = this.playerAdjacentTileIds.map(x => `#${x} > .collidable`).join(" , ");
for (let i = 0; i < webRaycasters.length; i++) {
webRaycasters[i].data.objects = adjacentSelector;
webRaycasters[i].refreshObjects();
}
}
return hitCrystal;
},
populateMap: function(x, y) {
// calculate the needed tiles
let neededTiles = {};
for (let d = 0; d < this.data.viewDistance; d++) {
for (let i = 0; i <= d; i++) {
neededTiles[this.getTileId(x + i, y + d - i)] = {
x: x + i,
y: y + d - i
};
neededTiles[this.getTileId(x - i, y + d - i)] = {
x: x - i,
y: y + d - i
};
neededTiles[this.getTileId(x + i, y - d + i)] = {
x: x + i,
y: y - d + i
};
neededTiles[this.getTileId(x - i, y - d + i)] = {
x: x - i,
y: y - d + i
};
}
if (d == 2) {
this.playerAdjacentTileIds = Object.keys(neededTiles);
}
}
// sync the needed list with the existing tiles
for (let pos in neededTiles) {
if (this.existingTiles[pos]) {
delete this.existingTiles[pos];
} else {
this.tileToElement[pos] = this.addTile(neededTiles[pos].x, neededTiles[pos].y);
}
}
for (let pos in this.existingTiles) {
this.removeTile(this.tileToElement[pos]);
delete this.tileToElement[pos];
}
this.existingTiles = neededTiles;
},
moveCrystal: function(playerWorldPosition, playerVelocity) {
let newPosition = new THREE.Vector3(
Math.round(playerWorldPosition.x / this.data.tileSize) * this.data.tileSize,
0,
Math.round(playerWorldPosition.z / this.data.tileSize) * this.data.tileSize
);
if (Math.abs(playerVelocity.x) > Math.abs(playerVelocity.z)) {
newPosition.x += Math.sign(playerVelocity.x) * this.data.crystalRespawnTiles * this.data.tileSize;
} else {
newPosition.z += (Math.sign(playerVelocity.z) || 1) * this.data.crystalRespawnTiles * this.data.tileSize;
}
newPosition.x += Math.random() * 40 - 20;
newPosition.y += Math.random() * 100 + 10;
newPosition.z += Math.random() * 40 - 20;
this.crystal.setAttribute("animation__appear", "enabled: false;");
this.crystal.setAttribute(
"animation__appear",
"property: scale; from: 0 0 0; to: 1 1 1; dur: 2000; easing: easeInOutElastic; enabled: true;"
);
this.crystal.object3D.position.copy(newPosition);
},
addTile: function(x, y) {
let tile = this.el.components.pool.requestEntity();
tile.setAttribute("id", this.getTileId(x, y));
if (tile.components["position"] !== undefined) {
tile.components["position"].throttledEmitComponentChanged = function emitChange () {};
}
tile.setAttribute("position", {
x: this.tileOffset + x * this.data.tileSize,
y: 0,
z: this.tileOffset + y * this.data.tileSize
});
return tile;
},
removeTile: function(tile) {
this.el.components.pool.returnEntity(tile);
},
getTileId: function(x, y) {
return `tile${x}_${y}`;
}
});