-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFogOfWarSuperExample.html
More file actions
707 lines (539 loc) · 20.8 KB
/
FogOfWarSuperExample.html
File metadata and controls
707 lines (539 loc) · 20.8 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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Fog Of War</title>
<style>
html, body {
margin: 0;
padding: 0;
overflow: hidden;
}
.info {
position: fixed;
z-index: 99999;
left: 50vw;
width: 500px;
bottom: 100px;
background: rgba(0,0,0,0.7);
font: 20px Verdana;
font-style: italic;
border-radius: 10px;
padding: 10px;
color: #fff;
text-align: center;
transform: translateX(-50%);
}
.error {
position: fixed;
top: 50px;
left: 50px;
color: #f00;
font: 14px Verdana;
background: rgba(0,0,0,0.7);
border-radius: 10px;
padding: 10px;
width: 300px;
text-align: center;
}
</style>
</head>
<body style="background:#08f;">
<div class="info">
Keep left click and move mouse to move<br />
Your units follow your mouse<br />
Press A to add units
</div>
<div id="error" style="display: none;" class="error">
There is already a unit at this location (mouse position), put your mouse elsewhere
</div>
<script type="text/javascript">
var mouse = {x:0,y:0,ax:0,ay:0,isDown:false};
window.addEventListener('load',function() {
// Just wait a little
setTimeout(function () {
var game = new Game(4000,4000);
// Enemy Players
game.addPlayer('#ff5d00',10);
game.addPlayer('#ff0000',15);
window.onmousemove = function(event) {
var rect = game.canvas.getBoundingClientRect();
mouse.x = event.clientX - rect.left;
mouse.y = event.clientY - rect.top;
if (mouse.isDown) {
game.camera.mx = mouse.x - mouse.ax;
game.camera.my = mouse.y - mouse.ay;
}
}
window.onmousedown = function(event) {
var rect = game.canvas.getBoundingClientRect();
mouse.ax = event.clientX - rect.left;
mouse.ay = event.clientY - rect.top;
mouse.isDown = true;
}
window.onmouseup = function(event) {
game.camera.x += game.camera.mx;
game.camera.y += game.camera.my;
game.camera.mx = 0;
game.camera.my = 0;
mouse.isDown = false;
}
window.onwheel = function (event) {
var wheel = event.wheelDelta < 0 ? -1 : 1;
var mouse = game.camera.getMouseRelativeCoordinate();
var oldScale = game.scale;
var origin = {x:mouse.x / oldScale, y:mouse.y / oldScale};
var newScale = event.wheelDelta > 0 ? oldScale * 1.05 : oldScale / 1.05;
if (newScale >= 8) {
newScale = 8;
} else if (newScale <= 0.4) {
newScale = 0.4;
}
game.scale = newScale;
game.camera.x += -(origin.x - mouse.x / newScale) * newScale;
game.camera.y += -(origin.y - mouse.y / newScale) * newScale;
}
window.onkeypress = function(event) {
if (event.key.toLowerCase() == 'a') {
var mouse = game.camera.getMouseRelativeCoordinateScale();
var test = new Unit(game,game.player,-1,-1,{x:mouse.x,y:mouse.y});
if (!game.isCollide(test)) {
game.player.add({x:mouse.x,y:mouse.y});
} else {
try {
clearTimeout(window.errortimeout);
} catch (e) {}
document.getElementById('error').style.display = 'block';
window.errortimeout = setTimeout(function () {
document.getElementById('error').style.display = 'none';
}, 2000);
}
}
}
}, 500);
});
class Unit {
constructor(game,player,id,gameid,settings) {
var defaultSettings = {
x: player.x,
y: player.y,
visibility: 100
};
settings = settings || defaultSettings;
this.game = game;
this.player = player;
this.id = id;
this.fogOfWar = this.game.fogOfWar; // on va utiliser la méthode FogOFWar
this.gameid = gameid;
this.speed = 2;
this.x = settings.x || defaultSettings.x;
this.y = settings.y || defaultSettings.y;
this.width = 20;
this.height = 20;
this.visibility = settings.visibility || defaultSettings.visibility;
}
// Une méthode draw
draw() {
// Pour l'instant, un simple carré suffira
var ctx = this.game.ctx;
ctx.beginPath();
ctx.rect(this.x-10,this.y-10,20,20);
ctx.fillStyle = this.player.color;
ctx.fill();
}
// Une méthode Clip
clip() {
this.game.ctx.moveTo(this.x,this.y);
this.game.ctx.arc(this.x,this.y,this.visibility,0,2*Math.PI,false);
}
explore() {
this.fogOfWar.explore(this.x,this.y,this.visibility);
}
followMouse() {
var mouse = this.game.camera.getMouseRelativeCoordinateScale();
var angle = Math.atan2(mouse.y-this.y,mouse.x-this.x);
this.move(angle);
}
move(angle) {
var test = {
x: this.x + this.speed * Math.cos(angle),
y: this.y + this.speed * Math.sin(angle),
width: this.width,
height: this.height,
gameid: this.gameid
};
if (this.game.isCollide(test)) {
var test = {
x: this.x + this.speed * Math.cos(angle),
y: this.y,
width: this.width,
height: this.height,
gameid: this.gameid
};
if (this.game.isCollide(test)) {
var test = {
x: this.x,
y: this.y + this.speed * Math.sin(angle),
width: this.width,
height: this.height,
gameid: this.gameid
};
if (this.game.isCollide(test)) {
return;
}
this.y += this.speed * Math.sin(angle);
return;
}
this.x += this.speed * Math.cos(angle);
return;
}
this.x += this.speed * Math.cos(angle);
this.y += this.speed * Math.sin(angle);
}
// Une méthode update
update() {
this.draw();
}
}
class Player {
constructor(game,id,color) {
this.game = game;
this.color = color;
var pos = this.game.randomPosition();
this.x = pos.x;
this.y = pos.y;
this.id = id;
this.gameid = this.game.absolute_id;
this.id_units = 0;
this.units = {};
// On ajoute une unité dès le départ
this.add();
// for bot (move randomly)
this.time = Date.now();
this.tick = this.game.getRandomIntInclusive(500,2000);
this.angle = this.game.getRandomIntInclusive(0,360) * Math.PI / 180;
}
add(settings) {
this.units[this.id_units] = new Unit(this.game,this,this.id_units,this.game.absolute_id,settings);
this.id_units++;
this.game.absolute_id++;
}
update(current) {
if (!current && Date.now() - this.time > this.tick) {
this.time = Date.now();
this.tick = this.game.getRandomIntInclusive(500,2000);
this.angle = this.game.getRandomIntInclusive(0,360) * Math.PI / 180;
}
for (var id in this.units) {
if (this.units.hasOwnProperty(id)) {
this.units[id].update();
}
}
var test = [];
for (var id in this.units) {
if (this.units.hasOwnProperty(id)) {
test.push({id:id,x:this.units[id].x,y:this.units[id].y});
}
}
test = test.sort(function(a,b){
var a_dx = mouse.x-(a.x+a.width/2);
var a_dy = mouse.y-(a.x+a.height/2);
var b_dx = mouse.x-(b.x+b.width/2);
var b_dy = mouse.y-(b.y+b.height/2);
var dist1 = Math.sqrt(a_dx*a_dx+a_dy*a_dy);
var dist2 = Math.sqrt(b_dx*b_dx+b_dy*b_dy);
return dist1-dist2;
});
for (var i = 0; i < test.length; i++) {
if (current) {
this.units[test[i].id].followMouse();
this.units[test[i].id].explore();
} else {
this.units[test[i].id].move(this.angle);
}
}
}
clip() {
this.game.ctx.beginPath();
for (var id in this.units) {
if (this.units.hasOwnProperty(id)) {
this.units[id].clip();
}
}
this.game.ctx.clip();
}
}
class FogOfWar {
constructor(game) {
this.game = game; // game est un instancier de la classe Game
// On créé un simple canvas
this.canvas = document.createElement('canvas');
// On fais en sorte qu'il soit de la même taille que le canvas de notre jeu
this.canvas.width = this.game.width;
this.canvas.height = this.game.height;
// On récupère le contexte 2D
this.ctx = this.canvas.getContext('2d');
// On colorie le tout en Noir
this.ctx.fillStyle = '#000000';
this.ctx.fillRect(0,0,this.canvas.width,this.canvas.height);
// Et on passe en mode destination-out
this.ctx.globalCompositeOperation = 'destination-out';
// On créé un autre canvas pour y stocker les zones d'ombres
this.shadow = document.createElement('canvas');
this.shadow.width = this.game.width;
this.shadow.height = this.game.height;
this.shadow_ctx = this.shadow.getContext('2d');
this.shadow_ctx.fillStyle = 'rgba(0,0,0,0.5)';
this.shadow_ctx.fillRect(0, 0, this.shadow.width, this.shadow.height);
}
// Une simple méthode pour explorer le "Fog Of War"
explore(x,y,radius) {
// on dessine juste un cercle
this.ctx.beginPath();
this.ctx.lineWidth = 0;
this.ctx.arc(x,y,radius,0,2*Math.PI,false);
this.ctx.fill();
// On dessine juste un cercle en mode destination-out
this.shadow_ctx.save();
this.shadow_ctx.globalCompositeOperation = 'destination-out';
this.shadow_ctx.beginPath();
this.shadow_ctx.arc(x,y,radius,0,2*Math.PI,false);
this.shadow_ctx.fillStyle = '#000000';
this.shadow_ctx.fill();
this.shadow_ctx.restore();
}
clearVisibility() {
// On efface tout
this.shadow_ctx.clearRect(0,0,this.shadow.width,this.shadow.height);
// Et On dessine tout en Noir transparent
this.shadow_ctx.fillStyle = 'rgba(0,0,0,0.5)';
this.shadow_ctx.fillRect(0,0,this.shadow.width,this.shadow.height);
}
// Une fonction update appelé par le jeu
update() {
// On dessine d'abord notre masque
this.game.ctx.drawImage(this.canvas,0,0);
// Puis on dessine les zones déjà exploré par le joueur
this.game.ctx.drawImage(this.shadow,0,0);
}
}
class Camera {
constructor(game) {
this.game = game;
this.x = 0;
this.y = 0;
this.mx = 0;
this.my = 0;
}
apply(ctx) {
ctx.setTransform(1,0,0,1,0,0);
ctx.translate(this.x+this.mx,this.y+this.my);
ctx.scale(this.game.scale,this.game.scale);
ctx.translate(-(this.x+this.mx),-(this.y+this.my));
ctx.translate(this.x+this.mx,this.y+this.my);
}
getMouseRelativeCoordinate() {
var x = mouse.x - (this.x+this.mx);
var y = mouse.y - (this.y+this.my);
return {x:x,y:y};
}
getMouseRelativeCoordinateScale() {
var x = (mouse.x - (this.x+this.mx))/this.game.scale;
var y = (mouse.y - (this.y+this.my))/this.game.scale;
return {x:x,y:y};
}
}
class Game {
constructor(width, height) {
this.width = width;
this.height = height;
this.absolute_id = 0;
this.canvas = document.createElement('canvas');
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
this.ctx = this.canvas.getContext('2d');
document.body.appendChild(this.canvas);
this.fogOfWar = new FogOfWar(this);
this.camera = new Camera(this);
this.player_id = 1;
this.player = new Player(this,0,'#0000ff');
this.other_players = {};
this.camera.x = -this.player.x+window.innerWidth/2;
this.camera.y = -this.player.y+window.innerHeight/2;
this.scale = 1;
this.background = this.generateMap();
this.update();
}
addPlayer(color,nb) {
this.other_players[this.player_id] = new Player(this,this.player_id,color);
var test = new Unit(this,this.other_players[this.player_id],0);
for (var i = 0; i < nb; i++) {
this.other_players[this.player_id].add({x:this.other_players[this.player_id].x+i*(test.width+1)});
}
this.player_id++;
this.absolute_id++;
}
update() {
var _this = this;
// Resize
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
// Clear
this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height);
this.camera.apply(this.ctx);
// Draw background
this.ctx.drawImage(this.background,0,0);
// Clear Visibility
this.fogOfWar.clearVisibility();
// Update the Current Player
this.player.update(true);
// Update the fogOfWar
this.fogOfWar.update();
this.ctx.save();
// Clip Visibility Zone
this.player.clip();
// Draw Ennemies
for (var id in this.other_players) {
if (this.other_players.hasOwnProperty(id)) {
this.other_players[id].update();
}
}
this.ctx.restore();
window.requestAnimationFrame(function(){_this.update()});
}
getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min +1)) + min;
}
randomPosition() {
return {
x: this.getRandomIntInclusive(0,this.width),
y: this.getRandomIntInclusive(0,this.height)
};
}
rectangleIsCollide(rect1,rect2) {
return (rect1.x < rect2.x + rect2.width &&
rect1.x + rect1.width > rect2.x &&
rect1.y < rect2.y + rect2.height &&
rect1.height + rect1.y > rect2.y);
}
isCollide(unit) {
if (unit.x-unit.width/2 < 0) return true;
if (unit.y-unit.height/2 < 0) return true;
if (unit.x+unit.width/2 > this.width) return true;
if (unit.y+unit.height/2 > this.height) return true;
for (var id in this.player.units) {
if (this.player.units.hasOwnProperty(id)) {
if (unit.gameid!=this.player.units[id].gameid) {
if (this.rectangleIsCollide(unit,this.player.units[id])) {
return true;
}
}
}
}
for (var id in this.other_players) {
if (this.other_players.hasOwnProperty(id)) {
for (var idunit in this.other_players[id].units) {
if (this.other_players[id].units.hasOwnProperty(idunit)) {
if (unit.gameid!=this.other_players[id].units[idunit].gameid) {
if (this.rectangleIsCollide(unit,this.other_players[id].units[idunit])) {
return true;
}
}
}
}
}
}
return false;
}
generateMap() {
var canvas = document.createElement('canvas');
canvas.width = this.width;
canvas.height = this.height;
var ctx = canvas.getContext('2d');
var perlinNoise = new PerlinNoise();
ctx.fillColor = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);
var size = 64;
var i = 0;
var w = canvas.width/size;
var h = canvas.height/size;
for (var x = 0; x < w; x++) {
for (var y = 0; y < h; y++) {
var nx = (x*size)/500, ny = (y*size)/500;
var n = perlinNoise.noise(1 * nx, 1 * ny, .8 );
var value = Math.round(255 * Math.pow(n, 1.18));
var color = '#000000';
if (value < 50) {
color = 'rgb(0,120,255)';
} else if (value < 80) {
color = 'rgb(255,230,153)';
} else {
color = 'rgb(102,214,74)';
}
ctx.beginPath(),
ctx.rect(x*size,y*size,size,size);
ctx.fillStyle = color;
ctx.fill();
i++;
}
}
return canvas;
}
}
class PerlinNoise {
constructor() {
this.permutation = [ 151,160,137,91,90,15,
131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180
];
}
noise(x, y, z) {
var p = new Array(512);
for (var i=0; i < 256 ; i++) {
p[256+i] = p[i] = this.permutation[i];
}
var X = Math.floor(x) & 255, // FIND UNIT CUBE THAT
Y = Math.floor(y) & 255, // CONTAINS POINT.
Z = Math.floor(z) & 255;
x -= Math.floor(x); // FIND RELATIVE X,Y,Z
y -= Math.floor(y); // OF POINT IN CUBE.
z -= Math.floor(z);
var u = this.fade(x), // COMPUTE FADE CURVES
v = this.fade(y), // FOR EACH OF X,Y,Z.
w = this.fade(z);
var A = p[X ]+Y, AA = p[A]+Z, AB = p[A+1]+Z, // HASH COORDINATES OF
B = p[X+1]+Y, BA = p[B]+Z, BB = p[B+1]+Z; // THE 8 CUBE CORNERS,
return this.scale(this.lerp(w, this.lerp(v, this.lerp(u, this.grad(p[AA ], x , y , z ), // AND ADD
this.grad(p[BA ], x-1, y , z )), // BLENDED
this.lerp(u, this.grad(p[AB ], x , y-1, z ), // RESULTS
this.grad(p[BB ], x-1, y-1, z ))),// FROM 8
this.lerp(v, this.lerp(u, this.grad(p[AA+1], x , y , z-1 ), // CORNERS
this.grad(p[BA+1], x-1, y , z-1 )), // OF CUBE
this.lerp(u, this.grad(p[AB+1], x , y-1, z-1 ),
this.grad(p[BB+1], x-1, y-1, z-1 )))));
}
fade(t) { return t * t * t * (t * (t * 6 - 15) + 10); }
lerp( t, a, b) { return a + t * (b - a); }
grad(hash, x, y, z) {
var h = hash & 15; // CONVERT LO 4 BITS OF HASH CODE
var u = h<8 ? x : y, // INTO 12 GRADIENT DIRECTIONS.
v = h<4 ? y : h==12||h==14 ? x : z;
return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v);
}
scale(n) { return (1 + n)/2; }
}
</script>
</body>
</html>