index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 getMapTile(pos) {
  59. if (!gMap[pos.y]) return null;
  60. if (!gMap[pos.y][pos.x]) return null;
  61. return gMap[pos.y][pos.x];
  62. }
  63. function setMapTile(pos, tile) {
  64. if (!tile) return;
  65. if (!gMap[pos.y]) return;
  66. if (!gMap[pos.y][pos.x]) return;
  67. gMap[pos.y][pos.x] = tile;
  68. }
  69. function calculateMapTile(pos) {
  70. var tile = getMapTile(pos);
  71. // top row
  72. var tl = getMapTile({ y: pos.y-1, x: pos.x-1 });
  73. var to = getMapTile({ y: pos.y-1, x: pos.x });
  74. var tr = getMapTile({ y: pos.y-1, x: pos.x+1 });
  75. // middle row
  76. var le = getMapTile({ y: pos.y, x: pos.x-1 });
  77. var re = getMapTile({ y: pos.y, x: pos.x+1 });
  78. // bottom row
  79. var bl = getMapTile({ y: pos.y+1, x: pos.x-1 });
  80. var bo = getMapTile({ y: pos.y+1, x: pos.x });
  81. var br = getMapTile({ y: pos.y+1, x: pos.x+1 });
  82. // top edge
  83. var toe;
  84. if (!to) {
  85. toe = '';
  86. } else if (to.name === 'grass' || to.name === 'beach') {
  87. toe = to.name;
  88. } else if (to.name === 'grass-beach' && to.type === 'straight') {
  89. if (to.orientation === 225) toe = 'grass';
  90. else toe = 'beach';
  91. } else if (to.name === 'grass-beach' && to.type === 'curve_in') {
  92. if (to.orientation === 45 || to.orientation === 135) toe = 'beach';
  93. else toe = 'grass';
  94. } else {
  95. toe = to.name;
  96. }
  97. // bottom edge
  98. var boe;
  99. if (!bo) {
  100. boe = '';
  101. } else if (bo.name === 'grass' || bo.name === 'beach') {
  102. boe = bo.name;
  103. } else if (bo.name === 'grass-beach' && bo.type === 'straight') {
  104. if (bo.orientation === 45) boe = 'grass';
  105. else boe = 'beach';
  106. } else if (bo.name === 'grass-beach' && bo.type === 'curve_in') {
  107. if (bo.orientation === 45 || bo.orientation === 135) boe = 'grass';
  108. else boe = 'beach';
  109. } else {
  110. boe = bo.name;
  111. }
  112. // left edge
  113. var lee;
  114. if (!le) {
  115. lee = '';
  116. } else if (le.name === 'grass' || le.name === 'beach') {
  117. lee = le.name;
  118. } else if (le.name === 'grass-beach' && le.type === 'straight') {
  119. if (le.orientation === 315) lee = 'grass';
  120. else lee = 'beach';
  121. } else if (le.name === 'grass-beach' && le.type === 'curve_in') {
  122. if (le.orientation === 45 || le.orientation === 315) lee = 'grass';
  123. else lee = 'beach';
  124. } else {
  125. lee = le.name;
  126. }
  127. // right edge
  128. var ree;
  129. if (!re) {
  130. ree = '';
  131. } else if (re.name === 'grass' || re.name === 'beach') {
  132. ree = re.name;
  133. } else if (re.name === 'grass-beach' && re.type === 'straight') {
  134. if (re.orientation === 135) ree = 'grass';
  135. else ree = 'beach';
  136. } else if (re.name === 'grass-beach' && re.type === 'curve_in') {
  137. if (re.orientation === 45 || re.orientation === 315) ree = 'beach';
  138. else ree = 'grass';
  139. } else {
  140. ree = re.name;
  141. }
  142. // top left corner
  143. var tlc;
  144. if (!tl) {
  145. tlc = '';
  146. } else if (tl.name === 'grass' || tl.name === 'beach') {
  147. tlc = tl.name;
  148. } else if (tl.name === 'grass-beach' && tl.type === 'curve_in') {
  149. if (tl.orientation === 135) tlc = 'beach';
  150. else tlc = 'grass';
  151. } else if (tl.name === 'grass-beach' && tl.type === 'curve_out') {
  152. if (tl.orientation === 315) tlc = 'grass';
  153. else tlc = 'beach';
  154. } else {
  155. tlc = tl.name;
  156. }
  157. // top right corner
  158. var trc;
  159. if (!tr) {
  160. trc = '';
  161. } else if (tr.name === 'grass' || tr.name === 'beach') {
  162. trc = tr.name;
  163. } else if (tr.name === 'grass-beach' && tr.type === 'curve_in') {
  164. if (tr.orientation === 45) trc = 'beach';
  165. else trc = 'grass';
  166. } else if (tr.name === 'grass-beach' && tr.type === 'curve_out') {
  167. if (tr.orientation === 225) trc = 'grass';
  168. else trc = 'beach';
  169. } else {
  170. trc = tr.name;
  171. }
  172. // bottom right corner
  173. var brc;
  174. if (!br) {
  175. brc = '';
  176. } else if (br.name === 'grass' || br.name === 'beach') {
  177. brc = br.name;
  178. } else if (br.name === 'grass-beach' && br.type === 'curve_in') {
  179. if (br.orientation === 315) brc = 'beach';
  180. else brc = 'grass';
  181. } else if (br.name === 'grass-beach' && br.type === 'curve_out') {
  182. if (br.orientation === 135) brc = 'grass';
  183. else brc = 'beach';
  184. } else {
  185. brc = br.name;
  186. }
  187. // bottom left corner
  188. var blc;
  189. if (!bl) {
  190. blc = '';
  191. } else if (bl.name === 'grass' || bl.name === 'beach') {
  192. blc = bl.name;
  193. } else if (bl.name === 'grass-beach' && bl.type === 'curve_in') {
  194. if (bl.orientation === 225) blc = 'beach';
  195. else blc = 'grass';
  196. } else if (bl.name === 'grass-beach' && bl.type === 'curve_out') {
  197. if (bl.orientation === 45) blc = 'grass';
  198. else blc = 'beach';
  199. } else {
  200. blc = bl.name;
  201. }
  202. console.log(pos, toe, boe, lee, ree);
  203. if (toe === 'grass' && lee === 'grass' && boe === 'grass' && ree === 'grass') tile = new Tile('grass', 'straight', 45);
  204. if (toe === 'beach' && lee === 'beach' && boe === 'beach' && ree === 'beach') {
  205. if (tlc === 'grass') tile = new Tile('grass-beach', 'curve_out', 135);
  206. else if (trc === 'grass') tile = new Tile('grass-beach', 'curve_out', 45);
  207. else if (brc === 'grass') tile = new Tile('grass-beach', 'curve_out', 315);
  208. else if (blc === 'grass') tile = new Tile('grass-beach', 'curve_out', 225);
  209. else tile = new Tile('beach', 'straight', 45);
  210. }
  211. if (toe === 'beach' && lee === 'grass' && boe === 'grass' && ree === 'grass') tile = new Tile('grass-beach', 'straight', 225);
  212. if (toe === 'beach' && lee === 'beach' && boe === 'grass' && ree === 'beach') tile = new Tile('grass-beach', 'straight', 225);
  213. if (toe === 'grass' && lee === 'beach' && boe === 'grass' && ree === 'grass') tile = new Tile('grass-beach', 'straight', 315);
  214. if (toe === 'grass' && lee === 'grass' && boe === 'beach' && ree === 'grass') tile = new Tile('grass-beach', 'straight', 45);
  215. if (toe === 'grass' && lee === 'beach' && boe === 'beach' && ree === 'beach') tile = new Tile('grass-beach', 'straight', 45);
  216. if (toe === 'grass' && lee === 'grass' && boe === 'grass' && ree === 'beach') tile = new Tile('grass-beach', 'straight', 135);
  217. if (toe === 'beach' && lee === 'grass' && boe === 'beach' && ree === 'beach') tile = new Tile('grass-beach', 'straight', 135);
  218. if (toe === 'beach' && lee === 'beach' && boe === 'beach' && ree === 'grass') tile = new Tile('grass-beach', 'straight', 315);
  219. if (toe === 'grass' && lee === 'grass' && boe === 'beach' && ree === 'beach') tile = new Tile('grass-beach', 'curve_in', 135);
  220. if (toe === 'grass' && lee === 'beach' && boe === 'grass' && ree === 'beach') tile = new Tile('grass-beach', 'curve_in', 45);
  221. if (toe === 'grass' && lee === 'beach' && boe === 'beach' && ree === 'grass') tile = new Tile('grass-beach', 'curve_in', 45);
  222. if (toe === 'beach' && lee === 'beach' && boe === 'grass' && ree === 'grass') tile = new Tile('grass-beach', 'curve_in', 315);
  223. if (toe === 'beach' && lee === 'grass' && boe === 'grass' && ree === 'beach') tile = new Tile('grass-beach', 'curve_in', 225);
  224. return tile;
  225. }
  226. function resetMapTile(pos) {
  227. setMapTile(pos, calculateMapTile(pos));
  228. }
  229. function putMapTile(pos, tile) {
  230. setMapTile(pos, tile);
  231. // Iteration one!
  232. // cross
  233. resetMapTile({ y: pos.y-1, x: pos.x });
  234. resetMapTile({ y: pos.y, x: pos.x-1 });
  235. resetMapTile({ y: pos.y, x: pos.x+1 });
  236. resetMapTile({ y: pos.y+1, x: pos.x });
  237. // top corners
  238. resetMapTile({ y: pos.y-1, x: pos.x-1 });
  239. resetMapTile({ y: pos.y-1, x: pos.x+1 });
  240. // bottom corners
  241. resetMapTile({ y: pos.y+1, x: pos.x-1 });
  242. resetMapTile({ y: pos.y+1, x: pos.x+1 });
  243. // // Iteration two!
  244. // // cross
  245. // resetMapTile({ y: pos.y-1, x: pos.x });
  246. // resetMapTile({ y: pos.y, x: pos.x-1 });
  247. // resetMapTile({ y: pos.y, x: pos.x+1 });
  248. // resetMapTile({ y: pos.y+1, x: pos.x });
  249. // // top corners
  250. // resetMapTile({ y: pos.y-1, x: pos.x-1 });
  251. // resetMapTile({ y: pos.y-1, x: pos.x+1 });
  252. // // bottom corners
  253. // resetMapTile({ y: pos.y+1, x: pos.x-1 });
  254. // resetMapTile({ y: pos.y+1, x: pos.x+1 });
  255. // // Iteration trhee!
  256. // // cross
  257. // resetMapTile({ y: pos.y-1, x: pos.x });
  258. // resetMapTile({ y: pos.y, x: pos.x-1 });
  259. // resetMapTile({ y: pos.y, x: pos.x+1 });
  260. // resetMapTile({ y: pos.y+1, x: pos.x });
  261. // // top corners
  262. // resetMapTile({ y: pos.y-1, x: pos.x-1 });
  263. // resetMapTile({ y: pos.y-1, x: pos.x+1 });
  264. // // bottom corners
  265. // resetMapTile({ y: pos.y+1, x: pos.x-1 });
  266. // resetMapTile({ y: pos.y+1, x: pos.x+1 });
  267. }
  268. function setupCanvas() {
  269. gMapCanvas = window.document.getElementById('map');
  270. gMapCanvas.oncontextmenu = function (e) {
  271. e.preventDefault();
  272. };
  273. gMapCanvas.addEventListener('mousemove', function (event) {
  274. var rect = gMapCanvas.getBoundingClientRect();
  275. gMousePos = {
  276. x: event.clientX - rect.left,
  277. y: event.clientY - rect.top
  278. };
  279. gMapHover = translateScreenToMap({ x: gMousePos.x, y: gMousePos.y });
  280. if (gMouseDrag && gCamera.drag) {
  281. gCamera.x = gCamera.drag.x - (gMousePos.x - gMouseDrag.x);
  282. gCamera.y = gCamera.drag.y - (gMousePos.y - gMouseDrag.y);
  283. }
  284. }, false);
  285. gMapCanvas.addEventListener('mousedown', function (e) {
  286. if (e.button === 2) {
  287. gMouseDrag = { x: gMousePos.x, y: gMousePos.y };
  288. gCamera.drag = { x: gCamera.x, y: gCamera.y };
  289. } else if (e.button === 0 && gTool) {
  290. putMapTile(gMapHover, gTool.tile);
  291. }
  292. }, false);
  293. gMapCanvas.addEventListener('mouseup', function () {
  294. gMouseDrag = null;
  295. gCamera.drag = null;
  296. }, false);
  297. gCtx = gMapCanvas.getContext('2d');
  298. }
  299. function saveMap() {
  300. localStorage.map = JSON.stringify(gMap);
  301. }
  302. function loadMap() {
  303. var map = [];
  304. if (localStorage.map) {
  305. map = JSON.parse(localStorage.map);
  306. } else {
  307. for (var y = 0; y < 100; y++) {
  308. map[y] = [];
  309. for (var x = 0; x < 100; x++) {
  310. map[y][x] = new Tile('grass', 'straight', 45);
  311. }
  312. }
  313. }
  314. gMap = map;
  315. }
  316. function init() {
  317. setupWindow();
  318. setupCanvas();
  319. setupToolbar();
  320. resize();
  321. gCamera.x = 0;
  322. gCamera.y = 0;
  323. loadMap();
  324. renderLoop();
  325. }
  326. function renderLoop(){
  327. window.requestAnimationFrame(renderLoop);
  328. render();
  329. }
  330. function translateMapToScreen(map) {
  331. var screen = {};
  332. screen.x = (map.x - map.y) * (64/2);
  333. screen.y = (map.x + map.y) * (64/4);
  334. // start in the horizontal middle
  335. // screen.x = (gMapCanvas.width/2) - (64/2) + screen.x;
  336. screen.x -= gCamera.x;
  337. screen.y -= gCamera.y;
  338. return screen;
  339. }
  340. function translateScreenToMap(screen) {
  341. var map = {};
  342. screen.x += gCamera.x;
  343. screen.y += gCamera.y;
  344. map.x = (screen.x / (64/2) + screen.y / (64/4)) / 2;
  345. map.y = (screen.y / (64/4) - (screen.x / (64/2))) / 2;
  346. map.y = map.y.toFixed() - 1;
  347. map.x = map.x.toFixed() - 2;
  348. return map;
  349. }
  350. function render() {
  351. gCtx.clearRect(0, 0, gMapCanvas.width, gMapCanvas.height);
  352. for (var y = 0; y < gMap.length; y++) {
  353. for (var x = 0; x < gMap[y].length; x++) {
  354. var coords = translateMapToScreen({x: x, y: y});
  355. var img = gTileCache[gMap[y][x].cacheKey];
  356. if (gTool && gMapHover && gMapHover.x === x && gMapHover.y === y) img = gTileCache[gTool.tile.cacheKey];
  357. gCtx.drawImage(img, coords.x, coords.y);
  358. }
  359. }
  360. }