#東方弾幕風[Single]
#ScriptVersion[3]
#Title["N3"]
#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();
	//resetVol();
	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);
	TPattern;
	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 TPattern(){
	let angle = GetAngleToPlayer(objEnemy);
	let max = 90;
	let min = -90;
	let n = 0;
	loop{
		n = rand(0, 90);
		max = GetAngleToPlayer(objEnemy) + 90;
		min = GetAngleToPlayer(objEnemy) - 90;
		let way1 = 0;
		let way2 = 0;
		loop(4){
			way1 = rand_int(3, 6);
			if(way1 == 3){way2 = 6;}
			if(way1 == 4){way2 = 5;}
			if(way1 == 5){way2 = 4;}
			if(way1 == 6){way2 = 3;}
			PolygonShotAimed(ObjMove_GetX(objEnemy) + 150, ObjMove_GetY(objEnemy), 3, 10, angle, way1, way2, 20, 1, BULLET_RED, 20, 60);
			PolygonShotAimed(ObjMove_GetX(objEnemy) - 150, ObjMove_GetY(objEnemy), 3, 10, -angle, way1, way2, -20, 1, BULLET_RED, 20, 60);
			
			PolygonShotAimed(ObjMove_GetX(objEnemy) + 50, ObjMove_GetY(objEnemy), 3, 10, -angle - 45, way1, way2, -20, 1, BULLET_RED, 20, 60);
			PolygonShotAimed(ObjMove_GetX(objEnemy) - 50, ObjMove_GetY(objEnemy), 3, 10, angle + 45, way1, way2, 20, 1, BULLET_RED, 20, 60);
			angle += 90;
		}
		
		angle += 13.4;
		wait(20);
		SE_Play(shot1, 50);
	}
}

function PolygonShotAimed(x, y, speed, speed2, angle, way, way2, way3, way_angle, graphic, delay, delay2){
	let i = 0;
	let n = 0;
    let bpos_x = [];
    let bpos_y = [];
    let dence = way3/(way * way2);
	let tempDelay = delay2 + (way * way2 * 2);
    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, speed2, angle + n, graphic, delay + i);
			DelayedAction(obj, x + 200 * cos(angle), y + 200 * sin(angle), vs*(speed-4), speed, va, angle, tempDelay, delay2);
			//n += dence;
			i+= 2;
        }
    }
    
    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 DelayedAction(obj, x, y, speed, speed2, angle, angle2, delay, delay2){
	ObjMove_SetMaxSpeed(obj, 0);
	ObjMove_SetAcceleration(obj, -0.2);
	wait(delay);
	ObjMove_SetMaxSpeed(obj, 10);
	ObjMove_SetAcceleration(obj, 0);
	ObjMove_SetDestAtFrameWeight(obj, x + ((speed*20)) * cos(angle), y + ((speed*20)) * sin(angle), delay2);
	wait(delay2);
	if(!Obj_IsDeleted(obj)){
		let temp = CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), speed2, angle2, BULLET_PURPLE, 10);
		Obj_Delete(obj);
	}
}

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