//playable character
blink_count = 0;
function init_person(x, y){
var person = character("person", x, y, 10, 20, true);
person.options = {x: x, y: y} //starting coordinates
person.color = 'rgba(255,0,255,1)'
person.speed = 3;
person.cProcesses={}
person.draw = function(){
	this.update();
    // this.collisions = checkAllCollisions(this);
	this.dirx = 0;
	this.dirx = -1;
	
	if(just_hit){
		blink_count++
		if(blink_count < 3){
			// console.log("color", person.color)
			this.color = 'rgba(255,255,255,1)';
		}else
            // this.color = 'rgba(255,0,255,1)'
            this.color = random_color()

		if(blink_count == 6){ blink_count = 0 }
	}
	else{
		blink_count = 0;
        // this.color = random_color()
        this.color = 'rgba(255,0,255,1)'
		
	}

	//jumping
	this.hanging = false;
	var collisionTop = checkCollisions(this, "top", this.dirx, 1)
	var collisionBottom = checkCollisions(this, "bottom", this.dirx, -1)
	if(collisionTop) {
		this.jumping = false;
		if(upArrow){
			this.diry = -1;
			this.acceleration = 10;
			this.hanging = true;
		}
	}
	if(spaceBar && !this.jumping && !this.falling && !collisionTop && collisionBottom ||
	    spaceBar && !this.jumping && !this.falling && collisionTop && !collisionBottom){
	    /*||
    		spaceBar && !this.jumping && !this.falling && this.collisions.top && this.collisions.right ||
    		spaceBar && !this.jumping && !this.falling && this.collisions.top && this.collisions.left*/
		this.jumping = true
		// this.jumping = new control.jump(this);
	}	
	if (!this.jumping && !collisionBottom && !this.hanging){
		// console.log("falling", this.acceleration)
		if(this.acceleration < this.gravity) this.acceleration ++;
		for(var a = 0; a < this.acceleration; a++){
			collisionBottom = checkCollisions(this, "bottom", this.dirx, -1)
			if(!collisionBottom) {
				this.y+= 1;
				// console.log('falling')
			} else {
				this.diry = 0;
				this.acceleration = this.gravity;
			}
		}
		this.diry = 1;
		// this.y+=this.acceleration;
	} else if (this.jumping && !collisionTop && !this.hanging){
		for(var a = 0; a < this.acceleration; a++){
			collisionTop = checkCollisions(this, "top", this.dirx, 1)
			if(!collisionTop) {
				this.y-= 1;
				// this.set()
			} else {/*console.log("collision top")*/}
		}
		this.diry = -1;
		// this.y-=this.acceleration;
		this.acceleration--;
		if(this.acceleration<=0){
			// this.acceleration = 10;
			this.jumping = false;
		}
	} else if (collisionBottom){
		// console.log("this")
		this.diry = 0;
		this.acceleration = this.gravity;
	}	
	//walking
	// console.log("walking right", rightArrow, !this.collisions.right)
	var collisionRight = checkCollisions(this, "right", 1, this.diry)
	var collisionLeft = checkCollisions(this, "left", -1, this.diry)
	if(rightArrow && !collisionRight && this.x+this.w <= 850-this.speed){
		// console.log("walking")
		// if(setx % 2 ==0) setStyle(this.id, {"background":"url('walk1.gif')"})
		// 				else setStyle(this.id, {"background":"url('walk2.gif')"})
		this.dirx = 1;
		this.x += this.speed;
		// control.walk.right(this);
	} else if(leftArrow && !collisionLeft && this.x >= 0+this.speed){
		this.dirx = -1;
		this.x -= this.speed;
		// control.walk.left(this);
	}
	if(this.y + this.h > 850){
		update_damage(-1);
		this.x = this.options.x;
		this.y = this.options.y;
	}
	//update
	// console.log("sending", setx, sety)
	this.set()
	
}
}
// ========
// = maps =
// ========
person.downmap = {
	"32": function(){spaceBar = true;},
	"39": function(){rightArrow = true;},
	"37": function(){leftArrow = true;},
	"38": function(){upArrow = true;}
};	
person.upmap = {
	"32": function(){spaceBar = false;},
	"39": function(){rightArrow = false;},
	"37": function(){leftArrow = false;},
	"38": function(){upArrow = false;}
};

function route(e){
    // e.stop()
	var fxn = (e.type == "keydown") ? person.downmap[e.keyCode+""] : person.upmap[e.keyCode+""]
	if(fxn) {fxn();}
}
function update_damage(change){
	if(!just_hit){
		if(change > 0 && num_hits+change<max_hits){
			num_hits+=change
			$("#hit" + num_hits).show()
		} else if(change < 0 && num_hits + change >= 0){
			$("#hit"+num_hits).hide()
			num_hits+=change
			just_hit = true;
			setTimeout(function(){just_hit = false}, 3000)
		} else if(num_hits + change < 0){
			console.log("DEAD!  :(")
		}
	}
}
var random_color = function(){
    return 'rgba('+ Math.round(Math.random()*255) +', '+ Math.round(Math.random()*255) +', '+ Math.round(Math.random()*255) +', 1)';
}
