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


#include"./lib/SHOT_REPLACE/shot_replace.dnh"
#include"./Mageinclude.txt"
let one = 1;
let objEnemy;
let duration = 0;
let stopstuff = false;
let angle = 0;
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(39);
	}
}


@Initialize{
	//Resource Loading
	shotinit;
	InstallFont("./fontSEI.ttf");
	LoadSound(shot);
	LoadSound(shot3);
	LoadSound(counterspell);
	LoadSound(frostcast);
	LoadSound(coneofcold);
	LoadSound(charge2);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(210, 90, 75);
	SetCommonData("alive", true);
	let Patchy = GetCurrentScriptDirectory~"sprites/patchy.png";
	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, 8);
	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;
	attack2;
	SetShotAutoDeleteClip(100, 70, 100, 40);
	
	
}



task CheckForFlip{
yield;
loop{
if(CheckDirectionAL1(objEnemy, "left")){
SetCommonData("flipsprite", true);
}
else if(CheckDirectionAL1(objEnemy, "right")){
SetCommonData("flipsprite", false);
}
if(GetCommonData("flipsprite", false)==true && CheckDirectionAL1(objEnemy, "none")){ ObjRender_SetScaleX(objEnemy, -1)}
else{ ObjRender_SetScaleX(objEnemy, 1); }


yield;
}
}



@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;
}

function castbar(seconds, spell, sound, size, width){
PlaySE(sound);
let alpha = 255;
let angle = 0;
let one = 1;
let numcasts = 0;
let green = 0;
let string = "Casting:" ~ spell;
let variable = 180;
let obj = ObjPrim_Create(OBJ_SPRITE_2D);
Obj_SetRenderPriority(obj, 0.9);
ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"tricky_system/img/Default_System.png");
ObjSprite2D_SetSourceRect(obj, 1, 1, 127, 7);
ObjSprite2D_SetDestRect(obj, GetStgFrameWidth+60, GetStgFrameHeight/2+65, GetStgFrameWidth+70, GetStgFrameHeight/2+65);
ObjRender_SetColor(obj, 255, 165, 0);
ObjRender_SetAlpha(obj, 255);
ObjMove_SetX(obj, GetStgFrameWidth/2);
ObjMove_SetY(obj, GetStgFrameHeight/2);
ObjRender_SetScaleXYZ(obj, 1, 1, 1);

let objText = ObjText_Create();
ObjText_SetFontType(objText, "Corbel");
	ObjText_SetText(objText, spell);
	ObjText_SetFontSize(objText, size);
	ObjText_SetFontBold(objText, true);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_CENTER);
	ObjText_SetMaxWidth(objText, width);
	ObjText_SetFontColorTop(objText, 224, 255, 255);
	ObjText_SetFontColorBottom(objText, 128, 128, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,0, 0, 0);
	ObjText_SetFontBorderWidth(objText, 1);

	Obj_SetRenderPriority(objText, 0.9);
	ObjRender_SetX(objText, GetStgFrameWidth/2+225);
	ObjRender_SetY(objText, GetStgFrameHeight/2+25);
angle = angletoplayer(objEnemy);
ascent(i in -5..6){
 CreateLaser1(GetEnemyX, GetEnemyY, angle+(i*2.93181818182), BLUE60, 500, 35, seconds);
 }
 

loop(seconds){
variable-=180/seconds;
ObjSprite2D_SetDestRect(obj, GetStgFrameWidth+60, GetStgFrameHeight/2+(180+65), GetStgFrameWidth+70, (GetStgFrameHeight/2+65)+variable);
if(GetVirtualKeyState(VK_USER1)==KEY_PUSH && GetCommonData("interruptavail", false)==true && GetPlayerState==STATE_NORMAL){  
ObjRender_SetColor(obj, 0, 255, 0);
StopSound(sound);
wait2(15);
loop(30){
alpha-=255/30;
ObjRender_SetAlpha(objText, alpha);
ObjRender_SetAlpha(obj, alpha);
yield;
}
return true;
 Obj_Delete(obj); break; }
wait2(1);
}
StopSound(sound);
ObjRender_SetColor(obj, 255, 0, 0);
if(sound == frostcast){ PlaySE(coneofcold); }
if(spell == "Cone of Cold"){  bloop(angle);} 

task bloop(angle){
let onee = 1;
let a = 0;
loop(60){
one*=-1;
if(popped==false){
CreateWavyBullet(GetEnemyX, GetEnemyY, 14, angle+rand(-2.5, 2.5), one);
}
wait(1);
}
}

task CreateLaser1(x, y, angle, graphic, length, width, delay){
let obj2 = ObjShot_Create(OBJ_STRAIGHT_LASER);
ObjMove_SetPosition(obj2, x, y);
ObjStLaser_SetAngle(obj2, angle);
ObjShot_SetGraphic(obj2, graphic);
ObjShot_SetDelay(obj2, delay+1);
ObjLaser_SetLength(obj2, length);
ObjLaser_SetRenderWidth(obj2, width);
ObjLaser_SetIntersectionWidth(obj2, 20);
ObjShot_SetSpellResist(obj2, true);
ObjStLaser_SetSource(obj2, false);
ObjRender_SetAlpha(obj2, 160);
ObjLaser_SetInvalidLength(obj2, 10, 10);
ObjShot_Regist(obj2);
ObjMove_SetPosition(obj2, x, y);
Wait2(delay);
Obj_Delete(obj2);

function Wait2(f){ loop(f){ if(popped==true ||(GetVirtualKeyState(VK_USER1)==KEY_PUSH && GetCommonData("interruptavail", false)==true && GetPlayerState==STATE_NORMAL)){ Obj_Delete(obj2); } yield;  } }

}



task CreateWavyBullet(x, y, v, angle, oneornot){
let radius = 0;
let angle2 = 0;
let xpos = x;
let inc = 0;
let ypos = y;
let variable = 0;
let originalangle = 0;
let variance = rand(0, 360);
let obj2 = ObjShot_Create(OBJ_SHOT);
ObjMove_SetSpeed(obj2, v);
ObjMove_SetAngle(obj2, angle);
ObjShot_SetSpellResist(obj2, true);
ObjShot_SetGraphic(obj2, BLUE50);
ObjShot_SetDelay(obj2, 0);
ObjShot_SetAutoDelete(obj2, true);
ObjMove_SetPosition(obj2, x, y);
ObjShot_Regist(obj2);
variable = rand(20, 45)*oneornot;
originalangle = angle;
while(!Obj_IsDeleted(obj2) && ObjEnemy_GetInfo(objEnemy, INFO_LIFE)>0){
xpos+=(radius^0.5)*cos(angle);
ypos+=(radius^0.5)*sin(angle);
ObjMove_SetPosition(obj2, xpos+(10+absolute((0+inc)*sin(angle)))*cos(angle+variance), ypos+(10+absolute((0+inc)*cos(angle)))*sin(angle+variance));
radius+=v/60;
variance+=7*oneornot;
inc+=0.55;
wait(1);
}
Obj_Delete(obj2);
}
wait2(15);
loop(30){
alpha-=255/30;
ObjRender_SetAlpha(objText, alpha);
ObjRender_SetAlpha(obj, alpha);
wait2(1);
}
return false;
Obj_Delete(obj);
function wait2(w){ loop(w){ if (popped==true){ StopSound(sound); Obj_Delete(objText); Obj_Delete(obj); } yield; } }
}


task attack2{
angle = 3;

wait(105);
loop{
if(popped==true){break;}
angle2 = (angle^2); 
ShootShape(GetEnemyX, GetEnemyY, 2.35, angle, 4, 11, BLUE23, 10);
angle+=360/15.75/4*one;
wait(15);
variable+=8;
}
}

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);
			CreateDNHShotA2(spawnX, spawnY, 5, atan2(sy-spawnY,sx-spawnX), -0.25, v*(((sx-spawnX)^2+(sy-spawnY)^2)^0.5),  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);
				CreateDNHShotA2(spawnX, spawnY, 5, atan2(sy2-spawnY,sx2-spawnX), -0.25, v*(((sx2-spawnX)^2+(sy2-spawnY)^2)^0.5),  graphic, delay);
			}
		}
	}

task attack1{	
wait(240);

let angle2 = 0;
let color = 0;
let angle3 = 0;
loop{
ObjAnim_SetAttackA1(objEnemy, true);
if(castbar(150, "Cone of Cold", frostcast, 15, 58) == true){
ObjAnim_SetAttackA1(objEnemy, false);
wait(30);
ExMoveBarely;
one*=-1;
wait(60);
}
else{
ObjAnim_SetAttackA1(objEnemy, false);
wait(60);
one*=-1;
ExMoveBarely;
wait(90);
}



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

