
/**
 * beinhaltet den Szenengraphen und Methoden, die die Szene ins canvas zeichnen.
 *
 */
function Scene(){
	this.name = "";
	this.backgroundColor = "rgb(16,79,16)";
	this.rootSceneNode = null;
	// in diesem Node befindet sich das Level. 
	this.worldSceneNode = null;
	this.characters = new Array();
	this.camera = new Camera();
	this.feuerwerke = new Array();
}

Scene.prototype.addCharacter = function(character){
	this.characters.push(character);
	this.worldSceneNode.addNode(character);
}

Scene.prototype.update = function(time){
	var camX = 0;
	for(var i = 0; i < this.characters.length; i++){
		this.characters[i].addTime(time);
		
		camX += this.characters[i].position.x;
	}
	
	for(var i = 0; i < this.feuerwerke.length; i++){
		this.feuerwerke[i].addTime(time);
	}
	
	// Kameraposition auf den Mittelpunkt der Characterpositionen setzen.
	camX = camX / this.characters.length;
	camX = Math.max(camX, 0);
	this.camera.position.x = camX;
	
}

Scene.prototype.draw = function(){
	// alles schwarz anfärbeln
	context.fillStyle = "rgb(0,0,0)";
	context.fillRect(0,00,800,200);
	
	// Levelhintergrundfarbe drüberstreichen
	context.fillStyle = this.backgroundColor;
	context.fillRect(0,200,800,400);

	// Alle Nodes sortiert nach 1.: layer und 2.: vom entferntesten beginnend zeichnen.
	var layers = this.getAllNodesSortedByLayersAndDistance();
	var sortedKeys = new Array();
	for(key in layers){
		sortedKeys.push(key);
	}
	sortfunction = function(a,b){
		return a - b;
	}
	sortedKeys.sort(sortfunction);
	for(var i = 0; i < sortedKeys.length; i++){
		var key = sortedKeys[i];
		var nodes = layers[key];
		for(var j = 0; j < nodes.length; j++){
			nodes[j].draw(this.camera);
		}
	}
	
	{// zeichne characterinfo
		// hintergrund
		context.fillStyle = "rgb(47,72,144)";
		context.fillRect(0,0,800,108);
		
		// helle striche
		context.fillStyle = "rgb(64,96,160)";
		context.fillRect(0,0,800,2);
		context.fillRect(0,54,800,2);
		for(var i = 198; i <= 800; i+=200){
			context.fillRect(i,0,2,108);
		}
		
		// dunkle striche
		context.fillStyle = "rgb(32,63,127)";
		context.fillRect(0,52,800,2);
		context.fillRect(0,106,800,2);
		for(var i = 0; i <= 600; i+=200){
			context.fillRect(i,0,2,108);
		}
		
		// Die 8 kastl ausmalen
		for(var i = 0; i < 8; i++){
			var x = (i % 4) * 200 + 60;
			var y = 25;
			if(i >= 4){
				y += 54;
			}
			
			//lifebar
			context.fillStyle = "rgb(64,96,160)";
			context.fillRect(x,y,130,2);
			context.fillRect(x,y-14,2,14);
			context.fillStyle = "rgb(32,63,127)";
			context.fillRect(x+130,y-14,2,14);
			context.fillRect(x,y-14,130,2);
			
			//manabar
			context.fillStyle = "rgb(64,96,160)";
			context.fillRect(x,20+y,130,2);
			context.fillRect(x,20+y-14,2,14);
			context.fillStyle = "rgb(32,63,127)";
			context.fillRect(x+130,20+y-14,2,14);
			context.fillRect(x,20+y-14,130,2);
			
			// character
			if(i < this.characters.length){
			
				// character icon
				var character = this.characters[i];
				var aIdle = character.animations["idle"];
				aIdle.sprite.draw(aIdle.getIndex(), x - 50, y-25, 0.6, 0.6);
				
				// character life
				context.fillStyle = "rgb(255,0,0)";
				var width = 128 * (character.currentlife / character.maxlife);
				context.fillRect(x+2,y-12,width,11);
				
				// character mana
				context.fillStyle = "rgb(0,0,255)";
				var width = 128 * (character.currentmana / character.maxmana);
				context.fillRect(x+2,y+8,width,11);
			}
		}
	}
}

Scene.prototype.getAllNodesSortedByLayersAndDistance = function(){
	var stack = new Array();
	var layers = new function(){};
	
	stack.push(this.rootSceneNode);
	while(stack.length > 0){
		var node = stack.pop();
		if(layers[node.layer] == null){
			layers[node.layer] = new Array();
		}
		layers[node.layer].push(node);
		
		for(var i = 0; i < node.getNodes().length; i++){
			stack.push(node.getNodes()[i]);
		}
	}
	
	sortfunction = function(a,b){
		return a.getDerivedPosition().z - b.getDerivedPosition().z;
	}
	
	for(key in layers){
		var nodes = layers[key];
		nodes.sort(sortfunction);
		layers[key] = nodes;
	}
	
	return layers;
}

function createWoodScene(){
	var imgForestt = new Image();
	imgForestt.src = "/blog_resources/littleFighterJavascript/resources/images/bg/lf/forestt.png";
	
	var imgForests = new Image();
	imgForests.src = "/blog_resources/littleFighterJavascript/resources/images/bg/lf/forests.png";
	
	var imgForestm1 = new Image();
	imgForestm1.src = "/blog_resources/littleFighterJavascript/resources/images/bg/lf/forestm1.png";
	
	var scene = new Scene();
	scene.name = "wood";
	scene.backgroundColor = "rgb(16,79,16)";
	
	var root = new SceneNode("root");
	scene.rootSceneNode = root;
	
	var world = new SceneNode("world");
	world.setPosition(new Vector3(0,128,0));
	root.addNode(world);
	scene.worldSceneNode = world;
	
	// bäume
	for(var i = 0; i < 20; i++){
		var trees = new ImageNode("trees_" + i, imgForestt );
		trees.setPosition(new Vector3(252*i,60,-0.01));
		trees.layer = 10;
		world.addNode(trees);
	}
	
	// himmel
	var inForests = new ImageNode("sky", imgForests);
	inForests.setPosition(new Vector3(0,0,-100000));
	inForests.layer = 5;
	world.addNode(inForests);
	
	// berge
	var inForestm1 = new ImageNode("mountain1", imgForestm1);
	inForestm1.setPosition(new Vector3(0,20,-10));
	inForestm1.layer = 7;
	world.addNode(inForestm1);
	
	var inForestm12 = new ImageNode("mountain2", imgForestm1);
	inForestm12.setPosition(new Vector3(800,30,-10));
	inForestm12.layer = 7;
	world.addNode(inForestm12);
	
	
	
	return scene;
}
