#TouhouDanmakufu
#Title[Aya ShaMEEMaru ns3]
#Text [TenguShots :U]
#Player[FREE]
#ScriptVersion[2]			

script_enemy_main{
#include_function ".\1Config.txt"          
	let CSD	= GetCurrentScriptDirectory;
let angul = 180;
let angull = 180;
    let MinX	  = GetClipMinX;
    let MaxX	  = GetClipMaxX;
    let CenX	  = GetCenterX;
    let MinY	  = GetClipMinY;
    let MaxY	  = GetClipMaxY;
    let CenY	  = GetCenterY;
let powerl    = GetCurrentScriptDirectory~".\enm4se\power_logo.wav";
 	let imgBoss = CSD ~ "system\Aya_Main.png";
	let cut = CSD ~ "system\GusanoCirnoCut.png";
	let bg = CSD ~ "system\lake.png";
        let bgm = CSD ~ "bgm\bgm.mp3";
#include_function ".\newsfx.txt";
#include_function ".\Obj_hpb.txt"
#include_function ".\Cutin_Eff.txt"
#include_function ".\system\Aura_Effects.dnh"
#include_function ".\lib_anime_Aya.txt"
	@Initialize{
                LoadUserShotData(CSD ~ "kshotData.txt");
		SetLife(2500);
		SetTimer(45);
SetDamageRate(150, 30);
SetDamageRate(140, 30);
if(lifebar_activate ==1){
hpb(0,1,0);
}
if(Cutin_Activate ==1){
Timer(2,0);
}
                SetInvincibility(180);
		SetMovePosition01(GetCenterX,GetCenterY,5);
		LoadGraphic(imgBoss);
		LoadGraphic(cut);
		LoadGraphic(bg);
                //PlayMusic(bgm);
InitializeAction;
	if(Aura_Activate==1){
	Aura5;
}
		mainTask;
	}

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

	@DrawLoop{
DrawText("Aya Shameimaru", 40, 35, 12, 255);
	if(GetSpeedX()>0){SetAction(ACT_MOVE,0)}
	if(GetSpeedX()<0){SetAction(ACT_MOVE,0)}		
	DrawBoss(imgBoss);
	}

	@BackGround{
		SetTexture(bg);
		SetRenderState(ALPHA);
		SetAlpha(255);
		SetGraphicRect(0,0,63,73);
		SetGraphicScale(2,2);
		SetGraphicAngle(0,0,0);
		DrawGraphic(GetCenterX,GetCenterY);
	}

	@Finalize{
		DeleteGraphic(imgBoss);
		DeleteGraphic(cut);
		DeleteGraphic(bg);
	}

	// main task, activates stuff.
	task mainTask{
		yield;
		fire;
		movement;
	}

	task movement{
		loop{
			SetMovePositionRandom01(50,50,9,GetCenterX-80,GetCenterY-120,GetCenterX+80,GetCenterY-100);
			wait(240);
			yield;
		}
	}
	task fire{
//StartMapleConcentrate;
SetColor(255,0,0);
Concentration01(60);
SetColor(255,255,255);
PlaySE(charge);
wait(60);

shotA;
		loop{
PlaySE(shot5);
CreateReflectShot02(GetX,GetY,0.1,rand(180,360),0.2,7,202,0,14,1,0);
CreateReflectShot02(GetX,GetY,0.1,rand(180,360),0.2,7,202,0,14,1,0);
CreateReflectShot02(GetX,GetY,0.1,rand(180,360),0.2,7,202,0,14,1,0);
CreateReflectShot02(GetX,GetY,0.1,rand(180,360),0.2,7,202,0,14,1,0);
wait(60);
yield;
}
	}
let dispoot =0;
let duspo =0;
task shotA{
loop{
PlaySE(shot2);
CircleShot(GetX,GetY,20,0,15,dispoot,5,1,0);
dispoot += 22;
wait(6);
yield;
  }
}
	// wait function
	function wait(w){
		loop(w){yield;}
	}
 function CircleShot(x,y,radius,angle,bullets,shotangle,speed,graphic,delay){
loop( bullets )
{
CreateShot01(x+radius*cos(angle),y+radius*sin(angle),speed,shotangle,graphic,delay);
angle+=360/bullets;
}
}
function CreateReflectShot02(PosX,PosY,Speed,Angle,Accelerate,MaxSpeed,Graphic,Delay,Wall,Count,Frame) {
  	let Obj = Obj_Create(OBJ_SHOT);
  	
  	
  	Obj_SetPosition(Obj,PosX,PosY);
  	Obj_SetSpeed(Obj,Speed);
  	Obj_SetAngle(Obj,Angle);
  	ObjShot_SetGraphic(Obj,Graphic);
  	ObjShot_SetDelay(Obj,Delay);
  	
  	
  	Obj_SetAutoDelete(Obj,false);
  	
  	
  	CreateReflectShot02Task(Obj,Accelerate,MaxSpeed,Wall,Count,Frame);
  	
  	
  	return Obj;
  }
  

  task CreateReflectShot02Task(Obj,Accelerate,MaxSpeed,Wall,argCount,argFrame) {
  	let Count = argCount;
  	let CountFlag = (Count > 0);
  	let Frame = argFrame;
  	let FrameFlag = (Frame > 0);
  	let Top = (floor(Wall / 8) == 1);
  	let Left = (floor((Wall % 8) / 4) == 1);
  	let Right = (floor((Wall % 4) / 2) == 1);
  	let Bottom = (Wall % 2 == 1);
  	let X = Obj_GetX(Obj);
  	let Y = Obj_GetY(Obj);
  	let Speed = Obj_GetSpeed(Obj);
  	let Angle = Obj_GetAngle(Obj);
  	

  	
  	
  	let OneStepX = cos(Angle) * Speed;
  	let OneStepY = sin(Angle) * Speed;
  	

  	let InFrameX = (GetClipMinX - X) / OneStepX;
  	let OutFrameX = (GetClipMaxX - X) / OneStepX;
  	if (InFrameX > OutFrameX) {
  		let Work = InFrameX;
  		InFrameX = OutFrameX;
  		OutFrameX = Work;
  	}
  	

  	let InFrameY = (GetClipMinY - Y) / OneStepY;
  	let OutFrameY = (GetClipMaxY - Y) / OneStepY;
  	if (InFrameY > OutFrameY) {
  		let Work = InFrameY;
  		InFrameY = OutFrameY;
  		OutFrameY = Work;
  	}
  	
  	
  	if ((InFrameX > OutFrameY)||(InFrameY > OutFrameX)) {
  		Obj_Delete(Obj);
  	}
  	
  	// 弾が削除されるか、画面内に入るまで待ちます。
  	while (Obj_BeDeleted(Obj) == false) {
  		X = Obj_GetX(Obj);
  		Y = Obj_GetY(Obj);
  		Speed = Obj_GetSpeed(Obj);
  		
  		// 弾が画面内に入った場合、ループを抜けます。
  		if ((X >= GetClipMinX)&&(X <= GetClipMaxX)&&(Y >= GetClipMinY)&&(Y <= GetClipMaxY)) {
  			break;
  		}
  		

  		if (Accelerate > 0) {
  			if (Speed + Accelerate < MaxSpeed) {
  				Obj_SetSpeed(Obj,Speed + Accelerate);
  			}
  		} else if (Accelerate < 0) {
  			if (Speed + Accelerate > MaxSpeed) {
  				Obj_SetSpeed(Obj,Speed + Accelerate);
  			}
  		}
  		
  		yield;
  		
  		
  		if (FrameFlag == true) {
  			Frame -= 1;
  			if (Frame <= 0) {
  				Obj_Delete(Obj);
  			}
  		}
  	}
  	
  
  	while (Obj_BeDeleted(Obj) == false) {
  		X = Obj_GetX(Obj);
  		Y = Obj_GetY(Obj);
  		Speed = Obj_GetSpeed(Obj);
  		Angle = Obj_GetAngle(Obj);
  		
  		
  		if (Y < GetClipMinY) {
PlaySE(kira);
CircleShot(Obj_GetX(Obj),Obj_GetY(Obj),50,0,30,90,3,2,20);
  			if ((Top == true)&&((Count > 0)||(CountFlag == false))) {
  				Y = (GetClipMinY * 2) - Y;
  				Angle = 0 - Angle;
  				Count -= 1;
  			} else {
  				Obj_Delete(Obj);
  			}
  		}
  		if (Y > GetClipMaxY) {
//CircleShot(Obj_GetX(Obj),Obj_GetY(Obj),50,0,30,90,3,2,20);
  			if ((Bottom == true)&&((Count > 0)||(CountFlag == false))) {
  				Y = (GetClipMaxY * 2) - Y;
  				Angle = 0 - Angle;
  				Count -= 1;
  			} else {
  				Obj_Delete(Obj);
  			}
  		}
  		if (X < GetClipMinX) {
PlaySE(kira);
CircleShot(Obj_GetX(Obj),Obj_GetY(Obj),50,0,30,90,3,2,20);
  			if ((Left == true)&&((Count > 0)||(CountFlag == false))) {
  				X = (GetClipMinX * 2) - X;
  				Angle = 180 - Angle;
  				Count -= 1;
  			} else {
  				Obj_Delete(Obj);
  			}
  		}
  		if (X > GetClipMaxX) {
  			if ((Right == true)&&((Count > 0)||(CountFlag == false))) {
PlaySE(kira);
CircleShot(Obj_GetX(Obj),Obj_GetY(Obj),50,0,30,90,3,2,20);
  				X = (GetClipMaxX * 2) - X;
  				Angle = 180 - Angle;
  				Count -= 1;
  			} else {
  				Obj_Delete(Obj);
  			}
  		}
  		

  		if (Accelerate > 0) {
  			if (Speed + Accelerate < MaxSpeed) {
  				Obj_SetSpeed(Obj,Speed + Accelerate);
  			}
  		} else if (Accelerate < 0) {
  			if (Speed + Accelerate > MaxSpeed) {
  				Obj_SetSpeed(Obj,Speed + Accelerate);
  			}
  		}
  		
  		Obj_SetPosition(Obj,X,Y);
  		Obj_SetAngle(Obj,Angle);
  		
  		yield;
  		
 
  		if (FrameFlag == true) {
  			Frame -= 1;
  			if (Frame <= 0) {
  				Obj_Delete(Obj);
  			}
  		}
  	}
  }

}