#TouhouDanmakufu
#Title[SatoriNonSpell H]
#Text[Nonspell]
#BackGround[User(.\img\stage.png,0,-0.5)]
#PlayLevel[Extra]
#Player[FREE]
#BGM[.\HeartfeltFancy.mp3]
#ScriptVersion[2]

script_enemy_main
{
#include_function ".\Library.dnh"

let SatoriSprite = csd~"img/Satori/SatoriSprite.png";
let SatoriCutin = csd~"img/Satori/SatoriCutin.png";
let SpellBG01 = csd~"img/Satori/SpellBG01.png";
let SpellBG02 = csd~"img/Satori/SpellBG02.png";
let shot1=csd~"se/shot1.wav";
let shot2=csd~"se/shot2.wav";
let shot3=csd~"se/shot3.wav";
let shot4=csd~"se/shot4.wav";
let spell=csd~"se/spellcard.wav";
let laser=csd~"se/lazer.wav";
let charge=csd~"se/charge2.wav";

@Initialize
	{
	LoadUserShotData(csd~"Shot.txt");
	LoadGraphic(SatoriSprite);
	LoadGraphic(SpellBG01);
	LoadGraphic(SpellBG02);
	LoadSE(shot1);
	LoadSE(shot2);
	LoadSE(shot3);
	LoadSE(shot4);
	LoadSE(spell);
	LoadSE(laser);
	LoadSE(charge);
	SetLife(2000);
	SetScore(100);
	SetEnemyMarker(true);
	SetDamageRate(5,0);
	SetTimer(53);
	SetDurableSpellCard;
	CutIn(YOUMU,"Rocks Memory -- Time Overhaul",SatoriCutin,0,0,256,512);
	//let a=0;
	SetEffectForZeroLife(60,0,0);	
	

MainTask;
Vulnerable;
Timer;
IdleSatori;
//MoveAbout;
SetMovePosition03(GetCenterX, GetCenterY - 30, 30,10);
}

task Vulnerable
{
Wait(600);
SetDamageRate(50,0);
}

task Icicle(s,a,k){
let obj=Obj_Create(OBJ_SHOT);
	Obj_SetX(obj,GetX);
	Obj_SetY(obj,GetY);
	Obj_SetSpeed(obj,s);
	Obj_SetAngle(obj,a);
	ObjShot_SetGraphic(obj,94);
	ObjShot_SetDelay(obj,0);
	let ti=30;
	let u=-1;
	let X=GetPlayerX;
	let Y=GetPlayerY;
	while(!Obj_BeDeleted(obj)){
		
		yield;
		ti--;
		u--;
		if(ti==0){Obj_SetSpeed(obj,0);
				Obj_SetAngle(obj,a+90*k);}
		if(ti==-120){Obj_SetSpeed(obj,s)};
	}
}

task Icicle2(s,a,k){
let obj=Obj_Create(OBJ_SHOT);
	Obj_SetX(obj,GetX);
	Obj_SetY(obj,GetY);
	Obj_SetSpeed(obj,s);
	Obj_SetAngle(obj,a);
	ObjShot_SetGraphic(obj,94);
	ObjShot_SetDelay(obj,0);
	let ti=30;
	let u=-1;
	let X=GetPlayerX;
	let Y=GetPlayerY;
	while(!Obj_BeDeleted(obj)){
		
		yield;
		ti--;
		u--;
		if(ti==0){Obj_SetSpeed(obj,0);
				Obj_SetAngle(obj,a+90*k);}
		if(ti==-120){Obj_SetSpeed(obj,s)};
		if(ti==floor(-150+30*sin(a))){Obj_SetAngle(obj,a+180*k)};
	}
}


task IcicleFall(s){
let a=-40;
loop{
	loop(24){
		loop(10){yield};
		ascent(i in 6..12){Icicle(s*i/2,a,1);Icicle(s*i/2,180-a,-1);}
		a+=5;}
	a=-40;
	loop(120){yield;}
	
	loop(30){
		loop(10){yield};
		ascent(i in 6..12){Icicle2(s*i/2,a,1);Icicle2(s*i/2,180-a,-1);}
		a+=5;}
	a=-40;
	loop(120){yield;}
	}
}

task DoubleWallBouncing(x,y,a,s,t,d,ba1,ba2,bd){
	let obj=Obj_Create(OBJ_SHOT);
	Obj_SetX(obj,GetX);
	Obj_SetY(obj,GetY);
	Obj_SetSpeed(obj,s);
	Obj_SetAngle(obj,a);
	ObjShot_SetGraphic(obj,t);
	ObjShot_SetDelay(obj,0);
	let ti=(d+30)*0.5;
	let u=0;
	let X=GetPlayerX;
	let Y=GetPlayerY;
	while(!Obj_BeDeleted(obj)){
		yield;
		ti=ti+0.5;
		u--;
		if(ti<=(bd-30)*0.5){
		if(absolute(cos(ba1)*Obj_GetX(obj)+sin(ba1)*Obj_GetY(obj))<ti+s && sin(Obj_GetAngle(obj)-ba1-90)>0){
		Obj_SetAngle(obj,-Obj_GetAngle(obj)+2*ba1+180);
		u=4;
		}
		if(absolute(cos(ba2)*Obj_GetX(obj)+sin(ba2)*Obj_GetY(obj)-cos(ba2)*448-sin(ba2)*480)<ti+s && sin(Obj_GetAngle(obj)-ba2-90)>0){
		Obj_SetAngle(obj,-Obj_GetAngle(obj)+2*ba2+180);
		u=4;
		}
		}
	}
}

task DoubleWallBouncing2(x,y,a,s,t,d,ba1,ba2,bd){
	let obj=Obj_Create(OBJ_SHOT);
	Obj_SetX(obj,GetX);
	Obj_SetY(obj,GetY);
	Obj_SetSpeed(obj,s);
	Obj_SetAngle(obj,a);
	ObjShot_SetGraphic(obj,t);
	ObjShot_SetDelay(obj,0);
	let ti=(d+30)*0.5;
	let u=0;
	let X=GetPlayerX;
	let Y=GetPlayerY;
	while(!Obj_BeDeleted(obj)){
		yield;
		ti=ti+0.5;
		u--;
		if(ti<=(bd-30)*0.5){
		if(absolute(cos(ba1)*Obj_GetX(obj)+sin(ba1)*Obj_GetY(obj)-sin(ba1)*480)<ti+s && sin(Obj_GetAngle(obj)-ba1-90)>0){
		Obj_SetAngle(obj,-Obj_GetAngle(obj)+2*ba1+180);
		u=4;
		}
		if(absolute(cos(ba2)*Obj_GetX(obj)+sin(ba2)*Obj_GetY(obj)-cos(ba2)*448)<ti+s && sin(Obj_GetAngle(obj)-ba2-90)>0){
		Obj_SetAngle(obj,-Obj_GetAngle(obj)+2*ba2+180);
		u=4;
		}
		}
	}
}


task DoubleWallExtBouncing(x,y,a,s,t,d,ba1,ba2,bd){
	let obj=Obj_Create(OBJ_SHOT);
	Obj_SetX(obj,GetX);
	Obj_SetY(obj,GetY);
	Obj_SetSpeed(obj,s);
	Obj_SetAngle(obj,a);
	ObjShot_SetGraphic(obj,t);
	ObjShot_SetDelay(obj,0);
	let ti=(d+30)*0.5;
	let u=0;
	let X=GetPlayerX;
	let Y=GetPlayerY;
	while(!Obj_BeDeleted(obj)){
		yield;
		ti=ti+0.5;
		if(ti<=(bd-30)*0.5){
		if(absolute(absolute(cos(ba1)*Obj_GetX(obj)+sin(ba1)*Obj_GetY(obj))-ti)<s && sin(Obj_GetAngle(obj)-ba1-90)<0 && u>0){
		Obj_SetAngle(obj,-Obj_GetAngle(obj)+2*ba1+180);
		}
		if(absolute(absolute(cos(ba2)*Obj_GetX(obj)+sin(ba2)*Obj_GetY(obj)-cos(ba2)*448-sin(ba2)*480)-ti)<s && sin(Obj_GetAngle(obj)-ba2-90)<0 && u>0){
		Obj_SetAngle(obj,-Obj_GetAngle(obj)+2*ba2+180);
		}
		if(absolute(cos(ba1)*Obj_GetX(obj)+sin(ba1)*Obj_GetY(obj))<ti|| 
		absolute(cos(ba2)*Obj_GetX(obj)+sin(ba2)*Obj_GetY(obj)-cos(ba2)*448-sin(ba2)*480)<ti){
		if(Obj_GetX(obj)<24 && cos(Obj_GetAngle(obj))<0){Obj_SetAngle(obj,-Obj_GetAngle(obj)+180);u++}
		if(Obj_GetY(obj)<24 && sin(Obj_GetAngle(obj))<0){Obj_SetAngle(obj,-Obj_GetAngle(obj)+0);u++}
		if(Obj_GetX(obj)>424 && cos(Obj_GetAngle(obj))>0){Obj_SetAngle(obj,-Obj_GetAngle(obj)+180);u++}
		if(Obj_GetY(obj)>456 && sin(Obj_GetAngle(obj))>0){Obj_SetAngle(obj,-Obj_GetAngle(obj)+0);u++}
		}
		}
	}
}

task LaserWall(x,y,a,d,t){
	let obj=Obj_Create(OBJ_LASER);
	let obj2=Obj_Create(OBJ_LASER);
	ObjLaser_SetWidth(obj,10);
	ObjLaser_SetLength(obj,400);
	ObjLaser_SetWidth(obj2,10);
	ObjLaser_SetLength(obj2,400);
	Obj_SetX(obj,x);
	Obj_SetY(obj,y);
	Obj_SetX(obj2,x);
	Obj_SetY(obj2,y);
	Obj_SetSpeed(obj,0);
	Obj_SetAngle(obj,a+90);
	Obj_SetSpeed(obj2,0);
	Obj_SetAngle(obj2,a-90);
	ObjShot_SetGraphic(obj,t);
	ObjShot_SetGraphic(obj2,t);
	ObjShot_SetDelay(obj,0);
	let ti=(0+30)*0.5;
	let u=0;
	let X=GetPlayerX;
	let Y=GetPlayerY;
	while(!Obj_BeDeleted(obj)){
		yield;
		ti=ti+0.5;
		u++;
		Obj_SetX(obj,x+ti*cos(a));
		Obj_SetY(obj,y+ti*sin(a));
		Obj_SetX(obj2,x+ti*cos(a));
		Obj_SetY(obj2,y+ti*sin(a));
		if(u==d-60){Obj_Delete(obj);Obj_Delete(obj2);}
		
	}
}

task Distant(let x,let y,S,a,t,d,let life){
	let obj=Obj_Create(OBJ_SHOT);
	ObjShot_SetBombResist(obj, true);
	let X=x;
	let Y=y;
	let extension=0;
	let s=S;
	let ch=0;
    while(((X-448*floor(X/448))>1) && ((Y-480*floor(Y/480))>1)){extension=extension+1/s;X=X-cos(a);Y=Y-sin(a);}
	life=life+extension;
	x=X;y=Y;
	
	while(X<0 || X>448){
	if(X<0){X=-X;}
	if(X>448){X=2*448-X;}
	}
	
	while(Y<0 || Y>480){
	if(Y<0){Y=-Y;}
	if(Y>480){Y=2*480-Y;}
	}
	Obj_SetX(obj,X);
	Obj_SetY(obj,Y);
	Obj_SetSpeed(obj,0);
	Obj_SetAngle(obj,a);
	ObjShot_SetGraphic(obj,t);
	ObjShot_SetDelay(obj,d);
	let c=0;
	while(!Obj_BeDeleted(obj)){
		extension++;
		yield;
		if(extension==660){PlaySE(shot2)}
		if(extension>660){Obj_SetSpeed(obj,s);c++;
		
		X=x+cos(a)*s*c;
		Y=y+sin(a)*s*c;
		while(X<0 || X>448){
	if(X<0){X=-X;}
	if(X>448){X=2*448-X;}
	}
	while(Y<0 || Y>480){
	if(Y<0){Y=-Y;}
	if(Y>480){Y=2*480-Y;}
	}
	Obj_SetX(obj,X);
	Obj_SetY(obj,Y);
		
	if(c>=life){Obj_Delete(obj);}}
	}
}


task MainTask
{PlaySE(spell);
Wait(120);
let a=0;
let t=0;
let d=600;
let v=0;
loop
	{ascent(i in 0..30){Distant(GetPlayerX+1800*cos(360/30*i),GetPlayerY+1800*sin(360/30*i),1.5,360/30*i+180,11+v,0,1200)}
	ascent(i in 0..45){Distant(GetPlayerX+1200*cos(360/45*i),GetPlayerY+1200*sin(360/45*i),1,360/45*i+180,15+v,0,1200)}
	ascent(i in 0..15){Distant(GetPlayerX+2400*cos(360/15*i),GetPlayerY+2400*sin(360/15*i),2,360/15*i+180,22+v,0,1200)}
	loop(1200){yield;}
	v=0;
	}
}

task MoveAbout
{
SetMovePosition02(GetCenterX,GetCenterY-120,60);
MoveSatori;
loop
	{
	Wait(250);
	SetMovePositionRandom01(64,64,1,GetClipMinX+64,GetClipMinY+64,GetClipMaxX-64,GetClipMinY+128);
	MoveSatori;
	Wait(150);
	}
}

@MainLoop
	{
	//SetCollisionA(GetX,GetY,32);
	//SetCollisionB(GetX,GetY,32);
	yield;
	}



@DrawLoop
	{
	SetAlpha(255);
	if (bossflip==0){SetGraphicScale(1,1);}
	if (bossflip==1){SetGraphicScale(-1,1);}
	SetColor(255,255,255);
	SetGraphicAngle(0,0,0);
	SetTexture(SatoriSprite);
	SetGraphicRect(BR[0],BR[1],BR[2],BR[3]);
	DrawGraphic(GetX,GetY);
	}

	let bgalpha = 0;
let bgangle = 0;
let bgscroll = 0;

@BackGround
	{
	if (bgalpha < 255){bgalpha += 5;}
	bgangle += 0.3;
	bgscroll += 1;

	SetAlpha(bgalpha);
	SetGraphicScale(1,1);
	SetColor(255,255,255);

	SetTexture(SpellBG01);
SetGraphicRect(0,0,512+bgscroll,512);

	DrawGraphic(GetCenterX,GetCenterY);

	SetTexture(SpellBG02);
	SetGraphicRect(0,0,256,256);
	SetGraphicAngle(0,0,bgangle);
	DrawGraphic(GetCenterX,GetCenterY);
	}
	
@Finalize
	{
	if(GotSpellCardBonus==true){
		
			CreateItem(ITEM_SCORE,GetX(),GetY());
			
		}
	DeleteGraphic(SatoriSprite);
	}

}