#東方弾幕風[Single]
#ScriptVersion[3]
#Title["S3"]
#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 redeyes = 1;
@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		
		SetScriptResult(6500);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(90);
	}
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(3);
	}
	
}

@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("Gibbious Moon \"Senseless Glaucoma\"",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,8,200,[255,0,0]);
	ConcentrationA3(objEnemy,"BULLET", 60 * 2,8,200,[255,0,200]);
	SE_Play(charge0, 70);
	ObjEnemy_SetDamageRate(objEnemy, 0, 0);
	wait(60*2 + 30);
	ObjEnemy_SetDamageRate(objEnemy, 100, 100);
	TPattern;
	TPattern2;
	wait(480);
	loop{
		SE_Play(charge4, 70);
		RedEyes();
		BgmVolDecrease(1, 210);
		SE_Play(boon0, 70);
		wait(720);
	}
}

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 way1 = 0;
	let way2 = 0;
	let i = 50;
	loop{
			way1 = rand_int(3, 6);
			if(way1 == 3){way2 = 20;}
			if(way1 == 4){way2 = 15;}
			if(way1 == 5){way2 = 12;}
			if(way1 == 6){way2 = 10;}
			PolygonShot(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), rand(2, 5), rand(0, 360), way1, way2, 1, BULLET_RED, 30, rand(30, 60));
			if(redeyes == 1){
				SE_Play(shot1, 50);
				i = 50;
			}
			else{
				i-= 10;
				SE_Play(shot1, i);
			}
		wait(30);
		
	}
}

task TPattern2(){
	wait(150);
	loop{
		if(redeyes == 1){
		PolygonShotAimed(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), 1.5, 0, rand_int(3, 6), 5, 1, BULLET_PURPLE, 10, 20, rand(30, 90), 2, GetAngleToPlayer(objEnemy));
		SE_Play(chime1, 50);
		}
		wait(150);
	}
}

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

    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);
			FadeShot(obj, delay2, vs);
        }
    }
    
    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, delay3, 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, delay3, speed2, vs, 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, delay2, speed, vs, 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 FadeShot(obj, delay, vs){
	let alpha = 255;
	let dif = alpha/delay;
	let n = 1;
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjRender_SetAlpha(obj, alpha);
	let speed = 1+ vs;
	while(!Obj_IsDeleted(obj)){
		ObjRender_SetAlpha(obj, alpha);
		if(alpha <= 100 && n == 1){
			ObjMove_SetSpeed(obj, 0);
			ObjShot_SetIntersectionEnable(obj, false);
		}
		if(alpha > 100 && n == -1){
			ObjMove_SetSpeed(obj, speed);
			ObjShot_SetIntersectionEnable(obj, true);
		}
		if(redeyes == 1){
			if(alpha > 0 && n == 1){
				alpha-= dif;
				if (alpha <= 0){
					n = -1;
				}
			}
			if(alpha < 255 && n == -1){
				alpha += dif;
				if (alpha >= 255){
					n = 1;
				}
			}
		}
		else if(redeyes == -1){
			ObjMove_SetSpeed(obj, speed);
			n = 1;
			if(alpha < 255){
				alpha = 255;
			}
		}
		yield;
	}
}

task RedEyes(){
	let red = CreateEffectObject(GetCenterX,GetCenterY,GetCurrentScriptDirectory~"./img/Red.png","ALPHA",0,0,1000,1000);
	BossDeleteRender(red);
	Obj_SetRenderPriority(red,0.7);
	ObjRender_SetAlpha(red, 6);
	let eyes = CreateEffectObject(GetCenterX,GetCenterY,GetCurrentScriptDirectory~"./img/eyes.png","ARGB",0,0,192,64);
	BossDeleteRender(eyes);
	Obj_SetRenderPriority(eyes,0.71);
	ObjRender_SetAlpha(eyes, 255);
	ObjRender_SetScaleXYZ(eyes, 2, 0, 1);
	let alpha = (255/2 - 26) / 180;
	let scale = 1/50;
	let i = 6;
	let j = 0;
	redeyes = -1;
	Obj_FadeDelete(eyes, 180);
	loop(10){
		let tempEyes = CreateEffectObject(GetCenterX,GetCenterY,GetCurrentScriptDirectory~"./img/eyes.png","ARGB",0,0,192,64);
		BossDeleteRender(tempEyes);
		Obj_SetRenderPriority(eyes,0.72);
		ObjRender_SetScaleXYZ(tempEyes, 2, j, 1);
		Obj_FadeDelete(tempEyes, 45);
		ObjRender_SetScaleXYZ(eyes, 2, j, 1);
		if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <= 0 || Obj_IsDeleted(objEnemy)){
			Obj_Delete(eyes);
			
		}
		j += 2/10;
		yield
	}
	j = 2;
	loop(50){
		let tempEyes = CreateEffectObject(GetCenterX,GetCenterY,GetCurrentScriptDirectory~"./img/eyes.png","ARGB",0,0,192,64);
		Obj_SetRenderPriority(eyes,0.72);
		ObjRender_SetScaleXYZ(tempEyes, j, j, 1);
		Obj_FadeDelete(tempEyes, 45);
		ObjRender_SetAlpha(red, i);
		ObjRender_SetScaleXYZ(eyes, j, j, 1);
		i += alpha;
		j -= scale;
		if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <= 0 || Obj_IsDeleted(objEnemy)){
			Obj_Delete(eyes);
			
		}
		yield;
	}
	loop(120){
		ObjRender_SetAlpha(red, i);
		i += alpha;
		yield;
		if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <= 0 || Obj_IsDeleted(objEnemy)){
			Obj_Delete(eyes);
			
		}
	}
	Obj_Delete(red);
	Obj_Delete(eyes);
	redeyes = 1;
}


task BossDeleteRender(obj){
	while(!Obj_IsDeleted(obj)){
		if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <= 0 || Obj_IsDeleted(objEnemy)){
			Obj_Delete(obj);
			
		}
		yield;
	}
}

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