#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/KoishiSASprite.png";
let overlay = GetCurrentScriptDirectory ~ "texture/splbg2.png";
let lake = GetCurrentScriptDirectory ~ "texture/splbg1.png";
let spellcardSFX = GetCurrentScriptDirectory ~ "sfx/SPELLCARD.wav";
let bdeathSFX = GetCurrentScriptDirectory ~ "sfx/DEFEATED.wav";


#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
	ObjMove_SetPosition(bossObj,192,120);
	
	// 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(5000);
	}
	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/2,768);
	ObjSprite2D_SetDestRect(obj,0,0,GetStgFrameWidth,GetStgFrameHeight);
	
	/*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,180,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,0,0); 
		}  // boss moving to the right
		animFrame+=1.5;
		animFrame2+=2;
		if(animFrame >60) {animFrame = 0;}
		
		yield;
	}
}

task movement {
	wait(60);
	
	loop {
		fire;
		loop(30) {
			indicator;
			wait(1);
		}
		fire2;
		loop(30) {
			indicator;
			wait(1);
		}
	}
}

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;
	
}
task fire {
	loop(10) {
	let ob = CreateShotA1(bossX,bossY,2,angleT+angleToPlayer,236,0);
	let ob2 = CreateShotA1(bossX,bossY,2.15,angleT+angleToPlayer+4,242,0);
	let ob3 = CreateShotA1(bossX,bossY,2.3,angleT+angleToPlayer+8,241,0);
	let ob4 = CreateShotA1(bossX,bossY,2.45,angleT+angleToPlayer+12,240,0);
	let ob5 = CreateShotA1(bossX,bossY,2.60,angleT+angleToPlayer+16,239,0);
	let ob6 = CreateShotA1(bossX,bossY,2.75,angleT+angleToPlayer+20,238,0);
	let ob7 = CreateShotA1(bossX,bossY,2.90,angleT+angleToPlayer+24,237,0);
	let ob8 = CreateShotA1(bossX,bossY,3.05,angleT+angleToPlayer+28,236,0);
	let ob9 = CreateShotA1(bossX,bossY,3.2,angleT+angleToPlayer+32,242,0);
	normal(ob,ob2,ob3,ob4,ob5,ob6,ob7,ob8,ob9);
	angleT+=360/10;
	}
}
task normal(ob,ob2,ob3,ob4,ob5,ob6,ob7,ob8,ob9) {
	wait(20);
	loop(1) {
	ObjMove_SetSpeed(ob,3.35);
	ObjMove_SetSpeed(ob2,3.2);
	ObjMove_SetSpeed(ob3,3.05);
	ObjMove_SetSpeed(ob4,2.90);
	ObjMove_SetSpeed(ob5,2.75);
	ObjMove_SetSpeed(ob6,2.60);
	ObjMove_SetSpeed(ob7,2.45);
	ObjMove_SetSpeed(ob8,2.3);
	ObjMove_SetSpeed(ob9,2.15);
	}
}








task fire2 {
	loop(10) {
	let o = CreateShotA1(bossX,bossY,3.2,angleT+angleToPlayer,236,0);
	let o2 = CreateShotA1(bossX,bossY,3.05,angleT+angleToPlayer+4,242,0);
	let o3 = CreateShotA1(bossX,bossY,2.90,angleT+angleToPlayer+8,241,0);
	let o4 = CreateShotA1(bossX,bossY,2.75,angleT+angleToPlayer+12,240,0);
	let o5 = CreateShotA1(bossX,bossY,2.60,angleT+angleToPlayer+16,239,0);
	let o6 = CreateShotA1(bossX,bossY,2.45,angleT+angleToPlayer+20,238,0);
	let o7 = CreateShotA1(bossX,bossY,2.3,angleT+angleToPlayer+24,237,0);
	let o8 = CreateShotA1(bossX,bossY,2.15,angleT+angleToPlayer+28,236,0);
	let o9 = CreateShotA1(bossX,bossY,2,angleT+angleToPlayer+32,242,0);
	normal2(o,o2,o3,o4,o5,o6,o7,o8,o9);
	angleT+=360/10;
	}
}
task normal2(o,o2,o3,o4,o5,o6,o7,o8,o9) {
	wait(20);
	loop(1) {
	ObjMove_SetSpeed(o9,3.35);
	ObjMove_SetSpeed(o8,3.2);
	ObjMove_SetSpeed(o7,3.05);
	ObjMove_SetSpeed(o6,2.90);
	ObjMove_SetSpeed(o5,2.75);
	ObjMove_SetSpeed(o4,2.60);
	ObjMove_SetSpeed(o3,2.45);
	ObjMove_SetSpeed(o2,2.3);
	ObjMove_SetSpeed(o,2.15);
	}
}
task indicator {
	loop(1) {
	let ind = CreateStraightLaserA1(-100,bossY,0,1000,2,0,48,1);
	let ind2 = CreateStraightLaserA1(bossX,-100,90,1000,2,0,48,1);
	ObjRender_SetAlpha(ind,100);
	ObjRender_SetAlpha(ind2,100);
	wait(1);
	}
}















