-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpacman.js
More file actions
790 lines (703 loc) · 47.5 KB
/
pacman.js
File metadata and controls
790 lines (703 loc) · 47.5 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
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
/***********************************************************************/
// Global namespace wrapper
// Content of game.js
var Game = {
};
/***********************************************************************/
// Display object
// Content of display.js
Game.Display = function (width, height, tileWidth, tileHeight, sheet) {
this.width = width;
this.height = height;
this.tileWidth = tileWidth;
this.tileHeight = tileHeight;
this.sheet = sheet;
// Create canvas
var canvas = document.createElement("canvas");
this._context = canvas.getContext("2d");
this._context.canvas.width = this.width * this.tileWidth;
this._context.canvas.height = this.height * this.tileHeight;
this._context.textAlign="center";
// Create noticeboard and position it
this._noticeBoard = document.createElement("div");
this._noticeBoard.setAttribute("id", "game-area-notice");
this._noticeBoard.setAttribute("style", "width: "+this.width * this.tileWidth+"px; top: "+(this.height * this.tileHeight)/4+"px;");
this.clear();
this.hideNotice();
};
Game.Display.prototype.getContainer = function () {
var frag = document.createDocumentFragment();
frag.appendChild(this._context.canvas);
frag.appendChild(this._noticeBoard);
return frag;
};
Game.Display.prototype.drawTile = function (x, y, tile, frame, clear) {
if (clear) this.clearTile(x, y);
this._context.drawImage(this.sheet, (frame * this.tileWidth), (tile * this.tileHeight), this.tileWidth, this.tileHeight, (x * this.tileWidth), (y * this.tileHeight), this.tileWidth, this.tileHeight);
};
Game.Display.prototype.drawText = function(x, y, size, text) {
this._context.font = this.tileWidth*size+"px Verdana";
this._context.fillStyle = '#000000';
var width = Math.ceil((this._context.measureText(text).width)/this.tileWidth);
this.drawBlock(x-(width/2), y-size, width, 1, "#000000");
this._context.fillStyle = '#FFFFFF';
this._context.fillText(text, this.tileWidth*x, this.tileHeight*y);
}
Game.Display.prototype.drawBlock = function (x, y, w, h, color) {
this._context.fillStyle = color;
this._context.fillRect((x * this.tileWidth), (y * this.tileHeight), (w * this.tileWidth), (h * this.tileHeight));
};
Game.Display.prototype.clear = function () {
this._context.fillStyle = '#000000';
this._context.fillRect(0, 0, this.width * this.tileWidth, this.height * this.tileHeight);
};
Game.Display.prototype.clearTile = function (x, y) {
this.drawBlock(x, y, 1, 1, "#000000");
};
Game.Display.prototype.showNotice = function( text ) {
this._notice = true;
this._noticeBoard.innerHTML = text;
this._noticeBoard.style.visibility = "visible";
}
Game.Display.prototype.hideNotice = function() {
this._notice = false;
this._noticeBoard.innerHTML = "";
this._noticeBoard.style.visibility = "hidden";
}
/***********************************************************************/
// Engine object
// Content of engine.js
Game.Engine = function (size, width, height, container) {
this.tileSize = size;
this.width = width;
this.height = height;
this._qeue = [];
this._levels = [];
this._level = 0;
this._lock = 1;
this._running = false;
this.display;
this.map;
this._refresher;
this._framecounter = 0;
this._container = container
};
Game.Engine.prototype.init = function (levels, spriteSheet) {
this._levels = levels;
this.display = new Game.Display(this.width, this.height, this.tileSize, this.tileSize, spriteSheet);
this.map = new Game.Map(this, this.display, this.width, this.height);
this._container.appendChild(this.display.getContainer());
};
Game.Engine.prototype.loadLevel = function (index) {
this.map.clear();
if (index >= this._levels.length) {
return false;
};
// Hand level the map reference.
this._levels[index].load(this.map);
return true;
};
Game.Engine.prototype.start = function () {
if(this.loadLevel(this._level))
{
this.add(this.map);
this.add(new Game.Engine.Timer(150, this));
this._level++;
this._refresher = window.setInterval(this.refresh.bind(this), (1000/30) );
window.addEventListener("keydown", this.map._player);
this._running = true;
this.unlock();
}
};
Game.Engine.prototype.stop = function () {
this.lock();
window.removeEventListener("keydown", this.map._player);
window.clearInterval(this._refresher);
this.clear();
this._running = false;
};
Game.Engine.prototype.checkMap = function () {
if (! this._running ) { return; };
if ( this._levels[this._level-1].checkVictory(this.map) ) {
this.display.showNotice("All cakes on map have been nomed!</br>You ended up with score of: "+this.map.getPlayer().getScore()+" points.");
this.stop();
};
};
Game.Engine.prototype.refresh = function() {
if ( this._framecounter == 6 ) { this._framecounter = 0; };
this.map.draw( this._framecounter == 0 );
this._framecounter++;
};
////////////////////
// Scheduler code //
////////////////////
Game.Engine.prototype.lock = function () {
this._lock++;
return this;
};
Game.Engine.prototype.unlock = function () {
this._lock--;
while (!this._lock) {
var actor = this._next();
if (!actor) {
return this.lock();
}
var result = actor.act();
if (result && result.then) { // Returned thenable function, Promise
this.lock(); // Lock for now
result.then(this.unlock.bind(this)); // And bind promise for unlocking
}
}
return this;
};
Game.Engine.prototype.add = function(actor) {
if(!actor.act) throw "Not a valid actor";
this._qeue.push(actor);
}
Game.Engine.prototype._next = function() {
var current = this._qeue.pop();
this._qeue.unshift(current);
return current;
};
Game.Engine.prototype.clear = function() {
this._qeue = [];
}
/***********************************************************************/
// Map object
// Content of map.js
Game.Map = function (game, display, width, height) {
this._game = game;
this._display = display;
this._width = width;
this._height = height;
this._objects = [];
this._dirty = [];
this._objectCount = [];
this.clear();
};
// Clear Game state
Game.Map.prototype.clear = function() {
for (var i = 0; i < this._width; i++) {
this._objects[i] = [];
this._dirty[i] = [];
};
this._objectCount = [];
this._objectDefs = [];
this._dynamics = [];
this._player = null;
this._foodCount = 0;
this._started = false;
};
// Set scoreboard position and size
Game.Map.prototype.scoreBoard = function (x, y, size) {
this._scoreX = x;
this._scoreY = y;
this._scoreSize = size;
};
/**
* Add object definition
*/
Game.Map.prototype.defineObject = function (name, props) {
if (this._objectDefs[name]) {
throw "There is already a type of object named " + name + "!";
}
this._objectDefs[name] = props;
this._objectCount[name] = 0;
};
// Get type count
Game.Map.prototype.getCount = function (type) {
return this._objectCount[type];
};
// Add dynamic object to map
Game.Map.prototype.addDynamic = function(nx, ny, type) {
var x = Math.floor(nx);
var y = Math.floor(ny);
if (!this._objectDefs[type])
throw "There is no type of object named " + type + "!";
var actor = new Game.DynamicObject(type, x, y, this._game, this);
this._dynamics.push( actor );
};
// Place a static object into the grid
Game.Map.prototype.placeObject = function (nx, ny, type) {
var x = Math.floor(nx);
var y = Math.floor(ny);
if (!this._objectDefs[type])
throw "There is no type of object named " + type + "!";
if ( x == this.getPlayerX() && y == this.getPlayerY() )
throw "Cell occupied by a player!";
if (typeof(this._objects[x][y]) === 'undefined') { // Cell empty
this._objects[x][y] = type;
this._dirty[x][y] = true;
this._objectCount[type]++;
} else { // Cell Is full
throw "Cell " + x +":"+ y + " is already full!";
}
};
// Destroy static object on given indices
Game.Map.prototype.destroyObject = function(nx, ny) {
var x = Math.floor(nx);
var y = Math.floor(ny);
if (typeof(this._objects[x]) !== 'undefined') { // Row is instanced
if (typeof(this._objects[x][y]) !== 'undefined') { // Cell is not empty
this._objectCount[ this._objects[x][y] ]--;
delete this._objects[x][y];
this._dirty[x][y] = true;
return true;
}
}
return false;
};
// Place player into the grid
Game.Map.prototype.placePlayer = function (nx, ny, tile) {
var x = Math.floor(nx);
var y = Math.floor(ny);
if (this._player){
throw "Player already in place!";
} else {
this._player = new Game.Player(x, y, tile, this, this._game);
this._game.add(this._player);
}
};
// Player getter
Game.Map.prototype.getPlayer = function() {
if (this._player) {return this._player;}
};
// Player X getter
Game.Map.prototype.getPlayerX = function() {
if (this._player) { return this._player.getX(); };
};
// Player Y getter
Game.Map.prototype.getPlayerY = function() {
if (this._player) { return this._player.getY(); };
};
// Object def getter
Game.Map.prototype.getDefinition = function(type) {
return this._objectDefs[type];
};
// Redraw dirty parts of the map
Game.Map.prototype.draw = function( flip ) {
var dirties = 0;
for(var coll in this._dirty) {
for(var row in this._dirty[coll]) {
var objectDef = this._objectDefs[this._objects[coll][row]];
delete this._dirty[coll][row];
dirties++;
if (typeof(objectDef) === 'undefined') {
this._display.clearTile(coll, row);
continue;
};
if ( objectDef.tile ) {
if ( objectDef.frame ) {
this._display.drawTile( coll, row, objectDef.tile, objectDef.frame, true );
} else {
this._display.drawTile( coll, row, objectDef.tile, 0, true );
}
}
else if( objectDef.color ) {
this._display.drawBlock( coll , row , 1, 1, objectDef.color);
}
}
}
if (this._player) {this._display.drawTile( this._player.getX(),
this._player.getY(),
this._player.getTile(),
this._player.getFrame(flip),
true );
};
for (key in this._dynamics) {
var curr = this._dynamics[key];
this._display.drawTile( curr.getX(), curr.getY(), curr.getTile(), curr.getFrame(flip), true );
};
if (this._scoreSize) {
this._display.drawText(this._scoreX, this._scoreY, this._scoreSize, "Score:");
this._display.drawText(this._scoreX, this._scoreY+this._scoreSize, this._scoreSize, this._player.getScore());
};
};
// Check if object can move to given position
Game.Map.prototype.canMoveTo = function(nx, ny, type) {
var x = Math.floor(nx);
var y = Math.floor(ny);
if (x >= this._width || y >= this._height || x < 0 || y < 0 ) {return false};
if (typeof(this._objects[x]) === 'undefined') { // Row not instanced yet
return true;
} else if (typeof(this._objects[x][y]) === 'undefined') { // Cell empty
return true;
} else { // Cell Is occupied
if( this._objectDefs[ this._objects[x][y] ].impassable ) return false;
else return true;
}
};
// Return object at given coordinates (No player)
Game.Map.prototype.getObjectOn = function(nx, ny) {
var x = Math.floor(nx);
var y = Math.floor(ny);
if (typeof(this._objects[x]) !== 'undefined') { // Row is instanced
if (typeof(this._objects[x][y]) !== 'undefined') { // Cell is not empty
return this._objectDefs[this._objects[x][y]]; // Return object def
}
}
for (key in this._dynamics) {
var curr = this._dynamics[key];
if (curr.getX() == x && curr.getY() == y) {
return curr;
};
};
};
// Player move checks
Game.Map.prototype.playerMoveTo = function(nx, ny) {
var x = Math.floor(nx);
var y = Math.floor(ny);
// Flag dirty block around player
this._dirty[this._player.getX()][this._player.getY()] = true;
this._dirty[x][y] = true;
// Check dynamics collisions first
for (key in this._dynamics) {
var curr = this._dynamics[key];
if (curr.getX() == x && curr.getY() == y) {
return curr.onCollision(this._player);
};
};
if (typeof(this._objects[x][y]) !== 'undefined') { // Cell is not empty
if ( this._objectDefs[this._objects[x][y]].onCollision ) {
this._objectDefs[this._objects[x][y]].onCollision({x:x, y:y}, this._player);
};
};
this._started = true;
return true;
};
// Is there a dynamic object at given coordinates?
Game.Map.prototype.checkForDynamic = function(nx, ny) {
var x = Math.floor(nx);
var y = Math.floor(ny);
for (key in this._dynamics) {
var curr = this._dynamics[key];
if (curr.getX() == x && curr.getY() == y) {
return true;
};
};
};
Game.Map.prototype.playerKilled = function(killer) {
this._display.showNotice("You have been caught by: "+killer+"!</br>Your score is: "+this._player.getScore()+" point.");
//this.draw(false);
this._game.stop();
};
// Map turn.
// Move all pieces and do a flip redraw
Game.Map.prototype.act = function() {
if(this._started === false) {
if (!this._display._notice) {this._display.showNotice("Start the game by moving.</br>Use arrows to move around.");};
return;
};
if (this._display._notice) {this._display.hideNotice();};
this._game.checkMap();
for(key in this._dynamics) {
this._dirty[this._dynamics[key].getX()][this._dynamics[key].getY()] = true;
this._dynamics[key].act();
this._dirty[this._dynamics[key].getX()][this._dynamics[key].getY()] = true;
};
//this.draw(true);
};
Game.Engine.Timer = function (time, game) {
this._game = game;
this._time = time;
};
Game.Engine.Timer.prototype.act = function() {
this._game.lock();
var seconds = new Date().getTime() / 1000;
window.setTimeout(this.timeout.bind(this), this._time);
};
Game.Engine.Timer.prototype.timeout = function() {
this._game.unlock();
};Game.Level01 = {
load: function(map) {
map.defineObject("block", Game.Map.Wall);
map.defineObject("blinky", new Game.Map.Ghost(1, "Blinky"));
map.defineObject("pinky", new Game.Map.Ghost(2, "Pinky"));
map.defineObject("inky", new Game.Map.Ghost(3, "Inky"));
map.defineObject("clyde", new Game.Map.Ghost(4, "Clyde"));
map.defineObject("food", Game.Map.Food );
map.scoreBoard(21,13,1);
Game.Level01.buildMaze(map, this.Walls);
},
checkVictory: function(map) {
if (map.getCount('food') == 0) { return true; };
return false;
},
checkDefeat: function(map) {
return true;
},
buildMaze: function(map, Walls) {
for (var y = 0; y < Walls.length; y++) {
for (var x = 0; x < Walls[y].length; x++) {
switch( Walls[y][x] ) {
case 'W': map.placeObject(x, y, 'block');
break;
case '*': map.placeObject(x, y, 'food');
break;
case 'P': map.addDynamic(x, y, "pinky");
break;
case 'B': map.addDynamic(x, y, "blinky");
break;
case 'I' : map.addDynamic(x, y, "inky");
break;
case 'C' : map.addDynamic(x, y, "clyde");
break;
case 'O' : map.placePlayer(x, y, 0);
//map.placeObject(x+1, y, 'food');
break;
}
};
};
},
Walls: [
'WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW',
'WWW***********************************____***********************************WWW',
'WWW*WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW__O_WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW*WWW',
'W*************************************____*************************************W',
'W*W*WWWWWWWWWWWWWWWWW_WWWWWWWWWWWWWWWW_WW_WWWWWWWWWWWWWWWW_WWWWWWWWWWWWWWWWW*W*W',
'W*W*WW*******************************W_WW_W*******************************WW*W*W',
'W*W*WW*WWWWWWWWWWWWWWWWWWWWWWWWWWWWW*W_WW_W*WWWWWWWWWWWWWWWWWWWWWWWWWWWWW*WW*W*W',
'W*W*WW*WW*************************WW*W_WW_W*WW*************************WW*WW*W*W',
'W*W*WW*WW*WWWWWWWWWWWWWWWWWWWWWWW*WW*W_WW_W*WW*WWWWWWWWWWWWWWWWWWWWWWW*WW*WW*W*W',
'W*W*WW*WW*WWWWWWWWWWWWWWWWWWWWWWW*WW*W_WW_W*WW*WWWWWWWWWWWWWWWWWWWWWWW*WW*WW*W*W',
'W*W*WW*WW*WW___________________WW*WW*W_WW_W*WW*WWWWWWWWWWWWWWWWWWWWWWW*WW*WW*W*W',
'W*W*WW*WW*WW___________________WW*WW*W_WW_W*WW*WWWWWWWWWWWWWWWWWWWWWWW*WW*WW*W*W',
'W*W*WW*WW*WW___________________WW*WW*W_WW_W*WW*WWWWWWWWWWWWWWWWWWWWWWW*WW*WW*W*W',
'W*W*WW*WW*WW___________________WW*WW*W****W*WW*WWWWWWWWWWWWWWWWWWWWWWW*WW*WW*W*W',
'W*W*WW*__*WW___________________WW*WW*WWWWWW*WW*WWWWWWWWWWWWWWWWWWWWWWW*__*WW*W*W',
'W*W*WW*WW*WW___________________WW*WW********WW*WWWWWWWWWWWWWWWWWWWWWWW*WW*WW*W*W',
'W*W*WW*WW*WWWWWWWWWWWWWWWWWWWWWWW*WWWW_WW_WWWW*WWWWWWWWWWWWWWWWWWWWWWW*WW*WW*W*W',
'W*W*WW*WW*WWWWWWWWWWWWWWWWWWWWWWW*WWWW_WW_WWWW*WWWWWWWWWWWWWWWWWWWWWWW*WW*WW*W*W',
'W*W*WW*WW**************************************************************WW*WW*W*W',
'W*W*__*WWWWW_WWWWWWWWWWWWWWWWWWWWWWWWW_PI_WWWWWWWWWWWWWWWWWWWWWWWWW_WWWWW*__*W*W',
'W*W*WW*WWWWW_WWWWWWWWWWWWWWWWWWWWWWWWW_CB_WWWWWWWWWWWWWWWWWWWWWWWWW_WWWWW*WW*W*W',
'W*W*WW*WW**************************************************************WW*WW*W*W',
'W*W*WW*WW*WWWWWWWWWWWWWWWWWWWWWWW*WWWW_WW_WWWW*WWWWWWWWWWWWWWWWWWWWWWW*WW*WW*W*W',
'W*W*WW*WW*WWWWWWWWWWWWWWWWWWWWWWW*WWWW_WW_WWWW*WWWWWWWWWWWWWWWWWWWWWWW*WW*WW*W*W',
'W*W*WW*WW*WWWWWWWWWWWWWWWWWWWWWWW*WW********WW*WWWWWWWWWWWWWWWWWWWWWWW*WW*WW*W*W',
'W*W*WW*__*WWWWWWWWWWWWWWWWWWWWWWW*WW*WWWWWW*WW*WWWWWWWWWWWWWWWWWWWWWWW*__*WW*W*W',
'W*W*WW*WW*WWWWWWWWWWWWWWWWWWWWWWW*WW*W****W*WW*WWWWWWWWWWWWWWWWWWWWWWW*WW*WW*W*W',
'W*W*WW*WW*WWWWWWWWWWWWWWWWWWWWWWW*WW*W*WW*W*WW*WWWWWWWWWWWWWWWWWWWWWWW*WW*WW*W*W',
'W*W*WW*WW*WWWWWWWWWWWWWWWWWWWWWWW*WW*W*WW*W*WW*WWWWWWWWWWWWWWWWWWWWWWW*WW*WW*W*W',
'W*W*WW*WW*WWWWWWWWWWWWWWWWWWWWWWW*WW*W*WW*W*WW*WWWWWWWWWWWWWWWWWWWWWWW*WW*WW*W*W',
'W*W*WW*WW*WWWWWWWWWWWWWWWWWWWWWWW*WW*W*WW*W*WW*WWWWWWWWWWWWWWWWWWWWWWW*WW*WW*W*W',
'W*W*WW*WW*WWWWWWWWWWWWWWWWWWWWWWW*WW*W*WW*W*WW*WWWWWWWWWWWWWWWWWWWWWWW*WW*WW*W*W',
'W*W*WW*WW*************************WW*W*WW*W*WW*************************WW*WW*W*W',
'W*W*WW*WWWWWWWWWWWWWWWWWWWWWWWWWWWWW*W*WW*W*WWWWWWWWWWWWWWWWWWWWWWWWWWWWW*WW*W*W',
'W*W*WW*******************************W*WW*W*******************************WW*W*W',
'W*W*WWWWWWWWWWWWWWWWW_WWWWWWWWWWWWWWWW*WW*WWWWWWWWWWWWWWWW_WWWWWWWWWWWWWWWWW*W*W',
'W**************************************WW**************************************W',
'WWW*WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW****WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW*WWW',
'WWW************************************WW************************************WWW',
'WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW'
]
}/***********************************************************************/
// Dynamic object
// Content of dynamic.js
Game.DynamicObject = function (type, x, y, game, map) {
this._game = game;
this._map = map;
this._type = type;
this._x = x;
this._y = y;
this._def = map.getDefinition(type);
}
Game.DynamicObject.prototype.getTile = function() {
if (this._def.getTile) { return this._def.getTile(); }
else return this._def.tile;
}
Game.DynamicObject.prototype.getFrame = function(flip) {
if ( this._def.getFrame ) { return this._def.getFrame(flip) };
return 0;
}
Game.DynamicObject.prototype.act = function() {
if (this._def.turn)
this._def.turn(this, this._map);
};
Game.DynamicObject.prototype.getX = function() {
return this._x;
};
Game.DynamicObject.prototype.getY = function() {
return this._y;
};
Game.DynamicObject.prototype.canMove = function( direction ) {
switch(direction) {
case 2: return this._map.canMoveTo( this._x-1, this._y ) && !this._map.checkForDynamic( this._x-1, this._y );
break;
case 0: return this._map.canMoveTo( this._x+1, this._y ) && !this._map.checkForDynamic( this._x+1, this._y );
break;
case 4: return this._map.canMoveTo( this._x, this._y-1 ) && !this._map.checkForDynamic( this._x, this._y-1 );
break;
case 6: return this._map.canMoveTo( this._x, this._y+1 ) && !this._map.checkForDynamic( this._x, this._y+1 );
break;
}
}
Game.DynamicObject.prototype.move = function(direction) {
var newX = this._x; var newY = this._y;
switch(direction) {
case 2: newX--;
break;
case 0: newX++;
break;
case 4: newY--;
break;
case 6: newY++;
break;
}
// Check if player is on new indices
if ( this._map.getPlayerX() == newX && this._map.getPlayerY() == newY ) {
this.onCollision(this._map.getPlayer());
} else {
this._x = newX;
this._y = newY;
}
};
Game.DynamicObject.prototype.onCollision = function(player) {
if (this._def.onCollision) {
return this._def.onCollision(this, player);
} else {
return true;
}
}
Game.Map.Food = {
tile: 5,
frame: 1,
value: 10,
onCollision: function(me, player) {
me.value = this.value;
player.nom(me);
return true;
}
};Game.Map.Ghost = function(tile, name) {
this.dynamic = true;
this.impassable = true;
this.name = name;
this.tile = tile;
this._animation = 0;
this._brain = [2, 0, 6, 4];
this._dir = this._brain[0];
};
Game.Map.Ghost.prototype.turn = function(me, map){
var dirs = [];
for (var i = 0; i < this._brain.length; i++) {
if (me.canMove(this._brain[i])) {
dirs.push(this._brain[i]);
}
};
dirs = this.shuffle(dirs);
if (dirs.length == 3 ) {
me.move(dirs[0]);
this._dir = dirs[0];
} else if ( dirs.indexOf(this._dir) > -1 ) {
me.move( this._dir );
} else if ( dirs.length > 0 ) {
me.move(dirs[0]);
this._dir = dirs[0];
}
};
Game.Map.Ghost.prototype.onCollision = function(me, player) {
player.killedBy(this.name+" the ghost");
return true;
}
Game.Map.Ghost.prototype.getFrame = function(flip) {
var frame = this._animation;
if(flip){
if (this._animation == 0) { this._animation++;}
else {this._animation = 0; }
}
return this._dir+frame;
};
Game.Map.Ghost.prototype.shuffle = function(array) {
var tmp, current, top = array.length;
if(top) while(--top) {
current = Math.floor(Math.random() * (top + 1));
tmp = array[current];
array[current] = array[top];
array[top] = tmp;
}
return array;
}/***********************************************************************/
// Player object
// Content of player.js
Game.Player = function (x, y, tile, map, game) {
this._x = x;
this._y = y;
this.killed = false;
this._game = game;
this._map = map;
this._score = 0;
this.tile = tile;
this._dir = 0;
this._animation = 0;
this._keyMap = {};
this._keyMap[37] = 2; // LEFT
this._keyMap[38] = 4; // UP
this._keyMap[39] = 0; // RIGHT
this._keyMap[40] = 6; // DOWN
this._myTurn = false;
};
Game.Player.prototype.act = function() {
this._myTurn = true;
};
Game.Player.prototype.killedBy = function(killer) {
//this._game.lock();
this.killed = true;
this._map.playerKilled(killer);
};
Game.Player.prototype.nom = function ( food ) {
this._score+=food.value;
this._map.destroyObject(food.x, food.y);
};
Game.Player.prototype.getScore = function() {
return this._score;
}
Game.Player.prototype.getY = function() {
return this._y
};
Game.Player.prototype.getX = function() {
return this._x
};
Game.Player.prototype.getTile = function() {
return this.tile;
};
Game.Player.prototype.getFrame = function(flip) {
var frame = this._animation;
if(flip){
if (this._animation == 0) { this._animation++;}
else {this._animation = 0; }
}
return this._dir+frame;
};
Game.Player.prototype.move = function(newX, newY) {
if (!this._map.canMoveTo(newX, newY, 'player')) { return false; };
if (this._map.playerMoveTo(newX, newY)) {
this._x = newX;
this._y = newY;
return true;
}
return false;
};
Game.Player.prototype.handleEvent = function(e) {
var code = e.keyCode;
if(!this._myTurn) { return; }
if (!(code in this._keyMap)) { return; }
var newX = this._x;
var newY = this._y;
switch(code) {
case 37: newX--; break;
case 38: newY--; break;
case 39: newX++; break;
case 40: newY++; break;
}
if (this.move(newX, newY)) {
this._dir = this._keyMap[code] ;
this._map.draw(false); // Non flip call
this._myTurn = false;
};
};
Game.Map.Wall = {
impassable:true ,
// tile: 5,
// frame: 0,
color: "#666666"
};