index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. 'use strict';
  2. window.addEventListener('load', init, false);
  3. var gMapCanvas = null;
  4. var gCtx = null;
  5. var gCamera = {};
  6. var gMousePos = {};
  7. var gMouseDrag = false;
  8. var gMapHover = {};
  9. var gTool = null;
  10. var gToolBarHeight = 64;
  11. var gMap = null;
  12. var gTileCache = {};
  13. function tileToFile(tile) {
  14. return 'Tiles/ts_' + tile.name + '0/' + tile.type + '/' + tile.orientation + '/0.png';
  15. }
  16. function loadTile(tile) {
  17. console.log('load tile', tile.name, tile.type, tile.orientation);
  18. gTileCache[tile.cacheKey] = new Image();
  19. gTileCache[tile.cacheKey].src = tileToFile(tile);
  20. }
  21. function Tile(name, type, orientation) {
  22. this.name = name;
  23. this.type = type;
  24. this.orientation = orientation;
  25. this.cacheKey = name + type + orientation;
  26. if (!gTileCache[this.cacheKey]) loadTile(this);
  27. this.img = gTileCache[this.cacheKey];
  28. }
  29. function setTool(source) {
  30. if (gTool) {
  31. gTool.button.classList.remove('active');
  32. } else {
  33. gTool = {};
  34. }
  35. source.classList.add('active');
  36. gTool.tool = source.attributes.tool.value;
  37. gTool.tile = new Tile(source.attributes.tool.value, 'straight', 45);
  38. gTool.button = source;
  39. }
  40. function resize() {
  41. if (gMapCanvas) {
  42. gMapCanvas.width = window.innerWidth;
  43. gMapCanvas.height = window.innerHeight - gToolBarHeight;
  44. }
  45. }
  46. function setupToolbar() {
  47. var toolbar = window.document.getElementsByClassName('toolbar')[0];
  48. for (var i = 0; i < toolbar.children.length; i++) {
  49. var child = toolbar.children[i];
  50. if (child.attributes.tool) {
  51. child.style.backgroundImage = 'url("' + tileToFile(new Tile(child.attributes.tool.value, 'straight', 45)) + '")';
  52. }
  53. }
  54. }
  55. function setupWindow() {
  56. window.addEventListener('resize', resize, false);
  57. }
  58. function setMapTile(pos, tile) {
  59. if (tile.name === 'beach') {
  60. if (gMap[pos.y-1][pos.x].name === 'grass') gMap[pos.y][pos.x] = new Tile('grass-beach', 'straight', 45);
  61. else gMap[pos.y][pos.x] = tile;
  62. } else {
  63. gMap[pos.y][pos.x] = tile;
  64. }
  65. }
  66. function setupCanvas() {
  67. gMapCanvas = window.document.getElementById('map');
  68. gMapCanvas.oncontextmenu = function (e) {
  69. e.preventDefault();
  70. };
  71. gMapCanvas.addEventListener('mousemove', function (event) {
  72. var rect = gMapCanvas.getBoundingClientRect();
  73. gMousePos = {
  74. x: event.clientX - rect.left,
  75. y: event.clientY - rect.top
  76. };
  77. gMapHover = translateScreenToMap({ x: gMousePos.x, y: gMousePos.y });
  78. if (gMouseDrag && gCamera.drag) {
  79. gCamera.x = gCamera.drag.x - (gMousePos.x - gMouseDrag.x);
  80. gCamera.y = gCamera.drag.y - (gMousePos.y - gMouseDrag.y);
  81. }
  82. }, false);
  83. gMapCanvas.addEventListener('mousedown', function (e) {
  84. if (e.button === 2) {
  85. gMouseDrag = { x: gMousePos.x, y: gMousePos.y };
  86. gCamera.drag = { x: gCamera.x, y: gCamera.y };
  87. } else if (e.button === 0 && gTool) {
  88. setMapTile(gMapHover, gTool.tile);
  89. }
  90. }, false);
  91. gMapCanvas.addEventListener('mouseup', function () {
  92. gMouseDrag = null;
  93. gCamera.drag = null;
  94. }, false);
  95. gCtx = gMapCanvas.getContext('2d');
  96. }
  97. function saveMap() {
  98. localStorage.map = JSON.stringify(gMap);
  99. }
  100. function loadMap() {
  101. var map = [];
  102. if (localStorage.map) {
  103. map = JSON.parse(localStorage.map);
  104. } else {
  105. for (var y = 0; y < 100; y++) {
  106. map[y] = [];
  107. for (var x = 0; x < 100; x++) {
  108. map[y][x] = new Tile('grass', 'straight', 45);
  109. }
  110. }
  111. }
  112. gMap = map;
  113. }
  114. function init() {
  115. setupWindow();
  116. setupCanvas();
  117. setupToolbar();
  118. resize();
  119. gCamera.x = 0;
  120. gCamera.y = 0;
  121. loadMap();
  122. renderLoop();
  123. }
  124. function renderLoop(){
  125. window.requestAnimationFrame(renderLoop);
  126. render();
  127. }
  128. function translateMapToScreen(map) {
  129. var screen = {};
  130. screen.x = (map.x - map.y) * (64/2);
  131. screen.y = (map.x + map.y) * (64/4);
  132. // start in the horizontal middle
  133. // screen.x = (gMapCanvas.width/2) - (64/2) + screen.x;
  134. screen.x -= gCamera.x;
  135. screen.y -= gCamera.y;
  136. return screen;
  137. }
  138. function translateScreenToMap(screen) {
  139. var map = {};
  140. screen.x += gCamera.x;
  141. screen.y += gCamera.y;
  142. map.x = (screen.x / (64/2) + screen.y / (64/4)) / 2;
  143. map.y = (screen.y / (64/4) - (screen.x / (64/2))) / 2;
  144. map.y = map.y.toFixed() - 1;
  145. map.x = map.x.toFixed() - 2;
  146. return map;
  147. }
  148. function render() {
  149. gCtx.clearRect(0, 0, gMapCanvas.width, gMapCanvas.height);
  150. for (var y = 0; y < gMap.length; y++) {
  151. for (var x = 0; x < gMap[y].length; x++) {
  152. var coords = translateMapToScreen({x: x, y: y});
  153. var img = gTileCache[gMap[y][x].cacheKey];
  154. if (gTool && gMapHover && gMapHover.x === x && gMapHover.y === y) img = gTileCache[gTool.tile.cacheKey];
  155. gCtx.drawImage(img, coords.x, coords.y);
  156. }
  157. }
  158. }