#東方弾幕風[Single]
#ScriptVersion[3]
#Title["S4"]
#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(8000);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(100);
	}
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(3);
	}
	
}

@Initialize
{
	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("Lunatic Full Moon \"Strabismus Bomb\"",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;
	wait(180 + 60);
}

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(){
	
	loop{
		if(redeyes == 1){
			let angle = 0;
			let angle2 = 0;
			loop(6){
				TrailShot(1, angle*0 , angle2 , 24, 2);
				angle2 += 360/6;
				angle += 120/12;
			}
			angle2 += 360/12;
			wait(5);
			loop(6){
				TrailShot(3.1, angle*0, angle2 , 28, 1);
				angle2 += 360/6;
				angle += 120/12;
			}
			wait(180*4);
			callRedEyes();
		}
		
		yield;
			
	}
}

task callRedEyes(){
	
		SE_Play(charge4, 70);
		RedEyes();
		BgmVolDecrease(1, 210);
		SE_Play(boon0, 70);
		wait(180 + 180);
}



task TrailShot(speed, angle, angle2, delay, n){
	let obj1 =  CreateShotA1(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), 5+speed, angle2, BULLET_RED, 10);
	ShotTrail(obj1, angle2, BULLET_RED, 10);
	SE_Play(shot1, 50);
	wait(delay/2);
	while(!Obj_IsDeleted(obj1)){
		bomb(obj1, delay);
		wait(delay);
		let GetClipMaxX = GetStgFrameWidth();
		let GetClipMaxY = GetStgFrameHeight();
		if(!Obj_IsDeleted(obj1)){
			PolygonShot(ObjMove_GetX(obj1), ObjMove_GetY(obj1), 0.5, angle, 6, 7, 1, BULLET_RED, delay, angle2);
		}
		
		SE_Play(explode, 70);
		n--;
		if(n == 0){break;}
	}
	if(!Obj_IsDeleted(obj1)){Obj_Delete(obj1);}
}
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 PolygonShot(x, y, speed, angle, way, way2, way_angle, graphic, delay, n){

    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, speed*vs, va, graphic, delay);
			ObjRender_SetBlendType(obj, BLEND_ALPHA);
			ShapeMove(obj, speed, va, vs, 20, n);
        }
    }
    
    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 ShapeMove(obj, speed, angle, vs, frames, n){
	while(redeyes == 1){
		yield;
		
		frames++;
	}
	if(!Obj_IsDeleted(obj)){
		let random = rand(-100, 100);
		let x = 0;
		if(random >= 0){x = ObjMove_GetX(objPlayer);}
		else{ 
			x = (GetCenterX - ObjMove_GetX(objPlayer));
			x +=  GetCenterX;
		}
		ObjShot_SetIntersectionEnable(obj, false);
		ObjRender_SetAlpha(obj, 100);
		ObjMove_SetDestAtFrameWeight(obj,  x + ((vs*frames)/5) * cos(angle), ObjMove_GetY(objPlayer) + ((vs*frames)/5) * sin(angle), 180);
		wait(180);
		if(!Obj_IsDeleted(obj)){
			
			let obj2 = CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), speed, angle + n/200 + 180, BULLET_RED, 10);
			Obj_Delete(obj);
			ShapeMove(obj2, speed, angle + 180, vs, frames, n);
		
		}
	}
}

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 bomb(obj, delay){
	wait(delay);
	if(!Obj_IsDeleted(obj)){
		let bomb = CreateEffectObject(GetCenterX,GetCenterY,GetCurrentScriptDirectory~"./img/bomb.png","ALPHA",0,0,64,64);
		BossDeleteRender(bomb);
		Obj_SetRenderPriority(bomb,0.72);
		ObjRender_SetX(bomb, ObjMove_GetX(obj));
		ObjRender_SetY(bomb, ObjMove_GetY(obj));
		ObjRender_SetScaleXYZ(bomb, 0, 0, 1);
		let dif = 1.5/15;
		let i = 0;
		loop(7){
			ObjRender_SetScaleXYZ(bomb, i, i, 1);
			i += dif*2;
			yield;
		}
		let random = 0.05;
		let n = 1;
		loop(45){
			ObjRender_SetScaleXYZ(bomb, i + (random * n), i + (random * n), 1);
			n *= -1;
			wait(2);
		}
		loop(15){
			ObjRender_SetScaleXYZ(bomb, i, i, 1);
			i -= dif;
			
			yield;
		}
		Obj_Delete(bomb);
	}
}


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