-
Notifications
You must be signed in to change notification settings - Fork 8
/
sketch.js
441 lines (360 loc) · 13.1 KB
/
sketch.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// var socket;
var myAudio;
var songNow;
var ampData;
var ampLevel;
var dataSound = {};
var viewport;
var gmap; // game map
var p;
var eArr = []; // enemys
var bArr = []; // bullets
var iArr = []; // items
var rArr = []; // rocks
var tArr = []; // trees
var pArr = []; // portals
var redArr = []; // redzones
var epArr = []; // explore points
var sArr = []; // smokes
var wArr = []; //waters
var iceArr = []; // ices
var notifi = []; // notification
var pname;
var team = 1;
var maxE = 15;
var world;
var quadPlayers;
var quadItems;
var quadBulls;
var boundMap;
var fr; // frameRate
var mil = 0; // milliseconds from begin of game
var _gameTime = 0; // time from begin of game to now
var gameTime = ""; // string time
var maxSizeNow = 100;
var weaponInfo;
var runGame = false;
var listMes = ['chào', 'hello', 'funny', 'best game', 'haha', 'my gun', 'awsome', 'nice', 'come on', 'beauty', 'sexy', 'cute',
'like', 'love', 'hit', 'giết tao nè', 'thích gì', 'biến', 'lêu lêu', 'good dog', 'huhu', 'đừng giết tao', 'vãi'];
function preload() {
dataSound['audio/ambient_stream_01.mp3'] = loadSound('audio/ambient_stream_01.mp3');
}
function setup() {
createCanvas(windowWidth, windowHeight).position(0, 0);
noSmooth();
rectMode(CENTER);
textAlign(LEFT);
textFont('Consolas');
cursor(CROSS);
pixelDensity(1);
ampData = new p5.Amplitude();
songNow = floor(random(musics.SongList.length));
setInterval(function() {
if (runGame) {
_gameTime++;
gameTime = prettyTime(_gameTime);
}
}, 1000);
setInterval(function() {
if (runGame && gmap) {
gmap.createMinimap();
}
}, 10000);
// auto chat
// autoChat(random(listMes), null, true);
// autoAddPlayers(5);
autoAddItems(5);
autoAddRedzones(30);
getMaxSizeNow(2);
autoAddPortals(3, 15, 14);
}
function start() {
// khoi tao moi truong ban do
gmap = new GameMap(10000, 10000, 300);
var w = document.getElementById('worlds-select').value;
world = worlds[w || getValueAtIndex(worlds, floor(random(getObjectLength(worlds))))];
// time
_gameTime = 0;
// get player name
pname = document.getElementById('ip-name').value || RandomName[floor(random(RandomName.length))];
reset();
weaponInfo = new InfoWeapon();
// dung cho quadtree
boundMap = new Rectangle(gmap.size.x / 2, gmap.size.y / 2, gmap.size.x, gmap.size.y);
quadItems = new QuadTree(boundMap, 5);
quadBulls = new QuadTree(boundMap, 5);
quadPlayers = new QuadTree(boundMap, 1);
help(10);
runGame = true;
document.getElementById('chatBox').style.display = "block";
if(!myAudio) changeSong(1);
// addSound('audio/ambient_stream_01.mp3', true);
addAlertBox('Please read the Rules in chat box.', '#f55', '#fff');
}
function draw() {
if (runGame) {
background(world.bg);
//star effects
if(world.bg[3])
for (var i = 0; i < 2; i++) {
stroke(random(200, 255), 100);
strokeWeight(random(7, 20));
point(random(width), random(height));
}
// get value
fr = frameRate();
mil = millis();
ampLevel = ampData.getLevel();
viewport.run();
push();
translate(-viewport.pos.x + width / 2, -viewport.pos.y + height / 2);
fill(world.bg);
noStroke();
rect(gmap.size.x / 2, gmap.size.y / 2, gmap.safezone.x, gmap.safezone.y);
gmap.safezone.x--;
gmap.safezone.y--;
if(keyIsDown(73)) {
for (var i = iArr.length - 1; i >= 0; i--)
iArr[i].pos = p5.Vector.lerp(iArr[i].pos, viewport.target.pos, 0.1);
}
if(keyIsDown(79)) {
for (var i = bArr.length - 1; i >= 0; i--)
bArr[i].pos = p5.Vector.lerp(bArr[i].pos, viewport.target.pos, 0.1);
}
if(keyIsDown(80)) {
for (var i = eArr.length - 1; i >= 0; i--)
if(!eArr[i].died)
eArr[i].pos = p5.Vector.lerp(eArr[i].pos, viewport.target.pos, 0.1);
}
gmap.run();
// update quadtrees
quadItems.clear();
for (var i of iArr) quadItems.insert(i);
quadBulls.clear();
for (var b of bArr) quadBulls.insert(b);
quadPlayers.clear();
if(!p.died) quadPlayers.insert(p);
for (var ei of eArr) if(!ei.died) quadPlayers.insert(ei);
// ices
for(var i of iceArr)
i.run();
// waters
for (var w of wArr)
w.run();
// bullets
for (var i = bArr.length - 1; i >= 0; i--)
bArr[i].run();
// // rocks
for (var i = rArr.length - 1; i >= 0; i--)
rArr[i].run();
// // items
for (var i = iArr.length - 1; i >= 0; i--)
iArr[i].run();
// characters
if (!p.died) p.move();
p.run();
for (var ei of eArr) {
if(!ei.died) {
ei.fire();
ei.move();
}
ei.run();
}
// reset hide value
if (!p.died) p.hide = false;
for (var ei of eArr) ei.hide = false;
// fire
if (!p.died && mouseIsPressed && mouseButton == 'left') p.fireTo(fakeToReal(mouseX, mouseY));
if (keyIsDown(32)) viewport.pos = viewport.target.pos.copy();
// portals
for (var i = pArr.length - 1; i >= 0; i--) {
if (!pArr[i].inGate.run() && pArr[i].outGate)
pArr[i].outGate.run();
}
// trees
for (var i = tArr.length - 1; i >= 0; i--)
tArr[i].run();
// smokes
for (var i = sArr.length - 1; i >= 0; i--)
sArr[i].show();
// red zone
for (var i = redArr.length - 1; i >= 0; i--)
redArr[i].show();
// explore points
for (var i = epArr.length - 1; i >= 0; i--) {
epArr[i].show();
epArr[i].checkExplore(epArr);
}
// waters
for (var w of wArr)
w.show();
pop();
// notifications
for (var n of notifi) {
n.run();
}
gmap.showMinimap();
weaponInfo.show();
// fps
textSize(20);
textAlign(LEFT);
noStroke();
fill(255, 150);
text('Fps: ' + floor(frameRate()), 5, 20);
text('Time: ' + gameTime, 5, 45);
text('Players: ' + getPlayerLife(), 5, 70);
text('Killed: ' + viewport.target.killed, 5, 95);
textAlign(CENTER);
text(floor(viewport.pos.x) + " " + floor(viewport.pos.y), width / 2, height - 25);
}
}
function keyPressed() {
if(keyCode == 27) { // ESC
var n = document.getElementById("menuWhenDie").style.display;
if(n == "block") {
runGame = true;
menuWhenDie("close");
} else {
if(p) runGame = false;
menuWhenDie("open");
}
}
if (!isTyping() && keyCode == 191) {
customWeapon('switch');
}
if (runGame && !isTyping()) {
if (keyCode == 86) { // V
viewport.follow = !viewport.follow;
} else if (keyCode == 77) { // M
gmap.hiddenMinimap = !gmap.hiddenMinimap;
} else if (keyCode == 69) { // E
if (!p.died) p.shield = !p.shield;
} else if (keyCode == 70) { // F
if (!p.died) p.pickWeapon();
} else if (keyCode == 66) { // B
if (!p.died) collisionEdge(p, 0.6);
} else if (keyCode >= 49 && keyCode <= 57) { // number
if (!p.died && keyCode - 49 < p.weaponBox.length) {
var weaponNow = p.weaponBox.indexOf(p.weapon);
p.changeWeapon(keyCode - 49 - weaponNow);
showWeaponInfo();
}
} else if (keyCode == 82) { // R
if (!p.died) p.weapon.gun.reload();
} else if (keyCode == 67) { // C
var value = document.getElementById('showHideChat').value;
if (value == 'Show') showChat(true);
else showChat(false);
} else if (keyCode == 78) { // N
changeSong(1);
} else if (keyCode == 72) { // H
// help(5);
} else if (keyCode == 13) {
showChat(true);
document.getElementById('inputMes').focus();
}
} else if (keyCode == 13) {
var ele = document.getElementById('inputMes');
switch (ele.value) {
case '':
break;
case '/help':
addMessage('/howtoplay, /showplayers, /clear, /more, /contact', 'Server');
break;
case '/howtoplay':
help();
break;
case '/showplayers':
var names = "";
for (var e of eArr) {
names += (e.name + ", ");
}
addMessage(names, 'Server', false, color(0));
break;
case '/clear':
clearChat();
break;
case '/more':
addMessage('click here \u2665', 'Visualize music', false, color(255, 0, 0),
function() {
window.open('https://github.com/HoangTran0410/Visualyze-design-your-own-')
});
addMessage('click here \u2665', 'Giphy Api', false, color(255, 100, 0),
function() {
window.open('https://hoangtran0410.github.io/giphyApi/')
});
addMessage('click here \u2665', 'Sort Simulate', false, color(255, 255, 0),
function() {
window.open('https://hoangtran0410.github.io/Sort-Simulate/')
});
addMessage('click here \u2665', 'Write Point', false, color(0, 255, 0),
function() {
window.open('https://hoangtran0410.github.io/Write-Points/')
});
addMessage('click here \u2665', 'Maze Pacman', false, color(0, 0, 255),
function() {
window.open('https://hoangtran0410.github.io/Maze-generate/')
});
addMessage('click here \u2665', 'Simple Paint', false, color(70, 60, 90),
function() {
window.open('https://hoangtran0410.github.io/Paint-P5/')
});
addMessage('click here \u2665', 'Box2D Testing', false, color(255, 0, 255),
function() {
window.open('https://hoangtran0410.github.io/box2D-2/')
});
addMessage('click here \u2665', 'Lsystem Simulate', false, color(255, 0, 0),
function() {
window.open('https://hoangtran0410.github.io/L-system-dat.gui/')
});
addMessage('click here \u2665', 'Simple Firework', false, color(255, 100, 0),
function() {
window.open('https://hoangtran0410.github.io/Fire-work/')
});
break;
case '/contact':
addMessage('click here \u2665', 'My Github', false, color(100),
function() {
window.open('https://github.com/HoangTran0410')
});
addMessage('click here \u2665', 'My Facebook', false, color(0, 0, 255),
function() {
window.open('https://www.facebook.com/people/Hoang-Tran/100004848287494')
});
break;
default:
var pcol = hexToRgb(document.getElementById('pickColor').value);
addMessage(event.target.value, pname, true, color(pcol.r, pcol.g, pcol.b));
for(var i = 0; i < random(3); i++)
autoChat(event.target.value);
break;
}
ele.blur();
ele.value = '';
}
}
function mousePressed(e) {
if (runGame && p.died && getPlayerLife() > 0) {
if (e.target.matches('canvas')) {
var newTarget = eArr[(eArr.indexOf(viewport.target) + 1) % eArr.length]
viewport.target = newTarget;
}
}
}
function mouseWheel(e) {
if (runGame && !p.died) {
if ((e.target.matches('canvas')) || document.getElementById('showHideChat').value == 'Show') {
//if (!p.shield) {
p.changeWeapon(e.delta > 0 ? 1 : -1);
showWeaponInfo();
//}
}
}
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight, true);
if (runGame) {
gmap.offSetX = width - gmap.minimapSize - 10;
weaponInfo = new InfoWeapon();
}
}