#東方弾幕風[Single]
#ScriptVersion[3]
#Title["NS4"]
#Text["Squares!"]
#System["./default_system/Default_System.txt"]
#include "./default_system/MS_Index.txt"

let objBoss;
let BossX;
let BossY;

@Initialize {
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	
	HitSound(objBoss, 80, 450);
	RenderCharacter(objBoss);
	DamageReduction(objBoss, 120);
	EnemyMarker(objBoss);
	Aura(objBoss, [25, 25, 25]);
	
	SetCommonData("ForceLifebar", 0);
	
	ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth/2, GetStgFrameHeight/2-100, 5, 4);
	
	if(GetCommonData("IsStage", false)==false){
	OxigenController;
	WorldBG;
	LoadAssets;}
	
	Main;
	end;
} 

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

@MainLoop {
	BossX = ObjMove_GetX(objBoss);
	BossY = ObjMove_GetY(objBoss);
	ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),21);
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),24);
	yield;
} 

@Finalize {
} 

let gAng = 90;

task Main { 
wait(90);
Move;
ascent(i in 0..2){
let obj = Bubble(BossX+100*cos(0+i*360/2), BossY+100*sin(0+i*360/2), 0.15);
Follow(obj, 0+i*360/2);
Shoot(obj);
}
loop{
ShootShapeA1(BossX, BossY, 3, 45, 4, 12, ORB_M_RED_ADD, 10);
ShootShapeA1(BossX, BossY, 3, 45, 4, 12, ORB_M_BLUE_ADD, 20);
ShootShapeA1(BossX, BossY, 3, 45, 4, 12, ORB_M_GREEN_ADD, 30);
ascent(i in 0..3){
PlaySound(GetCurrentScriptDirectory ~ "Default_System/se/smbullet.wav", 90, 10+10*i);
}
wait(140);
}
}

task Move{
let rad = 0;
let a = 0;
loop{
ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth/2+rad*cos(a), GetStgFrameHeight/2-100+(rad/2)*sin(a), 5, 7);
a++;
if(rad<150){rad+=0.5;}
yield;
}
}

task Follow(obj, off){
loop{
ObjMove_SetDestAtWeight(obj, BossX+100*cos(off), BossY+100*sin(off), 5, 4);
yield;
}
}

task Shoot(obj){
wait(140*2+140/2);
loop{
ShootShapeA1(GetX(obj), GetY(obj), 3, 45, 4, 9, ORB_S_RED_ADD, 10);
ShootShapeA1(GetX(obj), GetY(obj), 3, 45, 4, 9, ORB_S_BLUE_ADD, 20);
ShootShapeA1(GetX(obj), GetY(obj), 3, 45, 4, 9, ORB_S_GREEN_ADD, 30);
ascent(i in 0..3){
PlaySound(GetCurrentScriptDirectory ~ "Default_System/se/spirit.wav", 90, 10+10*i);
}
wait(140);
}
}

function ShootShapeA1(spawnX, spawnY, v, angle, num, bside, graphic, delay) {
	ascent(i in 0..num) {
		let sx = spawnX+cos(angle+i*360/num); let sy = spawnY+sin(angle+i*360/num); let sxN = spawnX+cos(angle+(i+1)*360/num); let syN = spawnY+sin(angle+(i+1)*360/num);
		ascent(j in 0..bside) {
			let toAngle = atan2(syN-sy,sxN-sx); let toDist = (((sxN-sx)^2+(syN-sy)^2)^0.5); let sx2 = sx+toDist/bside*j*cos(toAngle); let sy2 = sy+toDist/bside*j*sin(toAngle);
			let obj = FireShotA1(spawnX, spawnY, v*(((sx2-spawnX)^2+(sy2-spawnY)^2)^0.5), atan2(sy2-spawnY,sx2-spawnX), graphic, delay);
		}
	}
}

task end{

    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
	SetCommonData("bossx", BossX);
	SetCommonData("bossy", BossY);
	SetCommonData("RipNon", 1);
    Obj_Delete(objBoss);
    DeleteShotAll(TYPE_ALL, TYPE_ITEM);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
	
} 


