var playerShip;

function playerNewGame() {
	playerShip = new Fly();
	playerShip.hp = 100;
	playerShip.shotDamage = 3;
	playerShip.obj.x = 10;
	playerShip.obj.y = 210;
	playerShip.speed = 4;
	playerShip.score = 0;
	playerShip.weapon = 0;
	playerShip.isFriendly = true;
	playerShip.obj.img.src = "data/graphics/starship.png";
	playerShip.init();

	playerShip.playerShot = function() {
		switch (playerShip.weapon) {
			case 0:
				fireAShot(this.shotx, this.middley, this.speed+2, 0, true);
				break;
			case 1:
				fireAShot(this.shotx, this.middley-4, this.speed+2, 0, true);
				fireAShot(this.shotx, this.middley+4, this.speed+2, 0, true);
				break;
			case 2:
				fireAShot(this.shotx,this.middley, 3, -1, true);
				fireAShot(this.shotx,this.middley, 3, 0, true);
				fireAShot(this.shotx,this.middley, 3, 1, true);
				break;
			case 3:
				fireAShot(this.shotx, this.obj.y, 0, -2, true);
				fireAShot(this.obj.x2,this.middley, 3, -1, true);
				fireAShot(this.obj.x2,this.middley, 3, 0, true);
				fireAShot(this.obj.x2,this.middley, 3, 1, true);
				fireAShot(this.obj.x2,this.middley, 0, 2, true);
				break;
			case 4:
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*0, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*1, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*2, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*3, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*4, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*5, 3, 0, true);
				break;
			case 5:
				fireAShot(this.obj.x2,this.middley, 3, -1, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*0, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*1, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*2, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*3, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*4, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*5, 3, 0, true);
				fireAShot(this.obj.x2,this.middley, 3, 1, true);
				break;
			case 6:
				fireAShot(this.obj.x2,this.middley, 3, -1, true);
				fireAShot(this.obj.x2,this.middley, 3, -2, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*0, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*1, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*2, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*3, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*4, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*5, 3, 0, true);
				fireAShot(this.obj.x2,this.middley, 3, 1, true);
				fireAShot(this.obj.x2,this.middley, 3, 2, true);
				break;
			case 7:
				fireAShot(this.obj.x2,this.middley, 3, -3, true);
				fireAShot(this.obj.x2,this.middley, 3, -1, true);
				fireAShot(this.obj.x2,this.middley, 3, -2, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*0, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*1, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*2, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*3, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*4, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*5, 3, 0, true);
				fireAShot(this.obj.x2,this.middley, 3, 1, true);
				fireAShot(this.obj.x2,this.middley, 3, 2, true);
				fireAShot(this.obj.x2,this.middley, 3, 3, true);
				break;
			default:
				fireAShot(this.obj.x2,this.middley, 3, -2, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*-3, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*-2, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*-1, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*0, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*1, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*2, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*3, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*4, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*5, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*6, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*7, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*8, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*9, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*10, 3, 0, true);
				fireAShot(this.obj.x2,this.obj.y+shotImg.height*11, 3, 0, true);
				fireAShot(this.obj.x2,this.middley, 3, 2, true);
				break;

		}
	}
}

$(document).keydown(function(event) {
		playerHandling(event, true)
	});
$(document).keyup(function(event) {
		playerHandling(event, false)
	});

function playerHandling(event, isDown) {
	switch (event.keyCode) {
		case 37:
			playerShip.left = isDown;
			break;
		case 38:
			playerShip.up = isDown;
			break;
		case 39:
			playerShip.right = isDown;
			break;
		case 40:
			playerShip.down = isDown;
			break;
	}
}

function playerUpdate() {
	playerShip.obj.dirX = 0;
	playerShip.obj.dirY = 0;
	if (playerShip.left) {
		playerShip.obj.dirX = -playerShip.speed;
	}
	if (playerShip.up) {
		playerShip.obj.dirY = -playerShip.speed;
	}
	if (playerShip.right) {
		playerShip.obj.dirX = playerShip.speed;
	}
	if (playerShip.down) {
		playerShip.obj.dirY = playerShip.speed;
	}

	playerShip.update();
	ctx.fillText("Score " + playerShip.score, 0, 20);
	ctx.fillText("Health " + playerShip.hp, canvas.width-90, 20, 90);

	if (playerShip.isDead()) {
		gameover = true;
	}
}


