index.js 15 KB

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