#TouhouDanmakufu[Single]
#System["./system/Default_System.txt"]
#Title["Hina Spell 2"]
#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(6600*3);}
	case(EV_REQUEST_TIMER){SetScriptResult(66);}
	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(480);
	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("Misfortune \"The Eyes That Saw Uninvited Danmaku\" ", 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;
	
	let D=140;
	let FixAngle=14;
	let AngleFix=4;
	let spd=1;
		ChargeUp(enm, [ObjMove_GetX(enm), ObjMove_GetY(enm)], -9,3, angle, 50, 20, [255, 0, 0], "DARK");
	wait(40);
	TCharge01(EnemyX,EnemyY,4,30,182, 3, 252);
	wait(30);
	PlaySoundEffect("se_enep02");
	MapleExplode(50,2,2,8);
	ExplosionA2(enm, "purple");
	IJNFlag;
	Obj_SetValue(enm,"cast",1);
		PlaySoundEffect("se_boon01");
		ascent(i in 0..2){
			let hinafam= fam(EnemyX,EnemyY,1,false,FixAngle);
			FixAngle*=-1;
			ObjMove_SetDestAtWeight(hinafam,GetStgFrameWidth/2-D, GetStgFrameHeight/2, 6, 3.5);
			spd*=-1;
			D*=-1;
		}
		wait(240);
		
		loop{
		ExplosionA2(enm, "green");
		PlaySoundEffect("se_enep02");
		ascent(j in 0..4){
		let AngAim=rand(0,360);
		ascent(i in 0..4){
		PatternHina1(AngAim,AngleFix);
		AngAim+=360/4;}
		AngleFix*=-1;
		wait(210);}
		
		wait(45);
		PatternHina2;
		
		wait(420);
		TMovement(15,15,2);
		wait(60);
		}
		
	}}



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

	task PatternHina2{
	let angle=rand(0,360);
	let r=55;

	ascent(k in 0..20){
		PlaySoundEffect("se_kira00");
		ascent(j in 0..14){
			ascent(i in -1..2){
				CreateShotAHina2(EnemyX+r*cos(angle),EnemyY+r*sin(angle),0,angle+3/3*i+180,ARROWHEAD_OLIVE,15,BLEND_ALPHA);}
				angle+=360/14;
				}
			r+=-2;
			angle+=15;
			wait(15);
	}
	wait(90);
	ExplosionA2(enm, "green");
	ascent(k in 0..10){
		PlaySoundEffect("se_enep02");
		ascent(j in 0..12){
			ascent(i in -1..2){
				CreateShotAHina3(EnemyX,EnemyY,4.25-0.5*k,angle+3/3*i,-0.05,0,ARROWHEAD_AZURE,10,BLEND_ALPHA);}
				angle+=360/12;
				}
			r+=-3;
			angle+=21;
		
	}
	
	task CreateShotAHina2(x, y, speed, angle,graphic, delay,blendtype){

DelayShot(x, y, graphic, angle, speed, BLEND_ADD_ARGB, 3, delay);
wait(delay);
let shota1=CreateShotA1(x+speed*delay*cos(angle), y+speed*delay*sin(angle), speed, angle,graphic, 0);
ObjShot_SetSpellResist(shota1,true);
ObjRender_SetBlendType(shota1,blendtype);
ObjMove_AddPatternA2(shota1,60,NO_CHANGE,angle+85,0.1,0,5);
//ObjMove_AddPatternA2(shota1,100,NO_CHANGE,NO_CHANGE,-0.02,0,1.25);
}

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

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);
ObjShot_SetSpellResist(shota1,true);
ObjRender_SetBlendType(shota1,blendtype);
ObjMove_AddPatternA2(shota1,140,NO_CHANGE,angle,0.1,0,5);
//ObjMove_AddPatternA2(shota1,100,NO_CHANGE,NO_CHANGE,-0.02,0,1.25);
}
	}
	
	
task PatternHina1(angle,angFix){

//let angle=GetAngleToPlayer(enm)-20;
let cdr=20;
angle+=21*6/c;
	Obj_SetValue(enm,"cast",1);
	
		ascent(j in 0..15){
			PlaySoundEffect("se_kira00");
			ascent(r in 0..j+1){
				
				CreateShotAHina1(ObjMove_GetX(enm), ObjMove_GetY(enm), 0.45+j*0.175, angle-j*2+r*4, FIRE_GREEN, 15,BLEND_ALPHA_INV);}
				if(cdr<=6){cdr=6;}
				angle+=angFix;
				cdr+=-1;
				wait(cdr);
				}
				

	task CreateShotAHina1(x, y, speed, angle,graphic, delay,blendtype){

DelayShot(x, y, graphic, angle, speed, BLEND_ADD_ARGB, 3, delay);
wait(delay);
let shota1=CreateShotA1(x+speed*delay*cos(angle), y+speed*delay*sin(angle), speed, angle,graphic, 0);
ObjRender_SetBlendType(shota1,blendtype);

}
}

	function fam(x,y,life,collision,FixAngle){
	let obj = ObjEnemy_Create(OBJ_ENEMY);
	let shot;
	let c = 0;

	ObjEnemy_Regist(obj);
	ObjMove_SetPosition(obj,x,y);
	ObjEnemy_SetLife(obj,life);
	ObjEnemy_SetDamageRate(obj,100,100);
	renderHinafam(obj);
	Control;
	Shot(obj,FixAngle);
	return obj;

		task Control{	
			//PlaySound("fam",100,0);
			while(!Obj_IsDeleted(obj)){
			c++;
			
			if(ObjEnemy_GetInfo(obj,INFO_LIFE) <=0){
			//PlaySound("famdie",90,0);
			//TExplosionA(ObjMove_GetX(obj),ObjMove_GetY(obj),10,0.3);
			Obj_Delete(obj);
			}
		
			if(collision==true){ObjEnemy_SetIntersectionCircleToShot(obj,ObjMove_GetX(obj),ObjMove_GetY(obj),24);
			ObjEnemy_SetIntersectionCircleToPlayer(obj,ObjMove_GetX(obj),ObjMove_GetY(obj),30);}
			yield;
			if(Obj_IsDeleted(enm)){Obj_Delete(obj);}
			}
		}


	}
	
task Shot(obj,FixAngle){
	wait(90);
	PlaySoundEffect("se_ch00");
	MiniPattern;
	task MiniPattern{
	let a=80;
	let angle=0;
	let wave=0;
	while(!Obj_IsDeleted(obj)){
	ascent(i in 0..2){
		CreateShotA4(ObjMove_GetX(obj)+a*cos(angle),ObjMove_GetY(obj)+a*sin(angle), 0, angle, ICE_RED, 15,BLEND_ALPHA);
		angle+=360/2;
	}
	
	if(a<=80){a+=2*cos(wave);}
	else {a+=-3;}
	wave+=4;
	angle+=FixAngle;
	wait(9);
	}
	
	
task CreateShotA4(x, y, speed, angle,graphic, delay,blendtype){
DelayShot(x, y, graphic, angle, speed, BLEND_ADD_ARGB, 3, delay);
wait(delay);
let shota1=CreateShotA1(x+speed*delay*cos(angle), y+speed*delay*sin(angle), speed, angle,graphic, 0);
ObjRender_SetBlendType(shota1,blendtype);
ObjShot_SetSpellResist(shota1,true);
wait(60);
CreateShotA5(ObjMove_GetX(shota1), ObjMove_GetY(shota1), 0, GetAngleToPlayer(shota1),0.07,4,graphic+14, 15,BLEND_ALPHA);
Obj_Delete(shota1);
}
	}

task CreateShotA5(x, y, speed, angle,accel,maxspd,graphic, delay,blendtype){
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);
layerdown(shota1);
ObjMove_AddPatternA2(shota1,50, NO_CHANGE,NO_CHANGE,-0.03,0, 1.25);
}

}

task renderHinafam(obj){

	let wi = 96;
	let he = 135;
	
	let dir;
	let speed;
	let whichway = 0;
	let frame = 0;
	let idleframe = 0;
	let castframe = 0;
	Obj_SetValue(obj,"cast",0);

	ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "./img/hinasprite.png");
	ObjSprite2D_SetSourceRect(obj,0,0,wi,he);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,0.7,0.7,0);
	ObjRender_SetAngleXYZ(obj,0,0,0);
	ObjRender_SetColor(obj,31, 6, 84);
	//ObjRender_SetAlpha(obj,255/2);
	
	while(!Obj_IsDeleted(obj)){
		
		dir = ObjMove_GetAngle(obj);
		speed = ObjMove_GetSpeed(obj);

		if(speed == 0 && frame > 0){whichway = -1;}
		else if(speed == 0 && frame < 0){whichway = 1;}
		else if(speed == 0 && frame == 0){whichway = 0;}

		else if(cos(dir) < 0){whichway=-1;}
		else if(cos(dir) > 0){whichway=1;}
		
		if(Obj_GetValueD(obj,"cast",0) == 0 && speed == 0 && frame == 0){

			castframe = 0;
			if(idleframe >= 0 && idleframe < 10){ObjSprite2D_SetSourceRect(obj,0,0,wi,he);}
			else if(idleframe >= 10 && idleframe < 20){ObjSprite2D_SetSourceRect(obj,wi,0,wi*2,he);}
			else if(idleframe >= 20 && idleframe < 30){ObjSprite2D_SetSourceRect(obj,wi*2,0,wi*3,he);}
			else if(idleframe >= 30 && idleframe < 40){ObjSprite2D_SetSourceRect(obj,wi*3,0,wi*4,he);}
			else if(idleframe >= 40 && idleframe < 50){ObjSprite2D_SetSourceRect(obj,wi*4,0,wi*5,he);}
			
			
			
			if(idleframe <50){idleframe++;}
			else if(idleframe == 50){idleframe=10;}
			
		}
		
		else if(Obj_GetValueD(obj,"cast",0) == 1 && speed == 0 && frame == 0){
			
			idleframe=0;
			frame=0;
			if(castframe >= 0 && castframe < 7){ObjSprite2D_SetSourceRect(obj,0,   he*3,wi, he*4);}
			if(castframe >= 7 && castframe < 14){ObjSprite2D_SetSourceRect(obj,wi,	he*3,wi*2,	he*4);}
			if(castframe >= 14 && castframe < 21){ObjSprite2D_SetSourceRect(obj,wi*2,he*3,wi*3,	he*4);}
			if(castframe >= 21 && castframe < 28){ObjSprite2D_SetSourceRect(obj,wi*3,he*3,wi*4,	he*4);}
			if(castframe >= 28 && castframe < 35){ObjSprite2D_SetSourceRect(obj,wi*4,he*3,wi*5,	he*4);}
			
			if(castframe >= 35 && castframe < 42){ObjSprite2D_SetSourceRect(obj,0,he*4,wi,	he*5);}
			if(castframe >= 42 && castframe < 49){ObjSprite2D_SetSourceRect(obj,wi,he*4,wi*2,	he*5);}
			if(castframe >= 49 && castframe < 56){ObjSprite2D_SetSourceRect(obj,wi*2,he*4,wi*3,	he*5);}
			if(castframe >= 56 && castframe < 63){ObjSprite2D_SetSourceRect(obj,wi*3,he*4,wi*4,	he*5);}
			if(castframe >= 65 && castframe < 70){ObjSprite2D_SetSourceRect(obj,wi*4,he*4,wi*5,	he*5);}
			
			if(castframe < 70){castframe++;}
			if(castframe == 70){castframe=35;}
		}

		else if(speed != 0 || frame != 0){
		
			if(frame < 0 && frame >= -5){ObjSprite2D_SetSourceRect(obj,0,he*2,wi,he*3); }
			if(frame < -5 && frame >= -10){ObjSprite2D_SetSourceRect(obj,wi,he*2,wi*2,he*3); }
			if(frame < -10 && frame >= -15){ObjSprite2D_SetSourceRect(obj,wi*2,he*2,wi*3,he*3); }
			if(frame < -15 && frame >= -20){ObjSprite2D_SetSourceRect(obj,wi*3,he*2,wi*4,he*3);}
			//if(frame < -20 && frame >= -25){ObjSprite2D_SetSourceRect(obj,wi*3,he*2,wi*4,he*3); }
			
			
			if(frame > 0 && frame <= 5){ObjSprite2D_SetSourceRect(obj,0,he,wi,he*2); ObjRender_SetAngleXYZ(obj,0,0,0);}
			if(frame > 5 && frame <= 10){ObjSprite2D_SetSourceRect(obj,wi,he,wi*2,he*2); ObjRender_SetAngleXYZ(obj,0,0,0);}
			if(frame > 10 && frame <= 15){ObjSprite2D_SetSourceRect(obj,wi*2,he,wi*3,he*2); ObjRender_SetAngleXYZ(obj,0,0,0);}
			if(frame > 15 && frame <= 20){ObjSprite2D_SetSourceRect(obj,wi*3,he,wi*4,he*2); ObjRender_SetAngleXYZ(obj,0,0,0);}
			//if(frame > 20 && frame <= 25){ObjSprite2D_SetSourceRect(obj,wi*3,he,wi*4,he*2); ObjRender_SetAngleXYZ(obj,0,0,0);}

			idleframe=0;
			castframe=0;
			
		}
		ObjSprite2D_SetDestCenter(obj);
		frame=frame+whichway;
		if(frame > 16){frame=16;}
		if(frame < -16){frame=-16;}
		yield;
		
	}
	
}

task layerdown(obj){

while(!Obj_IsDeleted(obj)){
if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD){
ObjRender_SetAlpha(obj,255/3);
ObjShot_SetIntersectionEnable(obj,false);	

}

else{
ObjShot_SetIntersectionEnable(obj,true);	
ObjRender_SetAlpha(obj,255);}

yield;}

}