
/**
 * Alle Objekte in einem level(Bilder, Charactere, Items) sind SceneNodes. 
 *
 */
function SceneNode(name){
	this.name = name;
	this.children = new Array();
	// lokale Position
	this.position = new Vector3(0,0,0);
	this.parent = null;
	this.layer = 0;
}

SceneNode.prototype.setPosition = function(position){
	this.position = position;
}

SceneNode.prototype.getPosition = function(){
	return this.position;
}

SceneNode.prototype.draw = function(camera){

}

SceneNode.prototype.addNode = function(node){
	if(node.parent != null){
		node.parent.removeNode(node);
	}
	node.parent = this;
	this.children.push(node);
}

SceneNode.prototype.removeNode = function(node){
	for(var i = 0; i < this.children.length; i++){
		var child = this.children[i];
		if(child == node){
			this.children.splice(i, 1);
			return;
		}
	}
}

SceneNode.prototype.getNodes = function(){
	return this.children;
}

/**
 * Gibt die (globale) Position in Abhängigkeit der Hierarchie zurück. 
 *
 */
SceneNode.prototype.getDerivedPosition = function(){
	if(this.parent == null){
		return this.position;
	}else{
		return this.position.addVector(this.parent.getDerivedPosition());
	}
	
}

SceneNode.prototype.toString = function(){
	var msg = "== SceneNode ==\n";
	msg += "name:\t" + this.name + "\n";
	msg += "children:\t" + this.children.length + "\n";
	msg += "position:\t" + this.position.toString() + "\n";
	msg += "renderable:\t" + this.renderable + "\n";
	
	return msg;
}

SceneNode.prototype.getTreeAsString = function(level){
	var msg = "";
	for(var j = 0; j < level; j++){
		msg += "\t";
	}
	msg += this.name + "\t" + this.getDerivedPosition().toString() + "\n";
	for(var i = 0; i < this.children.length; i++){
		msg += this.children[i].getTreeAsString(level+1);
	}
	
	return msg;
}

/**
 * von SceneNode abgeleitet. Dient nur dazu, ein Bild zu rendern
 *
 */
function ImageNode(name, image){
	this.name = name;
	this.image = image;
}

ImageNode.prototype = new SceneNode();

ImageNode.prototype.toString = function(){
	var msg = "== ImageNode ==\n";
	msg += "name:\t\t" + this.name + "\n";
	msg += "image:\t\t" + this.image + "\n";
	msg += "children:\t" + this.children.length + "\n";
	msg += "position:\t" + this.position.toString() + "\n";
	
	return msg;
}

ImageNode.prototype.draw = function(camera){
	var pos = this.getDerivedPosition();
	pos.x = pos.x - (camera.position.x / (-pos.z+1));
	context.drawImage(this.image, pos.x,pos.y);
}





