ctx = document.getElementById("gfx").getContext("2d");
ctx.fillStyle="#004400";
ctx.strokeStyle="#00ff00";
ctx.font = "30px Arial";
ctx.textAlign = "center";



function draw_base(x,y){
	ctx.beginPath();
	ctx.moveTo(x-15,y);
	ctx.lineTo(x-15,y-30);
	ctx.lineTo(x-5,y-10);
	ctx.lineTo(x+5,y-10);
	ctx.lineTo(x+15,y-30);
	ctx.lineTo(x+15,y);
	ctx.lineTo(x-15,y);
	ctx.stroke();
}


function update_base(){
	draw_base(base_x,590);
	if(keys[68])base_x+=2;
	if(keys[65])base_x-=2;
	if(base_x<25)base_x=25;
	if(base_x>275)base_x=275;
	if(keys[32] && (bullet_timer==0) ){
		bullet_timer=35;
		bullets.push([base_x,575]);
	}
	if(bullet_timer>0)bullet_timer--;
}



function draw_bullet(x,y){
	ctx.beginPath();
	ctx.moveTo(x-5,y);
	ctx.lineTo(x,y-10);
	ctx.lineTo(x+5,y);
	ctx.lineTo(x-5,y);
	ctx.stroke();
}

function update_bullets(){
	for(var i in bullets){
		draw_bullet( bullets[i][0] , bullets[i][1] );
		bullets[i][1]-=5;
	}
	for(var i in bullets)
		if(bullets[i][1]<-5)
			bullets.splice(i,1); 
}

function draw_enemy(x,y){
	ctx.beginPath();
	ctx.moveTo(x-5,y+10);
	ctx.lineTo(x-15,y);
	ctx.lineTo(x-15,y+10);
	ctx.lineTo(x-5,y+20);
	ctx.lineTo(x,y+30);
	ctx.lineTo(x+5,y+20);
	ctx.lineTo(x+15,y+10);
	ctx.lineTo(x+15,y);
	ctx.lineTo(x+5,y+10);
	ctx.lineTo(x-5,y+10);
	ctx.stroke();
}

function bullet_enemy_collision(bx,by,ex,ey){
	var rex=ex;
	var rey=ey+15;
	var rbx=bx;
	var rby=by+2.5;
	var vx=rex-rbx;
	var vy=rey-rby;
	return vx*vx+vy*vy<18*18;
}



function update_enemys(){
	if(enemy_timer==0){
		var rax=25+Math.random()*250;
		var chance=0.5;
		if (level==3)chance=0.9;
		if(level>1 && Math.random()<chance && rax<200)enemys.push([rax,-40,1,0]);
		else enemys.push([rax,-40,0]);
		if (level==3)enemy_timer=30; 
		else enemy_timer=50;
	}else enemy_timer--;
	for(var i in enemys){
		draw_enemy( enemys[i][0] , enemys[i][1] );
		enemys[i][1]+=2;
		if(enemys[i][2]==1){
			enemys[i][0]+=enemys[i][3]*0.1;
			if(enemys[i][3]==35)enemys[i][3]=-35
			else enemys[i][3]++;
		}
	}
	for(var i in enemys){
		if(base_enemy_collision(base_x,590,enemys[i][0],enemys[i][1])&&base_active){
			base_active=false;
			tx=-100;
			msgt="GAME OVER";
			explosions.push([base_x,580,30]);
			setTimeout(init_menu,3000);
			break;
		}
		if(enemys[i][1]>600)
			enemys.splice(i,1);
		else{
			for(var j in bullets)
				if (bullet_enemy_collision( bullets[j][0] , bullets[j][1] , enemys[i][0] , enemys[i][1] )){
					explosions.push([enemys[i][0],enemys[i][1],20]);
					delete bullets[j];
					delete enemys[i];
					killed_enemys++;
					if(killed_enemys==40){
						killed_enemys=0;
						if(level==3){
							tx=-100;
							msgt="YOU WON!";
							setTimeout(init_menu,3000);
						}
						if(level==2){
							level=3;
							tx=-100;
							msgt="LEVEL 3";
						}
						if(level==1){
							level=2;
							tx=-100;
							msgt="LEVEL 2";
						}
					}
					break;
				}
				}
	}
}

function draw_explosion(x,y){
	ctx.beginPath();
	ctx.moveTo(x,y-10);
	ctx.lineTo(x-10,y-20);
	ctx.lineTo(x-10,y-10);
	ctx.lineTo(x-20,y);
	ctx.lineTo(x-10,y+10);
	ctx.lineTo(x-10,y+20);
	ctx.lineTo(x,y+10);
	ctx.lineTo(x+10,y+20);
	ctx.lineTo(x+10,y+10);
	ctx.lineTo(x+20,y);
	ctx.lineTo(x+10,y-10);
	ctx.lineTo(x+10,y-20);
	ctx.lineTo(x,y-10);
	ctx.stroke();
}



function update_explosions(){
	for(var i in explosions){
		draw_explosion( explosions[i][0] , explosions[i][1] );
		explosions[i][2]--;
	}
	for(var i in explosions)
		if ( explosions[i][2] < 1 )
			delete explosions[i];
}


function init(){
	tx=-100;
	msgt="LEVEL 1";
	explosions=[];
	enemys=[];
	enemy_timer=0;
	bullets=[];
	keys=[];
	base_x=0;
	bullet_timer=0;
	is_game=true;
	is_menu=false;
	base_active=true;
	killed_enemys=0
	level=1
}

function update_text_animation(){
	if(tx<700)tx+= Math.max(Math.abs((150-tx)/10),0.1) ;
	ctx.strokeText(msgt, tx, 300);
}



function init_menu(){
	is_game=false;
	tx=700;
	is_menu=true;
}

function update_menu(){
	ctx.strokeText("PRESS SPACE", 150, 300);
	ctx.strokeText("TO START", 150, 350);
	if(keys[32])init();
}

function base_enemy_collision(bx,by,ex,ey){
	var rex=ex;
	var rey=ey-10;
	var rbx=bx;
	var rby=by+2.5;
	var vx=rex-rbx;
	var vy=rey-rby;
	return vx*vx+vy*vy<30*30;
}

init();
init_menu();

function on_draw(){
	
	
	ctx.fillRect(0,0,300,600);
	update_text_animation();
	if(is_game){
		if(base_active)update_base();
		update_bullets();
		update_enemys();
		update_explosions();
	}
	if(is_menu)update_menu();
}

setInterval(on_draw,20);