This repository has been archived by the owner on Dec 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
colortest.htm
2324 lines (2004 loc) · 73.2 KB
/
colortest.htm
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<script>
// change this to increase/decrease the scale at which the map is drawn
var scale = 2;
// set this to false to hide the ghosts and pacman
var shouldDrawActors = true;
window.onload = function() {
var canvas = document.getElementById('canvas');
var rows = 36;
var cols = 28;
canvas.width = cols*tileSize*scale;
canvas.height = rows*tileSize*scale;
var ctx = canvas.getContext('2d');
ctx.scale(scale,scale);
var map = mapgen();
map.draw(ctx,0,0);
if (shouldDrawActors) {
drawActors(ctx);
}
};
////////////////////////////////////////////////////////
// source: http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
/**
* Converts an RGB color value to HSL. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSL_color_space.
* Assumes r, g, and b are contained in the set [0, 255] and
* returns h, s, and l in the set [0, 1].
*
* @param Number r The red color value
* @param Number g The green color value
* @param Number b The blue color value
* @return Array The HSL representation
*/
function rgbToHsl(r, g, b){
r /= 255, g /= 255, b /= 255;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var h, s, l = (max + min) / 2;
if(max == min){
h = s = 0; // achromatic
}else{
var d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch(max){
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
return [h, s, l];
}
/**
* Converts an HSL color value to RGB. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSL_color_space.
* Assumes h, s, and l are contained in the set [0, 1] and
* returns r, g, and b in the set [0, 255].
*
* @param Number h The hue
* @param Number s The saturation
* @param Number l The lightness
* @return Array The RGB representation
*/
function hslToRgb(h, s, l){
var r, g, b;
if(s == 0){
r = g = b = l; // achromatic
}else{
function hue2rgb(p, q, t){
if(t < 0) t += 1;
if(t > 1) t -= 1;
if(t < 1/6) return p + (q - p) * 6 * t;
if(t < 1/2) return q;
if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
}
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
var p = 2 * l - q;
r = hue2rgb(p, q, h + 1/3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1/3);
}
r *= 255;
g *= 255;
b *= 255;
return [r,g,b];
}
/**
* Converts an RGB color value to HSV. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSV_color_space.
* Assumes r, g, and b are contained in the set [0, 255] and
* returns h, s, and v in the set [0, 1].
*
* @param Number r The red color value
* @param Number g The green color value
* @param Number b The blue color value
* @return Array The HSV representation
*/
function rgbToHsv(r, g, b){
r = r/255, g = g/255, b = b/255;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var h, s, v = max;
var d = max - min;
s = max == 0 ? 0 : d / max;
if(max == min){
h = 0; // achromatic
}else{
switch(max){
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
return [h, s, v];
}
/**
* Converts an HSV color value to RGB. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSV_color_space.
* Assumes h, s, and v are contained in the set [0, 1] and
* returns r, g, and b in the set [0, 255].
*
* @param Number h The hue
* @param Number s The saturation
* @param Number v The value
* @return Array The RGB representation
*/
function hsvToRgb(h, s, v){
var r, g, b;
var i = Math.floor(h * 6);
var f = h * 6 - i;
var p = v * (1 - s);
var q = v * (1 - f * s);
var t = v * (1 - (1 - f) * s);
switch(i % 6){
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
r *= 255;
g *= 255;
b *= 255;
return [r,g,b];
}
function rgbString(rgb) {
var r = Math.floor(rgb[0]);
var g = Math.floor(rgb[1]);
var b = Math.floor(rgb[2]);
return 'rgb('+r+','+g+','+b+')';
}
// direction enums (in clockwise order)
var DIR_UP = 0;
var DIR_RIGHT = 1;
var DIR_DOWN = 2;
var DIR_LEFT = 3;
// get direction enum from a direction vector
var getEnumFromDir = function(dir) {
if (dir.x==-1) return DIR_LEFT;
if (dir.x==1) return DIR_RIGHT;
if (dir.y==-1) return DIR_UP;
if (dir.y==1) return DIR_DOWN;
};
// set direction vector from a direction enum
var setDirFromEnum = function(dir,dirEnum) {
if (dirEnum == DIR_UP) { dir.x = 0; dir.y =-1; }
else if (dirEnum == DIR_RIGHT) { dir.x =1; dir.y = 0; }
else if (dirEnum == DIR_DOWN) { dir.x = 0; dir.y = 1; }
else if (dirEnum == DIR_LEFT) { dir.x = -1; dir.y = 0; }
};
// size of a square tile in pixels
var tileSize = 8;
/////////////////////////////////////////////////
//
// actor starting states
midTile = {x:3,y:4};
var blinky = {};
blinky.color = "#FF0000";
var pinky = {};
pinky.color = "#FFB8FF";
var inky = {};
inky.color = "#00FFFF";
var clyde = {};
clyde.color = "#FFB851";
var pacman = {};
pacman.color = "#FFFF00";
var ghosts = [blinky,inky,pinky,clyde];
blinky.dirEnum = DIR_LEFT;
blinky.pixel = {
x: 14*tileSize-1,
y: 14*tileSize+midTile.y
};
pinky.dirEnum = DIR_DOWN;
pinky.pixel = {
x: 14*tileSize-1,
y: 17*tileSize+midTile.y,
};
inky.dirEnum = DIR_UP;
inky.pixel = {
x: 12*tileSize-1,
y: 17*tileSize + midTile.y,
};
clyde.dirEnum = DIR_UP;
clyde.pixel = {
x: 16*tileSize-1,
y: 17*tileSize + midTile.y,
};
pacman.dirEnum = DIR_LEFT;
pacman.pixel = {
x: 14*tileSize-1,
y: 26*tileSize + midTile.y,
};
//////////////////////////////////////////////////////////////
// Sprites
// (sprites are created using canvas paths)
var drawGhostSprite = (function(){
// add top of the ghost head to the current canvas path
var addHead = (function() {
// pixel coordinates for the top of the head
// on the original arcade ghost sprite
var coords = [
0,6,
1,3,
2,2,
3,1,
4,1,
5,0,
8,0,
9,1,
10,1,
11,2,
12,3,
13,6,
];
return function(ctx) {
var i;
ctx.save();
// translate by half a pixel to the right
// to try to force centering
ctx.translate(0.5,0);
ctx.moveTo(0,6);
ctx.quadraticCurveTo(1.5,0,6.5,0);
ctx.quadraticCurveTo(11.5,0,13,6);
// draw lines between pixel coordinates
/*
ctx.moveTo(coords[0],coords[1]);
for (i=2; i<coords.length; i+=2)
ctx.lineTo(coords[i],coords[i+1]);
*/
ctx.restore();
};
})();
// add first ghost animation frame feet to the current canvas path
var addFeet1 = (function(){
// pixel coordinates for the first feet animation
// on the original arcade ghost sprite
var coords = [
13,13,
11,11,
9,13,
8,13,
8,11,
5,11,
5,13,
4,13,
2,11,
0,13,
];
return function(ctx) {
var i;
ctx.save();
// translate half a pixel right and down
// to try to force centering and proper height
ctx.translate(0.5,0.5);
// continue previous path (assuming ghost head)
// by drawing lines to each of the pixel coordinates
for (i=0; i<coords.length; i+=2)
ctx.lineTo(coords[i],coords[i+1]);
ctx.restore();
};
})();
// add second ghost animation frame feet to the current canvas path
var addFeet2 = (function(){
// pixel coordinates for the second feet animation
// on the original arcade ghost sprite
var coords = [
13,12,
12,13,
11,13,
9,11,
7,13,
6,13,
4,11,
2,13,
1,13,
0,12,
];
return function(ctx) {
var i;
ctx.save();
// translate half a pixel right and down
// to try to force centering and proper height
ctx.translate(0.5,0.5);
// continue previous path (assuming ghost head)
// by drawing lines to each of the pixel coordinates
for (i=0; i<coords.length; i+=2)
ctx.lineTo(coords[i],coords[i+1]);
ctx.restore();
};
})();
// draw regular ghost eyes
var addEyes = function(ctx,dirEnum){
var i;
ctx.save();
ctx.translate(2,3);
var coords = [
0,1,
1,0,
2,0,
3,1,
3,3,
2,4,
1,4,
0,3
];
var drawEyeball = function() {
ctx.translate(0.5,0.5);
ctx.beginPath();
ctx.moveTo(coords[0],coords[1]);
for (i=2; i<coords.length; i+=2)
ctx.lineTo(coords[i],coords[i+1]);
ctx.closePath();
ctx.fill();
ctx.stroke();
ctx.translate(-0.5,-0.5);
//ctx.fillRect(1,0,2,5); // left
//ctx.fillRect(0,1,4,3);
};
// translate eye balls to correct position
if (dirEnum == DIR_LEFT) ctx.translate(-1,0);
else if (dirEnum == DIR_RIGHT) ctx.translate(1,0);
else if (dirEnum == DIR_UP) ctx.translate(0,-1);
else if (dirEnum == DIR_DOWN) ctx.translate(0,1);
// draw eye balls
ctx.fillStyle = "#FFF";
ctx.strokeStyle = "#FFF";
ctx.lineWidth = 1.0;
drawEyeball();
ctx.translate(6,0);
drawEyeball();
// translate pupils to correct position
if (dirEnum == DIR_LEFT) ctx.translate(0,2);
else if (dirEnum == DIR_RIGHT) ctx.translate(2,2);
else if (dirEnum == DIR_UP) ctx.translate(1,0);
else if (dirEnum == DIR_DOWN) ctx.translate(1,3);
// draw pupils
ctx.fillStyle = "#00F";
ctx.fillRect(0,0,2,2); // right
ctx.translate(-6,0);
ctx.fillRect(0,0,2,2); // left
ctx.restore();
};
// draw scared ghost face
var addScaredFace = function(ctx,flash){
ctx.strokeStyle = ctx.fillStyle = flash ? "#F00" : "#FF0";
// eyes
ctx.fillRect(4,5,2,2);
ctx.fillRect(8,5,2,2);
// mouth
var coords = [
1,10,
2,9,
3,9,
4,10,
5,10,
6,9,
7,9,
8,10,
9,10,
10,9,
11,9,
12,10,
];
ctx.translate(0.5,0.5);
ctx.beginPath();
ctx.moveTo(coords[0],coords[1]);
for (i=2; i<coords.length; i+=2)
ctx.lineTo(coords[i],coords[i+1]);
ctx.lineWidth = 1.0;
ctx.stroke();
ctx.translate(-0.5,-0.5);
/*
ctx.fillRect(1,10,1,1);
ctx.fillRect(12,10,1,1);
ctx.fillRect(2,9,2,1);
ctx.fillRect(6,9,2,1);
ctx.fillRect(10,9,2,1);
ctx.fillRect(4,10,2,1);
ctx.fillRect(8,10,2,1);
*/
};
return function(ctx,frame,dirEnum,scared,flash,eyes_only,color) {
if (scared)
color = energizer.isFlash() ? "#FFF" : "#2121ff";
if (!eyes_only) {
// draw body
ctx.beginPath();
addHead(ctx);
if (frame == 0)
addFeet1(ctx);
else
addFeet2(ctx);
ctx.closePath();
ctx.fillStyle = color;
ctx.fill();
}
// draw face
if (scared)
addScaredFace(ctx, flash);
else
addEyes(ctx,dirEnum);
};
})();
// draw pacman body
var drawPacmanSprite = function(ctx,dirEnum,angle,mouthShift,scale,centerShift,alpha) {
if (mouthShift == undefined) mouthShift = 0;
if (centerShift == undefined) centerShift = 0;
if (scale == undefined) scale = 1;
if (alpha == undefined) alpha = 1;
ctx.save();
// rotate to current heading direction
var d90 = Math.PI/2;
if (dirEnum == DIR_UP) ctx.rotate(3*d90);
else if (dirEnum == DIR_RIGHT) ctx.rotate(0);
else if (dirEnum == DIR_DOWN) ctx.rotate(d90);
else if (dirEnum == DIR_LEFT) ctx.rotate(2*d90);
// plant corner of mouth
ctx.beginPath();
ctx.moveTo(-3+mouthShift,0);
// draw head outline
ctx.arc(centerShift,0,6.5*scale,angle,2*Math.PI-angle);
ctx.closePath();
ctx.fillStyle = "rgba(255,255,0," + alpha + ")";
ctx.fill();
ctx.restore();
};
var drawGhosts = function(ctx) {
var i=0;
var actorSize = (tileSize-1)*2;
for (i=0;i<4;i++) {
var g = ghosts[i];
ctx.save();
ctx.translate(g.pixel.x-actorSize/2+1, g.pixel.y-actorSize/2);
var frame = 0;
drawGhostSprite(ctx,frame,g.dirEnum,g.scared,false,false,g.color);
ctx.restore();
}
};
var drawPlayer = function(ctx) {
ctx.save();
ctx.translate(pacman.pixel.x+1, pacman.pixel.y);
var frame = 1;
drawPacmanSprite(ctx, pacman.dirEnum, frame*Math.PI/6);
ctx.restore();
};
var drawActors = function(ctx) {
drawGhosts(ctx);
drawPlayer(ctx);
};
// constructor
var Map = function(numCols, numRows, tiles) {
// sizes
this.numCols = numCols;
this.numRows = numRows;
this.numTiles = numCols*numRows;
this.widthPixels = numCols*tileSize;
this.heightPixels = numRows*tileSize;
// ascii map
this.tiles = tiles;
this.resetCurrent();
this.parseWalls();
};
// reset current tiles
Map.prototype.resetCurrent = function() {
this.currentTiles = this.tiles.split(""); // create a mutable list copy of an immutable string
};
// This is a procedural way to generate original-looking maps from a simple ascii tile
// map without a spritesheet.
Map.prototype.parseWalls = function() {
var that = this;
// creates a list of drawable canvas paths to render the map walls
this.paths = [];
// a map of wall tiles that already belong to a built path
var visited = {};
// we extend the x range to suggest the continuation of the tunnels
var toIndex = function(x,y) {
if (x>=-2 && x<that.numCols+2 && y>=0 && y<that.numRows)
return (x+2)+y*(that.numCols+4);
};
// a map of which wall tiles that are not completely surrounded by other wall tiles
var edges = {};
var i=0,x,y;
for (y=0;y<this.numRows;y++) {
for (x=-2;x<this.numCols+2;x++,i++) {
if (this.getTile(x,y) == '|' &&
(this.getTile(x-1,y) != '|' ||
this.getTile(x+1,y) != '|' ||
this.getTile(x,y-1) != '|' ||
this.getTile(x,y+1) != '|' ||
this.getTile(x-1,y-1) != '|' ||
this.getTile(x-1,y+1) != '|' ||
this.getTile(x+1,y-1) != '|' ||
this.getTile(x+1,y+1) != '|')) {
edges[i] = true;
}
}
}
// walks along edge wall tiles starting at the given index to build a canvas path
var makePath = function(tx,ty) {
// get initial direction
var dir = {};
var dirEnum;
if (toIndex(tx+1,ty) in edges)
dirEnum = DIR_RIGHT;
else if (toIndex(tx, ty+1) in edges)
dirEnum = DIR_DOWN;
else
throw "tile shouldn't be 1x1 at "+tx+","+ty;
setDirFromEnum(dir,dirEnum);
// increment to next tile
tx += dir.x;
ty += dir.y;
// backup initial location and direction
var init_tx = tx;
var init_ty = ty;
var init_dirEnum = dirEnum;
var path = [];
var pad; // (persists for each call to getStartPoint)
var point;
var lastPoint;
var turn,turnAround;
/*
We employ the 'right-hand rule' by keeping our right hand in contact
with the wall to outline an individual wall piece.
Since we parse the tiles in row major order, we will always start
walking along the wall at the leftmost tile of its topmost row. We
then proceed walking to the right.
When facing the direction of the walk at each tile, the outline will
hug the left side of the tile unless there is a walkable tile to the
left. In that case, there will be a padding distance applied.
*/
var getStartPoint = function(tx,ty,dirEnum) {
var dir = {};
setDirFromEnum(dir, dirEnum);
if (!(toIndex(tx+dir.y,ty-dir.x) in edges))
pad = that.isFloorTile(tx+dir.y,ty-dir.x) ? 5 : 0;
var px = -tileSize/2+pad;
var py = tileSize/2;
var a = dirEnum*Math.PI/2;
var c = Math.cos(a);
var s = Math.sin(a);
return {
// the first expression is the rotated point centered at origin
// the second expression is to translate it to the tile
x:(px*c - py*s) + (tx+0.5)*tileSize,
y:(px*s + py*c) + (ty+0.5)*tileSize,
};
};
while (true) {
visited[toIndex(tx,ty)] = true;
// determine start point
point = getStartPoint(tx,ty,dirEnum);
if (turn) {
// if we're turning into this tile, create a control point for the curve
//
// >---+ <- control point
// |
// V
lastPoint = path[path.length-1];
if (dir.x == 0) {
point.cx = point.x;
point.cy = lastPoint.y;
}
else {
point.cx = lastPoint.x;
point.cy = point.y;
}
}
// update direction
turn = false;
turnAround = false;
if (toIndex(tx+dir.y, ty-dir.x) in edges) { // turn left
dirEnum = (dirEnum+3)%4;
turn = true;
}
else if (toIndex(tx+dir.x, ty+dir.y) in edges) { // continue straight
}
else if (toIndex(tx-dir.y, ty+dir.x) in edges) { // turn right
dirEnum = (dirEnum+1)%4;
turn = true;
}
else { // turn around
dirEnum = (dirEnum+2)%4;
turnAround = true;
}
setDirFromEnum(dir,dirEnum);
// commit path point
path.push(point);
// special case for turning around (have to connect more dots manually)
if (turnAround) {
path.push(getStartPoint(tx-dir.x, ty-dir.y, (dirEnum+2)%4));
path.push(getStartPoint(tx, ty, dirEnum));
}
// advance to the next wall
tx += dir.x;
ty += dir.y;
// exit at full cycle
if (tx==init_tx && ty==init_ty && dirEnum == init_dirEnum) {
that.paths.push(path);
break;
}
}
};
// iterate through all edges, making a new path after hitting an unvisited wall edge
i=0;
for (y=0;y<this.numRows;y++)
for (x=-2;x<this.numCols+2;x++,i++)
if (i in edges && !(i in visited)) {
visited[i] = true;
makePath(x,y);
}
};
Map.prototype.posToIndex = function(x,y) {
if (x>=0 && x<this.numCols && y>=0 && y<this.numRows)
return x+y*this.numCols;
};
// retrieves tile character at given coordinate
// extended to include offscreen tunnel space
Map.prototype.getTile = function(x,y) {
if (x>=0 && x<this.numCols && y>=0 && y<this.numRows)
return this.currentTiles[this.posToIndex(x,y)];
// extend walls and paths outward for entrances and exits
if ((x==-1 && this.getTile(x+1,y)=='|' && (this.isFloorTile(x+1,y+1)||this.isFloorTile(x+1,y-1))) ||
(x==this.numCols && this.getTile(x-1,y)=='|' && (this.isFloorTile(x-1,y+1)||this.isFloorTile(x-1,y-1))))
return '|';
if ((x==-1 && this.isFloorTile(x+1,y)) ||
(x==this.numCols && this.isFloorTile(x-1,y)))
return ' ';
};
// determines if the given character is a walkable floor tile
Map.prototype.isFloorTileChar = function(tile) {
return tile==' ' || tile=='.' || tile=='o';
};
// determines if the given tile coordinate has a walkable floor tile
Map.prototype.isFloorTile = function(x,y) {
return this.isFloorTileChar(this.getTile(x,y));
};
// function to draw the map as a tile map
Map.prototype.draw = function(ctx,left,top,print) {
// save state
ctx.save();
//ctx.translate(0.5,0.5); // pixel perfect lines?
// translate to the position of the map
ctx.translate(left,top);
// clip the drawing surface
ctx.beginPath();
ctx.rect(0,0,this.widthPixels, this.heightPixels);
ctx.clip();
if (!print) {
ctx.fillStyle = "#000";
ctx.fillRect(0,0,this.widthPixels, this.heightPixels);
}
// set colors
ctx.fillStyle = print?"#333":this.wallFillColor;
ctx.strokeStyle = print?"#333":this.wallStrokeColor;
var x,y;
var i,j;
var tile;
for (i=0; i<this.paths.length; i++) {
var path = this.paths[i];
ctx.beginPath();
ctx.moveTo(path[0].x, path[0].y);
for (j=1; j<path.length; j++) {
if (path[j].cx != undefined)
ctx.quadraticCurveTo(path[j].cx, path[j].cy, path[j].x, path[j].y);
else
ctx.lineTo(path[j].x, path[j].y);
}
ctx.quadraticCurveTo(path[j-1].x, path[0].y, path[0].x, path[0].y);
ctx.fill();
ctx.stroke();
}
// draw pellets for each path tile
var pelletSize = print?tileSize:2;
var energizerSize = tileSize;
for (y=0; y<this.numRows; y++) {
for (x=0; x<this.numCols; x++) {
switch(this.getTile(x,y)) {
case '.':
ctx.fillStyle=print?"#bbb":this.pelletColor;
ctx.fillRect(
x*tileSize+tileSize/2-pelletSize/2,
y*tileSize+tileSize/2-pelletSize/2,
pelletSize,pelletSize);
break;
case 'o':
ctx.fillStyle = this.energizerColor;
ctx.beginPath();
ctx.arc(
x*tileSize+tileSize/2,
y*tileSize+tileSize/2,
energizerSize/2,
0,Math.PI*2);
ctx.fill();
break;
}
}
}
ctx.restore();
};
var mapgen = (function(){
var getRandomInt = function(min,max) {
return Math.floor(Math.random() * (max-min+1)) + min;
};
var shuffle = function(list) {
var len = list.length;
var i,j;
var temp;
for (i=0; i<len; i++) {
j = getRandomInt(0,len-1);
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
};
var randomElement = function(list) {
var len = list.length;
if (len > 0) {
return list[getRandomInt(0,len-1)];
}
};
var UP = 0;
var RIGHT = 1;
var DOWN = 2;
var LEFT = 3;
var cells = [];
var tallRows = [];
var narrowCols = [];
var rows = 9;
var cols = 5;
var reset = function() {
var i;
var c;
// initialize cells
for (i=0; i<rows*cols; i++) {
cells[i] = {
x: i%cols,
y: Math.floor(i/cols),
filled: false,
connect: [false, false, false, false],
next: [],
no: undefined,
group: undefined,
};
}
// allow each cell to refer to surround cells by direction
for (i=0; i<rows*cols; i++) {
var c = cells[i];
if (c.x > 0)
c.next[LEFT] = cells[i-1];
if (c.x < cols - 1)
c.next[RIGHT] = cells[i+1];
if (c.y > 0)
c.next[UP] = cells[i-cols];
if (c.y < rows - 1)
c.next[DOWN] = cells[i+cols];
}
// define the ghost home square
i = 3*cols;
c = cells[i];
c.filled=true;
c.connect[LEFT] = c.connect[RIGHT] = c.connect[DOWN] = true;
i++;
c = cells[i];
c.filled=true;
c.connect[LEFT] = c.connect[DOWN] = true;
i+=cols-1;
c = cells[i];
c.filled=true;
c.connect[LEFT] = c.connect[UP] = c.connect[RIGHT] = true;
i++;
c = cells[i];
c.filled=true;
c.connect[UP] = c.connect[LEFT] = true;
};
var genRandom = function() {
var getLeftMostEmptyCells = function() {
var x;
var leftCells = [];
for (x=0; x<cols; x++) {
for (y=0; y<rows; y++) {
var c = cells[x+y*cols];
if (!c.filled) {
leftCells.push(c);
}
}
if (leftCells.length > 0) {
break;
}
}
return leftCells;
};
var isOpenCell = function(cell,i,prevDir,size) {
// prevent wall from going through starting position
if (cell.y == 6 && cell.x == 0 && i == DOWN ||
cell.y == 7 && cell.x == 0 && i == UP) {
return false;
}
// prevent long straight pieces of length 3
if (size == 2 && (i==prevDir || (i+2)%4==prevDir)) {
return false;
}
// examine an adjacent empty cell
if (cell.next[i] && !cell.next[i].filled) {
// only open if the cell to the left of it is filled
if (cell.next[i].next[LEFT] && !cell.next[i].next[LEFT].filled) {
}
else {
return true;
}
}
return false;
};
var getOpenCells = function(cell,prevDir,size) {
var openCells = [];