/**
 * Name: Main isoGame script file.
 * By:   FireFly (firefly.nu).
 * Date: 2009-04-22.
 * 
 * ---[ Log: ]---
 * [09-04-17]: First version, generates an isometric map (the format of a larger isometric tile). Only grass. (Only map.js.)
 * [09-04-20]: Adds water; 100% random maps (each tile has a certain chance of grass/water).
 * [09-04-22]: Moves scripts to js folder, separates styles to an external css file. Places the map generator file in a separate file, updates to better represent continents. (Algorithm: Place a tile, check if enough amount of grass; if not, has a certain chance of repeating the algorithm on nearby cells. This chance decreases further away from starting point.) Starting with water bordering tiles; creates units.js, moves global vars and init to (the new) main.js.
 * [09-04-24]: Finished water bordering tiles, optimised DOM element creation (ups speed a lot), added input fields for map rendering params, added clear debug output log button, show/hide debug/rerender info.
 */

	/*** Global variables ***/
var Game = {
	map: {
		width: 20,
		height: 20,
		borders: true
	},
	character: {
		x: 4,
		y: 4
	},
	
	utils: {
		convertIso: function(x, y) {
			return {
				x: (x + y)*17,
				y: (x - y)*8 + 16 * Game.map.height/2
			};
		}
	}
};
Game.map.tileTypes = [];

	/*** function: init (start of script) ***/
Game.init = function() {
	Game.canvas = document.getElementById('gamefield');
//	var multiplier = (document.getElementById('settWidth').value*1 +
//			document.getElementById('settHeight').value) / 2;
	var multi = (parseInt(document.getElementById('settWidth').value) +
			parseInt(document.getElementById('settHeight').value)) / 2;
	
	var seed = parseInt(document.getElementById('seed').value);
	Game.random = new FF.Random.MersenneTwister(seed);
	
	Game.canvas.width = (multi + 1)*33;
	Game.canvas.height = (multi + 1)*16;
	
	Game.ctx = Game.canvas.getContext('2d');
	
		// Setup stuff.
	Game.map.setupTiles();
	Game.map.generateMap();
	Game.map.setupMap();
	Game.repaint();
	
		/* Prepare keypress handling. */
	document.body.onkeydown = function(ev) {
		var c = Game.character;
		var oldHeight = Game.map.map[c.x][c.y].height;
		
		switch (ev.keyCode) {
			case 37: Game.character.x--; break; // left.
			case 38: Game.character.y++; break; // up.
			case 39: Game.character.x++; break; // right.
			case 40: Game.character.y--; break; // down.
		}
		
		var t = Game.map.map[c.x][c.y];
		if (Math.abs(t.height - oldHeight) > 1 || t.type == Game.map.tileTypes[1]) {
				/* Reverse the action. */
			switch (ev.keyCode) {
				case 37: Game.character.x++; break; // left.
				case 38: Game.character.y--; break; // up.
				case 39: Game.character.x--; break; // right.
				case 40: Game.character.y++; break; // down.
			}
		}
		
		Game.repaint();
	};
};

Game.repaint = function() {
	Debug.print("Ritar om...");
	var element, gridDiv, overlay;
	var width=Game.map.width, height=Game.map.height;
	var k = 0;
	
	Game.ctx.strokeStyle = 'rgba(0, 0, 0, 0.2)';
	for (var y=height-1; y>=0; y--) {
		for (var x=0; x<height; x++) {
			k++;
				/* Rita ut karta för denna tile. */
			Game.map.draw(x, y, Game.map.borders, k);
			
				/* Rita ev. ut karaktär på tile. */
			if (x == Game.character.x+1 && y == Game.character.y) {
				var h = Game.map.map[x-1][y].height;
				var p = Game.utils.convertIso(x, y);
				p.y -= h*4;
				
				Game.ctx.fillStyle = '#F00';
				Game.ctx.fillRect(p.x-3, p.y-3, 6, 6);
			}
		}
	}
};