let GCSD = GetCurrentScriptDirectory;
let imgBoss = GCSD ~ "sprite.png";
let imgBossShield = GCSD ~ "invincibility.png";
let imgBossName = GCSD ~ "nametag.png";
let imgBossCut = GCSD ~ "cutin.png";
let imgSpellCardAttack = GCSD ~ "spellcardanm.png";
let imgBossSpellBack1 = GCSD ~ "bgSpellA.png";
let imgBossSpellBack2 = GCSD ~ "bgSpellB.png";
let imgMagicCircle = GCSD~"Circle.png";
let imgMoveEasy = GCSD~"movearound.png";
let circsize = GetCommonDataDefault("Circle_Size",180);
let circsizeGrow = GetCommonDataDefault("Circle_SizeGrow",90);
let circangle = GetCommonDataDefault("Circle_Angle",0);
let circsizeAlter = GetCommonDataDefault("Circle_SizeAlter",1);
let circangleAlter = GetCommonDataDefault("Circle_AngleAlter",1);
let circX = GetCommonDataDefault("Circle_X",0);
let circY = GetCommonDataDefault("Circle_Y",0);
let shieldangle = 0;
let shieldSize = 0;
let backgroundangle = 0;
let backgroundangle2 = 0;
let isMoving = false;
if(!GetCommonDataDefault("AlreadyOpenedCircle", false)) { OpenCircle; SetCommonData("AlreadyOpenedCircle", true); }

#include_function ".\ShotVariable.~ath"
#include_function ".\CutinAnimation.~ath"
#include_function ".\Animate.~ath"

@Initialize {
	LoadUserShotData(GCSD ~ "ShotDefine.~ath");
	LoadGraphic(imgSpellCardAttack);
	LoadGraphic(imgBoss);
	LoadGraphic(imgBossShield);
	LoadGraphic(imgBossName);
	LoadGraphic(imgBossCut);
	LoadGraphic(imgBossSpellBack1);
	LoadGraphic(imgBossSpellBack2);
	LoadGraphic(imgMagicCircle);
	LoadGraphic(imgMoveEasy);
	MagicCircle(false);
	SetEnemyMarker(true);
	CreateAnimation("koishi", "idle", 6);
	SetAnimationFrame("koishi", "idle", 0, imgBoss, 10, 0, 0, 64, 64);
	SetAnimationFrame("koishi", "idle", 1, imgBoss, 10, 64, 0, 128, 64);
	SetAnimationFrame("koishi", "idle", 2, imgBoss, 10, 128, 0, 192, 64);
	SetAnimationFrame("koishi", "idle", 3, imgBoss, 10, 192, 0, 256, 64);
	SetAnimationFrame("koishi", "idle", 4, imgBoss, 10, 128, 0, 192, 64);
	SetAnimationFrame("koishi", "idle", 5, imgBoss, 10, 64, 0, 128, 64);
	CreateAnimation("koishi", "move", 4);
	SetAnimationFrame("koishi", "move", 0, imgBoss, 6, 0, 64, 64, 128);
	SetAnimationFrame("koishi", "move", 1, imgBoss, 6, 64, 64, 128, 128);
	SetAnimationFrame("koishi", "move", 2, imgBoss, 6, 128, 64, 192, 128);
	SetAnimationFrame("koishi", "move", 3, imgBoss, 6, 192, 64, 256, 128);
	CreateAnimation("koishi", "attack", 4);
	SetAnimationFrame("koishi", "attack", 0, imgBoss, 6, 0, 128, 64, 192);
	SetAnimationFrame("koishi", "attack", 1, imgBoss, 6, 64, 128, 128, 192);
	SetAnimationFrame("koishi", "attack", 2, imgBoss, 6, 128, 128, 192, 192);
	SetAnimationFrame("koishi", "attack", 3, imgBoss, 6, 192, 128, 256, 192);
	Animate("koishi", "idle", true);
	codeInit;
}
@Finalize {
	DeleteGraphic(imgSpellCardAttack);
	DeleteGraphic(imgBoss);
	DeleteGraphic(imgBossShield);
	DeleteGraphic(imgBossName);
	DeleteGraphic(imgBossCut);
	DeleteGraphic(imgBossSpellBack1);
	DeleteGraphic(imgBossSpellBack2);
	DeleteGraphic(imgMagicCircle);
	DeleteGraphic(imgMoveEasy);
}
@DrawLoop {
	let circDist = ((GetY-circY)^2+(GetX-circX)^2)^0.5;
	let circAngToBoss = atan2((GetY-circY),(GetX-circX));
	circX+=circDist/5*cos(circAngToBoss); circY+=circDist/5*sin(circAngToBoss);
	circsize+=2*circsizeAlter;
	circangle+=pi*circangleAlter;
	SetTexture(imgMagicCircle);
	SetGraphicRect(0,0,128,128);
	SetGraphicAngle(0, 0, circangle);
	SetGraphicScale(3/2*sin(circsizeGrow)+1/2*sin(circsize),3/2*sin(circsizeGrow)+1/2*sin(circsize));
	SetRenderState(ALPHA);
	SetColor(255,255,255);
	SetAlpha(155);
	DrawGraphic(circX, circY);
	SetCommonData("Circle_Size",circsize);
	SetCommonData("Circle_SizeGrow",circsizeGrow);
	SetCommonData("Circle_Angle",circangle);
	SetCommonData("Circle_SizeAlter",circsizeAlter);
	SetCommonData("Circle_AngleAlter",circangleAlter);
	SetCommonData("Circle_X",circX);
	SetCommonData("Circle_Y",circY);
	SetGraphicAngle(0, 0, 0); SetGraphicScale([1,-1][GetSpeedX<0], 1);
	SetRenderState(ALPHA); SetAlpha(255); SetColor(255, 255, 255);
	DrawAnimatedSprite("koishi",GetX,GetY);
}
@BackGround {
	SetTexture(imgBossSpellBack1);
	SetGraphicRect(0, 0, 383, 447);
	SetGraphicAngle(0, 0, 0); SetGraphicScale(1, 1);
	SetRenderState(ALPHA); SetAlpha(255); SetColor(255, 255, 255);
	DrawGraphic(GetCenterX, GetCenterY);
	SetTexture(imgBossSpellBack2);
	SetGraphicRect(0, 0, 1023, 1023);
	SetGraphicAngle(0, 0, backgroundangle); SetGraphicScale(1, 1);
	SetRenderState(ALPHA); SetAlpha(155); SetColor(255, 255, 255);
	DrawGraphic(GetCenterX+100*cos(backgroundangle2), GetCenterY+200*sin(backgroundangle2));
	backgroundangle+=3/7; backgroundangle%=360;
	backgroundangle2-=2/pi; backgroundangle2%=360;
}
@MainLoop {
	if(GetSpeed != 0 && isMoving) { Animate("koishi", "move", false); }
	if(GetSpeed == 0 && isMoving) { Animate("koishi", "idle", true); }
	shieldangle+=4; shieldangle%=360;
	if(invin) { shieldSize+=1/30; } else { shieldSize-=1/30; } if(shieldSize>1){shieldSize=1;}if(shieldSize<0){shieldSize=0;}
	isMoving = (GetSpeed != 0);
	if(GetHitCount && GetLife < 2000) { PlaySE2("se_boss_lowlife"); }
	yield;
}

task OpenCircle {
	circsize = 180; circsizeGrow = 0; circangle = 0; descent(i in 0..90) { circangle-=i*1/10; }
	circsizeAlter = 0; circangleAlter = 0; circX = GetX; circY = GetY;
	descent(i in 0..90) { circsizeGrow+=90/90; circangle+=i*1/10; AddLife(1000); yield; }
	loop(30) {
		circsizeAlter+=1/30;
		circangleAlter+=1/30;
		AddLife(1000);
		yield;
	}
}

function wait(let f) { loop(f) { yield; } }
function invin { return OnEnemySpell && OnBomb; }
function PlaySE2(string) { PlaySE(GCSD~string~".wav"); }

function ExMove01_(nBaseHeight, nHorizontal, nAddHeight, nVelocity) {
	if(GetPlayerX < GetX) {
		if(GetX - nHorizontal <= GetClipMinX) {
			SetMovePosition01(GetX+nHorizontal, nBaseHeight+rand(-nAddHeight, nAddHeight), nVelocity);
		} else {
			SetMovePosition01(GetX-nHorizontal, nBaseHeight+rand(-nAddHeight, nAddHeight), nVelocity);
		}
	} else {
		if(GetX + nHorizontal >= GetClipMaxX) {
			SetMovePosition01(GetX-nHorizontal, nBaseHeight+rand(-nAddHeight, nAddHeight), nVelocity);
		} else {
			SetMovePosition01(GetX+nHorizontal, nBaseHeight+rand(-nAddHeight, nAddHeight), nVelocity);
		}
	}
}
function ExMove02_(nBaseHeight, nHorizontal, nAddHeight, nFrame) {
	if(GetPlayerX < GetX) {
		if(GetX - nHorizontal <= GetClipMinX) {
			SetMovePosition02(GetX+nHorizontal, nBaseHeight+rand(-nAddHeight, nAddHeight), nFrame);
		} else {
			SetMovePosition02(GetX-nHorizontal, nBaseHeight+rand(-nAddHeight, nAddHeight), nFrame);
		}
	} else {
		if(GetX + nHorizontal >= GetClipMaxX) {
			SetMovePosition02(GetX-nHorizontal, nBaseHeight+rand(-nAddHeight, nAddHeight), nFrame);
		} else {
			SetMovePosition02(GetX+nHorizontal, nBaseHeight+rand(-nAddHeight, nAddHeight), nFrame);
		}
	}
}
function ExMove03_(nBaseHeight, nHorizontal, nAddHeight, nWeight, nVelocity) {
	if(GetPlayerX < GetX) {
		if(GetX - nHorizontal <= GetClipMinX) {
			SetMovePosition03(GetX+nHorizontal, nBaseHeight+rand(-nAddHeight, nAddHeight), nWeight, nVelocity);
		} else {
			SetMovePosition03(GetX-nHorizontal, nBaseHeight+rand(-nAddHeight, nAddHeight), nWeight, nVelocity);
		}
	} else {
		if(GetX + nHorizontal >= GetClipMaxX) {
			SetMovePosition03(GetX-nHorizontal, nBaseHeight+rand(-nAddHeight, nAddHeight), nWeight, nVelocity);
		} else {
			SetMovePosition03(GetX+nHorizontal, nBaseHeight+rand(-nAddHeight, nAddHeight), nWeight, nVelocity);
		}
	}
}
sub ExMove01 { ExMove01_(GetCenterY-100, rand(15, 50), 30, 1); }
sub ExMove01Slow { ExMove01_(GetCenterY-100, rand(5, 30), 10, 1/2); }
sub ExMove02 { ExMove02_(GetCenterY-100, rand(15, 50), 30, 60); }
sub ExMove02B { ExMove02_(GetCenterY-100, rand(30, 50), 50, 60); }
sub ExMove02Slow { ExMove02_(GetCenterY-100, rand(5, 30), 10, 90); }
sub ExMove03 { ExMove03_(GetCenterY-100, rand(15, 50), 30, 10, 2); }
sub ExMove03Slow { ExMove03_(GetCenterY-100, rand(5, 30), 10, 15, 1); }
sub ExMove { ExMove03; }
sub ExMoveSlow { ExMove03Slow; }