#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Koishi's panel"]
#Text["NewCutout"]

let bossObj;
let bossX = 0;
let bossY = 0;
let animFrame = 0;
let animFrame2 = 0;
let imgBoss = GetCurrentScriptDirectory ~ "Gizmo's Touhou Boss Sprite Library/spriteimg/AyaDSSprite.png";
let overlay = GetCurrentScriptDirectory ~ "texture/splbg2.png";
let lake = GetCurrentScriptDirectory ~ "texture/mountain.jpg";
let spellcardSFX = GetCurrentScriptDirectory ~ "sfx/SPELLCARD.wav";
let bdeathSFX = GetCurrentScriptDirectory ~ "sfx/DEFEATED.wav";
let snd = GetCurrentScriptDirectory ~ "./sfx/boss04a.mp3";
let objmusic = ObjSound_Create();

#include "./../shot/AllStarShot_Const.txt"
#include "script/default_system/Default_Effect.txt"
@Initialize {
	
	// define a boss in bossObj and register it
	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(bossObj);
	
	// warp the boss to this location when loaded
	
	
	// move boss to desired x y location at desired speed
	ObjMove_SetDestAtSpeed(bossObj,192,120,5);
	LoadSound(spellcardSFX);
	LoadSound(bdeathSFX);
	TFinalize;
	mainTask;	// run mainTask
} 

@Event { 
	// setting the boss timer and life
	alternative(GetEventType()) 
	case(EV_REQUEST_LIFE) { 
		SetScriptResult(1500);
	}
	case(EV_REQUEST_SPELL_SCORE) { 
		SetScriptResult(1000);
	}
	case(EV_REQUEST_TIMER) { 
		SetScriptResult(45);
	}
} 

@MainLoop {
	bossX = ObjMove_GetX(bossObj);
	bossY = ObjMove_GetY(bossObj);
	
	// collision for the shots and player
	ObjEnemy_SetIntersectionCircleToShot(bossObj,bossX,bossY,24);
	ObjEnemy_SetIntersectionCircleToPlayer(bossObj,bossX,bossY,32);
	
	yield;
} 

@Finalize {

} 

// your best friend, forever.
function wait(w) { loop(w) { yield; } } 

task mainTask { 
    renderBoss;
	movement;
	renderBg;
}
task renderBg {
	
	let scrollTex =0;
	
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,21);
	ObjPrim_SetTexture(obj,lake);
	ObjSprite2D_SetSourceRect(obj,0,0,1024/4,768/4);
	ObjSprite2D_SetDestRect(obj,0,0,GetStgFrameWidth,GetStgFrameHeight);
	ObjRender_SetAlpha(obj,60);
	
	/*let overlayObj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(overlayObj,21);
	ObjPrim_SetTexture(overlayObj,overlay);
	ObjRender_SetBlendType(overlayObj,BLEND_ADD_ARGB);
	ObjRender_SetAlpha(overlayObj,200);
	ObjSprite2D_SetSourceRect(overlayObj,0,0,512,512);
	ObjSprite2D_SetDestRect(overlayObj,0,0,GetStgFrameWidth,GetStgFrameHeight);
	
	while(!Obj_IsDeleted(bossObj)) {
		ObjSprite2D_SetSourceRect(overlayObj,0-scrollTex,0-scrollTex,512-scrollTex,512-scrollTex);
		scrollTex ++;
		yield;
	}
	Obj_Delete(overlayObj);*/
}
task renderBoss {
	let dir;
	let speed;
	
	// texture the boss, set centre as true centre.
	ObjPrim_SetTexture(bossObj,imgBoss);
	ObjSprite2D_SetSourceRect(bossObj,0,0,64,64);
	ObjSprite2D_SetDestCenter(bossObj);
	ObjRender_SetScaleXYZ(bossObj,1.0,1.0,0);
	
	while(!Obj_IsDeleted(bossObj)) {
		
		// update speed and location locally
		dir = ObjMove_GetAngle(bossObj);
		speed = ObjMove_GetSpeed(bossObj);
		
		if(speed == 0) {
			if(animFrame < 15) {ObjSprite2D_SetSourceRect(bossObj,0,0,64,64); }
			if(animFrame >= 15 && animFrame <30) {ObjSprite2D_SetSourceRect(bossObj,64,0,128,64); }
			if(animFrame >= 30 && animFrame <45) {ObjSprite2D_SetSourceRect(bossObj,128,0,192,64); }
			if(animFrame >= 45) {ObjSprite2D_SetSourceRect(bossObj,192,0,256,64); }
			animFrame2 = 0;
			ObjRender_SetAngleXYZ(bossObj,0,0,0); 
		}            // boss holding location
		else if (cos(dir) < 0) { 
		    if(animFrame2 < 15) {ObjSprite2D_SetSourceRect(bossObj,0,64,64,128); }
			if(animFrame2 >= 15 && animFrame2 <30) {ObjSprite2D_SetSourceRect(bossObj,64,64,128,128); }
			if(animFrame2 >= 30 && animFrame2 <45) {ObjSprite2D_SetSourceRect(bossObj,128,64,192,128); }
			if(animFrame2 >= 45) {ObjSprite2D_SetSourceRect(bossObj,192,64,256,128); }
		    ObjRender_SetAngleXYZ(bossObj,0,0,0); 
		}    // boss moving to the left
		else if (cos(dir) > 0) { 
		    if(animFrame2 < 15) {ObjSprite2D_SetSourceRect(bossObj,0,64,64,128); }
			if(animFrame2 >= 15 && animFrame2 <30) {ObjSprite2D_SetSourceRect(bossObj,64,64,128,128); }
			if(animFrame2 >= 30 && animFrame2 <45) {ObjSprite2D_SetSourceRect(bossObj,128,64,192,128); }
			if(animFrame2 >= 45) {ObjSprite2D_SetSourceRect(bossObj,192,64,256,128); }
		    ObjRender_SetAngleXYZ(bossObj,0,180,0); 
		}  // boss moving to the right
		animFrame+=1.5;
		animFrame2+=2;
		if(animFrame >60) {animFrame = 0;}
		
		yield;
	}
}

task movement {
	wait(120);
	
	loop {
		fire;
		rice;
		wait(60);
		fire2;
		rice;
		wait(60);
	}
}

function angleToPlayer{
	let dir = atan2(GetPlayerY-bossY,GetPlayerX-bossX);
	return dir;
}
let angleT = 0;
task TFinalize {
	while(ObjEnemy_GetInfo(bossObj,INFO_LIFE)>0){yield;}
	
    PlaySE(bdeathSFX);
	TExplosionA(bossX,bossY,10,0.5);
	Obj_Delete(bossObj);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	SetAutoDeleteObject(true);
	CloseScript(GetOwnScriptID);
	return;
	
}

let angleY = 0;

task fire {
	loop(20) {
	let obj = CreateShotA1(bossX+90*cos(angleT),bossY+90*sin(angleT),0,angleT,37,0);
	let obj2 = CreateShotA1(bossX+80*cos(angleT),bossY+80*sin(angleT),0,angleT,37,0);
	let obj3 = CreateShotA1(bossX+70*cos(angleT),bossY+70*sin(angleT),0,angleT,37,0);
	let obj4 = CreateShotA1(bossX+60*cos(angleT),bossY+60*sin(angleT),0,angleT,37,0);
	let obj5 = CreateShotA1(bossX+50*cos(angleT),bossY+50*sin(angleT),0,angleT,37,0);
	let obj6 = CreateShotA1(bossX+40*cos(angleT),bossY+40*sin(angleT),0,angleT,37,0);
	let obj7 = CreateShotA1(bossX+30*cos(angleT),bossY+30*sin(angleT),0,angleT,37,0);
	angleT+=360/20;
	explode(obj,obj2,obj3,obj4,obj5,obj6,obj7);
	wait(0);
	}
}
task explode(obj,obj2,obj3,obj4,obj5,obj6,obj7) {
	loop(6) {
	CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),5,angleY+angleToPlayer,37,0);
	CreateShotA1(ObjMove_GetX(obj2),ObjMove_GetY(obj2),5,angleY+angleToPlayer+4,37,0);
	CreateShotA1(ObjMove_GetX(obj3),ObjMove_GetY(obj3),5,angleY+angleToPlayer+8,37,0);
	CreateShotA1(ObjMove_GetX(obj4),ObjMove_GetY(obj4),5,angleY+angleToPlayer+12,37,0);
	CreateShotA1(ObjMove_GetX(obj5),ObjMove_GetY(obj5),5,angleY+angleToPlayer+16,37,0);
	CreateShotA1(ObjMove_GetX(obj6),ObjMove_GetY(obj6),5,angleY+angleToPlayer+20,37,0);
	CreateShotA1(ObjMove_GetX(obj7),ObjMove_GetY(obj7),5,angleY+angleToPlayer+24,37,0);
	
	angleY+=360/6;
	
	wait(0);
	}
	wait(1);
	Obj_Delete(obj);
	Obj_Delete(obj2);
	Obj_Delete(obj3);
	Obj_Delete(obj4);
	Obj_Delete(obj5);
	Obj_Delete(obj6);
	Obj_Delete(obj7);
}







task fire2 {
	loop(20) {
	let ob = CreateShotA1(bossX+90*cos(angleT),bossY+90*sin(angleT),0,angleT,37,0);
	let ob2 = CreateShotA1(bossX+80*cos(angleT),bossY+80*sin(angleT),0,angleT,37,0);
	let ob3 = CreateShotA1(bossX+70*cos(angleT),bossY+70*sin(angleT),0,angleT,37,0);
	let ob4 = CreateShotA1(bossX+60*cos(angleT),bossY+60*sin(angleT),0,angleT,37,0);
	let ob5 = CreateShotA1(bossX+50*cos(angleT),bossY+50*sin(angleT),0,angleT,37,0);
	let ob6 = CreateShotA1(bossX+40*cos(angleT),bossY+40*sin(angleT),0,angleT,37,0);
	let ob7 = CreateShotA1(bossX+30*cos(angleT),bossY+30*sin(angleT),0,angleT,37,0);
	angleT+=360/20;
	explod(ob,ob2,ob3,ob4,ob5,ob6,ob7);
	wait(0);
	}
}
task explod(ob,ob2,ob3,ob4,ob5,ob6,ob7) {
	loop(6) {
	CreateShotA1(ObjMove_GetX(ob),ObjMove_GetY(ob),5,angleY+angleToPlayer,46,0);
	CreateShotA1(ObjMove_GetX(ob2),ObjMove_GetY(ob2),5,angleY+angleToPlayer-4,46,0);
	CreateShotA1(ObjMove_GetX(ob3),ObjMove_GetY(ob3),5,angleY+angleToPlayer-8,46,0);
	CreateShotA1(ObjMove_GetX(ob4),ObjMove_GetY(ob4),5,angleY+angleToPlayer-12,46,0);
	CreateShotA1(ObjMove_GetX(ob5),ObjMove_GetY(ob5),5,angleY+angleToPlayer-16,46,0);
	CreateShotA1(ObjMove_GetX(ob6),ObjMove_GetY(ob6),5,angleY+angleToPlayer-20,46,0);
	CreateShotA1(ObjMove_GetX(ob7),ObjMove_GetY(ob7),5,angleY+angleToPlayer-24,46,0);
	
	angleY+=360/6;
	
	wait(0);
	}
	wait(1);
	Obj_Delete(ob);
	Obj_Delete(ob2);
	Obj_Delete(ob3);
	Obj_Delete(ob4);
	Obj_Delete(ob5);
	Obj_Delete(ob6);
	Obj_Delete(ob7);
}


task rice {
	loop(7.5) {
	CreateShotA1(rand(0,GetStgFrameWidth),0,rand(1,3),90,43,0);
	wait(8);
	}
}








































