//#TouhouDanmakufu
#ScriptVersion[3]
#Title["AliceS1_H"]
#Text["Bullets"]
let white = GetCurrentScriptDirectory~"sprites/white.png";

#include"./../lib/SHOT_REPLACE/shot_replace.dnh"
#include"./../includes/BossIncludes.txt"
let color2 = 1;
let color = 0;
let wave = 0;
let spawneddolls = 0;
let objEnemy;
let duration = 0;
let dollsarray = [];
let stopstuff = false;
let angle = 0;
let angle2 = 0;
let one = 1;
let type = 0;
let animframe = 0;
let frame = 0;
let objlasers = [];
let GetSpeedX = 0;
let variable = 0;
let thunder = GetCurrentScriptDirectory~"sfx/thunder.wav";
let n = 0;
let shotrate = 25;
let bombrate = 25;
let reimucut = GetCurrentScriptDirectory~"sprites/lettycut.png";

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
	SetScriptResult(1000);
	}
	case(EV_REQUEST_TIMER){
	SetScriptResult(999);
	}
	case(EV_REQUEST_SPELL_SCORE){
	SetScriptResult(10000000);
	}
	case(EV_GAIN_SPELL)
	{
	AddScore(ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE));	
	}
}

@Initialize{
	//Resource Loading
	shotinit;
	
	
	
	
	
	
	
	LoadTexture(reimucut);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(600, shotrate, bombrate);
	//BottomEnemyMarker(objEnemy, objEnemy);
	let Reimu = GetCurrentScriptDirectory~"sprites/letty.png";
	ObjPrim_SetTexture(objEnemy, Reimu); 
	ObjSprite2D_SetSourceRect(objEnemy, 0, 0, 128, 128);
	ascent(x in 0..3){
	ObjAnim_AddFrameA2(objEnemy, ANIM_IDLE, 0, 0+x*128, 128, 128+x*128, 0, 0);
	}
	ascent(x in 0..2){
	ObjAnim_AddFrameA2(objEnemy, ANIM_IDLE, 128, 0+x*128, 256, 128+x*128, 0, 0);
	}
	ascent(x in 0..2){ObjAnim_AddFrameA2(objEnemy, ANIM_MOVE_LEFT, 256, 128*x, 256+128, 128*x+128, 1, 0);}
	
	
	ascent(x in 2..4){ObjAnim_AddFrameA2(objEnemy, ANIM_MOVE_RIGHT, 256, 128*x, 256+128, 128*x+128, 0, 0);}
	
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_RIGHT, 256, 128*2, 128*3, 128*2+128, 0, 0);
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_RIGHT, 0, 0, 128, 128, 0, 0);
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_LEFT, 256, 0, 128*3, 128, 0, 0);
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_LEFT, 0, 0, 128, 128, 0, 0);
	
	ascent(x in 0..3){ObjAnim_AddFrameA2(objEnemy, ANIM_ATTACK, 128*3, 128*x, 128*4, 128*x+128, 0, 0);}
	ascent(x in 0..3){ObjAnim_AddFrameA2(objEnemy, ANIM_ATTACKEND, 128*3, 128*2-128*x, 128*4, 128*3-128*x, 0, 0);}
	
	ObjAnim_SetSpeedA1(objEnemy, ANIM_IDLE, 12);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_LEFT, 8);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_RIGHT, 8);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_ATTACK, 8);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_RIGHT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_LEFT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_ATTACK, 2);
	ObjAnim_StartAnimationA2(objEnemy, 0, 0);
		ObjSprite2D_SetDestCenter(objEnemy);  
	
	
	
LoadTexture(white);
	delayedstart;
	SetShotAutoDeleteClip(10, 10, 10, 10);
	
	
}



task delayedstart{
ObjMove_SetDestAtFrame(objEnemy, GetCenterX, 120, 60);
wait(60);

ObjEnemyBossScene_StartSpell(objScene);
ObjEnemyBossScene_SetSpellTimer(objScene, 60*49.75);

PlaySoundEffect("cardcall", 100, GetCenterX);
ObjAnim_SetAttackA2(objEnemy, 75);
ObjCutin_SetSpellcardS4("Winter's Roots &quot;Wild Dance of Freezing Mist&quot;", reimucut, "NAZRIN", 135, 206, 250);
ObjCutin_LaunchS3("NAZRIN", reimucut, "Hard");
NotifyEventAll(EV_SPELLBG, 5);
wait(75);
attack1;

}




@MainLoop{
	
	
	SetCommonData("sakuyax", ObjMove_GetX(objEnemy));
	SetCommonData("sakuyay", ObjMove_GetY(objEnemy));
	if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <=0 && stopstuff==false){stopstuff=true;}

		
if(stopstuff==true && popped==false){
DestroyBoss;
}	
	
	GetEnemyX = ObjMove_GetX(objEnemy);
	GetEnemyY = ObjMove_GetY(objEnemy);
	ObjEnemy_SetIntersectionCircleToShot(objEnemy, GetEnemyX, GetEnemyY, 25);//当たり判定(自機弾用)登録
	type++;
	ObjEnemy_SetIntersectionCircleToPlayer(objEnemy, GetEnemyX, GetEnemyY, 17);//当たり判定(体当たり用)登録
	
	frame++;
	yield;
	GetSpeedX = ObjMove_GetSpeed(objEnemy)*cos(ObjMove_GetAngle(objEnemy));
}



task attack1{	
let one = -1;
wait(90);
ChangeToAttack1;
let angle2 = 0;
let shotarray = [];
let angle3 = 0;
let radius = 40;
if(popped==true){return;}

if(popped==true){ return; }
loop{
PlaySoundEffect("charge2", 85, -1);
TCharge01(objEnemy, 3, 45, 155, 155, 255);
wait(30);
if(popped==true){ return; }
Dance(one);
wait(300);

ObjAnim_SetAttackA1(objEnemy, true);
PlaySoundEffect("charge2", 85, -1);
TCharge01(objEnemy, 3, 45, 155, 155, 255);
wait(30);
if(popped==true){ return; }
PlaySoundEffect("frostnova", 100, -1);
TExpan2(GetEnemyX, GetEnemyY, 4, 118, 141, 175, 180); potroot;
shotarray = GetShotIdInCircleA2(GetEnemyX, GetEnemyY, 90, TARGET_ENEMY);
one*=-1;
wait(60);
ObjAnim_SetAttackA1(objEnemy, false);
wait(60);
}
}


task Dance(one){
let angle = 100*one;
let angle2 = 0;
let radius = 75+55*cos(3*angle);
loop(90){
	ascent(p in 0..3){
	loop(2){
CreateShotA1(GetEnemyX+(1.25*radius)*cos(angle), GetEnemyY+radius*sin(angle), 1.9+(p/6), angle2+angle, AQUA04, 10);
angle+=360/2;
}
angle2+=360/3;
}
angle+=360/(70/2)*one;
angle2+=5*one;
radius = 75+55*cos(3*angle);
wait(4);
if(popped==true){ return; }
}
}

task potroot{
let xx = 0;
let time = 0;
let notfrozenalready = true;
loop(150){
	if(GetDistance(GetEnemyX, GetEnemyY, GetPlayerX, GetPlayerY)<=xx){
		if(GetPlayerState==STATE_NORMAL  && GetPlayerInvincibilityFrame<1 && notfrozenalready == true){
	PlaySoundEffect("frostroot", 85, GetPlayerX);
	let playx = GetPlayerX;
	let playy = GetPlayerY;
	RootPlayer(180-(xx/(128/30)), playx, playy);
		}
		notfrozenalready = false;
	}
	let shotarray = GetShotIdInCircleA2(GetEnemyX, GetEnemyY, xx, TARGET_ENEMY);
ascent(i in 0..length(shotarray)){
if(ObjMove_GetY(shotarray[i])>GetClipMaxY-25){ Obj_Delete(shotarray[i]); }
if(Obj_GetValueD(shotarray[i], "frozen", false)!=true && !Obj_IsDeleted(shotarray[i])){
ObjMove_AddPatternA3(shotarray[i], 0, 0, GetAngleToPlayer(shotarray[i]), 0, 0, 0, ObjShot_GetImageID(shotarray[i])+3);
ObjMove_AddPatternA3(shotarray[i], 120, 0, GetAngleToPlayer(shotarray[i]), 0.02, 0, 4, NO_CHANGE);
ascent(p in 1..11){
ObjMove_AddPatternA3(shotarray[i], 120+p, 0, GetAngleToPlayer(shotarray[i]), 0.02, 0, 4, NO_CHANGE);
}
Obj_SetValue(shotarray[i], "frozen", true);
}
}
	
	time++;
	xx+=128/30;
	wait(1);
	}
	task RootPlayer(plux, playx, playy){
	let timevar = 0;
	let timestring;
	let objText = ObjText_Create();
	ObjText_SetFontType(objText, "Corbel");
	ObjText_SetText(objText, "Rooted!");
	ObjText_SetFontSize(objText, 18);
	ObjText_SetFontBold(objText, true);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_CENTER);
	ObjText_SetMaxWidth(objText, 60);
	ObjText_SetFontColorTop(objText, 224, 255, 255);
	ObjText_SetFontColorBottom(objText, 128, 128, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,0, 0, 0);
	ObjText_SetFontBorderWidth(objText, 1);

	Obj_SetRenderPriority(objText, 0.9);
	ObjRender_SetX(objText, GetPlayerX);
	ObjRender_SetY(objText, GetPlayerY-45);
	ObjRender_SetColor(GetPlayerObjectID, 0, 0, 255);
	
	
	loop(plux){
	timestring = rtos("0.00", (plux-timevar)/60) ~"s";
		ObjText_SetText(objText, "Rooted!" ~ timestring);
		ObjRender_SetColor(GetPlayerObjectID, 60, 60, 255);
	ObjMove_SetPosition(GetPlayerObjectID, playx, playy);
	timevar++;
	wait(1);
	if(GetPlayerState!=STATE_NORMAL || GetPlayerInvincibilityFrame>0){ ObjRender_SetColor(GetPlayerObjectID, 255, 255, 255); return; }
	if(popped==true){ ObjRender_SetColor(GetPlayerObjectID, 255, 255, 255); Obj_Delete(objText); return; }
	}
	Obj_Delete(objText);
	ObjRender_SetColor(GetPlayerObjectID, 255, 255, 255);
	PlaySoundEffect("frostrootbreak", 85, GetPlayerX);
	break;
	}
	
	}



function TExpan2(px,py,v,r,g,b,alpha){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=GetCurrentScriptDirectory~"../sprites/charge.png";
	let scale = 0;
	let time = 0;
	
	let malpha = alpha;
	ObjPrim_SetTexture(obj,img);
	ObjRender_SetAlpha(obj, alpha);
	ObjSprite2D_SetSourceRect(obj,0,0,128,128);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,px,py,0);
	ObjRender_SetColor(obj,r,g,b);
	Obj_SetRenderPriorityI(obj,55);
	Expan;
		task Expan{
		loop{
			ObjRender_SetAlpha(obj,malpha);
			scale += v/60;
			ObjRender_SetScaleXYZ(obj,scale,scale,0);
			yield;
			time++;
			if(popped==true){
			Obj_Delete(obj);
			}
			if(time>90){ malpha-=alpha/30; ObjRender_SetAlpha(obj,malpha); }
			if(time>=120){ Obj_Delete(obj); }
			
			}
		}
	}



task ChangeToAttack1{
ascent(x in 0..3){ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACK, x, 128*4, 128*x, 128*5, 128*x+128, 0, 0);}
	ascent(x in 0..3){ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACKEND, x, 128*4, 128*2-128*x, 128*5, 128*3-128*x, 0, 0);}
}

task DestroyBoss{
NotifyEventAll(EV_SPELLBGEND, 1);
SetCommonData("timestop", false);
SetCommonData("petals", false);
popped=true;
StopSlow(TARGET_ALL);
loop(10){ yield; }
Obj_Delete(objEnemy);
loop(45){ yield; }
		CloseScript(GetOwnScriptID());
		return;
}
