#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["3rd Non-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";
let fdir = 90;
let sides = 5;
let lol = 1;
	
#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,145,5);
	
	mainTask;	// run mainTask
} 

@Event { 
       alternative(GetEventType())
       case(EV_REQUEST_LIFE){SetScriptResult(2500);}
       case(EV_REQUEST_TIMER){SetScriptResult(30);}
       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(60);
	fire;
	fireM;
	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 fireM{
	loop{
		loop(30){
			lol=1;
			sides = 2;
			fdir+=0;
		wait(4);
		}
		loop(40){
			lol=1;
			sides = 2;
			fdir+=17;
		wait(4);
		}
		loop(30){
			lol=0;
			sides = 2;
			fdir+=0;
		wait(4);
		}
		loop(40){
			lol=0;
			sides = 2;
			fdir+=17;
		wait(4);
		}
	}
}
task fire{
	let count = 0;
	let wow = 3;
	let innerlayer= 1;
	let middlelayer= 2;
	let outerlayer= 3;
	loop{

if(lol==0){
	loop(sides){
		CreateShotA1(bossX-50,bossY-100,wow,fdir,DS_BILL_RED,30);
		CreateShotA1(bossX+50,bossY-100,wow,-fdir,DS_BILL_RED,30);
		CreateShotA1(bossX-115,bossY,wow,fdir,DS_BILL_RED,30);
		CreateShotA1(bossX+115,bossY,wow,-fdir,DS_BILL_RED,30);
		CreateShotA1(bossX-50,bossY+100,wow,fdir,DS_BILL_RED,30);
		CreateShotA1(bossX+50,bossY+100,wow,-fdir,DS_BILL_RED,30);
	fdir+=360/sides;
	}
}

if(lol==1){
	loop(sides){
		CreateShotA1(bossX-25,bossY-50,wow,fdir,DS_BILL_BLUE,30);
		CreateShotA1(bossX+25,bossY-50,wow,-fdir,DS_BILL_BLUE,30);
		CreateShotA1(bossX-57.5,bossY,wow,fdir,DS_BILL_BLUE,30);
		CreateShotA1(bossX+57.5,bossY,wow,-fdir,DS_BILL_BLUE,30);
		CreateShotA1(bossX-25,bossY+50,wow,fdir,DS_BILL_BLUE,30);
		CreateShotA1(bossX+25,bossY+50,wow,-fdir,DS_BILL_BLUE,30);
	fdir+=360/sides;
	}
}

	//PlaySE(s1);
	count++;
	wait(1);
	}
}