#TouhouDanmakufu[Single]
#System["./system/Default_System.txt"]
#Title["Hina Spell 1"]
#include"./Effects_Main.txt"

let current = GetCurrentScriptDirectory;
let enm = ID_INVALID;
let GetX = 0;
let GetY = 0;
let EnemyX = 0;
let EnemyY = 0;

let s=1;
let angle2=0;
let c=6; let c2=15; let c3=7;
let ang=0;

let objScene=GetEnemyBossSceneObjectID();
// your best friend, forever.


@Event{
	alternative(GetEventType)
	//1;500;2500
	case(EV_REQUEST_LIFE){SetScriptResult(5200*3);}
	case(EV_REQUEST_TIMER){SetScriptResult(62);}
	case(EV_REQUEST_SPELL_SCORE){
			SetScriptResult(1000000);
		}
	//case(EV_REQUEST_IS_DURABLE_SPELL){SetScriptResult(true);}
}

@Initialize{


SetAutoDeleteObject(true);
	enm = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(enm);
			ObjEnemy_SetDamageRate(enm, 75, 0);
	CreateLifeBar(enm, 60, 55, 41, 0, [255,255,255], "SpellCard" , [false,false] );
renderHina(enm);
BossIndicator(enm);
TAura(enm, 6);
	ObjMove_SetDestAtWeight(enm, GetStgFrameWidth/2, GetStgFrameHeight/2-100, 12, 4);
	Invincible(400);
	BossActions;
}


@MainLoop{
	GetX = ObjMove_GetX(enm);
	GetY = ObjMove_GetY(enm);
EnemyX = ObjMove_GetX(enm);
angle2+=s;
	EnemyY = ObjMove_GetY(enm);
	ObjEnemy_SetIntersectionCircleToShot(enm, GetX, GetY, 28);
	ObjEnemy_SetIntersectionCircleToPlayer(enm, GetX, GetY, 32);
	yield;
}

function angleToBoss{
	let dir = atan2(GetY-GetPlayerY,GetX-GetPlayerX);
	return dir;
}

function angleToPlayer{
	let dir = atan2(GetPlayerY-GetY,GetPlayerX-GetX);
	return dir;
}



task BossActions{
	let dir = angleToPlayer;
	let angleT = rand(0,360);

	 
	PerformActions;
	while(ObjEnemy_GetInfo(enm, INFO_LIFE) > 0){
		yield;
	}
	

	//NotifyEventAll(EV_USER+10, [ObjMove_GetX(enm), ObjMove_GetY(enm)]);
	Obj_Delete(enm);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);

	yield;
	CloseScript(GetOwnScriptID);
	
	task PerformActions{
	loop(90){yield;}
	LoadMagicCircle(1);
	StartSpellMiddle("Tragic Greed \"Lottery Misfortune\" ", current~"./img/HinaBase.png",current~"./img/HinaBase2.png", 0, 227, 53,520,509,1);
	
	loop(60){yield;}

let angle = -90;

if (GetCommonData("rank", "Normal")=="Normal"){c=6; c2=3; c3=10;}
else{c=8; c2=4; c3=12;}
let a =0;
	
	loop{
		ChargeUp(enm, [ObjMove_GetX(enm), ObjMove_GetY(enm)], -9,3, angle, 50, 20, [255, 0, 0], "DARK");
		wait(40);
		Obj_SetValue(enm,"cast",1);
		Pattern1;
		wait(310);
		TMovement(90,60,0.5);
		Obj_SetValue(enm,"cast",0);
		Pattern2;
		wait(320);
		ObjMove_SetDestAtWeight(enm, GetStgFrameWidth/2, GetStgFrameHeight/2-100, 6, 2);
		wait(60);
	}
	}}
	

	task Invincible(frame){
		SetInvincibility(enm, frame);
		
	}
	

	
task CreateShotA4(x, y, speed, angle,accel,maxspd, graphic, delay,blendtype){

PlaySoundEffect("se_tan01");
DelayShot(x, y, graphic, angle, speed, BLEND_ADD_ARGB, 3, delay);
wait(delay);
let shota1=CreateShotA2(x+speed*delay*cos(angle), y+speed*delay*sin(angle), speed, angle,accel,maxspd, graphic, 0);
ObjRender_SetBlendType(shota1,blendtype);
ObjShot_SetSpellResist(shota1,true);
ObjMove_ReflectTrueWalls(shota1,1);
}

task CreateShotA5(x, y, speed, angle,accel,maxspd, graphic, delay,blendtype){

PlaySoundEffect("se_tan01");
DelayShot(x, y, graphic, angle, speed, BLEND_ADD_ARGB, 3, delay);
wait(delay);
let shota1=CreateShotA2(x+speed*delay*cos(angle), y+speed*delay*sin(angle), speed, angle,accel,maxspd, graphic, 0);
ObjRender_SetBlendType(shota1,blendtype);
ObjShot_SetSpellResist(shota1,true);
ObjMove_ReflectFrameWalls(shota1,1);
}

task ObjMove_ReflectFrameWalls(obj, limit){
	let count = 0;
	while(!Obj_IsDeleted(obj)){
		if(ObjMove_GetX(obj) <= 0 && ObjMove_GetX(obj) >= -20 && count < limit){
			PlaySoundEffect("se_kira00");
			ObjMove_SetAngle(obj, -ObjMove_GetAngle(obj) + 180);
			count++;
		}

		if(ObjMove_GetX(obj) >= GetStgFrameWidth && ObjMove_GetX(obj) <= GetStgFrameWidth + 20 && count < limit){
			ObjMove_SetAngle(obj, -ObjMove_GetAngle(obj) + 180);
			count++;
		}
		yield;
	}
	}
	
task ObjMove_ReflectTrueWalls(obj, limit){
	let count = 0;
	while(!Obj_IsDeleted(obj)){
		if(ObjMove_GetX(obj) <= 120 && ObjMove_GetX(obj) >= 100 && count < limit){
			PlaySoundEffect("se_kira00");
			ObjMove_SetAngle(obj, -ObjMove_GetAngle(obj) + 180);
			count++;
		}

		if(ObjMove_GetX(obj) >= GetStgFrameWidth-120 && ObjMove_GetX(obj) <= GetStgFrameWidth-100 && count < limit){
			ObjMove_SetAngle(obj, -ObjMove_GetAngle(obj) + 180);
			count++;
		}
		yield;
	}
	}

task Pattern1{
	let angle = rand(130,160);
	let angle2 = rand(210,270);
	let r = 0;
	let r2 = 15;
	
	//Attack(120);
	ascent(j in 0..8){
	ascent(i in 0..15){
		let sX = GetEllipticX(EnemyX,r,r2,angle,angle2-90);
		let sY = GetEllipticY(EnemyY,r,r2,angle,angle2-45);
		let trueAngle = angle+(angle2-90);
		//LaserShot(sX, sY, trueAngle,DS_PELLET_GREEN);
		CreateShotA4(sX, sY, 0.25, trueAngle,0.03,3-0.25*j, COIN_ORANGE, 10,BLEND_ALPHA);
		CreateShotA5(sX, sY, 0.25, trueAngle,0.03,3-0.25*j, FIRE_ORANGE, 10,BLEND_ADD_ARGB);
		
	angle += 360/15;}
	
	angle2+= 45;
	wait(16);
	}
	
	ChargeUp(enm, [ObjMove_GetX(enm), ObjMove_GetY(enm)], -9,3, angle, 50, 20, [255, 0, 0], "DARK");
	wait(40);
	angle=0;
	angle2=40;
	r=0;
	r2=45;
	
	ascent(j in 0..4){
	ascent(i in 0..15){
		let sX = GetEllipticX(EnemyX,r,r2,angle,angle2-90);
		let sY = GetEllipticY(EnemyY,r,r2,angle,angle2-90);
		let trueAngle = angle+(angle2-90);
		//LaserShot(sX, sY, trueAngle,DS_PELLET_GREEN);
		CreateShotA4(sX, sY, 0.25, trueAngle,0.03,2.5, COIN_GREY, 10,BLEND_ALPHA);
		CreateShotA5(sX, sY, 0.25, trueAngle,0.03,2.5, FIRE_GREY, 10,BLEND_ADD_ARGB);
		
	angle += 360/15;}
	
	angle2+= 360/4;
	}
	
}

task Pattern2{
	let angle = rand(130,160);
	let angle2 = rand(210,260);
	let r = 85;
	let r2 = 35;
	
	//Attack(120);
	ascent(j in 0..8){
	ascent(i in 0..25){
		let sX = GetEllipticX(EnemyX,r,r2,angle,angle2-90);
		let sY = GetEllipticY(EnemyY,r,r2,angle,angle2-90);
		let trueAngle = angle+(angle2-90);
		//LaserShot(sX, sY, trueAngle,DS_PELLET_GREEN);
		CreateShotA4(sX, sY, 0.25, trueAngle,0.03,2.75-0.25*j, COIN_RED, 10,BLEND_ALPHA);
		CreateShotA5(sX, sY, 0.25, trueAngle,0.03,2.75-0.25*j, FIRE_RED, 10,BLEND_ADD_ARGB);
		
	angle += 540/25;
	wait(1);}
	r+=-10;
	angle2+= 4;
	wait(1);
	}
}

function GetEllipticX(let bX, let xLength, let yLength, let gapAngle, let dispAngle){
    return bX+xLength*cos(gapAngle)*cos(dispAngle)-yLength*sin(gapAngle)*sin(dispAngle);
}

function GetEllipticY(let bY, let xLength, let yLength, let gapAngle, let dispAngle){
    return bY+xLength*cos(gapAngle)*sin(dispAngle)+yLength*sin(gapAngle)*cos(dispAngle);
}
