////#TouhouDanmakufu
#ScriptVersion[3]
#Title["SakuyaN1_H"]
#Text["Bullets"]


#include"./../lib/SHOT_REPLACE/shot_replace.dnh"
#include"./../includes/BossIncludes.txt"

let objEnemy;
let duration = 0;
let stopstuff = false;
let angle = 0;
let angle2 = 0;
let animframe = 0;
let frame = 0;
let GetSpeedX = 0;
let variable = 0;
let n = 0;
@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
	SetScriptResult(1);
	}
	case(EV_REQUEST_TIMER){
	SetScriptResult(9999);
	}
}


@Initialize{
	//Resource Loading
	shotinit;
	
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(180, 60, 75);
	SetCommonData("alive", true);
	let Reimu = GetCurrentScriptDirectory~"sprites/cirno.png";
	ObjPrim_SetTexture(objEnemy, Reimu); 
	ObjSprite2D_SetSourceRect(objEnemy, 0, 0, 128, 128);
	ascent(x in 0..4){
	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..2){ObjAnim_AddFrameA2(objEnemy, ANIM_ATTACK, 128*5, 128*x, 128*6, 128*x+128, 0, 0);}
	ascent(x in 0..2){ObjAnim_AddFrameA2(objEnemy, ANIM_ATTACKEND, 128*5, 128*1-128*x, 128*6, 128*2-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, 6);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_RIGHT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_LEFT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_ATTACK, 1);
	ObjAnim_StartAnimationA2(objEnemy, 0, 0);
		ObjSprite2D_SetDestCenter(objEnemy);  
	//BottomEnemyMarker(objEnemy, objEnemy);
	ObjMove_SetPosition(objEnemy, GetStgFrameWidth+50, -20);
	
	
	attack1;
	SetShotAutoDeleteClip(20, 40, 20, 20);
	
}


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 ChangeToSpell{

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


@MainLoop{
	
	
	if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <=0 && stopstuff==false){stopstuff=true;}

	GetEnemyX = ObjMove_GetX(objEnemy);
	GetEnemyY = ObjMove_GetY(objEnemy);

	frame++;
	yield;
	GetSpeedX = ObjMove_GetSpeed(objEnemy)*cos(ObjMove_GetAngle(objEnemy));
	SetCommonData("sakuyax", ObjMove_GetX(objEnemy));
	SetCommonData("sakuyay", ObjMove_GetY(objEnemy));




if(stopstuff==true && popped==false){
DestroyBoss;
}	
	
}

task DestroyBoss{
popped=true;
SetCommonData("alive", false);
loop(10){ yield; }
Obj_Delete(objEnemy);
loop(20){ yield; }
		CloseScript(GetOwnScriptID());
		return;
}


function ExMoveInstant { ExMoveA(GetClipMinY+120, 10, rand(40, 60)); }
function ExMoveA(height, ud, length) {
if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0){
	if(GetPlayerX < GetEnemyX) {
		if(GetEnemyX - length <= GetClipMinX+20) {
			ObjMove_SetPosition(objEnemy, GetEnemyX + length+20, GetClipMinY+120);
		} else {
			ObjMove_SetPosition(objEnemy, GetEnemyX - length-20, GetClipMinY+120);
		}
	} else {
		if(GetEnemyX + length >= GetClipMaxX-20) {
			ObjMove_SetPosition(objEnemy, GetEnemyX - length-20, GetClipMinY+120);
		} else {
			ObjMove_SetPosition(objEnemy, GetEnemyX + length+20, GetClipMinY+120);
		}
	}
}
}

task thin{
ascent(i in 0..7){
ObjRender_SetScaleXYZ(objEnemy, 1-(i/7), 1+(i/7), 1);
yield;
}
descent(i in 0..7){
ObjRender_SetScaleXYZ(objEnemy, 1-(i/7), 1+(i/7), 1);
yield;
}
}

task attack1{
let blah = false;
let circlemade = false;
loop{

if(GetCommonData("moveintoplace", false)==true){
	ObjMove_SetDestAtWeight(objEnemy, GetCenterX, 120, 20, 2);
	}

	if(GetCommonData("cirnodie", false)==true){
	ObjEnemy_SetLife(objEnemy, 0);
	}
	
	yield;

}
}