var brig=700;	// direita
var blef=0;		// esquerda
var btop=0;		// altura
var bbot=600;	// base
var tiro=false;
var tirot=false;	// tiro ativo/inativo

var xuh=yuh=true;
var xuo=yuo=true;

function updb() {
	if (bb.style.posLeft<=0) {xuh=true}
	if (bb.style.posLeft>=700) {xuh=false}
	if (xuh) {	
		bb.style.posLeft = bb.style.posLeft + 1;
	} else {
		bb.style.posLeft = bb.style.posLeft - 2;
	}
	if (bb.style.posTop<=btop) {yuh=true}
	if (bb.style.posTop>=bbot) {yuh=false}
	if (yuh) {
		bb.style.posTop = bb.style.posTop + 2;
	} else {
		bb.style.posTop = bb.style.posTop - 1;
	}
// ----- Tiro -----	
	if (tirot==false && Math.abs(bb.style.posTop-a.style.posTop)<=150 && Math.abs(bb.style.posLeft-a.style.posLeft)<=150) {
		tirot=true;
		tt.style.posLeft = bb.style.posLeft;
		tt.style.posTop = bb.style.posTop;
		hit = 0;
		// hipotenusa, arcotangente, seno, cosseno
		hbt = Math.sqrt(Math.pow(a.style.posTop-bb.style.posTop,2)+Math.pow(a.style.posLeft-bb.style.posLeft,2))
		att = Math.atan2(a.style.posTop-bb.style.posTop,a.style.posLeft-bb.style.posLeft);
		snt = Math.sin(att);
		cst = Math.cos(att);
		updtt();
	}
// ----- Fim Tiro -----	
	setTimeout('updb()', 20);
	return;
}

function upda() {
	if (a.style.posLeft<=blef) {xuo=true}
	if (a.style.posLeft>=brig) {xuo=false}
	if (xuo) {	
		a.style.posLeft = a.style.posLeft + 2;
	} else {
		a.style.posLeft = a.style.posLeft - 1;
	}
	if (a.style.posTop<=btop) {yuo=true}
	if (a.style.posTop>=bbot) {yuo=false}
	if (yuo) {
		a.style.posTop = a.style.posTop + 1;
	} else {
		a.style.posTop = a.style.posTop - 2;
	}
// ----- Tiro -----
	if (tiro==false && Math.abs(a.style.posTop-bb.style.posTop)<=150 && Math.abs(a.style.posLeft-bb.style.posLeft)<=150) {
		tiro=true;
		t.style.posLeft = a.style.posLeft;
		t.style.posTop = a.style.posTop;
		hi = 0;
		// hipotenusa, arcotangente, seno, cosseno
		hb = Math.sqrt(Math.pow(bb.style.posTop-a.style.posTop,2)+Math.pow(bb.style.posLeft-a.style.posLeft,2))
		at = Math.atan2(bb.style.posTop-a.style.posTop,bb.style.posLeft-a.style.posLeft);
		sn = Math.sin(at);
		cs = Math.cos(at);
		updt();
		
	}

	
// ----- Fim Tiro -----	
	setTimeout('upda()', 20);
	return;
}



function updt() {
	if (tiro) {
	    hi = hi + 3;
		t.style.posLeft = hi * cs + a.style.posLeft+20;
		t.style.posTop = hi * sn + a.style.posTop+20;
		if ((t.style.posLeft>brig)||(t.style.posLeft<blef)||(t.style.posTop>bbot)||(t.style.posTop<btop)||(hi>150)) {
			tiro=false;
			t.style.posLeft = -10;
			t.style.posTop = -10;
			return;
		}
	}
	setTimeout('updt()', 1);
	return;
}


function updtt() {
	if (tirot) {
	    hit = hit + 3;
		tt.style.posLeft = hit * cst + bb.style.posLeft+20;
		tt.style.posTop = hit * snt + bb.style.posTop+20;
		if ((tt.style.posLeft>brig)||(tt.style.posLeft<blef)||(tt.style.posTop>bbot)||(tt.style.posTop<btop)||(hit>150)) {
			tirot=false;
			tt.style.posLeft = -10;
			tt.style.posTop = -10;
			return;
		}
	}
	setTimeout('updtt()', 1);
	return;
}


upda();
updb();
