#東方弾幕風[Single]
#ScriptVersion[3]
#Title["N2"]
#System[".\Foxi's System.txt"]
//#BGM[".\bgm\boss.mp3"]
#include".\system/Resources.txt"
#include".\RenderBossFunctions.txt"
#include".\OzzieEffects.txt"
#include".\ShotFunctions.txt"
#include".\MiscFunctions.txt"
#include".\shot/shot_const.txt"
#include".\bgm functions.txt"

let objEnemy;
let objPlayer; 
let n = 0;

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		
		SetScriptResult(2000);
	}
}

@Initialize
{
	playBGM();
	PlaytestInvincibility();
	objPlayer = GetPlayerObjectID();

	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	
	TAnimate();
	TBoss();
	let cx = GetStgFrameWidth() / 2;
	let cy = 100;
	ObjMove_SetDestAtFrame(objEnemy, cx, cy, 60);
	TMain();
}


@MainLoop{
	
	yield;
}

task TBoss(){
	while(!Obj_IsDeleted(objEnemy)){
		ObjEnemy_SetIntersectionCircleToShot(objEnemy, ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), 10);
		ObjEnemy_SetIntersectionCircleToPlayer(objEnemy, ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), 14);
		if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <= 0){
			TEnd();
		}
		yield;
	}
}



task TMain
{
	wait(60);
	TPattern1;
	loop{
		movement(60);
		wait(120);
	}
}

task TEnd
{
	resetVol;
	
	
	Set2DCameraRatioX(cos(0));
	Set2DCameraRatioY(cos(0));
	Obj_Delete(objEnemy);
	let r = 0;
	loop(60)
	{
		DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
		yield;
	}
	DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
	//resetVol();
	CloseScript(GetOwnScriptID());
}

task movement(delay){
		wait(delay);
		ObjMove_SetDestAtFrameWeight(objEnemy,GetCenterX+rand(-150, 150),GetCenterY/3,delay);
}

task TPattern1(){
	loop{
		let random = rand(-5, 5);
		TrailShot(3, 90, 0 + random, 30, random);
		TrailShot(3, 90, 180 + random, 30, random);
		wait(45);
	}
}

task TrailShot(speed, angle, angle2, delay, random){
	let obj1 =  CreateShotA1(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), 15, angle2, BULLET_RED, 10);
	ShotTrail(obj1, angle2, BULLET_RED, 10);
	SE_Play(shot1, 50);
	wait(14);
	while(!Obj_IsDeleted(obj1)){
		PolygonShotAimed(ObjMove_GetX(obj1), ObjMove_GetY(obj1), 2.3, 0 + random, rand_int(3, 6), 3, 1, BULLET_PURPLE, delay, 10 + delay, speed, angle + random);
		PolygonShotAimed(ObjMove_GetX(obj1), ObjMove_GetY(obj1), 2.3, 0 + random, rand_int(3, 6), 3, 1, BULLET_PURPLE, delay, 10 + delay, speed, (180 + angle) + random);
		SE_Play(chime1, 50);
		wait(8);
	}
}
task ShotTrail(obj, angle, shot, delay){
	wait(delay);
	let x = ObjMove_GetX(obj);
	let y = ObjMove_GetY(obj);
	while(!Obj_IsDeleted(obj)){
		x = ObjMove_GetX(obj);
		y = ObjMove_GetY(obj);
		let temp =  CreateShotA1(x, y, 0, angle, shot, 0);
		Obj_FadeDelete(temp, 10);
		wait(1);
	}
}

function PolygonShotAimed(x, y, speed, angle, way, way2, way_angle, graphic, delay, delay2, speed2, angle2){

    let bpos_x = [];
    let bpos_y = [];
    
    ascent(i in 0..way){
        let tmp_angle = 360/way*i;
        bpos_x = bpos_x ~ [cos(tmp_angle)];
        bpos_y = bpos_y ~ [sin(tmp_angle)];
    }
    ascent(i in 0..way){
        let from_x = x + bpos_x[i%way];
        let from_y = y + bpos_y[i%way];
        let to_x = x + bpos_x[(i+way_angle)%way];
        let to_y = y + bpos_y[(i+way_angle)%way];
        ascent(j in 0..way2){
            let tmp = (j+1)/way2;
            let line_dat = GetLinePosition([from_x, from_y], [to_x, to_y], tmp);
            let vx = line_dat[0];
            let vy = line_dat[1];
            let va = atan2(vy-y, vx-x) + angle;
            let vs = GetDistance(x, y, vx, vy);
            let obj = CreateShotA1(x, y, vs*speed, va, graphic, delay);
			DelayedAim(obj, delay2, speed2, angle2);
        }
    }
    
    function GetLinePosition(from_pos, to_pos, pos_t){
        let tmp_x = from_pos[0] * (1-pos_t) + to_pos[0] * pos_t;
        let tmp_y = from_pos[1] * (1-pos_t) + to_pos[1] * pos_t;
        return [tmp_x, tmp_y];
    }
}

task DelayedAim(obj, delay, speed, angle){
	wait(delay);
	if(!Obj_IsDeleted(obj)){
		ObjMove_SetMaxSpeed(obj, 0);
		ObjMove_SetAcceleration(obj, -0.1);
		wait(10);
		ObjMove_SetAngle(obj, angle);
		ObjMove_SetSpeed(obj, speed*4);
		ObjMove_SetAcceleration(obj, -0.3);
		ObjMove_SetMaxSpeed(obj, speed);
		
		
	}
}

task Obj_FadeDelete(obj, delay){
	ObjRender_SetAlpha(obj, 255);
	let alpha = 255;
	let dif = 255/delay;
	loop(delay){
		ObjRender_SetAlpha(obj, alpha);
		alpha -= dif;
		yield;
	}
	Obj_Delete(obj);
}