#東方弾幕風[Single]
#ScriptVersion[3]
#Title["S1"]
#System[".\Foxi's System.txt"]
//#BGM[".\bgm\boss.mp3"]

#include".\system/Resources.txt"
#include".\OzzieEffects.txt"
#include".\RenderBossFunctions.txt"
#include".\ShotFunctions.txt"
#include".\MiscFunctions.txt"
#include".\Cutin.txt"
#include".\shot/shot_const.txt"
#include".\bgm functions.txt"

let objEnemy;
let objPlayer; 
let splitValue;

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

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

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


@MainLoop{
	yield;
}

task TSpellCardScene
{
	wait(60);
	TAttack(1);
	SE_Play(spellcard, 120);
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
	let enmcut = GetCurrentScriptDirectory ~ "img/bossImg2.png";
	TCutin("Crescent Waxing Moon \"Myopia Flaw\"",enmcut,KANAKO,[255,0,0],0);
}

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*4);
	ConcentrationA3(objEnemy,"BULLET", 60 * 2,4,150,[255,0,0]);
	ConcentrationA3(objEnemy,"BULLET", 60 * 2,4,150,[255,0,200]);
	SE_Play(charge0, 70);
	ObjEnemy_SetDamageRate(objEnemy, 0, 0);
	wait(60*2 + 30);
	ObjEnemy_SetDamageRate(objEnemy, 100, 100);
	splitValue = 100000;
	TPattern1;
	wait(350);
	splitIncrease;
}

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 TPattern1(){
	let speed = 0;
	let angle = 0;
	let i = 0;
	while(!Obj_IsDeleted(objEnemy)){
		speed = 2;
		loop(15){
			i = rand(40, 50);
			PolygonShot(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), speed, i, 4, rand(4, 10), 1, BULLET_RED, 0);
			speed += 0.1;
			SE_Play(shot1, 50);
			wait(10);
		}
		wait(60);
		angle = GetAngleToPlayer(objEnemy)-40;
		loop(4){
			loop(5){
				PolygonShotAimed(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), 2.3, angle, rand_int(3, 6), 3, 1, BULLET_PURPLE, 0, 10, 4, angle);
				angle += 20;
				SE_Play(shot1, 50);
				wait(5);
			}
			wait(10);
			
			angle += 360/4 - 80;
			wait(10);
		}
		
		movement(60);
	}
}

function PolygonShot(x, y, speed, angle, way, way2, way_angle, graphic, delay){

    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);
			Split(obj, graphic);
			Split2(obj, graphic);
        }
    }
    
    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];
    }
}

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);
			Reflect(obj, 1);
			Split(obj, graphic);
			Split2(obj, graphic);
        }
    }
    
    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];
    }
}
function PolygonShot2(x, y, speed, angle, way, way2, way_angle, graphic, delay){

    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);
        }
    }
    
    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];
    }
}

function PolygonShotAimed2(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);
			Reflect(obj, 1);
        }
    }
    
    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);
		ObjMove_SetAcceleration(obj, 0);
		
		
	}
}

task Reflect(obj, bounce)
{
    let GetClipMaxX = GetStgFrameWidth();
    let GetClipMaxY = GetStgFrameHeight();
    let value = bounce;
    while(!Obj_IsDeleted(obj))
    {
        if(value>0){
            if(ObjMove_GetX(obj)<0||ObjMove_GetX(obj)>GetClipMaxX)
            {
                ObjMove_SetAngle(obj,180-ObjMove_GetAngle(obj));
                value--;
            }
            if(ObjMove_GetY(obj)<0)
            {
                ObjMove_SetAngle(obj,-ObjMove_GetAngle(obj));
                value--
            }
        }
        if(value==0){break;}
        yield;
    }
}

task Split(obj, shot){
	let obj1 = CreateShotA1(0, 0, 0, 0, shot, 0);
	let obj2 = CreateShotA1(0, 0, 0, 0, shot, 0);  //creates 2 bullets
	
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjRender_SetAlpha(obj, 0);  //sets the original bullet invisible
	
	ObjShot_SetAutoDelete(obj1, false);
	ObjShot_SetAutoDelete(obj2, false);
	ObjShot_SetIntersectionEnable(obj1, false);
	ObjShot_SetIntersectionEnable(obj2, false);
	ObjRender_SetBlendType(obj1, BLEND_ALPHA);
	ObjRender_SetBlendType(obj2, BLEND_ALPHA);
	ObjRender_SetAlpha(obj1, 255/4);
	ObjRender_SetAlpha(obj2, 255/4);  //makes thebullets a little transperant
	
	let random = splitValue; //random value
	let random2 = rand(-2,2);
	while(!Obj_IsDeleted(obj)){ //if the original bullet is deleted, stop this while loop
		let distance = (GetDistance2(ObjMove_GetX(obj), ObjMove_GetY(obj), ObjMove_GetX(objPlayer), ObjMove_GetY(objPlayer))/random ) + random2; //takes the distance between the bullet and the player
		ObjMove_SetX(obj1, ObjMove_GetX(obj) + distance);
		ObjMove_SetX(obj2, ObjMove_GetX(obj) - distance); //sets the distance of the bullet, either to be on the left or right of the bullet position
		ObjMove_SetY(obj1, ObjMove_GetY(obj));
		ObjMove_SetY(obj2, ObjMove_GetY(obj)); //makes the bullets the same y axis as the original bullet
		ObjMove_SetAngle(obj1, ObjMove_GetAngle(obj));
		ObjMove_SetAngle(obj2, ObjMove_GetAngle(obj)); //makes the bullets same angle as the original bullet
		yield;
	}
	Obj_Delete(obj1);
	Obj_Delete(obj2); //deletes the red and aqua bullets
}
task Split2(obj, shot){
	let obj1 = CreateShotA1(0, 0, 0, 0, shot, 0);
	let obj2 = CreateShotA1(0, 0, 0, 0, shot, 0);  //creates 2 bullets
	
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjRender_SetAlpha(obj, 0);  //sets the original bullet invisible
	
	ObjShot_SetAutoDelete(obj1, false);
	ObjShot_SetAutoDelete(obj2, false);
	ObjShot_SetIntersectionEnable(obj1, false);
	ObjShot_SetIntersectionEnable(obj2, false);
	ObjRender_SetBlendType(obj1, BLEND_ALPHA);
	ObjRender_SetBlendType(obj2, BLEND_ALPHA);
	ObjRender_SetAlpha(obj1, 255/4);
	ObjRender_SetAlpha(obj2, 255/4);  //makes thebullets a little transperant
	
	let random = splitValue*2; //random value
	let random2 = rand(-2,2);
	
	while(!Obj_IsDeleted(obj)){ //if the original bullet is deleted, stop this while loop
		let distance = (GetDistance2(ObjMove_GetX(obj), ObjMove_GetY(obj), ObjMove_GetX(objPlayer), ObjMove_GetY(objPlayer))/random ) + random2; //takes the distance between the bullet and the player
		ObjMove_SetY(obj1, ObjMove_GetY(obj) + distance);
		ObjMove_SetY(obj2, ObjMove_GetY(obj) - distance); //sets the distance of the bullet, either to be on the top or the bottom of the bullet position
		ObjMove_SetX(obj1, ObjMove_GetX(obj));
		ObjMove_SetX(obj2, ObjMove_GetX(obj)); //makes the bullets the same y axis as the original bullet
		ObjMove_SetAngle(obj1, ObjMove_GetAngle(obj));
		ObjMove_SetAngle(obj2, ObjMove_GetAngle(obj)); //makes the bullets same angle as the original bullet
		yield;
	}
	Obj_Delete(obj1);
	Obj_Delete(obj2); //deletes the red and aqua bullets
}

task splitIncrease(){
	while(splitValue >= 1700){
		splitValue -= 200;
		yield;
	}
}

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