index.js 15 KB

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