#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["5th Spell"]
#Text["Hi[r]This is my first ph3 script."]

let bossObj;
let bossX = 0;
let bossY = 0;
let D = GetCurrentScriptDirectory;
let imgBoss = D ~ "cirno.png";
let s1 = D ~ "Shot.wav";
let background = D ~ "bg.png";
	
#include "script/default_system/Default_ShotConst.txt"

@Initialize {
	
	SetPlayerLife(10);

	// define a boss in bossObj and register it
	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(bossObj);

	TEnd;
	bg;
	LoadSound(s1);

	// 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,225,5);
	
	mainTask;	// run mainTask
} 

@Event { 
       alternative(GetEventType())
       case(EV_REQUEST_LIFE){SetScriptResult(3000);}
       case(EV_REQUEST_TIMER){SetScriptResult(45);}
       case(EV_REQUEST_SPELL_SCORE){SetScriptResult(1000000);}
} 

@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 TEnd{
	while(ObjEnemy_GetInfo(bossObj,INFO_LIFE)>0){yield;}
	yield;
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	Obj_Delete(bossObj);
	DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
	CloseScript(GetOwnScriptID());
}
task mainTask { 
	renderBoss;
	movement;
} 

task renderBoss {
	let dir;
	let speed;
	
	// texture the boss, set centre as true centre.
	ObjPrim_SetTexture(bossObj,imgBoss);
	ObjSprite2D_SetSourceRect(bossObj,0,0,110,110);
	ObjSprite2D_SetDestCenter(bossObj);	
	ObjRender_SetScaleXYZ(bossObj,0.7,0.7,0);
	
	while(!Obj_IsDeleted(bossObj)) {
		
		// update boss speed and direction locally
		dir = ObjMove_GetAngle(bossObj);
		speed = ObjMove_GetSpeed(bossObj);
		
		// if the boss is idle, show this image/rect
		if(speed == 0) { ObjSprite2D_SetSourceRect(bossObj,0,0,110,110); ObjRender_SetAngleXYZ(bossObj,0,0,0); } 			// boss holding location
		else if(cos(dir) < 0) { ObjSprite2D_SetSourceRect(bossObj,0,0,110,110); ObjRender_SetAngleXYZ(bossObj,0,0,0); } 	// boss moving to the left
		else if(cos(dir) > 0) { ObjSprite2D_SetSourceRect(bossObj,0,0,110,110); ObjRender_SetAngleXYZ(bossObj,0,180,0); } 	// boss moving to the right
		
		yield;
	}
	
} 

task movement {
	wait(180);
	fireb;
	wait(130);
	wait(200);
	//ObjMove_SetDestAtSpeed(bossObj,180,100,5);
}

function angleToPlayer {
	let dir = atan2(GetPlayerY-bossY,GetPlayerX-bossX);
	return dir;
}

task bg{
	let bg1 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(bg1, background);
	Obj_SetRenderPriorityI(bg1, 27);
	ObjSprite2D_SetSourceRect(bg1, 0, 0, 640, 480);
	ObjSprite2D_SetDestCenter(bg1);
	ObjRender_SetPosition(bg1,300,220,0);
}
   task bullet1(x, y, v, ang, graph, delay){
	let obj = CreateShotA1(x,y,v,ang,graph,delay);
	wait(60);
		loop(20){ObjMove_SetSpeed(obj, v); v+=0.01; wait(2); }
		wait(50);
	ObjShot_FadeDelete(obj);
   }

   task bullet2(x, y, v, ang, graph, delay){
	let obj = CreateShotA1(x,y,v,ang,graph,delay);
	wait(180);
		loop(10){ObjMove_SetSpeed(obj, v); v-=0.1; wait(2); }
	ObjShot_SetGraphic(obj,DS_SCALE_A_PURPLE);
	ObjShot_SetDelay(obj,20);
	ObjMove_SetAngle(obj,rand(0,356));
	ObjMove_SetDestAtSpeed(obj,GetPlayerX,GetPlayerY,1);
	bullet1(x,y,v,ang-rand(0,356),DS_BALL_SS_BLUE,20);
		wait(230);
		ang=270;
	ObjShot_FadeDelete(obj);
   }

   task bullet2b(x, y, v, ang, graph, delay){
	let obj = CreateShotA1(x,y,v,ang,graph,delay);
	wait(180);
		loop(10){ObjMove_SetSpeed(obj, v); v-=0.1; wait(2); }
	ObjShot_SetGraphic(obj,DS_SCALE_A_PURPLE);
	ObjShot_SetDelay(obj,20);
	ObjMove_SetAngle(obj,rand(0,356));
	ObjMove_SetDestAtSpeed(obj,GetPlayerX,GetPlayerY,1);
	//bullet1(x,y,v,ang-rand(0,356),DS_BALL_SS_BLUE,20);
		wait(230);
		ang=270;
	ObjShot_FadeDelete(obj);
   }

task fireb{
	let ok = 0;
	let wow = 0;
	let umm = 6;
	let dir = 180;
	let dir2 = 0;
	let dir3 = 90;
	let dir4 = 270;
	let count = 0;
	let sides = 5;
	let bloomspotx=0;
	let bloomspoty=0;
loop{
umm = 6;
ok=0;
dir+=40;
dir2-=40;
//wait(10);
sides=10;
	loop(60){

	loop(sides){
bullet2(bossX-bloomspotx+ok*cos(dir),bossY-bloomspoty+ok*sin(dir),wow,dir,DS_BALL_M_A_BLUE,40);
		dir+=360/sides;
		dir2+=360/sides;
	}
	wait(1);
	count++;
	umm-=0.115;
	ok+=umm;
	dir-=71;
	dir2+=71;

	loop(sides){
bullet2b(bossX-bloomspotx+ok*cos(dir),bossY-bloomspoty+ok*sin(dir),wow,dir,DS_BALL_M_A_BLUE,40);
		dir+=360/sides;
		dir2+=360/sides;
	}
	wait(1);
	count++;
	umm-=0.115;
	ok+=umm;
	dir-=71;
	dir2+=71;

}
wait(180);
ok=0;
dir-=40;
dir2+=40;
sides=5;
count=0;
umm = 6;
}
}