//#TouhouDanmakufu
#ScriptVersion[3]
#Title["MageN6_H"]
#Text["Bullets"]


#include"./lib/SHOT_REPLACE/shot_replace.dnh"
#include"./Mageinclude.txt"

let objEnemy;
let duration = 0;
let stopstuff = false;
let angle = -45;
let stuff = false;
let angle2 = 0;
let animframe = 0;
let frame = 0;
let GetSpeedX = 0;

let variable = 0;
let n = 0;
@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
	SetScriptResult(3000);
	}
	case(EV_REQUEST_TIMER){
	SetScriptResult(50);
	}
}
let Patchy = GetCurrentScriptDirectory~"sprites/patchy.png";

@Initialize{
	//Resource Loading
	shotinit;
	InstallFont("./fontSEI.ttf");
	LoadSound(shot);
	LoadSound(shot3);
	LoadSound(skillcall);
	LoadSound(ping);
	LoadSound(laser);
	LoadSound(counterspell);
	LoadSound(firecast);
	LoadSound(fireball);
	LoadSound(charge2);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(390, 75, 75);
	SetCommonData("alive", true);
	
	ObjPrim_SetTexture(objEnemy, Patchy); 
	ObjSprite2D_SetSourceRect(objEnemy, 0, 0, 128, 128);
	ascent(i in 0..3){
	ObjAnim_AddFrameA2(objEnemy, ANIM_IDLE, 0, 128*i, 128, 128*i+128, 0, 0);
	}
	ascent(i in 0..2){
	ObjAnim_AddFrameA2(objEnemy, ANIM_IDLE, 128, 128*i, 256, 128*i+128, 0, 0);
	}
	ascent(x in 0..2){ObjAnim_AddFrameA2(objEnemy, ANIM_MOVE_LEFT, 256, 128*x, 256+128, 128*x+128, 0, 0);}
	descent(x in 0..2){
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_LEFT, 256, 128*x, 256+128, 128*x+128, 0, 0);
	}
	
	ascent(x in 2..4){ObjAnim_AddFrameA2(objEnemy, ANIM_MOVE_RIGHT, 256, 128*x, 256+128, 128*x+128, 0, 0);}
	descent(x in 2..4){
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_RIGHT, 256, 128*x, 256+128, 128*x+128, 0, 0);
	}
	
	
	ascent(x in 0..3){ObjAnim_AddFrameA2(objEnemy, ANIM_ATTACK, 128*5, 128*x, 128*6, 128*x+128, 0, 0);}
	descent(x in 0..3){ObjAnim_AddFrameA2(objEnemy, ANIM_ATTACKEND, 128*5, 128*x, 128*6, 128*x+128, 0, 0);}
	
	ObjAnim_SetSpeedA1(objEnemy, ANIM_IDLE, 14);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_LEFT, 6);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_RIGHT, 6);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_ATTACK, 6);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_RIGHT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_LEFT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_ATTACK, 2);
	ObjAnim_StartAnimationA2(objEnemy, 0, 0);
	ObjSprite2D_SetDestCenter(objEnemy); 
	//BottomEnemyMarker(objEnemy, objEnemy);
	ObjMove_SetDestAtWeight(objEnemy, GetCenterX, 120, 12, 4);  
	
	attack1;
	SetShotAutoDeleteClip(120, 150, 120, 20);
	
	
	
}







@MainLoop{
	
	
	if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <=0 && stopstuff==false){stopstuff=true;}

	GetEnemyX = ObjMove_GetX(objEnemy);
	GetEnemyY = ObjMove_GetY(objEnemy);
	ObjEnemy_SetIntersectionCircleToShot(objEnemy, GetEnemyX, GetEnemyY, 23);//当たり判定(自機弾用)登録
	ObjEnemy_SetIntersectionCircleToPlayer(objEnemy, GetEnemyX, GetEnemyY, 17);//当たり判定(体当たり用)登録

	frame++;
	yield;
	GetSpeedX = ObjMove_GetSpeed(objEnemy)*cos(ObjMove_GetAngle(objEnemy));
	SetCommonData("kogasax", ObjMove_GetX(objEnemy));
	SetCommonData("kogasay", ObjMove_GetY(objEnemy));




if(stopstuff==true && popped==false){
DestroyBoss;
}	
	
}

task DestroyBoss{
popped=true;
SetCommonData("alive", false);
loop(10){ yield; }
Obj_Delete(objEnemy);
loop(20){ yield; }
		CloseScript(GetOwnScriptID());
		return;
}


task CreateLaser1(x, y, angle, graphic, length, width, delay, bul){
let obj2 = ObjShot_Create(OBJ_STRAIGHT_LASER);
let bulletgraphic = 0;
let w = width;
let angle2 = angle*(pi/rand(1, 10));
let waitincrement = 0;
ObjMove_SetPosition(obj2, x, y);
ObjStLaser_SetAngle(obj2, angle);
ObjShot_SetGraphic(obj2, graphic);
ObjShot_SetDelay(obj2, delay);
ObjLaser_SetLength(obj2, length);
ObjLaser_SetRenderWidth(obj2, width*2);
ObjLaser_SetIntersectionWidth(obj2, width);
ObjShot_SetSpellResist(obj2, true);
ObjStLaser_SetSource(obj2, false);
ObjLaser_SetInvalidLength(obj2, 10, 10);
ObjShot_Regist(obj2);
ObjMove_SetPosition(obj2, x, y);


let point = GetLineBorderPoint(GetEnemyX, GetEnemyY, GetEnemyX+1*cos(angle), GetEnemyY+1*sin(angle));
		
		
		

Wait2(delay);
if(bul==true){
loop(5){
	CreateDNHShotA1(point[0], point[1], 1.25, angle2, SP01, 20);
	angle2+=360/5;
	}
}
ObjLaser_SetRenderWidth(obj2, width);
ObjLaser_SetIntersectionWidth(obj2, width-3);
Wait2(delay);
loop(15){
w-=width/15;
ObjLaser_SetRenderWidth(obj2, w);
ObjLaser_SetIntersectionWidth(obj2, w);
if((w<width)){ ObjShot_SetIntersectionEnable(obj2, false); }
Wait2(1);
}
Obj_Delete(obj2);
function Wait2(f){ loop(f){ if(popped==true){ Obj_Delete(obj2); } yield;  } }

}


task attack1{	
wait(120);
let one = 1;
let angle2 = 0;
let color = 0;
let angle3 = 45;
let angle4 = 0;
let bullets = false;
angle = 45;
let r = 0;
n = 20;
loop{
	
	if(popped==true){ break; }
	ascent(n in 0..100){
			r = 70;
			//PlaySE(shot);
			loop(4){
				CreateDNHShotA1(GetEnemyX+r*cos(angle+angle2),GetEnemyY+r*sin(angle+angle2),2.35+n/125,(angle3*1.25)+angle2,[PURPLE02, BLUE01][color%2],5);
				angle2+=360/4;
			}
			r = 50;
			loop(4){
				CreateDNHShotA1(GetEnemyX+r*cos(angle+angle2),GetEnemyY+r*sin(angle+angle2),1.8+n/125,-angle3+angle2+180,[BLUE01, PURPLE02][color%2],5);
				angle2+=360/4;
			}
			angle += 25*one;
			angle3+=2.75*one;
			wait(3);
		}
one*=-1;
color++;
angle+=35;
wait(60);


	if(popped==true){break;}
	
	
}
}
