

/**
 * von SceneNode abgeleitet
 *
 */
function Character(name){
	this.name = name;
	
	// aussehen
	this.animationState = "idle";
	this.animations = new Array();
	
	// position/richtung
	this.position = new Vector3(200,0,-100);
	this.speed = new Vector3(0,0,0);
	this.dirx = 1;
	
	// Tastaturbelegung und keyevents
	this.keyLeft = 0;
	this.keyRight = 0;
	this.keyUp = 0;
	this.keyDown = 0;
	this.keyAttack = 0;
	this.keyJump = 0;
	this.keyDefend = 0;
	this.pressedKeyCodes = new Array();
	this.lastKeyDownTime = new Array();
	
	this.walkSpeed = 150;
	this.runSpeed = 300;
	
	// Der State, in dem sich der Character gerade befindet
	this.currentAction = null;
	// assoziatives array
	this.actions = new function(){}; 
	
	this.maxlife = 100;
	this.possibleLife = 100;
	this.currentlife = 100;
	this.maxmana = 100;
	this.currentmana = 100;
	
	this.layer = 100;
	
	this.commandHistory = new Array();
	this.commandHistory[cmd_left] = new Array();
	this.commandHistory[cmd_right] = new Array();
	this.commandHistory[cmd_up] = new Array();
	this.commandHistory[cmd_down] = new Array();
	this.commandHistory[cmd_attack] = new Array();
	this.commandHistory[cmd_jump] = new Array();
	this.commandHistory[cmd_defend] = new Array();
}

Character.prototype = new SceneNode();

Character.keyIntervallForRun = 500;

Character.prototype.setAnimationState = function(animationState){
	this.animationState = animationState;
}

/**
 * Das zeichnen vom Character ist ein bissl aufwändiger. 
 * Der Character bewegt sich nicht im ScreenSpace, seine Position muss daher erst in diesen umgerechnet werden.
 * 
 * CharacterSpace: 
 *	x: mit ScreenSpace ident
 * 	y: die Höhe auf der sich der Character befindet. 
 * 		Ändert sich z.B. Wenn der Character springt, aber nicht wenn er weiter weg geht. 
 *		Beim Umrechnen ins Screenspace-y müssen CharacterSpace y und z beachtet werden. 
 *	z: Wie weit der Character von der Kamera entfernt ist. Der Wert ist <= 0. Je niedriger der Wert, desto weiter weg!
 *		Wird direkt zum ScreenSpace-y dazuaddiert.
 *
 */
Character.prototype.draw = function(camera){
	var a = this.currentAction.animation;
	var ppos = this.parent.getDerivedPosition();
	var x = this.position.x - (camera.position.x - camera.getWidth());
	var y = 500 + this.position.z;
	var sy = y;
	y = y - a.sprite.parts[a.getIndex()].height - this.position.y;
	
	
	context.drawImage(this.imgShadow, x+20,sy-3);
	a.sprite.draw(a.getIndex(), x, y, this.dirx, 1);
}

Character.prototype.translate= function(vector){
	this.position = this.position.addVector(vector);
}

Character.prototype.setPosition = function(vector){
	this.position = vector;
}

/**
 * @param direction
 *		links: -1
 *		rechts: 1
 */
Character.prototype.setDirection = function(direction){
	this.dirx = direction;
}

Character.prototype.addTime = function(time){
	if(this.currentAction != null){
		this.setCurrentAction(this.currentAction.update(time));
	}
}

Character.prototype.getLastCommand = function(cmd_code){
	return this.getXBeforeLastCommand(cmd_code, 0);
}

Character.prototype.getXBeforeLastCommand = function(cmd_code, x){
	var lastCommands = this.commandHistory[cmd_code];
	var c = lastCommands[lastCommands.length -(x+1)];
	return c;
}

Character.prototype.isKeyDown = function(cmd_code){
	var c = this.getLastCommand(cmd_code);
	return (c != null && c.end == null);
}
 
Character.prototype.isArrowKeyDown = function(){
	var cLeft = this.getLastCommand(cmd_left);
	var cRight = this.getLastCommand(cmd_right);
	var cUp = this.getLastCommand(cmd_up);
	var cDown = this.getLastCommand(cmd_down);
	
	var res = (cLeft != null && cLeft.end == null);
	res |= (cRight != null && cRight.end == null);
	res |= (cUp != null && cUp.end == null);
	res |= (cDown != null && cDown.end == null);
	
	return res;
}

Character.prototype.setCurrentAction = function(action){
	if(this.currentAction != null && this.currentAction != action){
		this.currentAction.setTime(0);
	}
	this.currentAction = action;
}


Character.prototype.move = function(vector){
	this.translate(vector);
	
	if(vector.x < 0){
		this.setDirection(-1);
	}else if(vector.x > 0){
		this.setDirection(1);
	}
}

Character.prototype.setSpeed = function(speed){
	this.speed = speed;
}

Character.prototype.idle = function(){
	this.setAnimationState("idle");
}

Character.prototype.damage = function(damage){
	this.possibleLife = Math.max(0, this.possibleLife-damage);
	this.currentlife = Math.max(0, this.currentlife-damage);
	
	var feuerpos = new Vector3(this.position.x, this.position.y, this.position.z);
	var feuerwerk = new Feuerwerk(3, feuerpos);
	scene.worldSceneNode.addNode(feuerwerk);
	scene.feuerwerke.push(feuerwerk);
	
}

Character.prototype.invokeKeyDown = function(event){
	//logDebug(event.which);

	var command = new Command();
	command.start = new Date().getTime();
	if(event.which == this.keyLeft){
		command.code = cmd_left;
	}else if(event.which == this.keyRight){
		command.code = cmd_right;
	}else if(event.which == this.keyUp){
		command.code = cmd_up;
	}else if(event.which == this.keyDown){
		command.code = cmd_down;
	}else if(event.which == this.keyAttack){
		command.code = cmd_attack;
	}else if(event.which == this.keyJump){
		command.code = cmd_jump;
	}else if(event.which == this.keyDefend){
		command.code = cmd_defend;
	}
	
	if(command.code != null){
		var lastCommands = this.commandHistory[command.code];
		var lastCommand = lastCommands[lastCommands.length-1];
		if(lastCommand != null && lastCommand.end == null){
			
		}else{
			this.commandHistory[command.code].push(command);
		}
		
	}
}

Character.prototype.invokeKeyPress = function(event){

}

Character.prototype.invokeKeyUp = function(event){
	var commandCode = null;
	if(event.which == this.keyLeft){
		commandCode = cmd_left;
	}else if(event.which == this.keyRight){
		commandCode = cmd_right;
	}else if(event.which == this.keyUp){
		commandCode = cmd_up;
	}else if(event.which == this.keyDown){
		commandCode = cmd_down;
	}else if(event.which == this.keyAttack){
		commandCode = cmd_attack;
	}else if(event.which == this.keyJump){
		commandCode = cmd_jump;
	}else if(event.which == this.keyDefend){
		commandCode = cmd_defend;
	}
	
	if(commandCode != null){
		var lastCommands = this.commandHistory[commandCode];
		var lastCommand = lastCommands[lastCommands.length-1];
		lastCommand.end = new Date().getTime();
	}

}

