-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgame.js
36 lines (32 loc) · 876 Bytes
/
game.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
const totoroEl = document.querySelector('#totoro');
const sceneEl = document.querySelector('a-scene');
const scoreEl = document.querySelector('#score');
function randomPosition() {
return {
x: (Math.random() - 0.5) * 20,
y: 0,
z: (Math.random() - 0.5) * 20
};
}
let score = 0;
function updateScore() {
scoreEl.setAttribute('value', `Score: ${score}`);
}
function createClone() {
const clone = totoroEl.cloneNode();
clone.setAttribute('position', randomPosition());
clone.addEventListener('click', () => {
clone.dispatchEvent(new Event('collect'));
});
clone.addEventListener('animationcomplete', () => {
clone.setAttribute('position', randomPosition());
clone.setAttribute('scale', '0.1 0.1 0.1');
score++;
updateScore();
});
sceneEl.appendChild(clone);
}
for (let i = 0; i < 15; i++) {
createClone();
}
updateScore();