| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- 'use strict';
- window.addEventListener('load', init, false);
- var gMapCanvas = null;
- var gCtx = null;
- var gCamera = {};
- var gMousePos = {};
- var gMouseDrag = false;
- var gMapHover = {};
- var gTool = null;
- var gToolBarHeight = 64;
- var gMap = null;
- var gTileCache = {};
- function tileToFile(tile) {
- return 'Tiles/ts_' + tile.name + '0/' + tile.type + '/' + tile.orientation + '/0.png';
- }
- function loadTile(tile) {
- // console.log('load tile', tile.name, tile.type, tile.orientation);
- gTileCache[tile.cacheKey] = new Image();
- gTileCache[tile.cacheKey].src = tileToFile(tile);
- }
- function Tile(name, type, orientation) {
- this.name = name;
- this.type = type;
- this.orientation = orientation;
- this.cacheKey = name + type + orientation;
- if (!gTileCache[this.cacheKey]) loadTile(this);
- this.img = gTileCache[this.cacheKey];
- }
- function Item(name, file) {
- this.name = name;
- this.cacheKey = file;
- if (!gTileCache[this.cacheKey]) {
- gTileCache[this.cacheKey] = new Image();
- gTileCache[this.cacheKey].src = file;
- }
- this.img = gTileCache[this.cacheKey];
- }
- function setTool(source) {
- if (gTool) {
- gTool.button.classList.remove('active');
- } else {
- gTool = {};
- }
- source.classList.add('active');
- gTool.tool = source.attributes.tool.value;
- gTool.tile = new Tile(source.attributes.tool.value, 'straight', 45);
- gTool.button = source;
- }
- function resize() {
- if (gMapCanvas) {
- gMapCanvas.width = window.innerWidth;
- gMapCanvas.height = window.innerHeight - gToolBarHeight;
- }
- }
- function setupToolbar() {
- var toolbar = window.document.getElementsByClassName('toolbar')[0];
- for (var i = 0; i < toolbar.children.length; i++) {
- var child = toolbar.children[i];
- if (child.attributes.tool) {
- child.style.backgroundImage = 'url("' + tileToFile(new Tile(child.attributes.tool.value, 'straight', 45)) + '")';
- }
- }
- }
- function setupWindow() {
- window.addEventListener('resize', resize, false);
- }
- function getMapTile(pos) {
- if (!gMap[pos.y]) return null;
- if (!gMap[pos.y][pos.x]) return null;
- return gMap[pos.y][pos.x].tile;
- }
- function edgeMap(tile) {
- if (!tile) return null;
- var map = [];
- if (tile.name === 'grass') return ['g', 'g', 'g', 'g', 'g', 'g', 'g', 'g'];
- if (tile.name === 'beach') return ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'];
- if (tile.name === 'grass-beach') {
- map = ['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'];
- if (tile.type === 'straight') {
- if (tile.orientation === 45) map[0] = map[1] = map[2] = 'g';
- if (tile.orientation === 135) map[6] = map[7] = map[0] = 'g';
- if (tile.orientation === 225) map[4] = map[5] = map[6] = 'g';
- if (tile.orientation === 315) map[2] = map[3] = map[4] = 'g';
- } else if (tile.type === 'curve_in') {
- if (tile.orientation === 45) map[0] = map[1] = map[2] = map[3] = map[4] = 'g';
- if (tile.orientation === 135) map[6] = map[7] = map[0] = map[1] = map[2] = 'g';
- if (tile.orientation === 225) map[4] = map[5] = map[6] = map[7] = map[0] = 'g';
- if (tile.orientation === 315) map[2] = map[3] = map[4] = map[5] = map[6] = 'g';
- } else if (tile.type === 'curve_out') {
- if (tile.orientation === 45) map[2] = 'g';
- if (tile.orientation === 135) map[0] = 'g';
- if (tile.orientation === 225) map[6] = 'g';
- if (tile.orientation === 315) map[4] = 'g';
- } else {
- throw(new Error('unknown tile ' + tile.name + ' type ' + tile.type));
- }
- return map;
- }
- throw(new Error('unknown tile ' + tile.name + ' type ' + tile.type));
- }
- function edgeMapToTile(map) {
- function check(type) {
- var args = [];
- for (var a in arguments) args.push(arguments[a]);
- args.shift();
- return args.every(function (a) { return map[a] === type; });
- }
- if (check('g', 0, 1, 2, 3, 4, 5, 6, 7)) return new Tile('grass', 'straight', 45);
- if (check('b', 0, 1, 2, 3, 4, 5, 6, 7)) return new Tile('beach', 'straight', 45);
- if (check('g', 2) && check('b', 0, 1, 3, 4, 5, 6, 7)) return new Tile('grass-beach', 'curve_out', 45);
- if (check('g', 0) && check('b', 1, 2, 3, 4, 5, 6, 7)) return new Tile('grass-beach', 'curve_out', 135);
- if (check('g', 6) && check('b', 0, 1, 2, 3, 4, 5, 7)) return new Tile('grass-beach', 'curve_out', 225);
- if (check('g', 4) && check('b', 0, 1, 2, 3, 5, 6, 7)) return new Tile('grass-beach', 'curve_out', 315);
- if (check('g', 0, 1, 2) && check('b', 3, 4, 5, 6, 7)) return new Tile('grass-beach', 'straight', 45);
- if (check('g', 6, 7, 0) && check('b', 1, 2, 3, 4, 5)) return new Tile('grass-beach', 'straight', 135);
- if (check('g', 4, 5, 6) && check('b', 0, 1, 2, 3, 7)) return new Tile('grass-beach', 'straight', 225);
- if (check('g', 2, 3, 4) && check('b', 0, 1, 5, 6, 7)) return new Tile('grass-beach', 'straight', 315);
- if (check('g', 0, 1, 2, 3, 4) && check('b', 5, 6, 7)) return new Tile('grass-beach', 'curve_in', 45);
- if (check('g', 6, 7, 0, 1, 2) && check('b', 3, 4, 5)) return new Tile('grass-beach', 'curve_in', 135);
- if (check('g', 4, 5, 6, 7, 0) && check('b', 1, 2, 3)) return new Tile('grass-beach', 'curve_in', 225);
- if (check('g', 2, 3, 4, 5, 6) && check('b', 7, 0, 1)) return new Tile('grass-beach', 'curve_in', 315);
- // add those to allow placing tile without similar neighbours
- if (check('g', 0, 1, 2) && check('b', 5)) return new Tile('grass-beach', 'straight', 45);
- if (check('g', 6, 7, 0) && check('b', 3)) return new Tile('grass-beach', 'straight', 135);
- if (check('g', 4, 5, 6) && check('b', 1)) return new Tile('grass-beach', 'straight', 225);
- if (check('g', 2, 3, 4) && check('b', 7)) return new Tile('grass-beach', 'straight', 315);
- console.error('No tile found for', map);
- }
- function setMapTile(pos, tile) {
- if (!tile) return;
- if (!gMap[pos.y]) return;
- if (!gMap[pos.y][pos.x]) return;
- gMap[pos.y][pos.x].tile = tile;
- }
- function calculateMapTile(pos) {
- // top row
- var tl = edgeMap(getMapTile({ y: pos.y-1, x: pos.x-1 }));
- var to = edgeMap(getMapTile({ y: pos.y-1, x: pos.x }));
- var tr = edgeMap(getMapTile({ y: pos.y-1, x: pos.x+1 }));
- // middle row
- var le = edgeMap(getMapTile({ y: pos.y, x: pos.x-1 }));
- var ri = edgeMap(getMapTile({ y: pos.y, x: pos.x+1 }));
- // bottom row
- var bl = edgeMap(getMapTile({ y: pos.y+1, x: pos.x-1 }));
- var bo = edgeMap(getMapTile({ y: pos.y+1, x: pos.x }));
- var br = edgeMap(getMapTile({ y: pos.y+1, x: pos.x+1 }));
- // calculate edges and corners
- // 0
- // 7 - - 1
- // 6 - - 2
- // 5 - - 3
- // 4
- var tile = edgeMapToTile([tl[4], to[5], tr[6], ri[7], br[0], bo[1], bl[2], le[3]]);
- return tile;
- }
- function resetMapTile(pos) {
- setMapTile(pos, calculateMapTile(pos));
- }
- function putMapTile(pos, tile) {
- setMapTile(pos, tile);
- for (var i = 0; i < 10; ++i) {
- // cross
- resetMapTile({ y: pos.y-1, x: pos.x });
- resetMapTile({ y: pos.y, x: pos.x-1 });
- resetMapTile({ y: pos.y, x: pos.x+1 });
- resetMapTile({ y: pos.y+1, x: pos.x });
- // top corners
- resetMapTile({ y: pos.y-1, x: pos.x-1 });
- resetMapTile({ y: pos.y-1, x: pos.x+1 });
- // bottom corners
- resetMapTile({ y: pos.y+1, x: pos.x-1 });
- resetMapTile({ y: pos.y+1, x: pos.x+1 });
- }
- }
- function setupCanvas() {
- gMapCanvas = window.document.getElementById('map');
- gMapCanvas.oncontextmenu = function (e) {
- e.preventDefault();
- };
- gMapCanvas.addEventListener('mousemove', function (event) {
- var rect = gMapCanvas.getBoundingClientRect();
- gMousePos = {
- x: event.clientX - rect.left,
- y: event.clientY - rect.top
- };
- gMapHover = translateScreenToMap({ x: gMousePos.x, y: gMousePos.y });
- if (gMouseDrag && gCamera.drag) {
- gCamera.x = gCamera.drag.x - (gMousePos.x - gMouseDrag.x);
- gCamera.y = gCamera.drag.y - (gMousePos.y - gMouseDrag.y);
- }
- }, false);
- gMapCanvas.addEventListener('mousedown', function (e) {
- if (e.button === 2) {
- gMouseDrag = { x: gMousePos.x, y: gMousePos.y };
- gCamera.drag = { x: gCamera.x, y: gCamera.y };
- } else if (e.button === 0 && gTool) {
- putMapTile(gMapHover, gTool.tile);
- }
- }, false);
- gMapCanvas.addEventListener('mouseup', function () {
- gMouseDrag = null;
- gCamera.drag = null;
- }, false);
- gCtx = gMapCanvas.getContext('2d');
- }
- function saveMap() {
- localStorage.map = JSON.stringify(gMap);
- }
- function loadMap() {
- var map = [];
- if (localStorage.map) {
- map = JSON.parse(localStorage.map);
- } else {
- for (var y = 0; y < 100; y++) {
- map[y] = [];
- for (var x = 0; x < 100; x++) {
- map[y][x] = {
- tile: new Tile('grass', 'straight', 45),
- item: Math.random() < 0.03 ? new Item('tree_0', 'tree_0.png') : null
- };
- }
- }
- }
- gMap = map;
- }
- function init() {
- setupWindow();
- setupCanvas();
- setupToolbar();
- resize();
- gCamera.x = 0;
- gCamera.y = 0;
- loadMap();
- renderLoop();
- }
- function renderLoop(){
- // window.requestAnimationFrame(renderLoop);
- window.setTimeout(renderLoop, 500);
- render();
- }
- function translateMapToScreen(map) {
- var screen = {};
- screen.x = (map.x - map.y) * (64/2);
- screen.y = (map.x + map.y) * (64/4);
- // start in the horizontal middle
- // screen.x = (gMapCanvas.width/2) - (64/2) + screen.x;
- screen.x -= gCamera.x;
- screen.y -= gCamera.y;
- return screen;
- }
- function translateScreenToMap(screen) {
- var map = {};
- screen.x += gCamera.x;
- screen.y += gCamera.y;
- map.x = (screen.x / (64/2) + screen.y / (64/4)) / 2;
- map.y = (screen.y / (64/4) - (screen.x / (64/2))) / 2;
- map.y = map.y.toFixed() - 1;
- map.x = map.x.toFixed() - 2;
- return map;
- }
- function render() {
- gCtx.clearRect(0, 0, gMapCanvas.width, gMapCanvas.height);
- var y, x, coords;
- for (y = 0; y < gMap.length; y++) {
- for (x = 0; x < gMap[y].length; x++) {
- coords = translateMapToScreen({x: x, y: y});
- var img = gTileCache[gMap[y][x].tile.cacheKey];
- if (gTool && gMapHover && gMapHover.x === x && gMapHover.y === y) img = gTileCache[gTool.tile.cacheKey];
- gCtx.drawImage(img, coords.x, coords.y);
- }
- }
- for (y = 0; y < gMap.length; y++) {
- for (x = 0; x < gMap[y].length; x++) {
- if (!gMap[y][x].item) continue;
- coords = translateMapToScreen({x: x, y: y});
- var img = gTileCache[gMap[y][x].item.cacheKey];
- gCtx.drawImage(img, coords.x, coords.y);
- }
- }
- }
|