#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Psychic trap unused spell"]
#Text["NewCutout"]
#include"./Cutin.txt"
let bossObj;
let bossX = 0;
let bossY = 0;
let animFrame = 0;
let animFrame2 = 0;
let objScene = GetEnemyBossSceneObjectID();
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";
let snd = GetCurrentScriptDirectory ~ "./sfx/bgm.ogg";
let objmusic = ObjSound_Create();


#include "./../shot/AllStarShot_Const.txt"
#include "script/default_system/Default_Effect.txt"
@Initialize {
	
	ObjSound_Load(objmusic,snd);
	ObjSound_Play(objmusic);
	// 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,-100);
	
	// move boss to desired x y location at desired speed
	ObjMove_SetDestAtSpeed(bossObj,192,120,5);
	
	LoadSound(spellcardSFX);
	LoadSound(bdeathSFX);
	ObjEnemyBossScene_StartSpell(objScene);
	TFinalize;
	let patchy = GetCurrentScriptDirectory~"cutin1.png";
	let kyouko = GetCurrentScriptDirectory~"cutin2.png";
	ObjCutin_SetSpellcardS4("''People Hiding from the Satori''",patchy,MOKOU,240,104,104);
	ObjCutin_LaunchS3(NAZRIN, patchy, "Normal");
	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 {

} 

task TFinalize {
	while(ObjEnemy_GetInfo(bossObj,INFO_LIFE)>0){yield;}
	if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT)
        +ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0){
    AddScore(ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE));
}
    PlaySE(bdeathSFX);
	TExplosionA(bossX,bossY,10,0.5);
	Obj_Delete(bossObj);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	SetAutoDeleteObject(true);
	CloseScript(GetOwnScriptID);
	return;
}

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

task mainTask { 
    renderBoss;
	renderBg;
	movement;
}
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,1,0);
	
	while(!Obj_IsDeleted(bossObj)) {
		
		// update speed and location locally
		dir = ObjMove_GetAngle(bossObj);
		speed = ObjMove_GetSpeed(bossObj);
		
		// animate
		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);
	ObjMove_SetX(GetPlayerObjectID,GetStgFrameWidth/2);
	ObjMove_SetY(GetPlayerObjectID,GetStgFrameHeight/2+175);
	trap;
	loop {
		ObjMove_SetDestAtSpeed(bossObj,rand(0,GetStgFrameWidth),rand(0,GetStgFrameHeight),3);
		fire;
		wait(360);
	}
}

function angleToPlayer{
	let dir = atan2(GetPlayerY-bossY,GetPlayerX-bossX);
	return dir;
}
let angleT = 0;
task fire {
	loop(24) {
	firy;
	wait(15);
	}
}
task firy {
	loop(10) {
	CreateShotA1(bossX,bossY,2,angleT+angleToPlayer,292,0);
	angleT+=360/10;
	wait(0);
	}
}
task trap {
	ascent(i in 0..30) {
	let obj = CreateShotA1(GetStgFrameWidth/2-400+i*50,GetStgFrameHeight/2+200,0,0,39,0);
	let obj2 = CreateShotA1(GetStgFrameWidth/2-400+i*50,GetStgFrameHeight/2+150,0,0,39,0);
	let obj3 = CreateShotA1(GetStgFrameWidth/2-400+i*50,GetStgFrameHeight/2+100,0,0,39,0);
	let obj4 = CreateShotA1(GetStgFrameWidth/2-400+i*50,GetStgFrameHeight/2+50,0,0,39,0);
	let obj5 = CreateShotA1(GetStgFrameWidth/2-400+i*50,GetStgFrameHeight/2+0,0,0,39,0);
	let obj6 = CreateShotA1(GetStgFrameWidth/2-400+i*50,GetStgFrameHeight/2-50,0,0,39,0);
	let obj7 = CreateShotA1(GetStgFrameWidth/2-400+i*50,GetStgFrameHeight/2-100,0,0,39,0);
	let obj8 = CreateShotA1(GetStgFrameWidth/2-400+i*50,GetStgFrameHeight/2-150,0,0,39,0);
	let obj9 = CreateShotA1(GetStgFrameWidth/2-400+i*50,GetStgFrameHeight/2-200,0,0,39,0);

	let ob = CreateShotA1(GetStgFrameWidth/2-400+i*50,GetStgFrameHeight/2+200,0,0,236,0);
	let ob2 = CreateShotA1(GetStgFrameWidth/2-400+i*50,GetStgFrameHeight/2+100,0,0,236,0);
	let ob3 = CreateShotA1(GetStgFrameWidth/2-400+i*50,GetStgFrameHeight/2+0,0,0,236,0);
	let ob4 = CreateShotA1(GetStgFrameWidth/2-400+i*50,GetStgFrameHeight/2-100,0,0,236,0);
	let ob5 = CreateShotA1(GetStgFrameWidth/2-400+i*50,GetStgFrameHeight/2-200,0,0,236,0);
	
	ObjShot_SetSpellResist(obj,true);
	ObjShot_SetSpellResist(obj2,true);
	ObjShot_SetSpellResist(obj3,true);
	ObjShot_SetSpellResist(obj4,true);
	ObjShot_SetSpellResist(obj5,true);
	ObjShot_SetSpellResist(obj6,true);
	ObjShot_SetSpellResist(obj7,true);
	ObjShot_SetSpellResist(obj8,true);
	ObjShot_SetSpellResist(obj9,true);
	
	ObjShot_SetSpellResist(ob,true);
	ObjShot_SetSpellResist(ob2,true);
	ObjShot_SetSpellResist(ob3,true);
	ObjShot_SetSpellResist(ob4,true);
	ObjShot_SetSpellResist(ob5,true);
	
	ObjShot_SetAutoDelete(obj,false);
	ObjShot_SetAutoDelete(obj2,false);
	ObjShot_SetAutoDelete(obj3,false);
	ObjShot_SetAutoDelete(obj4,false);
	ObjShot_SetAutoDelete(obj5,false);
	ObjShot_SetAutoDelete(obj6,false);
	ObjShot_SetAutoDelete(obj7,false);
	ObjShot_SetAutoDelete(obj8,false);
	ObjShot_SetAutoDelete(obj9,false);
	
	ObjShot_SetAutoDelete(ob,false);
	ObjShot_SetAutoDelete(ob2,false);
	ObjShot_SetAutoDelete(ob3,false);
	ObjShot_SetAutoDelete(ob4,false);
	ObjShot_SetAutoDelete(ob5,false);
	
	playerboss(obj,obj2,obj3,obj4,obj5,obj6,obj7,obj8,obj9,ob,ob2,ob3,ob4,ob5);
	wait(0);
	}
}
task playerboss(obj,obj2,obj3,obj4,obj5,obj6,obj7,obj8,obj9,ob,ob2,ob3,ob4,ob5) {
	loop {
	playerbossy(obj,obj2,obj3,obj4,obj5,obj6,obj7,obj8,obj9,ob,ob2,ob3,ob4,ob5);
	wait(120*3);
	}
}
task playerbossy(obj,obj2,obj3,obj4,obj5,obj6,obj7,obj8,obj9,ob,ob2,ob3,ob4,ob5) {
	
	function angleToBoss(obj){
	let dir = atan2(bossY-ObjMove_GetY(obj),bossX-ObjMove_GetX(obj));
	return dir;
}
	function angleToBoss2(obj2){
	let dir = atan2(bossY-ObjMove_GetY(obj2),bossX-ObjMove_GetX(obj2));
	return dir;
}	
	function angleToBoss3(obj3){
	let dir = atan2(bossY-ObjMove_GetY(obj3),bossX-ObjMove_GetX(obj3));
	return dir;
}	
	function angleToBoss4(obj4){
	let dir = atan2(bossY-ObjMove_GetY(obj4),bossX-ObjMove_GetX(obj4));
	return dir;
}	
	function angleToBoss5(obj5){
	let dir = atan2(bossY-ObjMove_GetY(obj5),bossX-ObjMove_GetX(obj5));
	return dir;
}	
    function angleToBoss6(obj6){
	let dir = atan2(bossY-ObjMove_GetY(obj6),bossX-ObjMove_GetX(obj6));
	return dir;
}
	function angleToBoss7(obj7){
	let dir = atan2(bossY-ObjMove_GetY(obj7),bossX-ObjMove_GetX(obj7));
	return dir;
}	
	function angleToBoss8(obj8){
	let dir = atan2(bossY-ObjMove_GetY(obj8),bossX-ObjMove_GetX(obj8));
	return dir;
}	
	function angleToBoss9(obj9){
	let dir = atan2(bossY-ObjMove_GetY(obj9),bossX-ObjMove_GetX(obj9));
	return dir;
}	

	wait(120);
	ObjMove_SetAngle(obj,angleToBoss(obj)+180);
	ObjMove_SetAngle(obj2,angleToBoss2(obj2)+180);
	ObjMove_SetAngle(obj3,angleToBoss3(obj3)+180);
	ObjMove_SetAngle(obj4,angleToBoss4(obj4)+180);
	ObjMove_SetAngle(obj5,angleToBoss5(obj5)+180);
	ObjMove_SetAngle(obj6,angleToBoss2(obj6)+180);
	ObjMove_SetAngle(obj7,angleToBoss3(obj7)+180);
	ObjMove_SetAngle(obj8,angleToBoss4(obj8)+180);
	ObjMove_SetAngle(obj9,angleToBoss5(obj9)+180);
	ObjMove_SetAngle(ob,GetAngleToPlayer(ob));
	ObjMove_SetAngle(ob2,GetAngleToPlayer(ob2));
	ObjMove_SetAngle(ob3,GetAngleToPlayer(ob3));
	ObjMove_SetAngle(ob4,GetAngleToPlayer(ob4));
	ObjMove_SetAngle(ob5,GetAngleToPlayer(ob5));
	ObjMove_SetSpeed(obj,0.8);
	ObjMove_SetSpeed(obj2,0.8);
	ObjMove_SetSpeed(obj3,0.8);
	ObjMove_SetSpeed(obj4,0.8);
	ObjMove_SetSpeed(obj5,0.8);
	ObjMove_SetSpeed(obj6,0.8);
	ObjMove_SetSpeed(obj7,0.8);
	ObjMove_SetSpeed(obj8,0.8);
	ObjMove_SetSpeed(obj9,0.8);
	ObjMove_SetSpeed(ob,1.3);
	ObjMove_SetSpeed(ob2,1.3);
	ObjMove_SetSpeed(ob3,1.3);
	ObjMove_SetSpeed(ob4,1.3);
	ObjMove_SetSpeed(ob5,1.3);
	wait(120);
	ObjMove_SetSpeed(obj,-0.8);
	ObjMove_SetSpeed(obj2,-0.8);
	ObjMove_SetSpeed(obj3,-0.8);
	ObjMove_SetSpeed(obj4,-0.8);
	ObjMove_SetSpeed(obj5,-0.8);
	ObjMove_SetSpeed(obj6,-0.8);
	ObjMove_SetSpeed(obj7,-0.8);
	ObjMove_SetSpeed(obj8,-0.8);
	ObjMove_SetSpeed(obj9,-0.8);
	ObjMove_SetSpeed(ob,-1.3);
	ObjMove_SetSpeed(ob2,-1.3);
	ObjMove_SetSpeed(ob3,-1.3);
	ObjMove_SetSpeed(ob4,-1.3);
	ObjMove_SetSpeed(ob5,-1.3);
	wait(120);
	ObjMove_SetSpeed(obj,0);
	ObjMove_SetSpeed(obj2,0);
	ObjMove_SetSpeed(obj3,0);
	ObjMove_SetSpeed(obj4,0);
	ObjMove_SetSpeed(obj5,0);
	ObjMove_SetSpeed(obj6,0);
	ObjMove_SetSpeed(obj7,0);
	ObjMove_SetSpeed(obj8,0);
	ObjMove_SetSpeed(obj9,0);
	ObjMove_SetSpeed(ob,0);
	ObjMove_SetSpeed(ob2,0);
	ObjMove_SetSpeed(ob3,0);
	ObjMove_SetSpeed(ob4,0);
	ObjMove_SetSpeed(ob5,0);
}









