//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#System["./default_system/Default_System.txt"]
#Title["Signe de l'Automne \"Feuilles tombantes\""]
#Text["The first spellcard of Suwako+"]

#include "./img/All_Star_Shots/AllStarShot_Const.txt"
#include "./Sounds.txt"
#include "./Functions.txt"
#include "./Animation.txt"
#include "./Effects.txt"

let bossObj;
let sceneObj = GetEnemyBossSceneObjectID;

let TIME = 60;

@Initialize
{
	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(bossObj);
	
	DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
	ObjEnemyBossScene_StartSpell(sceneObj);
	SetCommonData("animStyle", "idle");
	renderSpellCard666;
	BottomEnemyMarker(bossObj);
	bossSummonCircle(102, 3, 105, 0.1);
	
	spellName(bossObj, "Possession \"Feu des ames\"");
	
	timer(TIME);
	renderBoss666;
	TFinalize;
	movement;
}

@Event
{
	alternative (GetEventType())
	case (EV_REQUEST_LIFE) {
		SetScriptResult(4500);
	}
	case (EV_REQUEST_TIMER) {
		SetScriptResult(TIME);
	}
	case (EV_REQUEST_SPELL_SCORE) {
		SetScriptResult(50000000);
	}
}

@MainLoop
{
	ObjEnemy_SetIntersectionCircleToShot(bossObj, ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 24);
	ObjEnemy_SetIntersectionCircleToPlayer(bossObj, ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 32);
	SetCommonData("bossX", ObjMove_GetX(bossObj));
	SetCommonData("bossY", ObjMove_GetY(bossObj));
	yield;
}

task movement {
	wait(60);
	while(!Obj_IsDeleted(bossObj)) {
	fire1;
	wait(60);
	}
}

function ObjPrim_SetDestCenter(obj,x1,y1,x2,y2)
{
    //指定域の画像の中心をobjの中心に設定する(よく使うタイプ)
    ObjPrim_SetVertexPosition(obj, 0, -(x2-x1)/2, -(y2-y1)/2, -0);
    ObjPrim_SetVertexPosition(obj, 1, (x2-x1)/2, -(y2-y1)/2, -0);
    ObjPrim_SetVertexPosition(obj, 2, (x2-x1)/2, (y2-y1)/2, 0);
    ObjPrim_SetVertexPosition(obj, 3, -(x2-x1)/2, (y2-y1)/2, 0);
    ObjPrim_SetVertexUVT(obj, 0, x1, y1);
    ObjPrim_SetVertexUVT(obj, 1, x2, y1);
    ObjPrim_SetVertexUVT(obj, 2, x2, y2);
    ObjPrim_SetVertexUVT(obj, 3, x1, y2);
}

task ReverseEffect
		{
			let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
			ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
			ObjRender_SetBlendType(obj, BLEND_INV_DESTRGB);
			ObjPrim_SetVertexCount(obj, 4);
			Obj_SetRenderPriorityI(obj,78);
			ObjRender_SetAngleZ(obj,0);
			ObjPrim_SetDestCenter(obj,0,0,640,640);
			ObjRender_SetPosition(obj,320,240,0);
			let Alpha=0;
			let Scale=0;
			let ScalePlus=0;
			let ScalePlusSpeed=0.0125/8*1.33;
		}

task fire1 {
	let angleT = rand(0, 360); 
	loop(40) {
	let obj1 = CreateShotA2(ObjMove_GetX(bossObj)-100*cos(angleT),ObjMove_GetY(bossObj)+100*sin(angleT), 6,-angleT,-0.25,2, 333, 20);
	let obj2 = CreateShotA2(ObjMove_GetX(bossObj)-100*cos(angleT),ObjMove_GetY(bossObj)+100*sin(angleT), 5,-angleT+4.5,-0.25,1, 34, 20);
	zigzag(obj1);
	angleT += 360/40;
	}
	wait(20);
	play(19, 30);
}

task zigzag(aaa) {
	while(!Obj_IsDeleted(aaa)) {
	ObjMove_SetAngularVelocity(aaa, 4);
	wait(30);
	ObjMove_SetAngularVelocity(aaa, -4);
	wait(30);
	}
}


task TMovement
{
	let angle;
	let radius;
	if (cos(bossAngleToPlayer) > 0) {
		angle = (randInt(-90, 90) + randInt(-90, 90)) / 2;
	} else {
		angle = (randInt(90, 270) + randInt(90, 270)) / 2;
	}
	if (absolute(distancePoints(GetPlayerX, GetPlayerY, ObjMove_GetX(bossObj), ObjMove_GetY(bossObj)) * cos(bossAngleToPlayer)) > GetStgFrameHeight / 4) {
		radius = (randInt(100, 150) + randInt(100, 150)) / 2;
	} else {
		radius = (randInt(0, 100) + randInt(0, 100)) / 2;
	}
	let x = radius * cos(angle) + ObjMove_GetX(bossObj);
	let y = radius * sin(angle) + ObjMove_GetY(bossObj);
	ObjMove_SetDestAtSpeed(bossObj, x, max(min(y, 100), 30), 1.5);
	wait(145);
}

task TFinalize
{
	while (ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) {yield;}
	wait(5);
	if (ObjEnemyBossScene_GetInfo(sceneObj, INFO_PLAYER_SHOOTDOWN_COUNT) + ObjEnemyBossScene_GetInfo(sceneObj, INFO_PLAYER_SPELL_COUNT) == 0) {
		AddScore(ObjEnemyBossScene_GetInfo(sceneObj, INFO_SPELL_SCORE));
	}
	loop(20) {
		let radius = (randInt(0, 100) + randInt(0, 100)) / 2;
		let angle = rand(0, 360);
		NotifyEvent(GetCommonData("scriptID", GetOwnScriptID), EV_USER + 1, [ITEM_POINT_S, 1000000, ObjMove_GetX(bossObj) + radius * cos(angle), ObjMove_GetY(bossObj) + radius * sin(angle)]);
	}	
	NotifyEvent(GetCommonData("scriptID", GetOwnScriptID), EV_USER + 3, [FROG_OVERLAY]);
	SetCommonData("bossX", ObjMove_GetX(bossObj));
	SetCommonData("bossY", ObjMove_GetY(bossObj));
	
	Obj_Delete(bossObj);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	SetAutoDeleteObject(true);
	CloseScript(GetOwnScriptID());
	return;
}



