-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
71 lines (62 loc) · 2.17 KB
/
script.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
let nextLevel = 'index.html';
const shoot = () => {
const bullet = document.createElement('a-sphere');
let pos = myCamera.getAttribute("position");
bullet.setAttribute("position", pos);
bullet.setAttribute("velocity", getDirection(myCamera,30));
bullet.setAttribute("dynamic-body", true);
bullet.setAttribute("radius", 0.5);
bullet.setAttribute("color", "red");
myScene.appendChild(bullet);
bullet.addEventListener('collide', shootCollided);
};
const teleport = () => {
const bullet = document.createElement('a-torus');
let pos = myCamera.getAttribute("position");
bullet.setAttribute("position", pos);
bullet.setAttribute("velocity", getDirection(myCamera,30));
bullet.setAttribute("dynamic-body", true);
bullet.setAttribute("radius", 0.5);
bullet.setAttribute("arc", 360);
bullet.setAttribute("radius-tubular", 0.1);
bullet.setAttribute("rotation", { x: 90, y: 90, z:0});
bullet.setAttribute("color", "limegreen");
myScene.appendChild(bullet);
bullet.addEventListener('collide', teleportCollided);
}
const shootCollided = event => {
if(event.detail.body.el.id === 'floor')
{
event.detail.target.el.removeEventListener('collide', shootCollided);
myScene.removeChild(event.detail.target.el);
}
else if (event.detail.body.el.className === 'target')
{
event.detail.target.el.removeEventListener('collide', shootCollided);
myScene.removeChild(event.detail.target.el);
myScene.removeChild(event.detail.body.el);
}
if(document.querySelectorAll('.target').length === 0)
{
location.href = nextLevel;
}
};
const teleportCollided = event => {
if (event.detail.body.el.id === 'floor')
{
event.detail.target.el.removeEventListener('collide', teleportCollided);
let pos = event.detail.target.el.getAttribute('position');
myScene.removeChild(event.detail.target.el);
myCamera.setAttribute('position', { x: pos.x, y: 2, z: pos.z});
}
}
document.onkeydown = event => {
if(event.which == 32) // Spacebar
{
shoot();
}
else if (event.which == 67) // C key
{
teleport();
}
};