#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);
	SetEnemyMarker(true);
	SetDamageRate(50,5);
	SetTimer(311);
	//CutIn(YOUMU,"Vague Recollection -- Icicle Fall",SatoriCutin,0,0,256,512);
	//let a=0;
	SetEffectForZeroLife(60,0,0);	
	

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

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

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,x1,y1,x2,y2,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)*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)-cos(ba1)*x1-sin(ba1)*y1)<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)*x2-sin(ba2)*y2)<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,offset){
	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+offset)*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,x1,y1,x2,y2,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)*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)-cos(ba1)*x1-sin(ba1)*y1)-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)*x2-sin(ba2)*y2)-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)-cos(ba1)*x1-sin(ba1)*y1)<ti|| 
		absolute(cos(ba2)*Obj_GetX(obj)+sin(ba2)*Obj_GetY(obj)-cos(ba2)*x2-sin(ba2)*y2)<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);
	Obj_SetCollisionToPlayer(obj,false);
	Obj_SetCollisionToPlayer(obj2,false);
	ObjLaser_SetWidth(obj,10);
	ObjLaser_SetLength(obj,500);
	ObjLaser_SetWidth(obj2,10);
	ObjLaser_SetLength(obj2,500);
	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)*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-30){Obj_Delete(obj);Obj_Delete(obj2);}
		
	}
}

task MainTask
{
Wait(120);
let p1=0;
let p2=0;
let p3=0;
let a=0;
let t=0;
let cj=1;
let d=250;
let w=0;
let v=180;
let r=6;
let R=160;
loop
	{r=4;
	LaserWall(GetX-R,GetY,w,d,4);
	LaserWall(GetX+R,GetY,v,d,4);
	loop(d/r){t=t+r;a+=11;
	ascent(j in 1..1+cj){ascent(i in 1..5){PlaySE(shot2);
	DoubleWallBouncing(GetX,GetY,90*i+a,0+0.5*(i%2)+j+t/120,15,t,w,v,GetX-R,GetY,GetX+R,GetY,d);}}
	
	loop(r){yield;}
	}
	t=0;
	SetMovePosition03(rand(200,248),rand(70,150),20,4);
	loop(120){yield;}
	
	
	}
}

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
	{
	DeleteGraphic(SatoriSprite);
	}

}