#東方弾幕風[Single]
#ScriptVersion[3]
#Title["S2"]
#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; 

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		
		SetScriptResult(5000);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(80);
	}
	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("Half Moon \"Peduncular Hallucinosis\"",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;
}

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 delay = 60*(1.5 + 5);
	let angle = 0;
	let n = 35;
	loop{
		n = 35;
		delay = 60*(1+3);
		angle = GetAngleToPlayer(objEnemy);
		ShotGunShoot(5, angle, delay);
		delay -= 60;
		wait(60);
		loop(2){
			ShotGunShoot(5, angle + n, delay);
			ShotGunShoot(5, angle - n, delay);
			n *= 2;
			delay -= 60;
			wait(60);
		}
		wait(60*7);
		delay = (60 + 60) + 60*2;
		angle = rand(0, 360);
		loop(3){
			ShotGunShoot(5, angle, delay);
			delay -= 10;
			angle += 360/3;
			wait(10);
		}
		delay-= 60;
		wait(60);
		angle += 360/6;
		loop(3){
			ShotGunShoot(5, angle, delay);
			delay -= 10;
			angle += 360/3;
			wait(10);
		}
		wait(60*7);
		delay = 60*(1+3);
		loop(3){
			angle = GetAngleToPlayer(objEnemy);
			ShotGunShoot(5, angle, delay);
			delay -= 60;
			wait(60);
		}
		wait(60*7);
	}
	
}

task ShotGunShoot(speed, angle, delay){
	let way1 = 0;
	let way2 = 0;
	LaserAim(angle);
	let obj = CreateShotA2(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), speed, angle, -0.04, 0, ORB_GRAY, 0);
	Reflect(obj, 30, 180);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjRender_SetAlpha(obj, 0);
	ObjShot_SetIntersectionEnable(obj, false);	
	while(ObjMove_GetSpeed(obj) > 0){
		yield;
	}
	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;}
	PolygonShotAimed(ObjMove_GetX(obj), ObjMove_GetY(obj), speed, angle, way1, way2, 1, BULLET_GRAY, 30, delay);
	wait(30);
	SE_Play(explode, 70);
	
	wait(delay-30);
	Obj_Delete(obj);
	SE_Play(charge4, 70);
	RedEyes;
	BgmVolDecrease(1, 180);
	SE_Play(boon0, 70);
	wait(180);
	

}

task LaserAim(angle){
	let i = angle;
	let j = angle;
	let n = 150;
	let dif = 20/120;
	//SE_Play(boon0, 70);
	loop(120){
		let laser1 = CreateStraightLaserA1(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), i, 1000, 10, 60*2, C_LASER_DARKRED, 1);
		let laser2 = CreateStraightLaserA1(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), j, 1000, 10, 60*2 + 30,  C_LASER_DARKRED, 1);
		ObjShot_SetIntersectionEnable(laser1, false);
		ObjShot_SetIntersectionEnable(laser2, false);
		i += dif;
		j -= dif;
		yield;
		Obj_FadeDelete(laser1, n);
		Obj_FadeDelete(laser2, n);
		n--;
	}
	SE_Play(EXattack, 70);
}
function PolygonShotAimed(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(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), rand(speed, speed+8), angle + rand(-20, 20), graphic, delay);
			
			Reflect(obj, 3, delay2);
			ObjRender_SetColorHSV(obj, 0, 0, 255);
			DelayedAction(obj, x, y, vs*(speed-4), va, delay2);
        }
    }
    
    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, angle, delay){
	ObjMove_SetMaxSpeed(obj, 0);
	ObjMove_SetAcceleration(obj, -0.2);
	wait(delay);
	ObjMove_SetMaxSpeed(obj, 10);
	ObjMove_SetAcceleration(obj, 0);
	ShiftColor(obj, 180, 180, rand(0, 359*10)/10);
	ShiftAngle(obj, 180, angle);
	ObjMove_SetDestAtFrameWeight(obj, x + ((speed*20)) * cos(angle), y + ((speed*20)) * sin(angle), 180);
	wait(180);
	if(!Obj_IsDeleted(obj)){
		ObjMove_SetAngle(obj, angle);
		ObjMove_SetSpeed(obj, speed);
	}
}

task AtDest(obj, x, y, speed){
	let i = 1;
	while(i && !Obj_IsDeleted(obj)){
		if(ObjMove_GetX(obj) == x && ObjMove_GetY(obj) == y){
			i = 0;
		}
		yield;
	}
	
}

task ShiftAngle(obj, delay, angle){

	wait(delay);
	ObjMove_SetAngle(obj, angle);
}

task ShiftColor(obj, delay, delay2, hue){
	let i = 0;
	let j = 0;
	let n = 1;
	let dif = hue - i;
	let dif2 = 255 / delay2;
	if(hue < 359/2){
		i = 359;
		n = -1;
		dif = i - hue;
	}
	dif = dif / delay * n;
	loop(delay){
		if(delay2 > 0){
			j += dif2;
		}
		ObjRender_SetColorHSV(obj, i, j, 255);
		i += dif;
		yield;
	}
	ObjRender_SetColorHSV(obj, hue, 255, 255);
}

task Reflect(obj, bounce, delay)
{
	let i = 0;
    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_GetY(obj)>GetClipMaxY)
            {
                ObjMove_SetAngle(obj,-ObjMove_GetAngle(obj));
                value--
            }
        }
        if(value==0){break;}
		i++;
		if(i == delay){break;}
        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;
	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);
}

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


