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

let objEnemy;
let objPlayer;

let kira = gcsd~"sfx/se_kira00.wav";
let tan2 = gcsd~"sfx/se_tan02.wav";

@Event
{
	SetLife(1600);
}

@Initialize
{
	objPlayer = GetPlayer;
	objEnemy = StartBoss;

	let img = gcsd ~ "kazu.png";
	SetTexture(objEnemy, img);
	ObjRender_SetScaleXYZ(objEnemy, 0.3, 0.3, 0.3);
	DrawSprite2D(objEnemy, 0, 0, 160, 256);

	MoveFrame(objEnemy, GetCenterX, 120, 60);

	SetDamageRate(objEnemy, 0, 0);
	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);
	text;

	LoadSound(kira);LoadSound(tan2);
}

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

	if(GetLife(objEnemy) <= 0)
	{
		Death;
	}

	yield;
}

task MainTask
{
	loop(60){yield;}
	ice;
	fire;
}

task text
{
	loop(60){yield;}

	let bub = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(bub, gcsd~"bubble.png");
	ObjSprite2D_SetSourceRect(bub, 0, 0, 192, 48);
	ObjSprite2D_SetDestRect(bub, 0, 0, 192, 32);
	ObjRender_SetPosition(bub, GetCenterX-78, 47, 0);

	let txt = ObjText_Create;
	InstallFont(gcsd~"font/verdana.ttf");
	ObjText_SetText(txt, "貴女を見つかった!");
	ObjText_SetFontType(txt, "Verdana");
	ObjText_SetFontSize(txt, 12);
	ObjText_SetFontBold(txt, true);
	ObjText_SetFontColorTop(txt, 0, 0, 0);ObjText_SetFontColorBottom(txt, 0, 0, 0);
	ObjText_SetHorizontalAlignment(txt, ALIGNMENT_LEFT);
	ObjRender_SetPosition(txt, GetCenterX-64, 54, 0);

	let timer = 0;

	while(((GetVirtualKeyState(VK_SHOT)!=KEY_PUSH)||timer<30)&&(timer<240))
	{
		timer++; yield;
	}
	timer=0; 
	ObjText_SetText(txt, "さって励みましょうね〜");
	yield;
	while(((GetVirtualKeyState(VK_SHOT)!=KEY_PUSH)||timer<30)&&(timer<240))
	{
		timer++; yield;
	}
	timer=0; 
	ObjText_SetText(txt, "僕の「MAGIC　SYNC」能力…");
	yield;
	while(((GetVirtualKeyState(VK_SHOT)!=KEY_PUSH)||timer<30)&&(timer<240))
	{
		timer++; yield;
	}
	timer=0; 
	ObjText_SetText(txt, "…対して何するか？　初め!");
	yield;
	while(((GetVirtualKeyState(VK_SHOT)!=KEY_PUSH)||timer<30)&&(timer<240))
	{
		timer++; yield;
	}
	Obj_Delete(bub); Obj_Delete(txt);
	SetDamageRate(objEnemy, 100, 100);
	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
	MainTask;
}

task ice
{
	let ang = -90;
	let angdif = 0;

	loop
	{
		loop(60)
		{
			PlaySE(kira);
			let sx = GetX(objEnemy) + 60*cos(ang);
			let sy = GetY(objEnemy) + 60*sin(ang);
			let pang = atan2(GetPlayerY-sy, GetPlayerX-sx);
			let shotIce1 = CreateShotA1(sx, sy, 2, pang+angdif, DS_ICE_BLUE, 10);
			let shotIce2 = CreateShotA1(sx, sy, 2, pang+120+angdif, DS_ICE_BLUE, 10);
			let shotIce3 = CreateShotA1(sx, sy, 2, pang-120+angdif, DS_ICE_BLUE, 10);
			let shotIce4 = CreateShotA1(sx, sy, 2, pang-angdif, DS_ICE_BLUE, 10);
			let shotIce5 = CreateShotA1(sx, sy, 2, pang+120-angdif, DS_ICE_BLUE, 10);
			let shotIce6 = CreateShotA1(sx, sy, 2, pang-120-angdif, DS_ICE_BLUE, 10);

			angdif+=0.25;
			ang+=18;
			loop(2){yield;}
		}
		angdif = 0;
		loop(120){yield;}

		loop(60)
		{
			PlaySE(kira);
			let sx = GetX(objEnemy) + 60*cos(ang);
			let sy = GetY(objEnemy) + 60*sin(ang);
			let pang = atan2(GetPlayerY-sy, GetPlayerX-sx);
			let shotIce1 = CreateShotA1(sx, sy, 2, pang+angdif, DS_ICE_BLUE, 10);
			let shotIce2 = CreateShotA1(sx, sy, 2, pang+120+angdif, DS_ICE_BLUE, 10);
			let shotIce3 = CreateShotA1(sx, sy, 2, pang-120+angdif, DS_ICE_BLUE, 10);
			let shotIce4 = CreateShotA1(sx, sy, 2, pang-angdif, DS_ICE_BLUE, 10);
			let shotIce5 = CreateShotA1(sx, sy, 2, pang+120-angdif, DS_ICE_BLUE, 10);
			let shotIce6 = CreateShotA1(sx, sy, 2, pang-120-angdif, DS_ICE_BLUE, 10);

			angdif+=0.25;
			ang-=18;
			loop(2){yield;}
		}
		angdif = 0;
		loop(120){yield;}
	}
}

task fire
{
	let ang = 0;

	loop
	{
		PlaySE(tan2);
		ascent(a in 0..12)
		{
			let shotFire1 = CreateShotA1(GetX(objEnemy)+80*cos(-ang+a*30), GetY(objEnemy)+80*sin(-ang+a*30), 2.5, -ang+a*30+45, DS_FIRE_RED, 10);
			let shotFire2 = CreateShotA1(GetX(objEnemy)+80*cos(ang+a*30), GetY(objEnemy)+80*sin(ang+a*30), 2.5, ang+a*30-45, DS_FIRE_RED, 10);
			let shotFire3 = CreateShotA1(GetX(objEnemy)+80*cos(-ang+a*30), GetY(objEnemy)+80*sin(-ang+a*30), 2, -ang+a*30+45, DS_FIRE_RED, 10);
			let shotFire4 = CreateShotA1(GetX(objEnemy)+80*cos(ang+a*30), GetY(objEnemy)+80*sin(ang+a*30), 2, ang+a*30-45, DS_FIRE_RED, 10);
		}

		ang+=7;
		loop(45){yield;}
	}
}

task Death
{
	Obj_Delete(objEnemy);
	yield;
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	CloseScript(GetOwnScriptID());
}
