forked from dtplsongithub/lowqualitygame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
264 lines (251 loc) · 6.52 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
function MAINLOAD(){
for (i in playlist) {
sounds[`${playlist[i]}`] = new Audio(`${playlistdir}/${playlist[i]}.${playlistfile[i]}`);
log.push(`loaded file /sounds/${playlist[i]}.ogg`);
}
fetch("f.json")
.then(data => data.text())
.then(body => {
log.push("loaded file /f.json");
var ee = JSON.parse(body);
for (a = 0; a < (ee.length); a++) {
if (ee[a]) {
(function (ee, a) {
fetch("js/" + (ee[a]))
.then(data => data.text())
.then(body => {
(function (body) {
log.push("loaded file /js/" + ee[a]);
try {
eval(body);
} catch (e) {
console.error(e, "file " + ee[a]);
}
})(body);
});
})(ee, a);
}
}
});
loadImages(imgtoload);
}
var playlist = ["elim","first","lap","powerup2","powerup","gameover","click","music/titlescreen"];
var playlistdir = "sounds";
var playlistfile = ["ogg","ogg","ogg","ogg","ogg","ogg","wav","mp3"]
var sounds = {}
var tlap = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
var log = []
pr=0
// load sounds
// load functions
js={}
var Images = {};
function loadImages(list){
var total = 0;
for(var i = 0; i < list.length; i++){
var img = new Image();
Images[list[i].name] = img;
img.onload = (()=>{
total++;
log.push(`loaded file /images/${gallery[total-1]}.png`)
});
img.src = list[i].url;
}
}
var gallery = ["arrow_01","arrow_02","face_1","face_2","face_3","face_4","title_screen"]
var imgtoload = gallery.map((a)=>({"name":a,"url":`images/${a}.png`}));
/*for (i in gallery) {
imgtoload.push({
"name":gallery[i],
"url":`images/${gallery[i]}.png`
})
}*/
var c = document.getElementById("canvas");
var ctx = c.getContext('2d');
var FPS = 60;
var vx,vy,px,py;
var cx, cy;
var tsh=[],menu=0;
c.width = 1152;
c.height = 864;
cx = c.width / 2;
cy = c.height / 2;
px=cx;
py=cy;
vx=0;
vy=0
var platforms = [{"x":0,"y":600,"w":1152,"h":400}];
var s =0;
var lasts = 0
var players =[];
var l =[]
var le = [];
var elim = false
for (let j = 0;j<40*14;j++){
if (random(0,1)==1){
s+=random(500,0)
} else {
s-=random(0,500)
}
if (isoffscreen(s,true)){
s=random(0,700)
}
lasts = s
platforms.push({"x":s,"y":-j*200+600,"w":random(40,300-(j/20)),"h":40});
if((j/40)%1==0){
platforms.push({"x":0,"y":-j*200+600,"w":1000,"h":20});
}
}
var color = ["red","orange","yellow","lightgreen","cyan","navy","purple","black","gray","pink","maroon","darkgreen","lightseagreen","lightcoral"]
for (let j = 0;j<14;j++){
players.push({"x":random(0,852),"y":cy,"color":color[j],"vx":0,"vy":0,"lastbounce":0,"lap":1,"nextbounce":0})
}
var keyb = [];
var realypos = 0;
document.addEventListener("keydown",function(e){
if(keyb.indexOf(e.keyCode)==-1){
keyb.push(e.keyCode)
}
})
document.addEventListener("keyup",function(e){
if(keyb.indexOf(e.keyCode)!=-1){
keyb.splice(keyb.indexOf(e.keyCode), 1);
}
})
var lap, lastlap
lastlap = 0
lap=1
var camy = 0;
let background=true
var isMobile = (/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(navigator.userAgent.toLowerCase()));
var difficulty =[2,3]//1,1 is easy, 1,3 is medium, 2,3 is hard, 3,3 is impossible
var reallap = 0
var errhandling=false;
var messages = []
var th = ["0nd 😪😪😪😪","1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th","11th","12th","13th","14th","15th"];
var t = 0
var soundtitlescreenplayed = false
var wintimer = ""
var creditsshown = false
setInterval(update, 1000 / FPS);
function bgm(){
sounds["music/titlescreen"].play().catch(()=>{showl("soundWarning")});
}
function update() {
ctx.fillStyle = "#aaa";
ctx.font="28px Arial"
if (background){
ctx.fillRect(0, 0, c.width, c.height)
}
if (isMobile) {
ctx.fillStyle="black"
ctx.font="28px Arial"
ctx.fillText("this game is pc only ._.",0,300)
} else if (log.length < 28){
ctx.fillStyle="black"
ctx.fillRect(0, 0, c.width, c.height)
ctx.fillStyle="white"
ctx.font="12px Consolas"
for (i in log){
ctx.fillText(log[i],0,130+i*14);
}
} else if (!soundtitlescreenplayed) {
sounds["music/titlescreen"].loop = true;
bgm();
soundtitlescreenplayed = true;
t = 0
} else if(t<FPS*1 && !creditsshown){
ctx.fillStyle="black"
ctx.fillRect(0, 0, c.width, c.height)
js.drawlogo(0);
js.tick();
}else if (menu==3){ // means that someone won
showl("won");
if(l[0].ii == false){
document.getElementById("whowon").innerText = "YOU"
document.getElementById("timer").innerText = "with a time of "+js.timerify(wintimer);
if(!localStorage.getItem("hiscore")){
localStorage.setItem("hiscore",wintimer)
document.getElementById("timer").innerText += "\n THATS A NEW RECORD!!"
} else {
if((~~localStorage.getItem("hiscore"))>wintimer){
localStorage.setItem("hiscore",wintimer)
document.getElementById("timer").innerText += "\n THATS A NEW RECORD!!"
}
}
} else {
document.getElementById("whowon").innerText = (l[0].color).toUpperCase();
}
showl("restartback");
menu=4
} else {
js.tick();
if (menu==1){
js.render();
if(!elim){
js.phi();
js.move();
js.renderplayer();
}
try{
js.ai();
} catch (e) {
//bruh
}
js.renderl();
js.lapcheck();
js.msghandling();
js.timerhandling();
js.image();
} else if (menu==0){
js.ts();
creditsshown = true
}
}
}
function isoffscreen(v,cq){ // true for x, false for y
if(cq){
return v < 0 || v > c.width -300
}else{
return v < 0 || v > c.height
}
}
function random(a, b) {
if (a > b) {
var _c = a;
a = b;
b = _c;
}
return Math.floor(Math.random() * (b - a + 1) + a);
}
function boxcollission(xd,yd,rect2){
return xd < rect2.x + rect2.w &&xd + 40 > rect2.x &&yd < rect2.y + rect2.h &&yd + 40 > rect2.y
}
function checkforcollission(px_,py_,j,vy_){
for (ii in platforms){
if (!boxcollission(px_,py_,platforms[ii]) )continue;
if(j!=null&&vy_>0){
players[j].lastbounce = ii;
players[j].nextbounce=random(difficulty[0],difficulty[1]);
}
return true
}
return false
}
function hidel(x) {
document.getElementById(x).style.display = "none";
}
function showl(x) {
document.getElementById(x).style.display = "block";
}
var diflist = [[1,2],[1,3],[2,3]]
// FORM HANDLING
function testResults (form) {
difficulty = diflist[form.dif.value]
}
// something
document.addEventListener('click', function(e){
if(e.target.tagName == 'BUTTON' || e.target.type == 'button'){
sounds.click.play()
}
})