TilemapWalker.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /**
  2. * @author Richard Davey <rich@photonstorm.com>
  3. * @copyright 2014 Photon Storm Ltd.
  4. * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
  5. */
  6. /**
  7. * Creates an object that is placed within a layer of a Phaser.Tilemap and can be moved around and rotated using the direction commands.
  8. *
  9. * @class Phaser.Plugin.TilemapWalker
  10. * @constructor
  11. * @param {Phaser.Game} game - Game reference to the currently running game.
  12. * @param {Phaser.Tilemap} map - A reference to the Tilemap this TilemapWalker belongs to.
  13. * @param {number|string|Phaser.TilemapLayer} [layer] - The layer to operate on. If not given will default to this.currentLayer.
  14. * @param {number} x - X position of the top left of the area to copy (given in tiles, not pixels)
  15. * @param {number} y - Y position of the top left of the area to copy (given in tiles, not pixels)
  16. * @return {Phaser.Plugin.TilemapWalker}
  17. */
  18. Phaser.Plugin.TilemapWalker = function (game, map, layer, x, y) {
  19. /**
  20. * @property {Phaser.Game} game - A reference to the currently running Game.
  21. */
  22. this.game = game;
  23. /**
  24. * @property {Phaser.Tilemap} map - A reference to the Tilemap this TilemapWalker belongs to.
  25. */
  26. this.map = map;
  27. /**
  28. * @property {number} locationLayer - The current layer of the location marker.
  29. */
  30. this.locationLayer = map.getLayer(layer);
  31. /**
  32. * @property {Phaser.Point} location - The current marker location. You can move the marker with the movement methods.
  33. */
  34. this.location = new Phaser.Point();
  35. /**
  36. * @property {number} facing - The direction the location marker is facing. You can rotate it using the turn and face methods.
  37. * @default
  38. */
  39. this.facing = Phaser.Tilemap.NORTH;
  40. /**
  41. * @property {boolean} collides - Does the TilemapWalker collide with the tiles in the map set for collision? If so it cannot move through them.
  42. * @default
  43. */
  44. this.collides = true;
  45. /**
  46. * @property {array} history - An array containing a history of movements through the map.
  47. */
  48. this.history = [];
  49. // TODO: History limit, History scan, record how many times walker has been on a tile before
  50. if (typeof x !== 'undefined' && typeof y !== 'undefined')
  51. {
  52. this.setLocation(x, y);
  53. }
  54. return this;
  55. };
  56. Phaser.Plugin.TilemapWalker.prototype = {
  57. /**
  58. * Sets the location marker to the given x/y coordinates within the map.
  59. * Once set you can move the marker around via the movement and turn methods.
  60. *
  61. * @method Phaser.Tilemap#setLocation
  62. * @param {number} x - X position of the top left of the area to copy (given in tiles, not pixels)
  63. * @param {number} y - Y position of the top left of the area to copy (given in tiles, not pixels)
  64. * @param {number|string|Phaser.TilemapLayer} [layer] - The layer to operate on. If not given will default to this.currentLayer.
  65. * @return {boolean} True if the location could be set, otherwise false.
  66. */
  67. setLocation: function (x, y, layer) {
  68. if (this.checkTile(x, y))
  69. {
  70. this.location.set(x, y);
  71. this.history.push( { x: x, y: y });
  72. return true;
  73. }
  74. return false;
  75. },
  76. /**
  77. * Checks if the given x/y coordinate has a tile into which we can move.
  78. *
  79. * @method Phaser.Tilemap#checkTile
  80. * @param {number} x - X position of the top left of the area to copy (given in tiles, not pixels)
  81. * @param {number} y - Y position of the top left of the area to copy (given in tiles, not pixels)
  82. * @return {boolean} True if the location can be moved into, false if not.
  83. */
  84. checkTile: function (x, y) {
  85. if (this.map.hasTile(x, y, this.locationLayer))
  86. {
  87. if (this.collides)
  88. {
  89. var tile = this.map.getTile(x, y, this.locationLayer);
  90. if (tile && tile.collides)
  91. {
  92. return false;
  93. }
  94. }
  95. return true;
  96. }
  97. return false;
  98. },
  99. updateLocation: function (x, y) {
  100. if (this.checkTile(this.location.x + x, this.location.y + y, this.locationLayer))
  101. {
  102. this.location.set(this.location.x + x, this.location.y + y);
  103. this.history.push( { x: x, y: y });
  104. return true;
  105. }
  106. return false;
  107. },
  108. moveForward: function () {
  109. if (this.facing === Phaser.Tilemap.NORTH)
  110. {
  111. return this.updateLocation(0, -1);
  112. }
  113. else if (this.facing === Phaser.Tilemap.EAST)
  114. {
  115. return this.updateLocation(1, 0);
  116. }
  117. else if (this.facing === Phaser.Tilemap.SOUTH)
  118. {
  119. return this.updateLocation(0, 1);
  120. }
  121. else if (this.facing === Phaser.Tilemap.WEST)
  122. {
  123. return this.updateLocation(-1, 0);
  124. }
  125. },
  126. moveBackward: function () {
  127. if (this.facing === Phaser.Tilemap.NORTH)
  128. {
  129. return this.updateLocation(0, 1);
  130. }
  131. else if (this.facing === Phaser.Tilemap.EAST)
  132. {
  133. return this.updateLocation(-1, 0);
  134. }
  135. else if (this.facing === Phaser.Tilemap.SOUTH)
  136. {
  137. return this.updateLocation(0, -1);
  138. }
  139. else if (this.facing === Phaser.Tilemap.WEST)
  140. {
  141. return this.updateLocation(1, 0);
  142. }
  143. },
  144. moveLeft: function () {
  145. if (this.facing === Phaser.Tilemap.NORTH)
  146. {
  147. return this.updateLocation(-1, 0);
  148. }
  149. else if (this.facing === Phaser.Tilemap.EAST)
  150. {
  151. return this.updateLocation(0, -1);
  152. }
  153. else if (this.facing === Phaser.Tilemap.SOUTH)
  154. {
  155. return this.updateLocation(1, 0);
  156. }
  157. else if (this.facing === Phaser.Tilemap.WEST)
  158. {
  159. return this.updateLocation(0, 1);
  160. }
  161. },
  162. moveRight: function () {
  163. if (this.facing === Phaser.Tilemap.NORTH)
  164. {
  165. return this.updateLocation(1, 0);
  166. }
  167. else if (this.facing === Phaser.Tilemap.EAST)
  168. {
  169. return this.updateLocation(0, 1);
  170. }
  171. else if (this.facing === Phaser.Tilemap.SOUTH)
  172. {
  173. return this.updateLocation(-1, 0);
  174. }
  175. else if (this.facing === Phaser.Tilemap.WEST)
  176. {
  177. return this.updateLocation(0, -1);
  178. }
  179. },
  180. turnLeft: function () {
  181. this.facing--;
  182. if (this.facing === -1)
  183. {
  184. this.facing = 3;
  185. }
  186. },
  187. turnRight: function () {
  188. this.facing++;
  189. if (this.facing === 4)
  190. {
  191. this.facing = 0;
  192. }
  193. },
  194. putTile: function (index) {
  195. this.map.putTile(index, this.location.x, this.location.y, this.locationLayer);
  196. },
  197. getTileFromLocation: function (x, y) {
  198. return this.map.getTile(this.location.x + x, this.location.y + y, this.locationLayer, true);
  199. },
  200. getTile: function () {
  201. return this.map.getTile(this.location.x, this.location.y, this.locationLayer, true);
  202. },
  203. getTiles: function (width, height, center) {
  204. var startX;
  205. var startX;
  206. var endX;
  207. var endY;
  208. var incX;
  209. var incY;
  210. var hw = Math.floor(width / 2);
  211. var hh = Math.floor(height / 2);
  212. // For now we assume that center = bottom middle tile
  213. if (center)
  214. {
  215. startX = this.location.x - hw;
  216. endX = this.location.x + hw;
  217. incX = 1;
  218. startY = this.location.y - hh;
  219. endY = this.location.y + hh;
  220. incY = 1;
  221. }
  222. else
  223. {
  224. if (this.facing === Phaser.Tilemap.NORTH)
  225. {
  226. startX = this.location.x - hw;
  227. endX = this.location.x + hw;
  228. incX = 1;
  229. // bottom middle align
  230. startY = this.location.y - (height - 1);
  231. endY = this.location.y;
  232. incY = 1;
  233. }
  234. else if (this.facing === Phaser.Tilemap.EAST)
  235. {
  236. startX = this.location.x;
  237. endX = this.location.x + (width - 1);
  238. incX = 1;
  239. // bottom middle align
  240. startY = this.location.y - hh;
  241. endY = this.location.y + hh;
  242. incY = 1;
  243. }
  244. else if (this.facing === Phaser.Tilemap.SOUTH)
  245. {
  246. startX = this.location.x - hw;
  247. endX = this.location.x + hw;
  248. incX = 1;
  249. // bottom middle align
  250. startY = this.location.y;
  251. endY = this.location.y + (height - 1);
  252. incY = 1;
  253. }
  254. else if (this.facing === Phaser.Tilemap.WEST)
  255. {
  256. startX = this.location.x - (width - 1);
  257. endX = this.location.x;
  258. incX = 1;
  259. // bottom middle align
  260. startY = this.location.y - hh;
  261. endY = this.location.y + hh;
  262. incY = 1;
  263. }
  264. }
  265. var output = [];
  266. var row = [];
  267. for (var y = startY; y <= endY; y += incY)
  268. {
  269. row = [];
  270. for (var x = startX; x <= endX; x += incX)
  271. {
  272. var tile = this.map.getTile(x, y, this.locationLayer, true);
  273. if (tile)
  274. {
  275. row.push(tile.index);
  276. }
  277. else
  278. {
  279. // out of bounds, so block it off
  280. row.push(0);
  281. }
  282. }
  283. output.push(row);
  284. }
  285. // console.log(printMatrix(output));
  286. if (this.facing === Phaser.Tilemap.EAST)
  287. {
  288. output = rotateMatrix(output, 90);
  289. }
  290. else if (this.facing === Phaser.Tilemap.SOUTH)
  291. {
  292. output = rotateMatrix(output, 180);
  293. }
  294. else if (this.facing === Phaser.Tilemap.WEST)
  295. {
  296. output = rotateMatrix(output, -90);
  297. }
  298. // console.log('rotate');
  299. // console.log(printMatrix(output));
  300. return output;
  301. },
  302. getMiniMap: function (width, height) {
  303. var startX;
  304. var startX;
  305. var endX;
  306. var endY;
  307. var diff = 0;
  308. var hw = Math.floor(width / 2);
  309. var hh = Math.floor(height / 2);
  310. startX = this.location.x - hw;
  311. endX = this.location.x + hw;
  312. startY = this.location.y - hh;
  313. endY = this.location.y + hh;
  314. // Bounds
  315. if (startX < 0)
  316. {
  317. endX += Math.abs(startX);
  318. startX = 0;
  319. }
  320. if (endX > this.map.width)
  321. {
  322. diff = endX - this.map.width;
  323. endX = this.map.width;
  324. startX -= diff;
  325. }
  326. if (startY < 0)
  327. {
  328. endY += Math.abs(startY);
  329. startY = 0;
  330. }
  331. if (endY > this.map.height)
  332. {
  333. diff = endY - this.map.height;
  334. endY = this.map.height;
  335. startY -= diff;
  336. }
  337. var output = [];
  338. var row = [];
  339. var walkerPosition = { x: 0, y: 0 };
  340. for (var y = startY; y < endY; y++)
  341. {
  342. row = [];
  343. for (var x = startX; x < endX; x++)
  344. {
  345. // Walker?
  346. if (x === this.location.x && y === this.location.y)
  347. {
  348. walkerPosition.x = row.length;
  349. walkerPosition.y = output.length;
  350. }
  351. var tile = this.map.getTile(x, y, this.locationLayer, true);
  352. if (tile)
  353. {
  354. row.push(tile.index);
  355. }
  356. else
  357. {
  358. // out of bounds, so block it off
  359. row.push(0);
  360. }
  361. }
  362. output.push(row);
  363. }
  364. // console.log(printMatrix(output));
  365. return { walker: walkerPosition, tiles: output };
  366. },
  367. getTileAhead: function (distance) {
  368. if (typeof distance === 'undefined') { distance = 1; }
  369. if (this.facing === Phaser.Tilemap.NORTH)
  370. {
  371. return this.getTileFromLocation(0, -distance);
  372. }
  373. else if (this.facing === Phaser.Tilemap.EAST)
  374. {
  375. return this.getTileFromLocation(distance, 0);
  376. }
  377. else if (this.facing === Phaser.Tilemap.SOUTH)
  378. {
  379. return this.getTileFromLocation(0, distance);
  380. }
  381. else if (this.facing === Phaser.Tilemap.WEST)
  382. {
  383. return this.getTileFromLocation(-distance, 0);
  384. }
  385. },
  386. getTileAheadLeft: function (distance) {
  387. if (typeof distance === 'undefined') { distance = 1; }
  388. if (this.facing === Phaser.Tilemap.NORTH)
  389. {
  390. return this.getTileFromLocation(-distance, -distance);
  391. }
  392. else if (this.facing === Phaser.Tilemap.EAST)
  393. {
  394. return this.getTileFromLocation(distance, -distance);
  395. }
  396. else if (this.facing === Phaser.Tilemap.SOUTH)
  397. {
  398. return this.getTileFromLocation(distance, distance);
  399. }
  400. else if (this.facing === Phaser.Tilemap.WEST)
  401. {
  402. return this.getTileFromLocation(-distance, distance);
  403. }
  404. },
  405. getTileAheadRight: function (distance) {
  406. if (typeof distance === 'undefined') { distance = 1; }
  407. if (this.facing === Phaser.Tilemap.NORTH)
  408. {
  409. return this.getTileFromLocation(distance, -distance);
  410. }
  411. else if (this.facing === Phaser.Tilemap.EAST)
  412. {
  413. return this.getTileFromLocation(distance, distance);
  414. }
  415. else if (this.facing === Phaser.Tilemap.SOUTH)
  416. {
  417. return this.getTileFromLocation(-distance, distance);
  418. }
  419. else if (this.facing === Phaser.Tilemap.WEST)
  420. {
  421. return this.getTileFromLocation(-distance, -distance);
  422. }
  423. },
  424. getTileBehind: function (distance) {
  425. if (typeof distance === 'undefined') { distance = 1; }
  426. if (this.facing === Phaser.Tilemap.NORTH)
  427. {
  428. return this.getTileFromLocation(0, distance);
  429. }
  430. else if (this.facing === Phaser.Tilemap.EAST)
  431. {
  432. return this.getTileFromLocation(-distance, 0);
  433. }
  434. else if (this.facing === Phaser.Tilemap.SOUTH)
  435. {
  436. return this.getTileFromLocation(0, -distance);
  437. }
  438. else if (this.facing === Phaser.Tilemap.WEST)
  439. {
  440. return this.getTileFromLocation(distance, 0);
  441. }
  442. },
  443. getTileBehindLeft: function (distance) {
  444. if (typeof distance === 'undefined') { distance = 1; }
  445. if (this.facing === Phaser.Tilemap.NORTH)
  446. {
  447. return this.getTileFromLocation(-distance, distance);
  448. }
  449. else if (this.facing === Phaser.Tilemap.EAST)
  450. {
  451. return this.getTileFromLocation(-distance, -distance);
  452. }
  453. else if (this.facing === Phaser.Tilemap.SOUTH)
  454. {
  455. return this.getTileFromLocation(distance, -distance);
  456. }
  457. else if (this.facing === Phaser.Tilemap.WEST)
  458. {
  459. return this.getTileFromLocation(distance, distance);
  460. }
  461. },
  462. getTileBehindRight: function (distance) {
  463. if (typeof distance === 'undefined') { distance = 1; }
  464. if (this.facing === Phaser.Tilemap.NORTH)
  465. {
  466. return this.getTileFromLocation(distance, distance);
  467. }
  468. else if (this.facing === Phaser.Tilemap.EAST)
  469. {
  470. return this.getTileFromLocation(-distance, distance);
  471. }
  472. else if (this.facing === Phaser.Tilemap.SOUTH)
  473. {
  474. return this.getTileFromLocation(-distance, -distance);
  475. }
  476. else if (this.facing === Phaser.Tilemap.WEST)
  477. {
  478. return this.getTileFromLocation(distance, -distance);
  479. }
  480. },
  481. getTileLeft: function (distance) {
  482. if (typeof distance === 'undefined') { distance = 1; }
  483. if (this.facing === Phaser.Tilemap.NORTH)
  484. {
  485. return this.getTileFromLocation(-distance, 0);
  486. }
  487. else if (this.facing === Phaser.Tilemap.EAST)
  488. {
  489. return this.getTileFromLocation(0, -distance);
  490. }
  491. else if (this.facing === Phaser.Tilemap.SOUTH)
  492. {
  493. return this.getTileFromLocation(distance, 0);
  494. }
  495. else if (this.facing === Phaser.Tilemap.WEST)
  496. {
  497. return this.getTileFromLocation(0, distance);
  498. }
  499. },
  500. getTileRight: function (distance) {
  501. if (typeof distance === 'undefined') { distance = 1; }
  502. if (this.facing === Phaser.Tilemap.NORTH)
  503. {
  504. return this.getTileFromLocation(distance, 0);
  505. }
  506. else if (this.facing === Phaser.Tilemap.EAST)
  507. {
  508. return this.getTileFromLocation(0, distance);
  509. }
  510. else if (this.facing === Phaser.Tilemap.SOUTH)
  511. {
  512. return this.getTileFromLocation(-distance, 0);
  513. }
  514. else if (this.facing === Phaser.Tilemap.WEST)
  515. {
  516. return this.getTileFromLocation(0, -distance);
  517. }
  518. }
  519. };
  520. // Original from http://jsfiddle.net/MrPolywhirl/NH42z/ - tided up and de-globalised by Richard Davey
  521. var rotateMatrix = function (matrix, direction) {
  522. direction = ((direction % 360) + 360) % 360;
  523. var ret = matrix;
  524. var transpose = function (m) {
  525. var result = new Array(m[0].length);
  526. for (var i = 0; i < m[0].length; i++) {
  527. result[i] = new Array(m.length - 1);
  528. for (var j = m.length - 1; j > -1; j--) {
  529. result[i][j] = m[j][i];
  530. }
  531. }
  532. return result;
  533. };
  534. var reverseRows = function (m) {
  535. return m.reverse();
  536. };
  537. var reverseCols = function (m) {
  538. for (var i = 0; i < m.length; i++) {
  539. m[i].reverse();
  540. }
  541. return m;
  542. };
  543. var rotate90Left = function (m) {
  544. m = transpose(m);
  545. m = reverseRows(m);
  546. return m;
  547. };
  548. var rotate90Right = function (m) {
  549. m = reverseRows(m);
  550. m = transpose(m);
  551. return m;
  552. };
  553. var rotate180 = function (m) {
  554. m = reverseCols(m);
  555. m = reverseRows(m);
  556. return m;
  557. };
  558. if (direction == 90 || direction == -270) {
  559. return rotate90Left(ret);
  560. } else if (direction == -90 || direction == 270) {
  561. return rotate90Right(ret);
  562. } else if (Math.abs(direction) == 180) {
  563. return rotate180(ret);
  564. }
  565. return matrix;
  566. };
  567. var pad = function (val, amt, ch) {
  568. ch = typeof ch !== 'undefined' ? ch : ' ';
  569. var str = val
  570. var max = Math.abs(amt);
  571. while (str.length < max) {
  572. if (amt < 0) {
  573. str += ch;
  574. } else {
  575. str = ch + str;
  576. }
  577. }
  578. return str;
  579. };
  580. var printMatrix = function (matrix) {
  581. var str = '';
  582. for (var r = 0; r < matrix.length; r++) {
  583. for (var c = 0; c < matrix[r].length; c++) {
  584. var cell = matrix[r][c].toString();
  585. if (cell != 'undefined') {
  586. str += pad(cell, 2);
  587. } else {
  588. str += '?';
  589. }
  590. if (c < matrix[r].length - 1) {
  591. str += ' |';
  592. }
  593. }
  594. if (r < matrix.length - 1) {
  595. str += '\n';
  596. for (var i = 0; i < matrix[r].length; i++) {
  597. str += '---'
  598. if (i < matrix[r].length - 1) {
  599. str += '+';
  600. }
  601. }
  602. str += '\n';
  603. }
  604. }
  605. return str;
  606. };