#TouhouDanmakufu[Single]
#ScriptVersion[3]
#ID["K-01-03"]
#Title["Ice Sign \"Blizzard\""]
#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 kira = gcsd~"sfx/se_kira00.wav";
let boom = gcsd~"sfx/se_enep00.wav";
let tan2 = gcsd~"sfx/se_tan02.wav";

@Event
{
	SetLife(320);
	SetTimer(60);
	SetScore(12000);
	LoadSound(kira);LoadSound(boom);LoadSound(tan2);
}

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

	objPlayer = GetPlayer;
	objEnemy = StartBoss;
	CutIn(objEnemy, 0, "Ice Sign \"Blizzard\"", "AQUA", ct, 0, 0, 240, 384);

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

	SetDamageRate(objEnemy, 10, 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;
	attack2;
	loop(60){yield;}
	attack3;
}

task attack
{
	loop
	{
		shotIceDown;
		loop(6){yield;}
	}
}

task shotIceDown
{
	let shotIce = CreateShotA1(GetX(objEnemy), GetY(objEnemy)-40, 2, rand(-150, -30), DS_ICE_BLUE, 10);
	while(!Obj_IsDeleted(shotIce))
	{
		if(GetY(shotIce)<0)
		{
			ObjMove_SetAngle(shotIce, rand(80, 100)); 
			ObjMove_SetSpeed(shotIce, 0);
			ObjMove_SetAcceleration(shotIce, 0.01);
			ObjMove_SetMaxSpeed(shotIce, 3);
			ObjMove_SetY(shotIce, 0);
		}
		yield;
	}
}

task attack2
{
	let ang = 0;
	let time = 0;

	loop
	{
		ascent(a in 0..18)
		{
			shotBlueSide(GetX(objEnemy)+60*cos((a*20)-90), GetY(objEnemy)+60*sin((a*20)-90), 0, -a);
			shotBlueSide(GetX(objEnemy)+60*cos(-(a*20)-90), GetY(objEnemy)+60*sin(-(a*20)-90), 2, -a);
			if(!dead){PlaySE(tan2);}
			yield;
		}
		loop(90){yield;}
		if(!dead){PlaySE(kira);}
		loop(150){yield;}

		ascent(a in 0..18)
		{
			shotBlueSide(GetX(objEnemy)+60*cos((a*20)), GetY(objEnemy)+60*sin((a*20)), 1, -a);
			shotBlueSide(GetX(objEnemy)+60*cos(-(a*20)), GetY(objEnemy)+60*sin(-(a*20)), 3, -a);
			if(!dead){PlaySE(tan2);}
			yield;
		}
		loop(90){yield;}
		if(!dead){PlaySE(kira);}
		loop(150){yield;}

		ascent(a in 0..18)
		{
			shotBlueSide(GetX(objEnemy)+60*cos((a*20)), GetY(objEnemy)+60*sin((a*20)), 1.5, -a);
			shotBlueSide(GetX(objEnemy)+60*cos(-(a*20)), GetY(objEnemy)+60*sin(-(a*20)), 3.5, -a);
			shotBlueSide(GetX(objEnemy)+60*cos((a*20)-90), GetY(objEnemy)+60*sin((a*20)-90), 0.5, -a);
			shotBlueSide(GetX(objEnemy)+60*cos(-(a*20)-90), GetY(objEnemy)+60*sin(-(a*20)-90), 2.5, -a);
			if(!dead){PlaySE(tan2);}
			yield;
		}
		loop(90){yield;}
		if(!dead){PlaySE(kira);}
		loop(150){yield;}
	}
}

task attack3
{
	loop
	{
		ascent(a in 0..48)
		{
			let shotBall1 = CreateShotA1(GetX(objEnemy), GetY(objEnemy), 3-0.02*a, a*13, DS_BALL_L_BLUE, 10);
			let shotBall2 = CreateShotA1(GetX(objEnemy), GetY(objEnemy), 3-0.02*a, a*13+180, DS_BALL_L_BLUE, 10);
			yield;
		}
		loop(210){yield;}
		ascent(a in 0..48)
		{
			let shotBall1 = CreateShotA1(GetX(objEnemy), GetY(objEnemy), 3-0.02*a, -a*13, DS_BALL_L_BLUE, 10);
			let shotBall2 = CreateShotA1(GetX(objEnemy), GetY(objEnemy), 3-0.02*a, -a*13+180, DS_BALL_L_BLUE, 10);
			yield;
		}
		loop(210){yield;}
	}
}

task shotBlueSide(let x, let y, let side, let time)
{
	let shotBlue = CreateShotA1(x, y, 2.5, 90*side, DS_BALL_M_BLUE, 10);
	ObjMove_AddPatternA2(shotBlue, 20, NO_CHANGE, NO_CHANGE, -0.05, 0, 0);
	ObjMove_AddPatternA2(shotBlue, 90+time, 0, NO_CHANGE, 0.1, 0, 2);
	loop(90+time){yield;}
	ObjMove_SetAngle(shotBlue, GetAngleToPlayer2(shotBlue));
}

task Death
{
	PlaySE(boom);
	TExplosionA(GetX(objEnemy), GetY(objEnemy), 10, 1);
	Obj_Delete(objEnemy);Obj_Delete(backSpell);Obj_Delete(backOverlay);
	loop(30){yield;DeleteShotAll(TYPE_ALL, TYPE_ITEM);}
	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;
	}
}