#TouhouDanmakufu
#Title[Sakuya's 2nd Spell -Normal-]
#Text[PAD- *knifed*!]
#Player[FREE]
#BGM[.\infinity.mp3]
#ScriptVersion[2]
#BackGround[User(.\stgbg.png, 0, 0)]


script_enemy_main {
	let TimeStopped = false;
	let radius = 40;
	let one = 1;
	let yy = 0;
	let cardcall = GetCurrentScriptDirectory~"cardcall.wav";
	let background = GetCurrentScriptDirectory~"spellbg2.png";
	let frame = 0;
	let frame2 = 0;
	let frame3 = 0;
	let angle = 0;
	let angle2 = 0;
	let sakuyacut = GetCurrentScriptDirectory~"sakuyacut.png";
	let shot = GetCurrentScriptDirectory~"shot.wav";
	let shot2 = GetCurrentScriptDirectory~"shot2.wav";
	let shot3 = GetCurrentScriptDirectory~"shot3.wav";
	let bounce = GetCurrentScriptDirectory~"bounce.wav";
	let explode = GetCurrentScriptDirectory~"explode.wav";
	let familiarspawn = GetCurrentScriptDirectory~"familiarspawn.wav";
	let time1 = GetCurrentScriptDirectory~"time1.wav";
	let time2 = GetCurrentScriptDirectory~"time2.wav";
	let frame = -120;
	let timer = 45;
	let Custom = GetCurrentScriptDirectory~"sup\supershot.txt";
	#include_function ".\sakuyaanimationyay.txt"
    @Initialize {
	LoadUserShotData(Custom);
	PlaySE(cardcall);
	CutIn(YOUMU, "Time Manipulation "\""Dali's Clock"\", sakuyacut, 0, 0, 180, 256);
	Spell;
	LoadGraphic(background);
	LoadGraphic(sakuyacut);
	DeleteSE("se\seEnemyExplode01.wav");
	DeleteSE("se\seUseSpellCard.wav");
        SetLife(1000);
	SetScore(101010);
        SetTimer(70);
        LoadSE(shot);
	LoadSE(bounce);
	LoadSE(time1);
	LoadSE(time2);
	LoadSE(shot2);
	LoadSE(shot3);
	LoadSE(familiarspawn);
	SetShotAutoDeleteClip(50, 50, 50, 50);
	SetEnemyMarker(true);
	SetInvincibility(120);
	SetDamageRate(25, 10);
        SetMovePosition02(GetCenterX, GetClipMinY + 120, 120);
        LoadUserShotData(Custom);
       
    }

task ModeBullet(x, y, v, angle, accel, maxspeed, graphic, delay){
		let obj = Obj_Create(OBJ_SHOT);
		let f = 0;
		
		Obj_SetPosition(obj, x, y);
		Obj_SetSpeed(obj, v);
		Obj_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetDelay(obj, delay);
		Obj_SetAutoDelete(obj, true);
		while(!Obj_BeDeleted(obj)){
			if(TimeStopped==true){
				if(f==0){
					ChangeMode(rand_int(0, 6));
					Obj_SetSpeed(obj, 0);
					f++;
					
					
				}
			}else{
				if(Obj_GetSpeed(obj)<maxspeed){
					Obj_SetSpeed(obj, Obj_GetSpeed(obj)+accel);
				}
				if(Obj_GetSpeed(obj)>=maxspeed){
					Obj_SetSpeed(obj, maxspeed);
				}
	
			}
			yield;
		}
		
		function ChangeMode(mode){
			wait(rand_int(0, 60));
			if(mode==1){
				ObjShot_SetGraphic(obj, 216);
				Obj_SetAngle(obj, atan2(GetPlayerY - Obj_GetY(obj), GetPlayerX+rand(-20, 20) - Obj_GetX(obj)));
			}
			if(mode==2 || mode==4 || mode==0){
				ObjShot_SetGraphic(obj, 218);
				Obj_SetAngle(obj, rand_int(0, 359));
			}
			if(mode==3){
				ObjShot_SetGraphic(obj, 219);
				Obj_SetAngle(obj, atan2(GetEnemyY - Obj_GetY(obj), GetPlayerX - Obj_GetX(obj)));
				Obj_SetSpeed(obj, Obj_GetSpeed(obj)+2);
			}

			if(mode==5){
			ObjShot_SetGraphic(obj, 220);
			Obj_SetAngle(obj, Obj_GetAngle(obj)-180);
		}
else{
				
			}
		}
	}


task Spell{
	wait(120);
	PlaySE(GetCurrentScriptDirectory~"charge2.wav");
	Concentration01(120);
	wait(90);
	Spam;
	wait(30);
	loop{
	wait(20);
	yy = rand(-20, 20);
	
	loop(8){
	ExMove3;
	wait(3);
	Bundles(GetEnemyY);

	wait(5);
	}
	TimeStop(60, 1, 1, 1);
	PlaySE(GetCurrentScriptDirectory~"timestop.wav");
	TimeStopped = true;
	wait(60);
	TimeStopped = false;
	wait(80);
	ExMove;
	wait(60);
	Spam;
	wait(100);
	ExMove;
	}
}

function Bundles(y){
	PlaySE(shot);
	loop(12){
	ModeBullet(GetX, GetY, 2.85, GetAngleToPlayer+angle, 0.04, 3, 215, 0);
	ModeBullet(GetX, GetY, 2.5, GetAngleToPlayer+angle, 0.04, 2.5, 215, 0);
	ModeBullet(GetX, GetY, 2, GetAngleToPlayer+angle, 0.04, 1.65, 215, 0);
	angle+=360/12;
	}
	
	 }


task Spam{
	PlaySE(shot);
	let randangle = rand(0, 360);
	loop(30){
	CreateShot01(GetX, GetY, rand(1, 3), randangle+angle, 229, 0);
	angle+=360/30;
	}
	}
	




function wait(w){ loop(w){yield;}}
 task CreateShotReflect(x, y, v, ang, graphic, delay) {
		let obj_id = Obj_Create(OBJ_SHOT); let border = 10; let  counter = 1;
		Obj_SetPosition(obj_id, x, y);
		ObjShot_SetGraphic(obj_id, graphic);
		Obj_SetSpeed(obj_id, v); Obj_SetAngle(obj_id, ang); ObjShot_SetDelay(obj_id, delay);
		while(!Obj_BeDeleted(obj_id)) {
	if(counter==-1){ ObjShot_SetGraphic(obj_id, graphic+1); } 	if(counter==0){ ObjShot_SetGraphic(obj_id, graphic+2); } 	if(counter>1){ ObjShot_SetGraphic(obj_id, graphic+3); }
			if(counter <= 1) {
				
				//if(Obj_GetX(obj_id) > GetClipMaxX+border) {  Obj_SetAngle(obj_id, atan2(GetPlayerY- Obj_GetY(obj_id), GetPlayerX - Obj_GetX(obj_id)));	Obj_SetX(obj_id, 2*(GetClipMaxX+border)-Obj_GetX(obj_id));  counter+=1; }
				 //if(Obj_GetX(obj_id) < GetClipMinX-border) { Obj_SetAngle(obj_id, atan2(GetPlayerY- Obj_GetY(obj_id), GetPlayerX - Obj_GetX(obj_id)));		Obj_SetX(obj_id, 2*(GetClipMinX-border)-Obj_GetX(obj_id));  counter+=1; }
				 if(Obj_GetY(obj_id) < GetClipMinY-border) {   Obj_SetAngle(obj_id, atan2(GetPlayerY- Obj_GetY(obj_id), GetPlayerX - Obj_GetX(obj_id)));	Obj_SetY(obj_id, 2*(GetClipMinY-border)-Obj_GetY(obj_id));  counter+=1; }
				//else if(Obj_GetY(obj_id) > GetClipMaxY+border) { Obj_SetY(obj_id, 2*(GetClipMaxY+border)-Obj_GetY(obj_id)); Obj_SetAngle(obj_id, -Obj_GetAngle(obj_id)); counter+=1; }
			}
			yield;
		}
	}
sub ExMove { ExMove_(GetCenterY-80, 50, rand(30, 60)); }
function ExMove_(height, ud, length) {
	
	if(GetPlayerX < GetX) {
		if(GetX - length <= GetClipMinX) {
			SetMovePosition03(GetX + length, rand(height+20 - ud, height + ud), 5, 1);
		} else {
			SetMovePosition03(GetX - length, rand(height - ud, height + ud), 5, 1);
		}
	} else {
		if(GetX + length >= GetClipMaxX) {
			SetMovePosition03(GetX - length, rand(height - ud, height + ud), 5, 1);
		} else {
			SetMovePosition03(GetX + length, rand(height - ud, height + ud), 5, 1);
		}
	}
}

sub ExMove2 { ExMove__(GetCenterY-80, 50, rand(30, 60)); }
function ExMove__(height, ud, length) {
	Explosion01(GetX, GetY, 12, 0.5, 20);
	if(GetPlayerX < GetX) {
		if(GetX - length <= GetClipMinX) {
			SetMovePosition03(GetX + length, rand(height+20 - ud, height + ud), 5, 10);
		} else {
			SetMovePosition03(GetX - length, rand(height - ud, height + ud), 5, 10);
		}
	} else {
		if(GetX + length >= GetClipMaxX) {
			SetMovePosition03(GetX - length, rand(height - ud, height + ud), 5, 10);
		} else {
			SetMovePosition03(GetX + length, rand(height - ud, height + ud), 5, 10);
		}
	}
}

sub ExMove3 { ExMove___(GetCenterY-120, 10, rand(30, 60)); }
function ExMove___(height, ud, length) {
	
	if(GetPlayerX < GetX) {
		if(GetX - length <= GetClipMinX) {
			SetMovePosition02(GetX + length, rand(height+20 - ud, height + ud), 1);
		} else {
			SetMovePosition02(GetX - length, rand(height - ud, height + ud), 1);
		}
	} else {
		if(GetX + length >= GetClipMaxX) {
			SetMovePosition02(GetX - length, rand(height - ud, height + ud), 1);
		} else {
			SetMovePosition02(GetX + length, rand(height - ud, height + ud), 1);
		}
	}
}

    @MainLoop {

SetCollisionA(GetX, GetY, 32);
SetCollisionB(GetX, GetY, 16);

if(TimeStopped!=true){
timer = GetTimer;
}

if(timer>GetTimer){
SetTimer(GetTimer+1);
}



frame++;
yield;
}    

    @BackGround{
	 SetTexture(background);
   SetGraphicRect(0, 0, 256, 256);
   SetGraphicScale(2.5, 2.5);
   SetGraphicAngle(0, 0, y);
   DrawGraphic(GetCenterX, GetCenterY);
	if(TimeStopped!=true){
   y+=1;
	}
	}


    @DrawLoop {
		animm;
    }




    @Finalize {
	

    }
}
