#TouhouDanmakufu[Single]
#ScriptVersion[3]
#ID["K-01-05"]
#Title["Sun Sign \"Apollon Bow\""]
#Text[""]

#include "script/default_system/Default_ShotConst.txt"
#include"script/default_system/Default_Effect.txt"
#include "./funclib.txt"
#include "./cutin.txt"

let objEnemy;
let objPlayer;
let backSpell;
let backOverlay;

let dead = false;

let boom = gcsd~"sfx/se_enep01.wav";
let lazer = gcsd~"sfx/se_lazer00.wav";
let tan2 = gcsd~"sfx/se_tan02.wav";

let aura = gcsd~"msaura.png";
let aa = 0;
let ar = 0;
let as = 0;

@Event
{
	SetLife(320);
	SetScore(20000);
	LoadSound(lazer);LoadSound(boom);LoadSound(tan2);
}

@Initialize
{
	let img = gcsd ~ "kazu.png";
	let ct = gcsd~ "ct.png";

	objPlayer = GetPlayer;
	objEnemy = StartBoss;
	CutIn2(objEnemy, 0, "Sun Sign \"Apollon Bow\"", "MAGIC SYNC \"MOVEMENT SYNC\"", "YELLOW", "PURPLE", ct, 0, 0, 240, 384, 120);

	SetTexture(objEnemy, img);
	ObjRender_SetScaleXYZ(objEnemy, 0.3, 0.3, 0.3);
	DrawSprite2D(objEnemy, 0, 0, 160, 256);

	SetDamageRate(objEnemy, 5, 0);
	MoveFrame(objEnemy, GetCenterX, 160, 60);

	MainTask;
	Background;
}

@MainLoop
{
	SetCollisionShot(objEnemy, GetX(objEnemy), GetY(objEnemy), 32);
	SetCollisionPlayer(objEnemy, GetX(objEnemy), GetY(objEnemy), 24);

	if(GetLife(objEnemy) <= 0 && dead==false)
	{
		Death;
		dead=true;
	}

	yield;
}

task MainTask
{
	loop(120){yield;}
	attack;
	arrow;
	Aura;
}

task attack
{
	loop
	{
		ascent(a in -10..11)
		{
			shotIceBall(GetAngleToPlayer2(objEnemy)+a*10);
		}
		loop(5){yield;}
		ascent(a in -6..7)
		{
			shotFireBall(GetAngleToPlayer2(objEnemy)+a*10);
		}
		loop(25){yield;}
	}
}

task shotIceBall(let ang)
{
	let dx; let dy; let dist;
	let shotIce = CreateShotA1(GetX(objEnemy), GetY(objEnemy), 2, ang, DS_BALL_M_BLUE, 10);
	while(!Obj_IsDeleted(shotIce))
	{
		dx = absolute(GetX(objEnemy)-GetX(shotIce));
		dy = absolute(GetY(objEnemy)-GetY(shotIce));
		dist = ((dx^2)+(dy^2))^0.5;

		if(dist<(16*as)&&aa>=100)
		{
			ObjShot_SetGraphic(shotIce, DS_BALL_M_PURPLE);
			if(GetVirtualKeyState(VK_LEFT)==KEY_PUSH||GetVirtualKeyState(VK_LEFT)==KEY_HOLD){ObjMove_SetX(shotIce, GetX(shotIce)-0.5);}
			if(GetVirtualKeyState(VK_RIGHT)==KEY_PUSH||GetVirtualKeyState(VK_RIGHT)==KEY_HOLD){ObjMove_SetX(shotIce, GetX(shotIce)+0.5);}
		}
		else{ObjShot_SetGraphic(shotIce, DS_BALL_M_BLUE);}

		yield;
	}
}

task shotFireBall(let ang)
{
	let dx; let dy; let dist;
	let shotFire = CreateShotA1(GetX(objEnemy), GetY(objEnemy), 2, ang, DS_BALL_S_RED, 10);
	while(!Obj_IsDeleted(shotFire))
	{
		dx = absolute(GetX(objEnemy)-GetX(shotFire));
		dy = absolute(GetY(objEnemy)-GetY(shotFire));
		dist = ((dx^2)+(dy^2))^0.5;

		if(dist<(16*as)&&aa>=100)
		{
			ObjShot_SetGraphic(shotFire, DS_BALL_S_PURPLE);
			if(GetVirtualKeyState(VK_LEFT)==KEY_PUSH||GetVirtualKeyState(VK_LEFT)==KEY_HOLD){ObjMove_SetX(shotFire, GetX(shotFire)+0.5);}
			if(GetVirtualKeyState(VK_RIGHT)==KEY_PUSH||GetVirtualKeyState(VK_RIGHT)==KEY_HOLD){ObjMove_SetX(shotFire, GetX(shotFire)-0.5);}
		}
		else{ObjShot_SetGraphic(shotFire, DS_BALL_S_RED);}

		yield;
	}
}

task arrow
{
	let xv = 0; 
	let yv = 0;

	while(GetLife(objEnemy)>120)
	{
		if(!dead){PlaySE(lazer);}
		ascent(a in -5..6){shotBow(a*8);}
		loop(240){yield;}
	}
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	SetDamageRate(objEnemy, 1, 0);
	loop
	{
		if(!dead){PlaySE(lazer);}
		ascent(a in -5..6){shotBow(a*8);}
		loop(120){yield;}
		if(!dead){PlaySE(lazer);}
		ascent(a in -5..6){shotBowH(16*a); shotBowH2(16*a);}
		loop(120){yield;}
	}
}

task shotBow(let def)
{
	let shotArrow2;
	let shotArrow = CreateLooseLaserA1(GetX(objEnemy), GetY(objEnemy), 4, atan2(-GetPlayerY - GetY(objEnemy), GetPlayerX - GetX(objEnemy)), 300, 16, DS_BALL_S_YELLOW, 10);
	let sfx = 0;
	while(!Obj_IsDeleted(shotArrow))
	{
		if(GetY(shotArrow)<0)
		{
			shotArrow2 = CreateLooseLaserA1(GetX(shotArrow), GetY(shotArrow), 4, GetAngleToPlayer2(shotArrow)+def, 300, 12, DS_BALL_S_YELLOW, 10);
			ObjMove_SetAcceleration(shotArrow2, 0.1);
			ObjMove_SetMaxSpeed(shotArrow2, 10);

			while(GetY(shotArrow2)<GetStgFrameHeight+96){yield;}
			if(!dead&&sfx==0){PlaySE(tan2);sfx=1;}
			ObjMove_SetAcceleration(shotArrow2, 0);
			ObjMove_SetSpeed(shotArrow2, 0);
			loop(60){yield;}
			ObjShot_FadeDelete(shotArrow2);
		}
		yield;
	}
}


task shotBowH(let def)
{
	let shotArrow2;
	let shotArrow = CreateLooseLaserA1(GetX(objEnemy), GetY(objEnemy), 4, atan2(GetPlayerY - GetY(objEnemy), -GetPlayerX - GetX(objEnemy)), 300, 12, DS_BALL_S_YELLOW, 10);
	let sfx = 0;
	while(!Obj_IsDeleted(shotArrow))
	{
		if(GetX(shotArrow)<0)
		{
			shotArrow2 = CreateLooseLaserA1(GetX(shotArrow), GetY(shotArrow), 4, GetAngleToPlayer2(shotArrow)+def, 300, 12, DS_BALL_S_YELLOW, 10);
			ObjMove_SetAcceleration(shotArrow2, 0.1);
			ObjMove_SetMaxSpeed(shotArrow2, 10);

			while(GetX(shotArrow2)<GetStgFrameWidth+96){yield;}
			if(!dead&&sfx==0){PlaySE(tan2);sfx=1;}
			ObjMove_SetAcceleration(shotArrow2, 0);
			ObjMove_SetSpeed(shotArrow2, 0);
			loop(60){yield;}
			ObjShot_FadeDelete(shotArrow2);
		}
		yield;
	}
}

task shotBowH2(let def)
{
	let shotArrow2;
	let shotArrow = CreateLooseLaserA1(GetX(objEnemy), GetY(objEnemy), 4, atan2(GetPlayerY - GetY(objEnemy), GetStgFrameWidth+GetPlayerX - GetX(objEnemy)), 300, 12, DS_BALL_S_YELLOW, 10);
	let sfx = 0;
	while(!Obj_IsDeleted(shotArrow))
	{
		if(GetX(shotArrow)>GetStgFrameWidth)
		{
			shotArrow2 = CreateLooseLaserA1(GetX(shotArrow), GetY(shotArrow), 4, GetAngleToPlayer2(shotArrow)+def, 300, 12, DS_BALL_S_YELLOW, 10);
			ObjMove_SetAcceleration(shotArrow2, 0.1);
			ObjMove_SetMaxSpeed(shotArrow2, 10);

			while(GetX(shotArrow2)>-96){yield;}
			if(!dead&&sfx==0){PlaySE(tan2);sfx=1;}
			ObjMove_SetAcceleration(shotArrow2, 0);
			ObjMove_SetSpeed(shotArrow2, 0);
			loop(60){yield;}
			ObjShot_FadeDelete(shotArrow2);
		}
		
		yield;
	}
}

task Death
{
	PlaySE(boom);
	TExplosionA(GetX(objEnemy), GetY(objEnemy), 10, 1);
	Obj_Delete(objEnemy);Obj_Delete(backSpell);Obj_Delete(backOverlay);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	loop(180){yield;DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);}
	CloseScript(GetOwnScriptID());
}

task Background
{
	let a = 0;

	backSpell = ObjPrim_Create(OBJ_SPRITE_2D);
	SetTexture(backSpell, gcsd~"back.png");
	ObjSprite2D_SetSourceRect(backSpell, 0, 0, 512, 512);
	ObjSprite2D_SetDestRect(backSpell, 0, 0, 512, 512);
	ObjRender_SetAlpha(backSpell, 0);
	Obj_SetRenderPriorityI(backSpell, 20);

	backOverlay = ObjPrim_Create(OBJ_SPRITE_2D);
	SetTexture(backOverlay, gcsd~"backover.png");
	ObjSprite2D_SetSourceRect(backOverlay, 0, 0, 200, 200);
	ObjSprite2D_SetDestRect(backOverlay, -320, -320, 320, 320);
	Obj_SetRenderPriorityI(backOverlay, 20);
	ObjRender_SetAlpha(backOverlay, 0);
	ObjRender_SetPosition(backOverlay, GetCenterX, GetCenterY, 0);

	let r = 0;

	loop
	{
		if(a<255){a+=2.5;}
		else{a=255;}
		ObjRender_SetAlpha(backSpell, a);
		ObjRender_SetAlpha(backOverlay, (a/255*100));
		r+=0.2;
		ObjRender_SetAngleZ(backOverlay, r+20*sin(5*r));
		yield;
	}
}

task Aura
{
	let objAura = ObjPrim_Create(OBJ_SPRITE_2D);
	SetTexture(objAura, aura);
	ObjSprite2D_SetSourceRect(objAura, 0, 0, 128, 128);
	ObjSprite2D_SetDestRect(objAura, -16, -16, 16, 16);
	Obj_SetRenderPriorityI(objAura, 21);
	ObjRender_SetAlpha(objAura, aa);
	ObjRender_SetScaleXYZ(objAura, as, as, 1);
	ObjRender_SetAngleZ(objAura, ar);
	ObjRender_SetPosition(objAura, GetX(objEnemy), GetY(objEnemy), 0);

	while(!Obj_IsDeleted(objEnemy))
	{
		if(aa<100){aa+=0.2;}
		if(aa<255&&GetLife(objEnemy)<60){aa+=0.5;}
		ar+=0.1;
		as = (320/GetLife(objEnemy)*4);
		ObjRender_SetAlpha(objAura, aa);
		ObjRender_SetScaleXYZ(objAura, as, as, 1);
		ObjRender_SetAngleZ(objAura, ar);
		yield;
	}
	loop(120){aa-=(255/120);ObjRender_SetAlpha(objAura, aa);yield;}
}