// JavaScript Document



/*
 자바스크립트로 슈팅게임 만들어보자.
 여기부터 게임관련 코드 시작입니다.
*/

시작등록(Timer);
function Timer() {
	Timer_2.Interval = setInterval ("Timer_2()",delay);
}

function Timer_2() {
	var pause = document.getElementById("pause");
	if (잠깐멈춤 == true) {
		pause.setAttribute("style","display:block");
		컨트롤버튼제어(false);		
		return false;

		}
	else {
		pause.setAttribute("style","display:none");
		//컨트롤버튼제어(true);
	}
		
	for (j=0;j<unit.length;j++) {
		for (i=0; i<unit[j].length; i++) {
			애니메이션(j,i);
		}
	}
	var ii = 0;
	do{
		적군이동(ii);
		충돌검사B(5,ii);	
		충돌검사B(6,ii);
		ii++;
	}while (ii<unit[5].length);
	
	아군이동();	
	충돌검사A();
	HP_EP_감소();		
	상태창출력();		

}

function 애니메이션(j,i) {
			if (!unit[j][i].getAttribute("src")) return false;	
			if (unit[j][i].anime_type == null) return false;
			유닛좌표계산(j,i,true);
			if (unit[j][i].xx <-100 || unit[j][i].yy < -100) return false;
			if (unit[j][i].end_x >700 || unit[j][i].end_y >400) return false;  
			unit[j][i].anime_cnt++;
			unit[j][i].setAttribute("src",unit[j][i].pic[unit[j][i].anime_type][unit[j][i].anime_cnt]);
			if (unit[j][i].anime_cnt >= unit[j][i].pic[unit[j][i].anime_type].length-1) {
				if(unit[j][i].anime_type != 0|| unit[j][i].anime_type != 4)  unit[j][i].anime_type = 0;
				unit[j][i].anime_cnt  = 0;
			}
}

function 적군이동(i) {
		//유닛좌표계산(1,i,true)
		// 1번타입 적군 이동경로 설정
			if (unit[5][i].xx <= -200) {
				unit[5][i].xx = 900
				unit[5][i].style.left = Math.floor(unit[5][i].xx)+"px";
				//유닛좌표계산(5,i,true);
				if (gameover == false) unit[0][0].point = unit[0][0].point + 감점;
			}
			if (unit[5][i].end_y >400) unit[0][0].point = unit[0][0].point + 감점;
			if (unit[5][i].yy <= -200) unit[0][0].point = unit[0][0].point + 득점;
			if (unit[5][i].yy <= -200 || unit[5][i].end_y >400)	{
				unit[5][i].xx = 900
				unit[5][i].style.left = Math.floor(unit[5][i].xx)+"px";
				unit[5][i].yy = Math.random(i)*250;
				unit[5][i].style.top = Math.floor(unit[5][i].yy)-20+"px";
				unit[5][i].move_y = 0;
				unit[5][i].move_x = -Math.floor(Math.random(1)*20);
				//유닛좌표계산(5,i,true);
			}
			if (unit[5][i].move_x == undefined || unit[5][i].move_y == undefined) return false;
	
			if (unit[5][i].move_x != 0){
				unit[5][i].xx = unit[5][i].xx + unit[5][i].move_x;
				unit[5][i].style.left = Math.floor(unit[5][i].xx) + "px"; 
				디버그출력(5,i);
			}
			if (unit[5][i].move_y != 0) {
				unit[5][i].yy = unit[5][i].yy+ unit[5][i].move_y;
				unit[5][i].style.top = Math.floor(unit[5][i].yy) + "px";
				//unit[6][i].style.top = unit[5][i].style.top; 
				디버그출력(5,i);
				if (unit[5][i].math>100) unit[5][i].math = 0;
				unit[5][i].math++;	
			}
			unit[6][i].xx = (unit[5][i].xx+ unit[5][i].width)- 30;
			unit[6][i].style.left = Math.floor(unit[6][i].xx) + "px";
			unit[6][i].yy = parseInt(unit[5][i].style.top);
			unit[6][i].style.top = unit[6][i].yy + "px";
}

function 아군이동() {
	유닛좌표계산(0,0,true);
	유닛좌표계산(3,0,true);
	for (i=0; i<unit[4].length;i++) {
		유닛좌표계산(4,i,true) ;
	}
	if (gameover == true) {
		unit[0][0].final_y = 300;
	}
	
	if (unit[0][0].xx > unit[0][0].final_x) {
		var dist = (unit[0][0].xx-unit[0][0].final_x)/8;
		unit[0][0].xx = unit[0][0].xx - dist
		unit[0][0].style.left = unit[0][0].xx +"px";
		for (i=0; i<unit[4].length;i++) {
			unit[4][i].xx = unit[4][i].xx - (dist*unit[4][i].speed)
			unit[4][i].style.left = Math.floor(unit[4][i].xx ) +"px";
			디버그출력(4,i)
		}
		디버그출력(3,0)		
	} /// 왼쪽으로 갑니다.
	if (unit[0][0].xx < unit[0][0].final_x) {
		var dist = (unit[0][0].final_x-unit[0][0].xx)/8;
		unit[0][0].xx = unit[0][0].xx + dist
		unit[0][0].style.left = Math.floor(unit[0][0].xx )+"px";
		for (i=0; i<unit[4].length;i++) {
			unit[4][i].xx = unit[4][i].xx + (dist*unit[4][i].speed)
			unit[4][i].style.left = Math.floor(unit[4][i].xx) +"px";
			디버그출력(4,i) 
		}
		디버그출력(3,0)
		} // 오른쪽으로 가요.
	if (unit[0][0].yy > unit[0][0].final_y) {
		var dist = (unit[0][0].yy-unit[0][0].final_y)/8;	
		unit[0][0].yy = unit[0][0].yy  - dist 
		unit[0][0].style.top = Math.floor(unit[0][0].yy )+"px";
		for (i=0; i<unit[4].length;i++) {
			unit[4][i].yy = unit[4][i].yy - (dist*unit[4][i].speed)
			unit[4][i].style.top = Math.floor(unit[4][i].yy) +"px";
			디버그출력(4,i)
		}		
		디버그출력(3,0)
	}// 위로 가요.
	if (unit[0][0].yy < unit[0][0].final_y) {
		var dist = (unit[0][0].final_y-unit[0][0].yy)/8;
		unit[0][0].yy = unit[0][0].yy + dist
		unit[0][0].style.top = Math.floor(unit[0][0].yy )+"px";
		for (i=0; i<unit[4].length;i++) {
			unit[4][i].yy = unit[4][i].yy + (dist*unit[4][i].speed)
			unit[4][i].style.top = Math.floor(unit[4][i].yy) +"px";
			디버그출력(4,i)
		}			
		디버그출력(3,0)	
	} // 아래로 가요.
	
	unit[7][0].style.left = unit[0][0].xx -37 + "px";
	unit[7][0].style.top = unit[0][0].yy -16 + "px";
	
}
function 충돌검사A() {	
// 여기서부터 주인공의 이동에 관한 충돌검사입니다.
	var xxx_start = unit[0][0].speed/2;
	var yyy_start = unit[0][0].speed/2;
	var xxx_end = 500 - unit[0][0].height;
	var yyy_end = 300 - unit[0][0].width;
	
	if (unit[0][0].xx < xxx_start) {
		control_button[0].style.display="none";
		control_button[3].style.display="none";
		control_button[6].style.display="none";
	}
	if (unit[0][0].yy < yyy_start) {
		control_button[0].style.display="none";
		control_button[1].style.display="none";
		control_button[2].style.display="none";		
	}
	if (unit[0][0].yy >= yyy_end){
		control_button[6].style.display="none";
		control_button[7].style.display="none";
		control_button[8].style.display="none";
	}
	if (unit[0][0].xx >= xxx_end){
		control_button[2].style.display="none";
		control_button[5].style.display="none";
		control_button[8].style.display="none";			
	}
	if (unit[0][0].xx > xxx_start && unit[0][0].yy >  yyy_start) {
			control_button[0].style.display="block";
	}
	if (unit[0][0].yy > yyy_start) {
			control_button[1].style.display="block";
	}
	if (unit[0][0].xx < xxx_end && unit[0][0].yy > yyy_start) {
			control_button[2].style.display="block";
	}
	if (unit[0][0].xx > xxx_start) {
			control_button[3].style.display="block";		
	}
	if (unit[0][0].xx < xxx_end) {
			control_button[5].style.display="block";
	}
	if (unit[0][0].xx > xxx_start && unit[0][0].yy < yyy_end) {
			control_button[6].style.display="block";		
	}
	if (unit[0][0].yy < yyy_end) {
			control_button[7].style.display="block";
	}
	if (unit[0][0].xx < xxx_end && unit[0][0].yy < yyy_end) {
			control_button[8].style.display="block";
	}
	control_button[4].style.display="block";
// 여기까지 주인공의 이동에 대한 충돌검사.
		디버그출력(0,0);
}

function 충돌검사B(i,j) {
	if (gameover == true) return false;
	// 여기서부터 적기체와 충돌에 대한 검사입니다.
	유닛좌표계산(0,0,true);
	유닛좌표계산(i,j,true);
	var ax = unit[0][0].dead_x;
	var ay = unit[0][0].dead_y;
	var axx = unit[0][0].dead_end_x;
	var ayy = unit[0][0].dead_end_y;
	var bx = unit[i][j].xx;
	var by = unit[i][j].yy;
	var bxx = unit[i][j].end_x;
	var byy = unit[i][j].end_y;
	var text = j + "번 ax: "+ ax +  " ay: "+ ay + " bx: "+ bx + " by: "+ by+ " "; 
			if (axx > bx && axx < bxx && ayy > by  && axx < byy){
				충돌하다("A",i,j,-20,-5);
			}else if (axx > bx && axx < bxx && ay > by && ay < byy) {
				충돌하다("B",i,j,-20,-2);
			}else if (ax > bx && ax < bxx && ay > by && ay < by){
				충돌하다("C",i,j,20,-5);			
			}else if (ax > bx && ax < bxx && ayy > by && ayy < byy) {
				충돌하다("D",i,j,20,-2);							
			}
			else {
				충돌하다(false);
			}
	// 여기까지 적기체와 충돌에 대한 검사입니다.
}

function 충돌하다(type,i,j,my,mx) {
	if (type != false) {
		unit[0][0].anime_type = 3;
		unit[7][0].anime_type = 2;
		unit[7][0].anime_cnt  = 0
		unit[i][j].move_y = my;
		unit[i][j].move_x = mx;
		//unit[0][0].point = unit[0][0].point + 득점;
		if (i==6) {
			데미지(1,false);	
			unit[7][0].anime_type = 1;
			unit[0][0].anime_type = 2;
		}
	}
}
function 데미지(a,type) {
	if (unit[0][0].life > 0) unit[0][0].life = unit[0][0].life  - a;
	if (type == false) return false;
}

시작등록(컨트롤정의);
function 컨트롤정의() {
	var startbutton = document.getElementById("gamestart");
	var GameContent = document.getElementById("GameContent")
	startbutton.onmousedown = function () {
		잠깐멈춤 = false;
		this.style.display = "none";
		GameContent.style.opacity = 1;
	}
	control_button_li = document.getElementById("GameControl").getElementsByTagName("li")	 
	control_button = document.getElementById("GameControl").getElementsByTagName("a")	
	control_button[0].onmousedown= function () { // 왼쪽위
		unit[0][0].move_x = -unit[0][0].speed;
		unit[0][0].move_y = -unit[0][0].speed;	
		unit[0][0].anime_type =1;
		unit[0][0].anime_cnt  = 0
		유닛좌표계산(0,0,false)
	}
	control_button[1].onmousedown= function () { // 위
		unit[0][0].move_x = 0
		unit[0][0].move_y = - unit[0][0].speed;	
		unit[0][0].anime_type =1;
		unit[0][0].anime_cnt  = 0
		유닛좌표계산(0,0,false)
	}	
	control_button[2].onmousedown= function () { // 오른쪽위
		unit[0][0].move_x = unit[0][0].speed;
		unit[0][0].move_y = - unit[0][0].speed;	
		unit[0][0].anime_type =2;
		unit[0][0].anime_cnt  = 0
		유닛좌표계산(0,0,false)
	}	
	control_button[3].onmousedown= function () { // 왼쪽
		unit[0][0].move_x = - unit[0][0].speed;
		unit[0][0].move_y = 0;	
		unit[0][0].anime_type =1;
		unit[0][0].anime_cnt  = 0
		유닛좌표계산(0,0,false)	
	}	
	control_button[4].onmousedown= function () { // 발사버튼
		unit[0][0].anime_type =3;
		unit[0][0].anime_cnt  = 0
		unit[0][0].energy = unit[0][0].energy + 10;
	}	
	control_button[5].onmousedown= function () { // 오른쪽
		unit[0][0].move_x = unit[0][0].speed;
		unit[0][0].move_y = 0;	
		unit[0][0].anime_type =2;
		unit[0][0].anime_cnt  = 0
		유닛좌표계산(0,0,false)
	}	
	control_button[6].onmousedown= function () { // 왼쪽아래
		unit[0][0].move_x = - unit[0][0].speed;
		unit[0][0].move_y = unit[0][0].speed;	
		unit[0][0].anime_type =1;
		unit[0][0].anime_cnt  = 0
		유닛좌표계산(0,0,false)
	}	
	control_button[7].onmousedown= function () { // 아래
		unit[0][0].move_x = 0;
		unit[0][0].move_y = unit[0][0].speed;	
		unit[0][0].anime_type =2;
		unit[0][0].anime_cnt  = 0
		유닛좌표계산(0,0,false)
	}	
	control_button[8].onmousedown= function () { // 오른쪽아래
		unit[0][0].move_x = unit[0][0].speed;
		unit[0][0].move_y = unit[0][0].speed;	
		unit[0][0].anime_type =2;
		unit[0][0].anime_cnt  = 0
		유닛좌표계산(0,0,false)
	}	
	control_button[9].onmousedown= function () { // 오른쪽아래
		잠깐멈춤 = true;
		control_button_li[9].style.display="none";
		control_button_li[10].style.display="block";
	}
	control_button[10].onmousedown= function () { // 오른쪽아래
		잠깐멈춤 = false;
		control_button_li[9].style.display="block";
		control_button_li[10].style.display="none";
	}
}

function HP_EP_감소() {
		if (unit[0][0].energy > 0) unit[0][0].energy = unit[0][0].energy - unit[0][0].energy_minus;
		if (unit[0][0].energy <= 0 && unit[0][0].life > 0) unit[0][0].life = unit[0][0].life -1;
		if (unit[0][0].life <=0) 게임오버(); 
}

function 게임오버() {
		gameover = true;
		unit[0][0].anime_type = 4;
		unit[0][0].anime_cnt  = 0;
		컨트롤버튼제어(false);
		title[0].innerHTML = "GAME OVER";
}

function 컨트롤버튼제어(type) {
		for (i=0;i<9;i++) {
			if (type == false) control_button[i].style.display="none";
			else control_button[i].style.display="block";
		}
}


시작등록 (마우스좌표입력정의);
function 마우스좌표입력정의(){
	var div_el = document.getElementById("GameContent");
	div_el.setAttribute("onmousedown","마우스좌표입력(event,this)");
}

function 마우스좌표입력(evt,object){
	var div_el = document.getElementById("GameContent");
	if (evt.ctrlKey == false) unit[0][0].final_x = evt.clientX -30;
	unit[0][0].final_y = evt.clientY -130;
	
	if (unit[0][0].xx < unit[0][0].final_x) {
		unit[0][0].anime_type =2;
		unit[0][0].anime_cnt = 0;
	}
	else {
		unit[0][0].anime_type =1;
		unit[0][0].anime_cnt = 0;
	}
	//유닛좌표계산(0,0,false);
}
var game ;
var start_button;
var end_button;
시작등록 (게임시작종료버튼정의);
function 게임시작종료버튼정의() {
	game = document.getElementById("GameContainer");
	start_button = document.getElementById("gameStart_button");
	end_button = document.getElementById("gameExit_button");
	
	start_button.onclick = function () {
		game.setAttribute("style","display:block");
		잠깐멈춤 = true;

	}
	end_button.onclick = function () {
		game.setAttribute("style","display:none");
		잠깐멈춤 = true;

	}
	
	
}
