index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 Item(name, file) {
  30. this.name = name;
  31. this.cacheKey = file;
  32. if (!gTileCache[this.cacheKey]) {
  33. gTileCache[this.cacheKey] = new Image();
  34. gTileCache[this.cacheKey].src = file;
  35. }
  36. this.img = gTileCache[this.cacheKey];
  37. }
  38. function setTool(source) {
  39. if (gTool) {
  40. gTool.button.classList.remove('active');
  41. } else {
  42. gTool = {};
  43. }
  44. source.classList.add('active');
  45. gTool.tool = source.attributes.tool.value;
  46. gTool.tile = new Tile(source.attributes.tool.value, 'straight', 45);
  47. gTool.button = source;
  48. }
  49. function resize() {
  50. if (gMapCanvas) {
  51. gMapCanvas.width = window.innerWidth;
  52. gMapCanvas.height = window.innerHeight - gToolBarHeight;
  53. }
  54. }
  55. function setupToolbar() {
  56. var toolbar = window.document.getElementsByClassName('toolbar')[0];
  57. for (var i = 0; i < toolbar.children.length; i++) {
  58. var child = toolbar.children[i];
  59. if (child.attributes.tool) {
  60. child.style.backgroundImage = 'url("' + tileToFile(new Tile(child.attributes.tool.value, 'straight', 45)) + '")';
  61. }
  62. }
  63. }
  64. function setupWindow() {
  65. window.addEventListener('resize', resize, false);
  66. }
  67. function getMapTile(pos) {
  68. if (!gMap[pos.y]) return null;
  69. if (!gMap[pos.y][pos.x]) return null;
  70. return gMap[pos.y][pos.x].tile;
  71. }
  72. function edgeMap(tile) {
  73. if (!tile) return null;
  74. var map = [];
  75. if (tile.name === 'grass') return ['g', 'g', 'g', 'g', 'g', 'g', 'g', 'g'];
  76. if (tile.name === 'beach') return ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'];
  77. if (tile.name === 'grass-beach') {
  78. map = ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'];
  79. if (tile.type === 'straight') {
  80. if (tile.orientation === 45) map[0] = map[1] = map[2] = 'g';
  81. if (tile.orientation === 135) map[6] = map[7] = map[0] = 'g';
  82. if (tile.orientation === 225) map[4] = map[5] = map[6] = 'g';
  83. if (tile.orientation === 315) map[2] = map[3] = map[4] = 'g';
  84. } else if (tile.type === 'curve_in') {
  85. if (tile.orientation === 45) map[0] = map[1] = map[2] = map[3] = map[4] = 'g';
  86. if (tile.orientation === 135) map[6] = map[7] = map[0] = map[1] = map[2] = 'g';
  87. if (tile.orientation === 225) map[4] = map[5] = map[6] = map[7] = map[0] = 'g';
  88. if (tile.orientation === 315) map[2] = map[3] = map[4] = map[5] = map[6] = 'g';
  89. } else if (tile.type === 'curve_out') {
  90. if (tile.orientation === 45) map[2] = 'g';
  91. if (tile.orientation === 135) map[0] = 'g';
  92. if (tile.orientation === 225) map[6] = 'g';
  93. if (tile.orientation === 315) map[4] = 'g';
  94. } else {
  95. throw(new Error('unknown tile ' + tile.name + ' type ' + tile.type));
  96. }
  97. return map;
  98. }
  99. throw(new Error('unknown tile ' + tile.name + ' type ' + tile.type));
  100. }
  101. function edgeMapToTile(map) {
  102. function check(type) {
  103. var args = [];
  104. for (var a in arguments) args.push(arguments[a]);
  105. args.shift();
  106. return args.every(function (a) { return map[a] === type; });
  107. }
  108. if (check('g', 0, 1, 2, 3, 4, 5, 6, 7)) return new Tile('grass', 'straight', 45);
  109. if (check('b', 0, 1, 2, 3, 4, 5, 6, 7)) return new Tile('beach', 'straight', 45);
  110. if (check('g', 2) && check('b', 0, 1, 3, 4, 5, 6, 7)) return new Tile('grass-beach', 'curve_out', 45);
  111. if (check('g', 0) && check('b', 1, 2, 3, 4, 5, 6, 7)) return new Tile('grass-beach', 'curve_out', 135);
  112. if (check('g', 6) && check('b', 0, 1, 2, 3, 4, 5, 7)) return new Tile('grass-beach', 'curve_out', 225);
  113. if (check('g', 4) && check('b', 0, 1, 2, 3, 5, 6, 7)) return new Tile('grass-beach', 'curve_out', 315);
  114. if (check('g', 0, 1, 2) && check('b', 3, 4, 5, 6, 7)) return new Tile('grass-beach', 'straight', 45);
  115. if (check('g', 6, 7, 0) && check('b', 1, 2, 3, 4, 5)) return new Tile('grass-beach', 'straight', 135);
  116. if (check('g', 4, 5, 6) && check('b', 0, 1, 2, 3, 7)) return new Tile('grass-beach', 'straight', 225);
  117. if (check('g', 2, 3, 4) && check('b', 0, 1, 5, 6, 7)) return new Tile('grass-beach', 'straight', 315);
  118. if (check('g', 0, 1, 2, 3, 4) && check('b', 5, 6, 7)) return new Tile('grass-beach', 'curve_in', 45);
  119. if (check('g', 6, 7, 0, 1, 2) && check('b', 3, 4, 5)) return new Tile('grass-beach', 'curve_in', 135);
  120. if (check('g', 4, 5, 6, 7, 0) && check('b', 1, 2, 3)) return new Tile('grass-beach', 'curve_in', 225);
  121. if (check('g', 2, 3, 4, 5, 6) && check('b', 7, 0, 1)) return new Tile('grass-beach', 'curve_in', 315);
  122. // add those to allow placing tile without similar neighbours
  123. if (check('g', 0, 1, 2) && check('b', 5)) return new Tile('grass-beach', 'straight', 45);
  124. if (check('g', 6, 7, 0) && check('b', 3)) return new Tile('grass-beach', 'straight', 135);
  125. if (check('g', 4, 5, 6) && check('b', 1)) return new Tile('grass-beach', 'straight', 225);
  126. if (check('g', 2, 3, 4) && check('b', 7)) return new Tile('grass-beach', 'straight', 315);
  127. console.error('No tile found for', map);
  128. }
  129. function setMapTile(pos, tile) {
  130. if (!tile) return;
  131. if (!gMap[pos.y]) return;
  132. if (!gMap[pos.y][pos.x]) return;
  133. gMap[pos.y][pos.x].tile = tile;
  134. }
  135. function calculateMapTile(pos) {
  136. // top row
  137. var tl = edgeMap(getMapTile({ y: pos.y-1, x: pos.x-1 }));
  138. var to = edgeMap(getMapTile({ y: pos.y-1, x: pos.x }));
  139. var tr = edgeMap(getMapTile({ y: pos.y-1, x: pos.x+1 }));
  140. // middle row
  141. var le = edgeMap(getMapTile({ y: pos.y, x: pos.x-1 }));
  142. var ri = edgeMap(getMapTile({ y: pos.y, x: pos.x+1 }));
  143. // bottom row
  144. var bl = edgeMap(getMapTile({ y: pos.y+1, x: pos.x-1 }));
  145. var bo = edgeMap(getMapTile({ y: pos.y+1, x: pos.x }));
  146. var br = edgeMap(getMapTile({ y: pos.y+1, x: pos.x+1 }));
  147. // calculate edges and corners
  148. // 0
  149. // 7 - - 1
  150. // 6 - - 2
  151. // 5 - - 3
  152. // 4
  153. var tile = edgeMapToTile([tl[4], to[5], tr[6], ri[7], br[0], bo[1], bl[2], le[3]]);
  154. return tile;
  155. }
  156. function resetMapTile(pos) {
  157. setMapTile(pos, calculateMapTile(pos));
  158. }
  159. function putMapTile(pos, tile) {
  160. setMapTile(pos, tile);
  161. for (var i = 0; i < 10; ++i) {
  162. // cross
  163. resetMapTile({ y: pos.y-1, x: pos.x });
  164. resetMapTile({ y: pos.y, x: pos.x-1 });
  165. resetMapTile({ y: pos.y, x: pos.x+1 });
  166. resetMapTile({ y: pos.y+1, x: pos.x });
  167. // top corners
  168. resetMapTile({ y: pos.y-1, x: pos.x-1 });
  169. resetMapTile({ y: pos.y-1, x: pos.x+1 });
  170. // bottom corners
  171. resetMapTile({ y: pos.y+1, x: pos.x-1 });
  172. resetMapTile({ y: pos.y+1, x: pos.x+1 });
  173. }
  174. }
  175. function setupCanvas() {
  176. gMapCanvas = window.document.getElementById('map');
  177. gMapCanvas.oncontextmenu = function (e) {
  178. e.preventDefault();
  179. };
  180. gMapCanvas.addEventListener('mousemove', function (event) {
  181. var rect = gMapCanvas.getBoundingClientRect();
  182. gMousePos = {
  183. x: event.clientX - rect.left,
  184. y: event.clientY - rect.top
  185. };
  186. gMapHover = translateScreenToMap({ x: gMousePos.x, y: gMousePos.y });
  187. if (gMouseDrag && gCamera.drag) {
  188. gCamera.x = gCamera.drag.x - (gMousePos.x - gMouseDrag.x);
  189. gCamera.y = gCamera.drag.y - (gMousePos.y - gMouseDrag.y);
  190. }
  191. }, false);
  192. gMapCanvas.addEventListener('mousedown', function (e) {
  193. if (e.button === 2) {
  194. gMouseDrag = { x: gMousePos.x, y: gMousePos.y };
  195. gCamera.drag = { x: gCamera.x, y: gCamera.y };
  196. } else if (e.button === 0 && gTool) {
  197. putMapTile(gMapHover, gTool.tile);
  198. }
  199. }, false);
  200. gMapCanvas.addEventListener('mouseup', function () {
  201. gMouseDrag = null;
  202. gCamera.drag = null;
  203. }, false);
  204. gCtx = gMapCanvas.getContext('2d');
  205. }
  206. function saveMap() {
  207. localStorage.map = JSON.stringify(gMap);
  208. }
  209. function loadMap() {
  210. var map = [];
  211. if (localStorage.map) {
  212. map = JSON.parse(localStorage.map);
  213. } else {
  214. for (var y = 0; y < 100; y++) {
  215. map[y] = [];
  216. for (var x = 0; x < 100; x++) {
  217. map[y][x] = {
  218. tile: new Tile('grass', 'straight', 45),
  219. item: Math.random() < 0.03 ? new Item('tree_0', 'tree_0.png') : null
  220. };
  221. }
  222. }
  223. }
  224. gMap = map;
  225. }
  226. function init() {
  227. setupWindow();
  228. setupCanvas();
  229. setupToolbar();
  230. resize();
  231. gCamera.x = 0;
  232. gCamera.y = 0;
  233. loadMap();
  234. renderLoop();
  235. }
  236. function renderLoop(){
  237. // window.requestAnimationFrame(renderLoop);
  238. window.setTimeout(renderLoop, 500);
  239. render();
  240. }
  241. function translateMapToScreen(map) {
  242. var screen = {};
  243. screen.x = (map.x - map.y) * (64/2);
  244. screen.y = (map.x + map.y) * (64/4);
  245. // start in the horizontal middle
  246. // screen.x = (gMapCanvas.width/2) - (64/2) + screen.x;
  247. screen.x -= gCamera.x;
  248. screen.y -= gCamera.y;
  249. return screen;
  250. }
  251. function translateScreenToMap(screen) {
  252. var map = {};
  253. screen.x += gCamera.x;
  254. screen.y += gCamera.y;
  255. map.x = (screen.x / (64/2) + screen.y / (64/4)) / 2;
  256. map.y = (screen.y / (64/4) - (screen.x / (64/2))) / 2;
  257. map.y = map.y.toFixed() - 1;
  258. map.x = map.x.toFixed() - 2;
  259. return map;
  260. }
  261. function render() {
  262. gCtx.clearRect(0, 0, gMapCanvas.width, gMapCanvas.height);
  263. var y, x, coords;
  264. for (y = 0; y < gMap.length; y++) {
  265. for (x = 0; x < gMap[y].length; x++) {
  266. coords = translateMapToScreen({x: x, y: y});
  267. var img = gTileCache[gMap[y][x].tile.cacheKey];
  268. if (gTool && gMapHover && gMapHover.x === x && gMapHover.y === y) img = gTileCache[gTool.tile.cacheKey];
  269. gCtx.drawImage(img, coords.x, coords.y);
  270. }
  271. }
  272. for (y = 0; y < gMap.length; y++) {
  273. for (x = 0; x < gMap[y].length; x++) {
  274. if (!gMap[y][x].item) continue;
  275. coords = translateMapToScreen({x: x, y: y});
  276. var img = gTileCache[gMap[y][x].item.cacheKey];
  277. gCtx.drawImage(img, coords.x, coords.y);
  278. }
  279. }
  280. }