index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. if (check('g', 1, 2) && check('b', 3, 4, 5, 6, 7, 0)) return new Tile('grass-beach', 'curve_out', 45);
  128. if (check('g', 2, 3) && check('b', 4, 5, 6, 7, 0, 1)) return new Tile('grass-beach', 'curve_out', 45);
  129. if (check('g', 0, 7) && check('b', 1, 2, 3, 4, 5, 6)) return new Tile('grass-beach', 'curve_out', 135);
  130. if (check('g', 0, 1) && check('b', 2, 3, 4, 5, 6, 7)) return new Tile('grass-beach', 'curve_out', 135);
  131. if (check('g', 5, 6) && check('b', 7, 0, 1, 2, 3, 4)) return new Tile('grass-beach', 'curve_out', 225);
  132. if (check('g', 6, 7) && check('b', 0, 1, 2, 3, 4, 5)) return new Tile('grass-beach', 'curve_out', 225);
  133. if (check('g', 3, 4) && check('b', 5, 6, 7, 0, 1, 2)) return new Tile('grass-beach', 'curve_out', 315);
  134. if (check('g', 4, 5) && check('b', 6, 7, 0, 1, 2, 3)) return new Tile('grass-beach', 'curve_out', 315);
  135. // fallbacks in case all edges are from the same tile, ignore corners now
  136. if (check('b', 1, 3, 5, 7)) return new Tile('beach', 'straight', 45);
  137. if (check('g', 1, 3, 5, 7)) return new Tile('grass', 'straight', 45);
  138. console.error('No tile found for', map);
  139. }
  140. function setMapTile(pos, tile) {
  141. if (!tile) return;
  142. if (!gMap[pos.y]) return;
  143. if (!gMap[pos.y][pos.x]) return;
  144. gMap[pos.y][pos.x].tile = tile;
  145. }
  146. function calculateMapTile(pos) {
  147. // top row
  148. // var tl = edgeMap(getMapTile({ y: pos.y-1, x: pos.x-1 }));
  149. var to = edgeMap(getMapTile({ y: pos.y-1, x: pos.x }));
  150. // var tr = edgeMap(getMapTile({ y: pos.y-1, x: pos.x+1 }));
  151. // middle row
  152. var le = edgeMap(getMapTile({ y: pos.y, x: pos.x-1 }));
  153. var ri = edgeMap(getMapTile({ y: pos.y, x: pos.x+1 }));
  154. // bottom row
  155. // var bl = edgeMap(getMapTile({ y: pos.y+1, x: pos.x-1 }));
  156. var bo = edgeMap(getMapTile({ y: pos.y+1, x: pos.x }));
  157. // var br = edgeMap(getMapTile({ y: pos.y+1, x: pos.x+1 }));
  158. // calculate edges and corners
  159. // 0
  160. // 7 - - 1
  161. // 6 - - 2
  162. // 5 - - 3
  163. // 4
  164. // return = edgeMapToTile([tl[4], to[5], tr[6], ri[7], br[0], bo[1], bl[2], le[3]]);
  165. return edgeMapToTile([to[6], to[5], to[4], ri[7], bo[2], bo[1], bo[0], le[3]]);
  166. }
  167. function resetMapTile(pos) {
  168. setMapTile(pos, calculateMapTile(pos));
  169. }
  170. function putMapTile(pos, tile) {
  171. setMapTile(pos, tile);
  172. for (var i = 0; i < 10; ++i) {
  173. // cross
  174. resetMapTile({ y: pos.y-1, x: pos.x });
  175. resetMapTile({ y: pos.y, x: pos.x-1 });
  176. resetMapTile({ y: pos.y, x: pos.x+1 });
  177. resetMapTile({ y: pos.y+1, x: pos.x });
  178. // top corners
  179. resetMapTile({ y: pos.y-1, x: pos.x-1 });
  180. resetMapTile({ y: pos.y-1, x: pos.x+1 });
  181. // bottom corners
  182. resetMapTile({ y: pos.y+1, x: pos.x-1 });
  183. resetMapTile({ y: pos.y+1, x: pos.x+1 });
  184. }
  185. }
  186. function setupCanvas() {
  187. gMapCanvas = window.document.getElementById('map');
  188. gMapCanvas.oncontextmenu = function (e) {
  189. e.preventDefault();
  190. };
  191. gMapCanvas.addEventListener('mousemove', function (event) {
  192. var rect = gMapCanvas.getBoundingClientRect();
  193. gMousePos = {
  194. x: event.clientX - rect.left,
  195. y: event.clientY - rect.top
  196. };
  197. gMapHover = translateScreenToMap({ x: gMousePos.x, y: gMousePos.y });
  198. if (gMouseDrag && gCamera.drag) {
  199. gCamera.x = gCamera.drag.x - (gMousePos.x - gMouseDrag.x);
  200. gCamera.y = gCamera.drag.y - (gMousePos.y - gMouseDrag.y);
  201. }
  202. }, false);
  203. gMapCanvas.addEventListener('mousedown', function (e) {
  204. if (e.button === 2) {
  205. gMouseDrag = { x: gMousePos.x, y: gMousePos.y };
  206. gCamera.drag = { x: gCamera.x, y: gCamera.y };
  207. } else if (e.button === 0 && gTool) {
  208. putMapTile(gMapHover, gTool.tile);
  209. }
  210. }, false);
  211. gMapCanvas.addEventListener('mouseup', function () {
  212. gMouseDrag = null;
  213. gCamera.drag = null;
  214. }, false);
  215. gCtx = gMapCanvas.getContext('2d');
  216. }
  217. function saveMap() {
  218. localStorage.map = JSON.stringify(gMap);
  219. }
  220. function loadMap() {
  221. var map = [];
  222. if (localStorage.map) {
  223. map = JSON.parse(localStorage.map);
  224. } else {
  225. for (var y = 0; y < 100; y++) {
  226. map[y] = [];
  227. for (var x = 0; x < 100; x++) {
  228. map[y][x] = {
  229. tile: new Tile('grass', 'straight', 45),
  230. item: Math.random() < 0.03 ? new Item('tree_0', 'tree_0.png') : null
  231. };
  232. }
  233. }
  234. }
  235. gMap = map;
  236. }
  237. function init() {
  238. setupWindow();
  239. setupCanvas();
  240. setupToolbar();
  241. resize();
  242. gCamera.x = 0;
  243. gCamera.y = 0;
  244. loadMap();
  245. renderLoop();
  246. }
  247. function renderLoop(){
  248. // window.requestAnimationFrame(renderLoop);
  249. window.setTimeout(renderLoop, 500);
  250. render();
  251. }
  252. function translateMapToScreen(map) {
  253. var screen = {};
  254. screen.x = (map.x - map.y) * (64/2);
  255. screen.y = (map.x + map.y) * (64/4);
  256. // start in the horizontal middle
  257. // screen.x = (gMapCanvas.width/2) - (64/2) + screen.x;
  258. screen.x -= gCamera.x;
  259. screen.y -= gCamera.y;
  260. return screen;
  261. }
  262. function translateScreenToMap(screen) {
  263. var map = {};
  264. screen.x += gCamera.x;
  265. screen.y += gCamera.y;
  266. map.x = (screen.x / (64/2) + screen.y / (64/4)) / 2;
  267. map.y = (screen.y / (64/4) - (screen.x / (64/2))) / 2;
  268. map.y = map.y.toFixed() - 1;
  269. map.x = map.x.toFixed() - 2;
  270. return map;
  271. }
  272. function render() {
  273. gCtx.clearRect(0, 0, gMapCanvas.width, gMapCanvas.height);
  274. var y, x, coords;
  275. for (y = 0; y < gMap.length; y++) {
  276. for (x = 0; x < gMap[y].length; x++) {
  277. coords = translateMapToScreen({x: x, y: y});
  278. var img = gTileCache[gMap[y][x].tile.cacheKey];
  279. if (gTool && gMapHover && gMapHover.x === x && gMapHover.y === y) img = gTileCache[gTool.tile.cacheKey];
  280. gCtx.drawImage(img, coords.x, coords.y);
  281. }
  282. }
  283. for (y = 0; y < gMap.length; y++) {
  284. for (x = 0; x < gMap[y].length; x++) {
  285. if (!gMap[y][x].item) continue;
  286. coords = translateMapToScreen({x: x, y: y});
  287. var img = gTileCache[gMap[y][x].item.cacheKey];
  288. gCtx.drawImage(img, coords.x, coords.y);
  289. }
  290. }
  291. }