script_enemy_main secant {
    let ImgBoss = GetCurrentScriptDirectory~"secant.png";
	let shot = GetCurrentScriptDirectory~"shot.wav";
	let timer = 0;
	let time1 = GetCurrentScriptDirectory~"time1.wav";
	let time2 = GetCurrentScriptDirectory~"time2.wav";
	let shot2 = GetCurrentScriptDirectory~"shot2.wav";
	let base = GetCurrentScriptDirectory~"ufo.wav";
	let angleAcc = 1;
	let bounce = GetCurrentScriptDirectory~"bounce.wav";
	let bn = 0;
	let a = 0;
	let explode = GetCurrentScriptDirectory~"explode.wav";
	let b = 0;
	let tilt = 0;
	let settext = 0;
	let count = 90;
	let speedo = 0;
	let radius = 0;
	let fram = 0;
	let angle = GetAngleToPlayer;
	let alive = 1;
	let  obj_id2 = Obj_Create(OBJ_SHOT);
	 let obj_id3 = Obj_Create(OBJ_SHOT);
	let obj_id4 = Obj_Create(OBJ_SHOT);
	let obj_id5 = Obj_Create(OBJ_SHOT);
	let laser = GetCurrentScriptDirectory~"laser.wav";
	let waffle = 1;
	let moo = 867;
	let sx = 0;
	let sy = 0;
	let xy = 0;
	let bbb = -100;
	let frame3=-60;
	let nyan = 1;
	let v = 1;
    let frame = -90;
	let count2 = 0;
	let frame2 = 1;
	let obj_id = Obj_Create(OBJ_SHOT);
	let obj_id6 = Obj_Create(OBJ_SHOT);
	let hihi=0;
	let n = 0;
#include_function ".\lib\SHOT_REPLACE\shot_replace.dnh"
    @Initialize {
	
        SetLife(85000);
	LoadSE(base);
	SetCommonData("alive", true);
	LoadSE(laser);
        SetTimer(99999999);
        LoadSE(shot);
	SetCommonData("secy", GetY);
	SetCommonData("secx", GetX);
	LoadSE(bounce);
	LoadSE(shot2);
	SetShotAutoDeleteClip(100, 200, 100, 100);
	shotinit;
	LoadSE(time1);
	LoadSE(time2);
	SetEnemyMarker(true);
	SetInvincibility(120);
	SetDamageRate(0, 0);        
	SetMovePosition02(GetCenterX-100, GetClipMinY + 120, 120);
        LoadGraphic(ImgBoss);
        SetTexture(ImgBoss);
        SetGraphicRect(1, 1, 128, 128);
    }

    @MainLoop {
	fram++;
	b+=0.01;
	

	if(GetTimer!=timer && (GetTimer < 10 && GetTimer>3)){
	PlaySE(time1);
	}

	if(GetTimer!=timer && GetTimer < 4){
	PlaySE(time2);
	}

	timer = GetTimer;

	sx = GetCommonData("secx");
	sy = GetCommonData("secy");

	SetCommonData("secy", GetY);
	SetCommonData("secx", GetX);
	
	if(GetCommonDataDefault("toggling", false)==true){
	SetCommonData("toggling", false);
	repair;
	}
	
	if(GetCommonDataDefault("attack", 0)==1){
	
	if(frame == 60){
	ExMove;
	let angle = GetAngleToPlayer;
	PlaySE(shot);
	
	let a = 0;
	let num = 40;
	loop(2){
	PlaySE(shot2);
	loop(num){
	let xx = GetX+rand(-60, 60);
	let yy = GetY+rand(-60, 60);
	CreateShotReflect(xx+radius*cos(angle), yy+radius*sin(angle), 2.2, angle, ORANGE12, 20);
	angle+=360/num;	
	}
	
	}
	frame=-120;
	}
	}

	if(GetCommonDataDefault("attack", 0)==2 ){

	if(frame==0){
	
	CreateLaserA(1, GetX, GetClipMaxY+50, 100, 200, ORANGE60, 80);
	SetLaserDataA(1, 0, 270, 0, 20, 0, 0);
	SetShotKillTime(1, 80);
	PlaySE(laser);
	FireShot(1);
	CreateLaserA(2, GetClipMinX-50, GetY, 100, 200, ORANGE60, 80);
	SetLaserDataA(2, 0, 0, 0, 20, 0, 0);
	SetShotKillTime(2, 80);
	FireShot(2);


	
	}
	if(frame==-30){
	SetMovePosition02(GetPlayerX, GetClipMinY+120, 30);
	}
	

	if(frame==60){
	CreateLaserA(2, GetX, GetClipMaxY+50, 200, 70, ORANGE60, 0);
	SetLaserDataA(2, 0, 270, 0, 20, 0, 180);	
	SetLaserDataA(2, 10, 270, 0, 90, 2.5, 180);
	SetShotKillTime(2, 50);
	PlaySE(GetCurrentScriptDirectory~"revive.wav");
	FireShot(2);
	CreateLaserA(3, GetX, GetClipMaxY+50, 200, 70, ORANGE60, 0);
	SetLaserDataA(3, 0, 270, 0, 20, 0, 180);	
	SetLaserDataA(3, 10, 270, 0, 90, -2.5, 180);
	SetShotKillTime(3, 50);
	
	FireShot(3.1);
	CreateLaserA(3.1, GetClipMinX-50, GetY, 200, 70, ORANGE60, 0);
	SetLaserDataA(3.1, 0, 0, 0, 20, 0, 90);	
	SetLaserDataA(3.1, 10, 0, 0, 90, 2.5, 90);
	SetShotKillTime(3.1, 50);
	
	FireShot(3.2);
	CreateLaserA(3.1, GetClipMinX-50, GetY, 200, 70, ORANGE60, 0);
	SetLaserDataA(3.1, 0, 0, 0, 20, 0, 90);	
	SetLaserDataA(3.1, 10, 0, 0, 90, -2.5, 90);
	SetShotKillTime(3.1, 50);
	
	FireShot(3.1);
	FireShot(3.2);
	
	frame=-120;
	
	}
}
		
	speedo+=0.1;
	if(GetCommonDataDefault("attack", 0)==3 ){
	
	if(frame==60){
	PlaySE(shot);
	ShootShape(GetX, GetY, 4, GetAngleToPlayer-40, 4, 34, SP09, 10);
	ShootShape(GetX, GetY, 3, GetAngleToPlayer-40, 3, 34, SP09, 20);
	frame=-60;
	ExMove;
	}
	}
	

	if(GetCommonDataDefault("attack", 0)==0){
	if(frame == 20 || frame == 60){
	PlaySE(bounce);
	let a = 0;
	let num = 4;
	loop(num){
	FantasyShinki(1, 0.000000000000000001, GetAngleToPlayer+a, WHITE03);
	a+=360/num;
	}
	frame=-80;
         }
     

}	if(GetCommonDataDefault("attack", 0)==4 ){
	
	if(frame==60){
	let angle = GetAngleToPlayer;
	let num = 1;
	loop(num){
	DLaser(angle);
	DLaser(angle+15);
	DLaser(angle-15);
	angle+=360/num;
	}
	
	PlaySE(laser);
	frame=-180;
	}
	}
	if(GetCommonDataDefault("attack", 0)==5 ){
	
	SetMovePosition02(GetCenterX, GetClipMinY+120, 30);
	if(frame==60){
	hib;
	hib2;
	frame=45;
	
	}


	if(fram == 120){
	ExMove;
	fram=-120;
	}
	}

count2++;
	
	yield;
	frame3++;
	a++;
	speedo+=0.05;
	if(speedo>5){
	speedo=0;
	}
        frame++;
	
	
    }

task repair{
	hihi=1;
	wait(150);
	frame=-80;
	SetMovePosition02(GetCenterX+rand(-120, 120), GetClipMinY+rand(60, 80), 30);
	wait(30);
	hihi=0;
}

task hib{	PlaySE(shot);
		loop(6){
			PlaySE(shot);
			wait(7);
			let b = 0;
			let num = 6;
			loop(num){
				CreateShot01(GetX, GetY, 3, b+90+a*5, BLUE01, 10);
				b+=360/num;
				}
			
		
	}
}

task hib2{
	
		loop(3){
			
			wait(5);
			let b = 0;
			let num = 6;
			loop(num){
				CreateShot01(GetX, GetY, 4, b+90-a*3, AQUA01, 10);
				CreateShot01(GetX, GetY, 4, b+90-a*3-2, AQUA01, 10);
				CreateShot01(GetX, GetY, 4, b+90-a*3-4, AQUA01, 10);
				CreateShot01(GetX, GetY, 4, b+90-a*3-6, AQUA01, 10);
				b+=360/num;
				}
			}
	}

function DLaser(angle) { DecorationLaser(angle, 1); DecorationLaser(angle, -1); }
	task DecorationLaser(angle,side) {
		let obj_id = Obj_Create(OBJ_SINUATE_LASER);
		Obj_SetPosition(obj_id, GetX, GetY);
		ObjSinuateLaser_SetLength(obj_id, 75);
		ObjSinuateLaser_SetWidth(obj_id, 10);
		Obj_SetSpeed(obj_id, 7);
		Obj_SetAngle(obj_id, angle);
		ObjShot_SetGraphic(obj_id,YELLOW52);
		let count = -20; let a = 0;
		while(!Obj_BeDeleted(obj_id)) {
			a+=17;
			Obj_SetAngle(obj_id, angle+50*sin(a)*side);
			wait(1);
		}
	}

function ObjShot_Create(x, y, speed, angle, graphic, delay){
  let obj = Obj_Create(OBJ_SHOT);
  Obj_SetPosition(obj, x, y);
  Obj_SetSpeed(obj, speed+speedo);
  Obj_SetAngle(obj, angle);
  ObjShot_SetGraphic(obj, graphic);
  ObjShot_SetDelay(obj, delay);
  return obj;
}




task CreateShotReflect(x, y, v, ang, graphic, delay) {
		let obj_id = Obj_Create(OBJ_SHOT); let border = 0; let  counter = 0;
		Obj_SetPosition(obj_id, x, y);
		ObjShot_SetGraphic(obj_id, graphic);
		Obj_SetSpeed(obj_id, v); Obj_SetAngle(obj_id, ang); ObjShot_SetDelay(obj_id, delay);
		while(!Obj_BeDeleted(obj_id)) {
			if(counter <= 1) {
				
				if(Obj_GetX(obj_id) > GetClipMaxX+border) {  Obj_SetX(obj_id, 2*(GetClipMaxX+border)-Obj_GetX(obj_id)); Obj_SetAngle(obj_id, 180-Obj_GetAngle(obj_id)); counter+=2; }
				else if(Obj_GetX(obj_id) < GetClipMinX-border) { Obj_SetX(obj_id, 2*(GetClipMinX-border)-Obj_GetX(obj_id)); Obj_SetAngle(obj_id, 180-Obj_GetAngle(obj_id)); counter+=2; }
				else if(Obj_GetY(obj_id) < GetClipMinY-border) { Obj_SetY(obj_id, 2*(GetClipMinY-border)-Obj_GetY(obj_id)); Obj_SetAngle(obj_id, -Obj_GetAngle(obj_id)); counter+=1; }
				//else if(Obj_GetY(obj_id) > GetClipMaxY+border) { Obj_SetY(obj_id, 2*(GetClipMaxY+border)-Obj_GetY(obj_id)); Obj_SetAngle(obj_id, -Obj_GetAngle(obj_id)); counter+=1; }
			}
			yield;
		}
	}

sub ExMove { ExMove_(GetCenterY-75, 30, rand(15, 30)); }
function ExMove_(height, ud, length) {
	
	if(GetPlayerX < GetX) {
		if(GetX - length <= GetClipMinX) {
			SetMovePosition03(GetX + length, rand(height-70 - ud, height + ud), 5, 11);
		} else {
			SetMovePosition03(GetX - length, rand(height-70 - ud, height + ud), 5, 11);
		}
	} else {
		if(GetX + length >= GetClipMaxX) {
			SetMovePosition03(GetX - length, rand(height-70 - ud, height + ud), 5, 11);
		} else {
			SetMovePosition03(GetX + length, rand(height-70 - ud, height + ud), 5, 11);
		}
	}
}
	// Movement functions for the boss to move towards the player.

function wait(w) { loop(w) { yield; } }	

task LifeGet{ AddLife(1); }

task dead{	

loop(1){
		PlaySE(explode);
		Explosion01(GetX, GetY, 50, 1, 60);
		SetDamageRate(0, 0);
		tilt=1;
		xy = 1;
		wait(360);
		alive=1;
		SetDamageRate(25, 40);
		AddLife(GetLife);
		
		Explosion01(GetX, GetY, 50, 1, 60);
		xy=0;
		tilt=0;
		frame3=-10;
		SetCommonData("alive", true);
		yield;
		
}
}


	function ShootShape(spawnX, spawnY, v, angle, num, bside, graphic, delay) {
	ascent(i in 0..num) {
		let sx = spawnX+cos(angle+i*360/num); let sy = spawnY+sin(angle+i*360/num); let sxN = spawnX+cos(angle+(i+1)*360/num); let syN = spawnY+sin(angle+(i+1)*360/num);
		CreateShot01(spawnX, spawnY, v*(((sx-spawnX)^2+(sy-spawnY)^2)^0.5), atan2(sy-spawnY,sx-spawnX), graphic, delay);
		ascent(j in 0..bside) {
			let toAngle = atan2(syN-sy,sxN-sx); let toDist = (((sxN-sx)^2+(syN-sy)^2)^0.5); let sx2 = sx+toDist/bside*j*cos(toAngle); let sy2 = sy+toDist/bside*j*sin(toAngle);
			CreateShot01(spawnX, spawnY, v*(((sx2-spawnX)^2+(sy2-spawnY)^2)^0.5), atan2(sy2-spawnY,sx2-spawnX), graphic, delay);
		}
	}
}

task FantasyShinki(v, t, ang, graphic) {
                let obj = Obj_Create(OBJ_SHOT);
                Obj_SetPosition(obj, GetX, GetY);
                Obj_SetSpeed(obj, v);
                Obj_SetAngle(obj, ang);
                ObjShot_SetGraphic(obj, graphic);
                while(!Obj_BeDeleted(obj)) {
                        let toplayer = atan2(GetPlayerY-Obj_GetY(obj), GetPlayerX-Obj_GetX(obj));
                        let angle = Obj_GetAngle(obj); let difference = angle-toplayer;
                       if((|difference|)<20) {
                                angle = toplayer;
                        } else if((|difference|)>10 && (|difference|)<90) {
                                angle+=t*[-1, 1][difference < 0];
                        }
                      Obj_SetAngle(obj, angle);
                        Obj_SetSpeed(obj, Obj_GetSpeed(obj)+1/100);
                        yield;
                }
        }
 

    @DrawLoop {
		if(tilt == 0){
		SetGraphicAngle(0, 0, 0);
                DrawGraphic(GetX,GetY);
		}

		if(tilt == 1){
		SetTexture(ImgBoss);
		SetGraphicAngle(0, 0, 90);
		DrawGraphic(GetX, GetY);
}
    }
let v = 0;
    @Finalize {
	PlaySE(explode);
        DeleteGraphic(ImgBoss);
    }
}
