Guest User

Untitled

a guest
Feb 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 49.33 KB | None | 0 0
  1. (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. 'use strict';
  3.  
  4. Object.defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. var Pacman = exports.Pacman = {};
  8.  
  9. Pacman.WALL = 0;
  10. Pacman.COOKIE = 1;
  11. Pacman.EMPTY = 2;
  12. Pacman.BLOCK = 3;
  13. Pacman.PILL = 4;
  14.  
  15. Pacman.FPS = 30;
  16.  
  17. Pacman.MAP = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 4, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 4, 0], [0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0], [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0], [0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0], [2, 2, 2, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 2, 2, 2], [0, 0, 0, 0, 1, 0, 1, 0, 0, 3, 0, 0, 1, 0, 1, 0, 0, 0, 0], [2, 2, 2, 2, 1, 1, 1, 0, 3, 3, 3, 0, 1, 1, 1, 2, 2, 2, 2], [0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0], [2, 2, 2, 0, 1, 0, 1, 1, 1, 2, 1, 1, 1, 0, 1, 0, 2, 2, 2], [0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0], [0, 4, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 4, 0], [0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0], [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0], [0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]];
  18.  
  19. Pacman.WALLS = [[{ 'move': [0, 9.5] }, { 'line': [3, 9.5] }, { 'curve': [3.5, 9.5, 3.5, 9] }, { 'line': [3.5, 8] }, { 'curve': [3.5, 7.5, 3, 7.5] }, { 'line': [1, 7.5] }, { 'curve': [0.5, 7.5, 0.5, 7] }, { 'line': [0.5, 1] }, { 'curve': [0.5, 0.5, 1, 0.5] }, { 'line': [9, 0.5] }, { 'curve': [9.5, 0.5, 9.5, 1] }, { 'line': [9.5, 3.5] }], [{ 'move': [9.5, 1] }, { 'curve': [9.5, 0.5, 10, 0.5] }, { 'line': [18, 0.5] }, { 'curve': [18.5, 0.5, 18.5, 1] }, { 'line': [18.5, 7] }, { 'curve': [18.5, 7.5, 18, 7.5] }, { 'line': [16, 7.5] }, { 'curve': [15.5, 7.5, 15.5, 8] }, { 'line': [15.5, 9] }, { 'curve': [15.5, 9.5, 16, 9.5] }, { 'line': [19, 9.5] }], [{ 'move': [2.5, 5.5] }, { 'line': [3.5, 5.5] }], [{ 'move': [3, 2.5] }, { 'curve': [3.5, 2.5, 3.5, 3] }, { 'curve': [3.5, 3.5, 3, 3.5] }, { 'curve': [2.5, 3.5, 2.5, 3] }, { 'curve': [2.5, 2.5, 3, 2.5] }], [{ 'move': [15.5, 5.5] }, { 'line': [16.5, 5.5] }], [{ 'move': [16, 2.5] }, { 'curve': [16.5, 2.5, 16.5, 3] }, { 'curve': [16.5, 3.5, 16, 3.5] }, { 'curve': [15.5, 3.5, 15.5, 3] }, { 'curve': [15.5, 2.5, 16, 2.5] }], [{ 'move': [6, 2.5] }, { 'line': [7, 2.5] }, { 'curve': [7.5, 2.5, 7.5, 3] }, { 'curve': [7.5, 3.5, 7, 3.5] }, { 'line': [6, 3.5] }, { 'curve': [5.5, 3.5, 5.5, 3] }, { 'curve': [5.5, 2.5, 6, 2.5] }], [{ 'move': [12, 2.5] }, { 'line': [13, 2.5] }, { 'curve': [13.5, 2.5, 13.5, 3] }, { 'curve': [13.5, 3.5, 13, 3.5] }, { 'line': [12, 3.5] }, { 'curve': [11.5, 3.5, 11.5, 3] }, { 'curve': [11.5, 2.5, 12, 2.5] }], [{ 'move': [7.5, 5.5] }, { 'line': [9, 5.5] }, { 'curve': [9.5, 5.5, 9.5, 6] }, { 'line': [9.5, 7.5] }], [{ 'move': [9.5, 6] }, { 'curve': [9.5, 5.5, 10.5, 5.5] }, { 'line': [11.5, 5.5] }], [{ 'move': [5.5, 5.5] }, { 'line': [5.5, 7] }, { 'curve': [5.5, 7.5, 6, 7.5] }, { 'line': [7.5, 7.5] }], [{ 'move': [6, 7.5] }, { 'curve': [5.5, 7.5, 5.5, 8] }, { 'line': [5.5, 9.5] }], [{ 'move': [13.5, 5.5] }, { 'line': [13.5, 7] }, { 'curve': [13.5, 7.5, 13, 7.5] }, { 'line': [11.5, 7.5] }], [{ 'move': [13, 7.5] }, { 'curve': [13.5, 7.5, 13.5, 8] }, { 'line': [13.5, 9.5] }], [{ 'move': [0, 11.5] }, { 'line': [3, 11.5] }, { 'curve': [3.5, 11.5, 3.5, 12] }, { 'line': [3.5, 13] }, { 'curve': [3.5, 13.5, 3, 13.5] }, { 'line': [1, 13.5] }, { 'curve': [0.5, 13.5, 0.5, 14] }, { 'line': [0.5, 17] }, { 'curve': [0.5, 17.5, 1, 17.5] }, { 'line': [1.5, 17.5] }], [{ 'move': [1, 17.5] }, { 'curve': [0.5, 17.5, 0.5, 18] }, { 'line': [0.5, 21] }, { 'curve': [0.5, 21.5, 1, 21.5] }, { 'line': [18, 21.5] }, { 'curve': [18.5, 21.5, 18.5, 21] }, { 'line': [18.5, 18] }, { 'curve': [18.5, 17.5, 18, 17.5] }, { 'line': [17.5, 17.5] }], [{ 'move': [18, 17.5] }, { 'curve': [18.5, 17.5, 18.5, 17] }, { 'line': [18.5, 14] }, { 'curve': [18.5, 13.5, 18, 13.5] }, { 'line': [16, 13.5] }, { 'curve': [15.5, 13.5, 15.5, 13] }, { 'line': [15.5, 12] }, { 'curve': [15.5, 11.5, 16, 11.5] }, { 'line': [19, 11.5] }], [{ 'move': [5.5, 11.5] }, { 'line': [5.5, 13.5] }], [{ 'move': [13.5, 11.5] }, { 'line': [13.5, 13.5] }], [{ 'move': [2.5, 15.5] }, { 'line': [3, 15.5] }, { 'curve': [3.5, 15.5, 3.5, 16] }, { 'line': [3.5, 17.5] }], [{ 'move': [16.5, 15.5] }, { 'line': [16, 15.5] }, { 'curve': [15.5, 15.5, 15.5, 16] }, { 'line': [15.5, 17.5] }], [{ 'move': [5.5, 15.5] }, { 'line': [7.5, 15.5] }], [{ 'move': [11.5, 15.5] }, { 'line': [13.5, 15.5] }], [{ 'move': [2.5, 19.5] }, { 'line': [5, 19.5] }, { 'curve': [5.5, 19.5, 5.5, 19] }, { 'line': [5.5, 17.5] }], [{ 'move': [5.5, 19] }, { 'curve': [5.5, 19.5, 6, 19.5] }, { 'line': [7.5, 19.5] }], [{ 'move': [11.5, 19.5] }, { 'line': [13, 19.5] }, { 'curve': [13.5, 19.5, 13.5, 19] }, { 'line': [13.5, 17.5] }], [{ 'move': [13.5, 19] }, { 'curve': [13.5, 19.5, 14, 19.5] }, { 'line': [16.5, 19.5] }], [{ 'move': [7.5, 13.5] }, { 'line': [9, 13.5] }, { 'curve': [9.5, 13.5, 9.5, 14] }, { 'line': [9.5, 15.5] }], [{ 'move': [9.5, 14] }, { 'curve': [9.5, 13.5, 10, 13.5] }, { 'line': [11.5, 13.5] }], [{ 'move': [7.5, 17.5] }, { 'line': [9, 17.5] }, { 'curve': [9.5, 17.5, 9.5, 18] }, { 'line': [9.5, 19.5] }], [{ 'move': [9.5, 18] }, { 'curve': [9.5, 17.5, 10, 17.5] }, { 'line': [11.5, 17.5] }], [{ 'move': [8.5, 9.5] }, { 'line': [8, 9.5] }, { 'curve': [7.5, 9.5, 7.5, 10] }, { 'line': [7.5, 11] }, { 'curve': [7.5, 11.5, 8, 11.5] }, { 'line': [11, 11.5] }, { 'curve': [11.5, 11.5, 11.5, 11] }, { 'line': [11.5, 10] }, { 'curve': [11.5, 9.5, 11, 9.5] }, { 'line': [10.5, 9.5] }]];
  20.  
  21. var KEY = exports.KEY = { 'BACKSPACE': 8, 'TAB': 9, 'NUM_PAD_CLEAR': 12, 'ENTER': 13, 'SHIFT': 16, 'CTRL': 17, 'ALT': 18, 'PAUSE': 19, 'CAPS_LOCK': 20, 'ESCAPE': 27, 'SPACEBAR': 32, 'PAGE_UP': 33, 'PAGE_DOWN': 34, 'END': 35, 'HOME': 36, 'ARROW_LEFT': 37, 'ARROW_UP': 38, 'ARROW_RIGHT': 39, 'ARROW_DOWN': 40, 'PRINT_SCREEN': 44, 'INSERT': 45, 'DELETE': 46, 'SEMICOLON': 59, 'WINDOWS_LEFT': 91, 'WINDOWS_RIGHT': 92, 'SELECT': 93, 'NUM_PAD_ASTERISK': 106, 'NUM_PAD_PLUS_SIGN': 107, 'NUM_PAD_HYPHEN-MINUS': 109, 'NUM_PAD_FULL_STOP': 110, 'NUM_PAD_SOLIDUS': 111, 'NUM_LOCK': 144, 'SCROLL_LOCK': 145, 'EQUALS_SIGN': 187, 'COMMA': 188, 'HYPHEN-MINUS': 189, 'FULL_STOP': 190, 'SOLIDUS': 191, 'GRAVE_ACCENT': 192, 'LEFT_SQUARE_BRACKET': 219, 'REVERSE_SOLIDUS': 220, 'RIGHT_SQUARE_BRACKET': 221, 'APOSTROPHE': 222, 'N': 78 };
  22.  
  23. var NONE = exports.NONE = 4,
  24. UP = exports.UP = 3,
  25. LEFT = exports.LEFT = 2,
  26. DOWN = exports.DOWN = 1,
  27. RIGHT = exports.RIGHT = 11,
  28. WAITING = exports.WAITING = 5,
  29. PAUSE = exports.PAUSE = 6,
  30. PLAYING = exports.PLAYING = 7,
  31. COUNTDOWN = exports.COUNTDOWN = 8,
  32. EATEN_PAUSE = exports.EATEN_PAUSE = 9,
  33. DYING = exports.DYING = 10;
  34.  
  35. },{}],2:[function(require,module,exports){
  36. 'use strict';
  37.  
  38. Object.defineProperty(exports, "__esModule", {
  39. value: true
  40. });
  41.  
  42. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  43.  
  44. var _constants = require('./constants.js');
  45.  
  46. var _map = require('./map.js');
  47.  
  48. var _map2 = _interopRequireDefault(_map);
  49.  
  50. var _player = require('./sprites/player.js');
  51.  
  52. var _player2 = _interopRequireDefault(_player);
  53.  
  54. var _ghost = require('./sprites/ghost.js');
  55.  
  56. var _ghost2 = _interopRequireDefault(_ghost);
  57.  
  58. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  59.  
  60. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  61.  
  62. var GameMechanics = function () {
  63. function GameMechanics() {
  64. _classCallCheck(this, GameMechanics);
  65.  
  66. this.state = _constants.WAITING;
  67. this.stateChanged = true;
  68. this.tick = 0;
  69. this.lastTime = 0;
  70. this.ctx = null;
  71. this.timer = null;
  72. this.stored = null;
  73. }
  74.  
  75. _createClass(GameMechanics, [{
  76. key: 'setState',
  77. value: function setState(nState) {
  78. this.state = nState;
  79. this.stateChanged = true;
  80. }
  81. }, {
  82. key: 'getTick',
  83. value: function getTick() {
  84. return this.tick;
  85. }
  86. }, {
  87. key: 'drawScore',
  88. value: function drawScore(text, position) {
  89. var x = position.new.x / 10 * this.map.blockSize;
  90. var y = (position.new.y + 5) / 10 * this.map.blockSize;
  91. this.drawText('#FFFFFF', text, x, y);
  92. }
  93. }, {
  94. key: 'dialog',
  95. value: function dialog(text) {
  96. var width = this.ctx.measureText(text).width;
  97. var x = (this.map.width * this.map.blockSize - width) / 2;
  98. var y = this.map.height * 10 + 8;
  99.  
  100. this.ctx.fillStyle = '#FFFF00';
  101. this.ctx.font = '12px Press_Start_2P';
  102. this.ctx.fillText(text, x, y);
  103. // this.drawText('#FFFF00', text, x, y);
  104. }
  105. }, {
  106. key: 'drawText',
  107. value: function drawText(color, text, position) {
  108. this.ctx.fillStyle = color;
  109. this.ctx.font = '8px Press_Start_2P';
  110. this.ctx.fillText(text, position.x, position.y);
  111. }
  112. }, {
  113. key: 'keyDown',
  114. value: function keyDown(ev) {
  115. if (ev.keyCode === _constants.KEY.N) {
  116. this.startNewGame();
  117. } else if (ev.keyCode === _constants.KEY.P && this.state === _constants.PAUSE) {
  118. this.map.draw(this.ctx);
  119. this.setState(this.stored);
  120. } else if (ev.keyCode === _constants.KEY.P) {
  121. this.stored = this.state;
  122. this.setState(_constants.PAUSE);
  123. this.map.draw(this.ctx);
  124. this.dialog('Paused');
  125. } else if (this.state !== _constants.PAUSE) {
  126. return this.player.keyDown(ev);
  127. }
  128. return true;
  129. }
  130. }, {
  131. key: 'setState',
  132. value: function setState(nState) {
  133. this.state = nState;
  134. this.stateChanged = true;
  135. }
  136. }, {
  137. key: 'drawFooter',
  138. value: function drawFooter() {
  139. var topLeft = this.map.height * this.map.blockSize;
  140. var textBase = topLeft + 17;
  141.  
  142. this.ctx.fillStyle = '#000000';
  143. this.ctx.fillRect(0, topLeft, this.map.width * this.map.blockSize, 30);
  144.  
  145. this.ctx.fillStyle = '#FFFF00';
  146.  
  147. var i = 0;
  148. var len = this.player.getLives();
  149. while (i < len) {
  150. this.ctx.fillStyle = '#FFFF00';
  151. this.ctx.beginPath();
  152. this.ctx.moveTo(150 + 25 * i + this.map.blockSize / 2, topLeft + 1 + this.map.blockSize / 2);
  153.  
  154. this.ctx.arc(150 + 25 * i + this.map.blockSize / 2, topLeft + 1 + this.map.blockSize / 2, this.map.blockSize / 2, Math.PI * 0.25, Math.PI * 1.75, false);
  155. this.ctx.fill();
  156. i += 1;
  157. }
  158.  
  159. this.ctx.fillStyle = '#FFFF00';
  160. this.ctx.font = "10px Press_Start_2P";
  161. this.ctx.fillText('Score: ' + this.player.getScore(), 30, textBase);
  162. this.ctx.fillText('Level: ' + this.level, 260, textBase);
  163. }
  164. }, {
  165. key: 'redrawBlock',
  166. value: function redrawBlock(pos) {
  167. this.map.drawBlock(Math.floor(pos.y / 10), Math.floor(pos.x / 10), this.ctx);
  168. this.map.drawBlock(Math.ceil(pos.y / 10), Math.ceil(pos.x / 10), this.ctx);
  169. }
  170. }, {
  171. key: 'mainDraw',
  172. value: function mainDraw() {
  173.  
  174. var p = void 0,
  175. nScore = void 0;
  176.  
  177. this.ghostPos = [];
  178.  
  179. var i = 0;
  180. var len = this.ghosts.length;
  181.  
  182. while (i < len) {
  183. this.ghostPos.push(this.ghosts[i].move(this.ctx));
  184. i += 1;
  185. }
  186.  
  187. p = this.player.move(this.ctx);
  188.  
  189. for (i = 0, len = this.ghosts.length; i < len; i += 1) {
  190. this.redrawBlock(this.ghostPos[i].old);
  191. }
  192. this.redrawBlock(p.old);
  193.  
  194. for (i = 0, len = this.ghosts.length; i < len; i += 1) {
  195. this.ghosts[i].draw(this.ctx);
  196. }
  197. this.player.draw(this.ctx);
  198.  
  199. this.userPos = p.new;
  200.  
  201. for (i = 0, len = this.ghosts.length; i < len; i += 1) {
  202. if (this.collided(this.userPos, this.ghostPos[i].new)) {
  203. if (this.ghosts[i].isVunerable()) {
  204. this.ghosts[i].eat();
  205. this.eatenCount += 1;
  206. nScore = this.eatenCount * 50;
  207. this.drawScore(nScore, this.ghostPos[i]);
  208. this.player.addScore(nScore);
  209. this.setState(_constants.EATEN_PAUSE);
  210. this.timerStart = this.tick;
  211. } else if (this.ghosts[i].isDangerous()) {
  212. this.setState(_constants.DYING);
  213. this.timerStart = this.tick;
  214. }
  215. }
  216. }
  217. }
  218. }, {
  219. key: 'keyPress',
  220. value: function keyPress(e) {
  221. if (this.state !== _constants.WAITING && this.state !== _constants.PAUSE) {
  222. e.preventDefault();
  223. e.stopPropagation();
  224. }
  225. }
  226. }, {
  227. key: 'init',
  228. value: function init(wrapper) {
  229. var i = void 0,
  230. len = this.ghostColors.length;
  231. var ghost = void 0;
  232. var blockSize = wrapper.offsetWidth / 19;
  233. var canvas = document.createElement('canvas');
  234.  
  235. canvas.setAttribute('width', blockSize * 19 + 'px');
  236. canvas.setAttribute('height', blockSize * 22 + 30 + 'px');
  237.  
  238. wrapper.appendChild(canvas);
  239. this.ctx = canvas.getContext('2d');
  240.  
  241. this.map = new _map2.default(blockSize);
  242. this.player = new _player2.default(this, this.map);
  243.  
  244. for (i = 0; i < len; i += 1) {
  245. ghost = new _ghost2.default(this, this.map, this.ghostColors[i]);
  246. this.ghosts.push(ghost);
  247. }
  248.  
  249. this.map.draw(this.ctx);
  250.  
  251. this.dialog('Press N to Start');
  252.  
  253. document.addEventListener('keydown', function (e) {
  254. if (!e) {
  255. return;
  256. }
  257. this.keyDown(e);
  258. }.bind(this), true);
  259. document.addEventListener('keypress', function (e) {
  260. if (!e) {
  261. return;
  262. }
  263. this.keyPress(e);
  264. }.bind(this), true);
  265.  
  266. this.timer = window.setInterval(function (self) {
  267. //Self-executing func which takes 'this' as self
  268. return function () {
  269. //Return a function in the context of 'self'
  270. self.mainLoop(); //Thing you wanted to run as non-window 'this'
  271. };
  272. }(this), 1000 / _constants.Pacman.FPS);
  273. }
  274. }]);
  275.  
  276. return GameMechanics;
  277. }();
  278.  
  279. exports.default = GameMechanics;
  280.  
  281. },{"./constants.js":1,"./map.js":6,"./sprites/ghost.js":7,"./sprites/player.js":8}],3:[function(require,module,exports){
  282. 'use strict';
  283.  
  284. Object.defineProperty(exports, "__esModule", {
  285. value: true
  286. });
  287.  
  288. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  289.  
  290. var _constants = require('./constants.js');
  291.  
  292. var _map = require('./map.js');
  293.  
  294. var _map2 = _interopRequireDefault(_map);
  295.  
  296. var _player = require('./sprites/player.js');
  297.  
  298. var _player2 = _interopRequireDefault(_player);
  299.  
  300. var _ghost = require('./sprites/ghost.js');
  301.  
  302. var _ghost2 = _interopRequireDefault(_ghost);
  303.  
  304. var _gameMechanics = require('./game-mechanics');
  305.  
  306. var _gameMechanics2 = _interopRequireDefault(_gameMechanics);
  307.  
  308. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  309.  
  310. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  311.  
  312. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  313.  
  314. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  315.  
  316. var Game = function (_GameMechanics) {
  317. _inherits(Game, _GameMechanics);
  318.  
  319. function Game(wrapper) {
  320. _classCallCheck(this, Game);
  321.  
  322. var _this = _possibleConstructorReturn(this, (Game.__proto__ || Object.getPrototypeOf(Game)).call(this, wrapper));
  323.  
  324. _this.ghosts = [];
  325. _this.ghostColors = ['#00FFDE', '#FF0000', '#FFB8DE', '#FFB847'];
  326. _this.eatenCount = 0;
  327. _this.level = 0;
  328. _this.ghostPos = null;
  329. _this.playerPos = null;
  330. _this.timerStart = null;
  331. _this.map = null;
  332. _this.player = null;
  333. _this.init(wrapper);
  334. return _this;
  335. }
  336.  
  337. _createClass(Game, [{
  338. key: 'startLevel',
  339. value: function startLevel() {
  340. this.player.newLevel();
  341. var i = 0;
  342. while (i < this.ghosts.length) {
  343. this.ghosts[i].reset();
  344. i += 1;
  345. }
  346. this.timerStart = this.tick;
  347. this.setState(_constants.COUNTDOWN);
  348. }
  349. }, {
  350. key: 'startNewGame',
  351. value: function startNewGame() {
  352. this.setState(_constants.WAITING);
  353. this.level = 1;
  354. this.player.reset();
  355. this.map.initMap();
  356. this.map.draw(this.ctx);
  357. this.startLevel();
  358. }
  359. }, {
  360. key: 'loseLife',
  361. value: function loseLife() {
  362. this.setState(_constants.WAITING);
  363. this.player.loseLife();
  364. if (this.player.getLives() > 0) {
  365. this.startLevel();
  366. }
  367. }
  368. }, {
  369. key: 'collided',
  370. value: function collided(player, ghost) {
  371. return Math.sqrt(Math.pow(ghost.x - player.x, 2) + Math.pow(ghost.y - player.y, 2)) < 10;
  372. }
  373. }, {
  374. key: 'mainLoop',
  375. value: function mainLoop() {
  376. var diff = void 0;
  377.  
  378. if (this.state !== _constants.PAUSE) {
  379. ++this.tick;
  380. }
  381.  
  382. this.map.drawPills(this.ctx);
  383.  
  384. if (this.state === _constants.PLAYING) {
  385. this.mainDraw();
  386. } else if (this.state === _constants.WAITING && this.stateChanged) {
  387. this.stateChanged = false;
  388. this.map.draw(this.ctx);
  389. this.dialog('Press N to start a New game');
  390. } else if (this.state === _constants.EATEN_PAUSE && this.tick - this.timerStart > _constants.Pacman.FPS / 3) {
  391. this.map.draw(this.ctx);
  392. this.setState(_constants.PLAYING);
  393. } else if (this.state === _constants.DYING) {
  394. if (this.tick - this.timerStart > _constants.Pacman.FPS * 2) {
  395. this.loseLife();
  396. } else {
  397. this.redrawBlock(this.userPos);
  398. for (var i = 0; i < this.ghosts.length; i += 1) {
  399. this.redrawBlock(this.ghostPos[i].old);
  400. this.ghostPos.push(this.ghosts[i].draw(this.ctx));
  401. }
  402. this.player.drawDead(this.ctx, (this.tick - this.timerStart) / (_constants.Pacman.FPS * 2));
  403. }
  404. } else if (this.state === _constants.COUNTDOWN) {
  405.  
  406. diff = 5 + Math.floor((this.timerStart - this.tick) / _constants.Pacman.FPS);
  407.  
  408. if (diff === 0) {
  409. this.map.draw(this.ctx);
  410. this.setState(_constants.PLAYING);
  411. } else {
  412. if (diff !== this.lastTime) {
  413. this.lastTime = diff;
  414. this.map.draw(this.ctx);
  415. this.dialog('Starting in: ' + diff);
  416. }
  417. }
  418. }
  419.  
  420. this.drawFooter();
  421. }
  422. }, {
  423. key: 'eatenPill',
  424. value: function eatenPill() {
  425. this.timerStart = this.tick;
  426. this.eatenCount = 0;
  427. for (var i = 0; i < this.ghosts.length; i += 1) {
  428. this.ghosts[i].makeEatable(this.ctx);
  429. }
  430. }
  431. }, {
  432. key: 'completedLevel',
  433. value: function completedLevel() {
  434. this.setState(_constants.WAITING);
  435. this.level += 1;
  436. this.map.reset();
  437. this.player.newLevel();
  438. this.startLevel();
  439. }
  440. }]);
  441.  
  442. return Game;
  443. }(_gameMechanics2.default);
  444.  
  445. exports.default = Game;
  446.  
  447. },{"./constants.js":1,"./game-mechanics":2,"./map.js":6,"./sprites/ghost.js":7,"./sprites/player.js":8}],4:[function(require,module,exports){
  448. 'use strict';
  449.  
  450. var _game = require('./game.js');
  451.  
  452. var _game2 = _interopRequireDefault(_game);
  453.  
  454. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  455.  
  456. var wrapper = document.getElementById('pacman');
  457. var game = new _game2.default(wrapper);
  458.  
  459. },{"./game.js":3}],5:[function(require,module,exports){
  460. 'use strict';
  461.  
  462. Object.defineProperty(exports, "__esModule", {
  463. value: true
  464. });
  465.  
  466. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  467.  
  468. var _constants = require('./constants.js');
  469.  
  470. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  471.  
  472. var MapSprite = function () {
  473. function MapSprite() {
  474. _classCallCheck(this, MapSprite);
  475. }
  476.  
  477. _createClass(MapSprite, [{
  478. key: 'drawPills',
  479. value: function drawPills(ctx) {
  480. if (++this.pillSize > 30) {
  481. this.pillSize = 0;
  482. }
  483. var i = 0;
  484. while (i < this.height) {
  485. var j = 0;
  486. while (j < this.width) {
  487. if (this.map[i][j] === _constants.Pacman.PILL) {
  488. ctx.beginPath();
  489.  
  490. ctx.fillStyle = '#000';
  491. ctx.fillRect(j * this.blockSize, i * this.blockSize, this.blockSize, this.blockSize);
  492.  
  493. ctx.fillStyle = '#FFF';
  494. ctx.arc(j * this.blockSize + this.blockSize / 2, i * this.blockSize + this.blockSize / 2, Math.abs(5 - this.pillSize / 3), 0, Math.PI * 2, false);
  495. ctx.fill();
  496. ctx.closePath();
  497. }
  498. j += 1;
  499. }
  500. i += 1;
  501. }
  502. }
  503. }, {
  504. key: 'draw',
  505. value: function draw(ctx) {
  506. var i = void 0,
  507. j = void 0;
  508. var size = this.blockSize;
  509.  
  510. ctx.fillStyle = '#000';
  511. ctx.fillRect(0, 0, this.width * size, this.height * size);
  512.  
  513. this.drawWall(ctx);
  514.  
  515. i = 0;
  516. while (i < this.height) {
  517. j = 0;
  518. while (j < this.width) {
  519. this.drawBlock(i, j, ctx);
  520. j += 1;
  521. }
  522. i += 1;
  523. }
  524. }
  525. }, {
  526. key: 'drawBlock',
  527. value: function drawBlock(y, x, ctx) {
  528. var layout = this.map[y][x];
  529.  
  530. if (layout === _constants.Pacman.PILL) {
  531. return;
  532. }
  533.  
  534. ctx.beginPath();
  535.  
  536. if (layout === _constants.Pacman.EMPTY || layout === _constants.Pacman.BLOCK || layout === _constants.Pacman.COOKIE) {
  537.  
  538. ctx.fillStyle = '#000';
  539. ctx.fillRect(x * this.blockSize, y * this.blockSize, this.blockSize, this.blockSize);
  540.  
  541. if (layout === _constants.Pacman.COOKIE) {
  542. ctx.fillStyle = '#FFF';
  543. ctx.fillRect(x * this.blockSize + this.blockSize / 2.5, y * this.blockSize + this.blockSize / 2.5, this.blockSize / 6, this.blockSize / 6);
  544. }
  545. }
  546. ctx.closePath();
  547. }
  548. }, {
  549. key: 'drawWall',
  550. value: function drawWall(ctx) {
  551.  
  552. var i = void 0,
  553. j = void 0,
  554. p = void 0,
  555. line = void 0;
  556.  
  557. ctx.strokeStyle = '#2ecc71';
  558. ctx.lineWidth = 5;
  559. ctx.lineCap = 'round';
  560.  
  561. i = 0;
  562. while (i < _constants.Pacman.WALLS.length) {
  563. line = _constants.Pacman.WALLS[i];
  564. ctx.beginPath();
  565.  
  566. j = 0;
  567. while (j < line.length) {
  568. p = line[j];
  569. if (p.move) {
  570. ctx.moveTo(p.move[0] * this.blockSize, p.move[1] * this.blockSize);
  571. } else if (p.line) {
  572. ctx.lineTo(p.line[0] * this.blockSize, p.line[1] * this.blockSize);
  573. } else if (p.curve) {
  574. ctx.quadraticCurveTo(p.curve[0] * this.blockSize, p.curve[1] * this.blockSize, p.curve[2] * this.blockSize, p.curve[3] * this.blockSize);
  575. }
  576.  
  577. j += 1;
  578. }
  579. ctx.stroke();
  580.  
  581. i += 1;
  582. }
  583. }
  584. }]);
  585.  
  586. return MapSprite;
  587. }();
  588.  
  589. exports.default = MapSprite;
  590.  
  591. },{"./constants.js":1}],6:[function(require,module,exports){
  592. 'use strict';
  593.  
  594. Object.defineProperty(exports, "__esModule", {
  595. value: true
  596. });
  597.  
  598. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  599.  
  600. var _constants = require('./constants.js');
  601.  
  602. var _mapSprite = require('./map-sprite');
  603.  
  604. var _mapSprite2 = _interopRequireDefault(_mapSprite);
  605.  
  606. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  607.  
  608. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  609.  
  610. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  611.  
  612. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  613.  
  614. var Map = function (_MapSprite) {
  615. _inherits(Map, _MapSprite);
  616.  
  617. function Map(blockSize) {
  618. _classCallCheck(this, Map);
  619.  
  620. var _this = _possibleConstructorReturn(this, (Map.__proto__ || Object.getPrototypeOf(Map)).call(this));
  621.  
  622. _this.initMap();
  623. _this.blockSize = blockSize;
  624. _this.pillSize = 0;
  625. return _this;
  626. }
  627.  
  628. _createClass(Map, [{
  629. key: 'initMap',
  630. value: function initMap() {
  631. this.map = _constants.Pacman.MAP;
  632. this.height = this.map.length;
  633. this.width = this.map[0].length;
  634. }
  635. }, {
  636. key: 'getBlock',
  637. value: function getBlock(pos) {
  638. return this.map[pos.y][pos.x];
  639. }
  640. }, {
  641. key: 'setBlock',
  642. value: function setBlock(pos, type) {
  643. this.map[pos.y][pos.x] = type;
  644. }
  645.  
  646. // Laten schrijven door kinderen
  647.  
  648. }, {
  649. key: 'withinMap',
  650. value: function withinMap(x, y) {
  651. return x >= 0 && x < this.width && y >= 0 && y < this.height;
  652. }
  653. }, {
  654. key: 'isWall',
  655. value: function isWall(pos) {
  656. var withinMap = this.withinMap(pos.x, pos.y);
  657. var isWallBlock = this.getBlock(pos) === _constants.Pacman.WALL;
  658. return withinMap && isWallBlock;
  659. }
  660. }, {
  661. key: 'isFloor',
  662. value: function isFloor(pos) {
  663. if (!this.withinMap(pos.x, pos.y)) {
  664. return false;
  665. }
  666. var block = this.getBlock(pos);
  667. return block === _constants.Pacman.EMPTY || block === _constants.Pacman.COOKIE || block === _constants.Pacman.PILL;
  668. }
  669. }]);
  670.  
  671. return Map;
  672. }(_mapSprite2.default);
  673.  
  674. exports.default = Map;
  675.  
  676. },{"./constants.js":1,"./map-sprite":5}],7:[function(require,module,exports){
  677. 'use strict';
  678.  
  679. Object.defineProperty(exports, "__esModule", {
  680. value: true
  681. });
  682.  
  683. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  684.  
  685. var _constants = require('../constants.js');
  686.  
  687. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  688.  
  689. var Ghost = function () {
  690. function Ghost(game, map, colour) {
  691. _classCallCheck(this, Ghost);
  692.  
  693. this.position = {
  694. x: 90,
  695. y: 80
  696. };
  697. this.direction = this.getRandomDirection();
  698. this.due = this.getRandomDirection();
  699. this.eatable = null;
  700. this.eaten = null;
  701. this.colour = colour;
  702. this.game = game;
  703. this.map = map;
  704. }
  705.  
  706. _createClass(Ghost, [{
  707. key: 'getNewCoord',
  708. value: function getNewCoord(dir, current) {
  709. var speed = void 0;
  710. if (this.isVunerable()) {
  711. speed = 0.25;
  712. } else if (this.isHidden()) {
  713. speed = 2;
  714. } else {
  715. speed = 0.5;
  716. }
  717. var xSpeed = dir === _constants.LEFT && -speed || dir === _constants.RIGHT && speed || 0;
  718. var ySpeed = dir === _constants.DOWN && speed || dir === _constants.UP && -speed || 0;
  719.  
  720. var x = this.addBounded(current.x, xSpeed);
  721. var y = this.addBounded(current.y, ySpeed);
  722. return { x: x, y: y };
  723. }
  724.  
  725. /* Keep this method */
  726.  
  727. }, {
  728. key: 'addBounded',
  729. value: function addBounded(x1, x2) {
  730. var rem = x1 % 10;
  731. var result = rem + x2;
  732.  
  733. if (rem !== 0 && result > 10) {
  734. return x1 + (10 - rem);
  735. } else if (rem > 0 && result < 0) {
  736. return x1 - rem;
  737. }
  738.  
  739. return x1 + x2;
  740. }
  741. }, {
  742. key: 'isVunerable',
  743. value: function isVunerable() {
  744. return this.eatable !== null;
  745. }
  746. }, {
  747. key: 'isDangerous',
  748. value: function isDangerous() {
  749. return this.eaten === null;
  750. }
  751. }, {
  752. key: 'isHidden',
  753. value: function isHidden() {
  754. return !this.isVunerable() && !this.isDangerous();
  755. }
  756. }, {
  757. key: 'getRandomDirection',
  758. value: function getRandomDirection() {
  759. var moves = void 0;
  760. if (this.direction === _constants.LEFT || this.direction === _constants.RIGHT) {
  761. moves = [_constants.UP, _constants.DOWN];
  762. } else {
  763. moves = [_constants.LEFT, _constants.RIGHT];
  764. }
  765. return moves[Math.floor(Math.random() * 2)];
  766. }
  767. }, {
  768. key: 'reset',
  769. value: function reset() {
  770. this.eaten = null;
  771. this.eatable = null;
  772. this.position = {
  773. x: 90,
  774. y: 80
  775. };
  776. this.direction = this.getRandomDirection();
  777. this.due = this.getRandomDirection();
  778. }
  779. }, {
  780. key: 'onWholeSquare',
  781. value: function onWholeSquare(x) {
  782. return x % 10 === 0;
  783. }
  784. }, {
  785. key: 'oppositeDirection',
  786. value: function oppositeDirection(dir) {
  787. var oppositeDirection = void 0;
  788. /* No switch statements allowed? Got a esinting error */
  789. switch (dir) {
  790. case _constants.LEFT:
  791. oppositeDirection = _constants.RIGHT;
  792. break;
  793. case _constants.RIGHT:
  794. oppositeDirection = _constants.LEFT;
  795. break;
  796. case _constants.UP:
  797. oppositeDirection = _constants.DOWN;
  798. break;
  799. case _constants.DOWN:
  800. oppositeDirection = _constants.UP;
  801. break;
  802. }
  803. return oppositeDirection;
  804. }
  805. }, {
  806. key: 'makeEatable',
  807. value: function makeEatable() {
  808. this.direction = this.oppositeDirection(this.direction);
  809. this.eatable = this.game.getTick();
  810. }
  811. }, {
  812. key: 'eat',
  813. value: function eat() {
  814. this.eatable = null;
  815. this.eaten = this.game.getTick();
  816. }
  817. }, {
  818. key: 'pointToCoord',
  819. value: function pointToCoord(x) {
  820. return Math.round(x / 10);
  821. }
  822. }, {
  823. key: 'nextSquare',
  824. value: function nextSquare(x, dir) {
  825. var rem = x % 10;
  826. if (rem === 0) {
  827. return x;
  828. } else if (dir === _constants.RIGHT || dir === _constants.DOWN) {
  829. return x + (10 - rem);
  830. }
  831. return x - rem;
  832. }
  833. }, {
  834. key: 'onGridSquare',
  835. value: function onGridSquare(pos) {
  836. return this.onWholeSquare(pos.y) && this.onWholeSquare(pos.x);
  837. }
  838. }, {
  839. key: 'secondsAgo',
  840. value: function secondsAgo(tick) {
  841. return (this.game.getTick() - tick) / _constants.Pacman.FPS;
  842. }
  843. }, {
  844. key: 'getColour',
  845. value: function getColour() {
  846. if (this.eatable) {
  847. if (this.secondsAgo(this.eatable) > 5) {
  848. if (this.game.getTick() % 20 > 10) {
  849. return '#FFFFFF';
  850. }
  851. return '#0000BB';
  852. }
  853. return '#0000BB';
  854. } else if (this.eaten) {
  855. return '#222';
  856. }
  857. return this.colour;
  858. }
  859. }, {
  860. key: 'draw',
  861. value: function draw(ctx) {
  862. var s = this.map.blockSize;
  863. var top = this.position.y / 10 * s;
  864. var left = this.position.x / 10 * s;
  865.  
  866. if (this.eatable && this.secondsAgo(this.eatable) > 8) {
  867. this.eatable = null;
  868. }
  869.  
  870. if (this.eaten && this.secondsAgo(this.eaten) > 3) {
  871. this.eaten = null;
  872. }
  873.  
  874. var tl = left + s;
  875. var base = top + s - 3;
  876. var inc = s / 10;
  877.  
  878. var high = void 0;
  879. if (this.game.getTick() % 10 > 5) {
  880. high = 3;
  881. } else {
  882. high = -3;
  883. }
  884.  
  885. var low = void 0;
  886. if (this.game.getTick() % 10 > 5) {
  887. low = -3;
  888. } else {
  889. low = 3;
  890. }
  891.  
  892. ctx.fillStyle = this.getColour();
  893. ctx.beginPath();
  894.  
  895. ctx.moveTo(left, base);
  896.  
  897. ctx.quadraticCurveTo(left, top, left + s / 2, top);
  898. ctx.quadraticCurveTo(left + s, top, left + s, base);
  899.  
  900. // Wavy things at the bottom
  901. ctx.quadraticCurveTo(tl - inc * 1, base + high, tl - inc * 2, base);
  902. ctx.quadraticCurveTo(tl - inc * 3, base + low, tl - inc * 4, base);
  903. ctx.quadraticCurveTo(tl - inc * 5, base + high, tl - inc * 6, base);
  904. ctx.quadraticCurveTo(tl - inc * 7, base + low, tl - inc * 8, base);
  905. ctx.quadraticCurveTo(tl - inc * 9, base + high, tl - inc * 10, base);
  906.  
  907. ctx.closePath();
  908. ctx.fill();
  909.  
  910. ctx.beginPath();
  911. ctx.fillStyle = '#FFF';
  912. ctx.arc(left + 6, top + 6, s / 6, 0, 300, false);
  913. ctx.arc(left + s - 6, top + 6, s / 6, 0, 300, false);
  914. ctx.closePath();
  915. ctx.fill();
  916.  
  917. var f = s / 12;
  918. var off = {};
  919. off[_constants.RIGHT] = [f, 0];
  920. off[_constants.LEFT] = [-f, 0];
  921. off[_constants.UP] = [0, -f];
  922. off[_constants.DOWN] = [0, f];
  923.  
  924. ctx.beginPath();
  925. ctx.fillStyle = '#000';
  926. ctx.arc(left + 6 + off[this.direction][0], top + 6 + off[this.direction][1], s / 15, 0, 300, false);
  927. ctx.arc(left + s - 6 + off[this.direction][0], top + 6 + off[this.direction][1], s / 15, 0, 300, false);
  928. ctx.closePath();
  929. ctx.fill();
  930. }
  931. }, {
  932. key: 'pane',
  933. value: function pane(pos) {
  934.  
  935. if (pos.y === 100 && pos.x >= 190 && this.direction === _constants.RIGHT) {
  936. return {
  937. y: 100,
  938. x: -10
  939. };
  940. }
  941.  
  942. if (pos.y === 100 && pos.x <= -10 && this.direction === _constants.LEFT) {
  943. return this.position = {
  944. y: 100,
  945. x: 190
  946. };
  947. }
  948.  
  949. return false;
  950. }
  951. }, {
  952. key: 'move',
  953. value: function move(ctx) {
  954.  
  955. var oldPos = this.position;
  956. var onGrid = this.onGridSquare(this.position);
  957. var npos = null;
  958.  
  959. if (this.due !== this.direction) {
  960. npos = this.getNewCoord(this.due, this.position);
  961.  
  962. if (onGrid && this.map.isFloor({
  963. x: this.pointToCoord(this.nextSquare(npos.x, this.due)),
  964. y: this.pointToCoord(this.nextSquare(npos.y, this.due))
  965. })) {
  966. this.direction = this.due;
  967. } else {
  968. npos = null;
  969. }
  970. }
  971.  
  972. if (npos === null) {
  973. npos = this.getNewCoord(this.direction, this.position);
  974. }
  975.  
  976. if (onGrid && this.map.isWall({
  977. y: this.pointToCoord(this.nextSquare(npos.y, this.direction)),
  978. x: this.pointToCoord(this.nextSquare(npos.x, this.direction))
  979. })) {
  980.  
  981. this.due = this.getRandomDirection();
  982. return this.move(ctx);
  983. }
  984.  
  985. this.position = npos;
  986.  
  987. var tm = this.pane(this.position);
  988. if (tm) {
  989. this.position = tm;
  990. }
  991.  
  992. this.due = this.getRandomDirection();
  993.  
  994. return {
  995. new: this.position,
  996. old: oldPos
  997. };
  998. }
  999. }]);
  1000.  
  1001. return Ghost;
  1002. }();
  1003.  
  1004. exports.default = Ghost;
  1005.  
  1006. },{"../constants.js":1}],8:[function(require,module,exports){
  1007. 'use strict';
  1008.  
  1009. Object.defineProperty(exports, "__esModule", {
  1010. value: true
  1011. });
  1012.  
  1013. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  1014.  
  1015. var _constants = require('../constants.js');
  1016.  
  1017. var _playerSprite = require('./playerSprite');
  1018.  
  1019. var _playerSprite2 = _interopRequireDefault(_playerSprite);
  1020.  
  1021. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  1022.  
  1023. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  1024.  
  1025. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  1026.  
  1027. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  1028.  
  1029. var Player = function (_PlayerSprite) {
  1030. _inherits(Player, _PlayerSprite);
  1031.  
  1032. function Player(game, map) {
  1033. _classCallCheck(this, Player);
  1034.  
  1035. var _this = _possibleConstructorReturn(this, (Player.__proto__ || Object.getPrototypeOf(Player)).call(this, game, map));
  1036.  
  1037. _this.position = null;
  1038. _this.direction = null;
  1039. _this.eaten = null;
  1040. _this.due = null;
  1041. _this.lives = null;
  1042. _this.score = 5;
  1043. _this.resetPlayerPosition();
  1044. return _this;
  1045. }
  1046.  
  1047. _createClass(Player, [{
  1048. key: 'addScore',
  1049. value: function addScore(nScore) {
  1050. this.score += nScore;
  1051. if (this.score >= 10000 && this.score - nScore < 10000) {
  1052. this.lives += 1;
  1053. }
  1054. }
  1055. }, {
  1056. key: 'getScore',
  1057. value: function getScore() {
  1058. return this.score;
  1059. }
  1060. }, {
  1061. key: 'loseLife',
  1062. value: function loseLife() {
  1063. this.lives -= 1;
  1064. }
  1065. }, {
  1066. key: 'getLives',
  1067. value: function getLives() {
  1068. return this.lives;
  1069. }
  1070. }, {
  1071. key: 'initLevel',
  1072. value: function initLevel() {
  1073. this.score = 0;
  1074. this.lives = 3;
  1075. this.newLevel();
  1076. }
  1077. }, {
  1078. key: 'newLevel',
  1079. value: function newLevel() {
  1080. this.resetPlayerPosition();
  1081. this.eaten = 0;
  1082. }
  1083. }, {
  1084. key: 'resetPlayerPosition',
  1085. value: function resetPlayerPosition() {
  1086. this.position = {
  1087. x: 90,
  1088. y: 120
  1089. };
  1090. this.direction = _constants.LEFT;
  1091. this.due = _constants.LEFT;
  1092. }
  1093. }, {
  1094. key: 'reset',
  1095. value: function reset() {
  1096. this.initLevel();
  1097. this.resetPlayerPosition();
  1098. }
  1099. }, {
  1100. key: 'move',
  1101. value: function move() {
  1102. var npos = null;
  1103. var nextWhole = null;
  1104. var oldPosition = this.position;
  1105. var block = null;
  1106.  
  1107. if (this.due !== this.direction) {
  1108. npos = this.getNewCoord(this.due, this.position);
  1109.  
  1110. if (this.isOnSamePlane(this.due, this.direction) || this.onGridSquare(this.position) && this.map.isFloor(this.next(npos, this.due))) {
  1111. this.direction = this.due;
  1112. } else {
  1113. npos = null;
  1114. }
  1115. }
  1116.  
  1117. if (npos === null) {
  1118. npos = this.getNewCoord(this.direction, this.position);
  1119. }
  1120.  
  1121. if (this.onGridSquare(this.position) && this.map.isWall(this.next(npos, this.direction))) {
  1122. this.direction = _constants.NONE;
  1123. }
  1124.  
  1125. if (this.direction === _constants.NONE) {
  1126. return {
  1127. new: this.position,
  1128. old: this.position
  1129. };
  1130. }
  1131.  
  1132. if (npos.y === 100 && npos.x >= 190 && this.direction === _constants.RIGHT) {
  1133. npos = {
  1134. y: 100,
  1135. x: -10
  1136. };
  1137. }
  1138.  
  1139. if (npos.y === 100 && npos.x <= -12 && this.direction === _constants.LEFT) {
  1140. npos = {
  1141. y: 100,
  1142. x: 190
  1143. };
  1144. }
  1145.  
  1146. this.position = npos;
  1147. nextWhole = this.next(this.position, this.direction);
  1148.  
  1149. block = this.map.getBlock(nextWhole);
  1150.  
  1151. if ((this.isMidSquare(this.position.y) || this.isMidSquare(this.position.x)) && block === _constants.Pacman.COOKIE || block === _constants.Pacman.PILL) {
  1152.  
  1153. this.map.setBlock(nextWhole, _constants.Pacman.EMPTY);
  1154. this.addScore(block === _constants.Pacman.COOKIE ? 10 : 50);
  1155. this.eaten += 1;
  1156.  
  1157. if (this.eaten === 182) {
  1158. this.game.completedLevel();
  1159. }
  1160.  
  1161. if (block === _constants.Pacman.PILL) {
  1162. this.game.eatenPill();
  1163. }
  1164. }
  1165.  
  1166. return {
  1167. new: this.position,
  1168. old: oldPosition
  1169. };
  1170. }
  1171. }]);
  1172.  
  1173. return Player;
  1174. }(_playerSprite2.default);
  1175.  
  1176. exports.default = Player;
  1177.  
  1178. },{"../constants.js":1,"./playerSprite":9}],9:[function(require,module,exports){
  1179. 'use strict';
  1180.  
  1181. Object.defineProperty(exports, "__esModule", {
  1182. value: true
  1183. });
  1184.  
  1185. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  1186.  
  1187. var _constants = require('../constants.js');
  1188.  
  1189. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  1190.  
  1191. var PlayerSprite = function () {
  1192. function PlayerSprite(game, map) {
  1193. _classCallCheck(this, PlayerSprite);
  1194.  
  1195. this.game = game;
  1196. this.map = map;
  1197. this.keyMap = {};
  1198. this.keyMap[_constants.KEY.ARROW_LEFT] = _constants.LEFT;
  1199. this.keyMap[_constants.KEY.ARROW_UP] = _constants.UP;
  1200. this.keyMap[_constants.KEY.ARROW_RIGHT] = _constants.RIGHT;
  1201. this.keyMap[_constants.KEY.ARROW_DOWN] = _constants.DOWN;
  1202. }
  1203.  
  1204. _createClass(PlayerSprite, [{
  1205. key: 'keyDown',
  1206. value: function keyDown(ev) {
  1207. if (this.keyMap[ev.keyCode] !== 'undefined') {
  1208. this.due = this.keyMap[ev.keyCode];
  1209. ev.preventDefault();
  1210. ev.stopPropagation();
  1211. return false;
  1212. }
  1213. return true;
  1214. }
  1215. }, {
  1216. key: 'getNewCoord',
  1217. value: function getNewCoord(dir, current) {
  1218. return {
  1219. x: current.x + (dir === _constants.LEFT && -2 || dir === _constants.RIGHT && 2 || 0),
  1220. y: current.y + (dir === _constants.DOWN && 2 || dir === _constants.UP && -2 || 0)
  1221. };
  1222. }
  1223. }, {
  1224. key: 'onWholeSquare',
  1225. value: function onWholeSquare(x) {
  1226. return x % 10 === 0;
  1227. }
  1228. }, {
  1229. key: 'pointToCoord',
  1230. value: function pointToCoord(x) {
  1231. return Math.round(x / 10);
  1232. }
  1233. }, {
  1234. key: 'nextSquare',
  1235. value: function nextSquare(x, dir) {
  1236. var rem = x % 10;
  1237. if (rem === 0) {
  1238. return x;
  1239. } else if (dir === _constants.RIGHT || dir === _constants.DOWN) {
  1240. return x + (10 - rem);
  1241. }
  1242. return x - rem;
  1243. }
  1244. }, {
  1245. key: 'next',
  1246. value: function next(pos, dir) {
  1247. return {
  1248. y: this.pointToCoord(this.nextSquare(pos.y, dir)),
  1249. x: this.pointToCoord(this.nextSquare(pos.x, dir))
  1250. };
  1251. }
  1252. }, {
  1253. key: 'onGridSquare',
  1254. value: function onGridSquare(pos) {
  1255. return this.onWholeSquare(pos.y) && this.onWholeSquare(pos.x);
  1256. }
  1257. }, {
  1258. key: 'isOnSamePlane',
  1259. value: function isOnSamePlane(due, dir) {
  1260. return (due === _constants.LEFT || due === _constants.RIGHT) && (dir === _constants.LEFT || dir === _constants.RIGHT) || (due === _constants.UP || due === _constants.DOWN) && (dir === _constants.UP || dir === _constants.DOWN);
  1261. }
  1262. }, {
  1263. key: 'isMidSquare',
  1264. value: function isMidSquare(x) {
  1265. var rem = x % 10;
  1266. return rem > 3 || rem < 7;
  1267. }
  1268. }, {
  1269. key: 'move',
  1270. value: function move() {
  1271. var npos = null;
  1272. var nextWhole = null;
  1273. var oldPosition = this.position;
  1274. var block = null;
  1275.  
  1276. if (this.due !== this.direction) {
  1277. npos = this.getNewCoord(this.due, this.position);
  1278.  
  1279. if (this.isOnSamePlane(this.due, this.direction) || this.onGridSquare(this.position) && this.map.isFloor(this.next(npos, this.due))) {
  1280. this.direction = this.due;
  1281. } else {
  1282. npos = null;
  1283. }
  1284. }
  1285.  
  1286. if (npos === null) {
  1287. npos = this.getNewCoord(this.direction, this.position);
  1288. }
  1289.  
  1290. if (this.onGridSquare(this.position) && this.map.isWall(this.next(npos, this.direction))) {
  1291. this.direction = _constants.NONE;
  1292. }
  1293.  
  1294. if (this.direction === _constants.NONE) {
  1295. return {
  1296. new: this.position,
  1297. old: this.position
  1298. };
  1299. }
  1300.  
  1301. if (npos.y === 100 && npos.x >= 190 && this.direction === _constants.RIGHT) {
  1302. npos = {
  1303. y: 100,
  1304. x: -10
  1305. };
  1306. }
  1307.  
  1308. if (npos.y === 100 && npos.x <= -12 && this.direction === _constants.LEFT) {
  1309. npos = {
  1310. y: 100,
  1311. x: 190
  1312. };
  1313. }
  1314.  
  1315. this.position = npos;
  1316. nextWhole = this.next(this.position, this.direction);
  1317.  
  1318. block = this.map.getBlock(nextWhole);
  1319.  
  1320. if ((this.isMidSquare(this.position.y) || this.isMidSquare(this.position.x)) && block === _constants.Pacman.COOKIE || block === _constants.Pacman.PILL) {
  1321.  
  1322. this.map.setBlock(nextWhole, _constants.Pacman.EMPTY);
  1323. this.addScore(block === _constants.Pacman.COOKIE ? 10 : 50);
  1324. this.eaten += 1;
  1325.  
  1326. if (this.eaten === 182) {
  1327. this.game.completedLevel();
  1328. }
  1329.  
  1330. if (block === _constants.Pacman.PILL) {
  1331. this.game.eatenPill();
  1332. }
  1333. }
  1334.  
  1335. return {
  1336. new: this.position,
  1337. old: oldPosition
  1338. };
  1339. }
  1340. }, {
  1341. key: 'drawDead',
  1342. value: function drawDead(ctx, amount) {
  1343.  
  1344. var size = this.map.blockSize;
  1345. var half = size / 2;
  1346.  
  1347. if (amount >= 1) {
  1348. return;
  1349. }
  1350.  
  1351. ctx.fillStyle = '#FFFF00';
  1352. ctx.beginPath();
  1353. ctx.moveTo(this.position.x / 10 * size + half, this.position.y / 10 * size + half);
  1354.  
  1355. ctx.arc(this.position.x / 10 * size + half, this.position.y / 10 * size + half, half, 0, Math.PI * 2 * amount, true);
  1356.  
  1357. ctx.fill();
  1358. }
  1359. }, {
  1360. key: 'draw',
  1361. value: function draw(ctx) {
  1362. var s = this.map.blockSize;
  1363. var angle = this.calcAngle(this.direction, this.position);
  1364.  
  1365. ctx.fillStyle = '#FFFF00';
  1366.  
  1367. ctx.beginPath();
  1368.  
  1369. ctx.moveTo(this.position.x / 10 * s + s / 2, this.position.y / 10 * s + s / 2);
  1370.  
  1371. ctx.arc(this.position.x / 10 * s + s / 2, this.position.y / 10 * s + s / 2, s / 2, Math.PI * angle.start, Math.PI * angle.end, angle.direction);
  1372.  
  1373. ctx.fill();
  1374. }
  1375.  
  1376. // Angle calculation for drawing
  1377.  
  1378. }, {
  1379. key: 'calcAngle',
  1380. value: function calcAngle(dir, pos) {
  1381. if (dir === _constants.RIGHT && pos.x % 10 < 5) {
  1382. return {
  1383. start: 0.25,
  1384. end: 1.75,
  1385. direction: false
  1386. };
  1387. } else if (dir === _constants.DOWN && pos.y % 10 < 5) {
  1388. return {
  1389. start: 0.75,
  1390. end: 2.25,
  1391. direction: false
  1392. };
  1393. } else if (dir === _constants.UP && pos.y % 10 < 5) {
  1394. return {
  1395. start: 1.25,
  1396. end: 1.75,
  1397. direction: true
  1398. };
  1399. } else if (dir === _constants.LEFT && pos.x % 10 < 5) {
  1400. return {
  1401. start: 0.75,
  1402. end: 1.25,
  1403. direction: true
  1404. };
  1405. }
  1406. return {
  1407. start: 0,
  1408. end: 2,
  1409. direction: false
  1410. };
  1411. }
  1412. }]);
  1413.  
  1414. return PlayerSprite;
  1415. }();
  1416.  
  1417. exports.default = PlayerSprite;
  1418.  
  1419. },{"../constants.js":1}]},{},[4])
  1420.  
  1421. //# sourceMappingURL=build.js.map
Add Comment
Please, Sign In to add comment