-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
180 lines (168 loc) · 6.7 KB
/
Copy pathscript.js
File metadata and controls
180 lines (168 loc) · 6.7 KB
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
let currentCounter = 121;
let restarWithKey = false;
let first = true;
const startAudio = new Audio("assets/start.mp3");
const killAudio = new Audio("assets/kill.mp3");
const loseAudio = new Audio("assets/lose.mp3");
const wonAudio = new Audio("assets/won.mp3");
const counterTimer = setInterval(() => {
currentCounter--;
const counterElement = document.getElementById("counter");
counterElement.setAttribute(
"text",
`width:50;value:Kill them All in ${currentCounter}s;color:#c19f9f;height:100`
);
if (currentCounter == 0) {
lost();
}
}, 1000);
let currentPosition = "0 1 0";
myCamera.addEventListener("collide", (event) => {
console.log(event);
});
const shoot = () => {
const bullet = document.createElement("a-sphere");
let pos = myCamera.getAttribute("position");
bullet.setAttribute("position", pos);
bullet.setAttribute("velocity", getDirection(myCamera, 50));
bullet.setAttribute("dynamic-body", true);
bullet.setAttribute("radius", 0.00001);
bullet.setAttribute("src", "");
bullet.setAttribute("transparent", true);
myScene.appendChild(bullet);
bullet.addEventListener("collide", shootCollided);
};
const shootCollided = (event) => {
if (event.detail.body.el.id === "floor") {
console.log("Hit the floor");
event.detail.target.el.removeEventListener("collide", shootCollided);
myScene.removeChild(event.detail.target.el);
} else if (event.detail.body.el.className === "target") {
console.log("Hit the target!");
event.detail.target.el.removeEventListener("collide", shootCollided);
const id = event.detail.body.el.id;
const oldAmong = document.getElementById("among" + id);
const newAmong = document.getElementById("amongDead" + id);
myScene.removeChild(event.detail.target.el);
myScene.removeChild(event.detail.body.el);
oldAmong.setAttribute("visible", false);
newAmong.setAttribute("visible", true);
killAudio.play();
}
if (document.querySelectorAll(".target").length === 0) {
console.log("You win!");
won();
}
};
const lost = () => {
const AmongDead1 = document.getElementById("amongDead1");
const AmongDead2 = document.getElementById("amongDead2");
const AmongDead3 = document.getElementById("amongDead3");
const AmongDead4 = document.getElementById("amongDead4");
const AmongDead5 = document.getElementById("amongDead5");
const Among1 = document.getElementById("among1");
const Among2 = document.getElementById("among2");
const Among3 = document.getElementById("among3");
const Among4 = document.getElementById("among4");
const Among5 = document.getElementById("among5");
AmongDead1.setAttribute("visible", false);
AmongDead2.setAttribute("visible", false);
AmongDead3.setAttribute("visible", false);
AmongDead4.setAttribute("visible", false);
AmongDead5.setAttribute("visible", false);
Among1.setAttribute("visible", true);
Among2.setAttribute("visible", true);
Among3.setAttribute("visible", true);
Among4.setAttribute("visible", true);
Among5.setAttribute("visible", true);
Among1.setAttribute("position", "-2.808 0.110 -5.074");
Among2.setAttribute("position", "-1.271 0.110 -6.084");
Among3.setAttribute("position", "0.082 0.120 -7.669");
Among4.setAttribute("position", "2.121 0.110 -7.971");
Among5.setAttribute("position", "3.801 0.110 -4.477");
Among1.setAttribute("rotation", "0 40.000 0");
Among2.setAttribute("rotation", "0 32.000 0");
Among3.setAttribute("rotation", "0 15.000 0");
Among4.setAttribute("rotation", "0 -10.000 0");
Among5.setAttribute("rotation", "0 -41.000 0");
const counterElement = document.getElementById("counter");
counterElement.setAttribute("position", "23.325 9.272 -35.792");
counterElement.setAttribute(
"text",
`width:50;value:You Lost!;color:#c19f9f;height:100`
);
clearInterval(counterTimer);
myCamera.setAttribute("position", "1.180 2.600 -1.359");
myCamera.setAttribute("rotation", "-8.480 -723.760 0.000");
loseAudio.play();
restarWithKey = true;
};
const won = () => {
const AmongDead1 = document.getElementById("amongDead1");
const AmongDead2 = document.getElementById("amongDead2");
const AmongDead3 = document.getElementById("amongDead3");
const AmongDead4 = document.getElementById("amongDead4");
const AmongDead5 = document.getElementById("amongDead5");
const Among1 = document.getElementById("among1");
const Among2 = document.getElementById("among2");
const Among3 = document.getElementById("among3");
const Among4 = document.getElementById("among4");
const Among5 = document.getElementById("among5");
AmongDead1.setAttribute("visible", true);
AmongDead2.setAttribute("visible", true);
AmongDead3.setAttribute("visible", true);
AmongDead4.setAttribute("visible", true);
AmongDead5.setAttribute("visible", true);
Among1.setAttribute("visible", false);
Among2.setAttribute("visible", false);
Among3.setAttribute("visible", false);
Among4.setAttribute("visible", false);
Among5.setAttribute("visible", false);
AmongDead1.setAttribute("position", "-2.808 0.110 -5.074");
AmongDead2.setAttribute("position", "-1.271 0.110 -6.084");
AmongDead3.setAttribute("position", "0.082 0.120 -7.669");
AmongDead4.setAttribute("position", "2.121 0.110 -7.971");
AmongDead5.setAttribute("position", "3.801 0.110 -4.477");
AmongDead1.setAttribute("rotation", "0 40.000 0");
AmongDead2.setAttribute("rotation", "0 32.000 0");
AmongDead3.setAttribute("rotation", "0 15.000 0");
AmongDead4.setAttribute("rotation", "0 -10.000 0");
AmongDead5.setAttribute("rotation", "0 -41.000 0");
console.log(AmongDead1, AmongDead2, AmongDead3, AmongDead4, AmongDead5);
const counterElement = document.getElementById("counter");
counterElement.setAttribute("position", "23.325 9.272 -35.792");
counterElement.setAttribute(
"text",
`width:50;value:You Won!;color:#c19f9f;height:100`
);
clearInterval(counterTimer);
myCamera.setAttribute("position", "1.180 2.600 -1.359");
myCamera.setAttribute("rotation", "-8.480 -723.760 0.000");
wonAudio.play();
restarWithKey = true;
};
let isWalkingAudioOn = false;
document.onkeydown = (event) => {
if (
event.key == "w" ||
event.key == "W" ||
event.key == "a" ||
event.key == "A" ||
event.key == "s" ||
event.key == "S" ||
event.key == "d" ||
event.key == "D"
) {
currentPosition = myCamera.getAttribute("position");
console.log({ currentPosition });
}
if (event.key == "|") clearInterval(counterTimer);
if (first) {
first = false;
startAudio.play();
}
if (restarWithKey) window.location.reload();
if (event.which == 32) {
shoot();
}
};