#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 2"]
#Text["Wooooooop"]

let objBoss;
#include "./Functions/Index.txt"


@Initialize {
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/death2.wav", 90);
	ObjEnemy_SetDamageRate(objBoss,0,0);
	ObjMove_SetDestAtSpeed(objBoss,384/2,200,4);
	
	mainTask;	
} 

@Event { 
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(1500);
        }
	case(EV_REQUEST_TIMER){
		SetScriptResult(55);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(1000000);
	}
} 

let BossX = 0;
let BossY = 0;

@MainLoop {
	BossX = ObjMove_GetX(objBoss);
	BossY = ObjMove_GetY(objBoss);
	
	ObjEnemy_SetIntersectionCircleToShot(objBoss,BossX,BossY,24);
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,BossX,BossY,24);
	
	yield;
} 

@Finalize {

} 

task mainTask { 
	renderdiancie(objBoss);
	BottomEnemyMarker(objBoss, objBoss);
	end;
	action;
	BossShotSound(objBoss, 75, 200);
} 

task SpellCall {
	bspell = true;
	TCallBg2;
}
let Diancie = GetCurrentScriptDirectory ~ "Functions/img/Cutin/DiancieCutin.png";
task action{
wait(60);
ObjCutin_SetSpellcardS3("Lunar &quot;Moonblast&quot;", 255, 15, 200);
ObjCutin_LaunchS3("KANAKO", Diancie,"Normal");
SpellCall;
let objScene = GetEnemyBossSceneObjectID();
ObjEnemyBossScene_StartSpell(objScene);
wait(180);
Attack;
wait(180);
ObjEnemy_SetDamageRate(objBoss,26,30);
}

task Attack {
	loop{
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/chargelong.wav", 100);
	ConcentrationA1(objBoss,"MAGIC",180,3,120,[255,150,255]);
	wait(180);
		Moonblast(objBoss,2,GetAngleToPlayer(objBoss),0.4);
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/barrage.wav", 100);
		wait(600);
	}
}



task Moonblast(tar,speed,angle,scale){ //OR A SMALL SUN! 						........Please don't hurt me...
	let mx = speed*cos(angle);
	let my = speed*sin(angle);
	let counter = 0;
	let Xscale = 0;
	let Yscale = 0;
	let Xscale2 = 0;
	let Yscale2 = 0;
	let Zangle = 0;
	let alpha = 255;
	let pointer = CreateShotA1(ObjMove_GetX(tar),ObjMove_GetY(tar)-20,0,-angle,0,0);
	ObjMove_AddPatternA4(pointer, 120, 0, NO_CHANGE, 0.1, 0, 10, 0, NO_CHANGE);
	ObjRender_SetAlpha(pointer, 0);
	ObjShot_SetSpellResist(pointer, 1);
	ObjShot_SetIntersectionEnable(pointer,false);
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetPosition(obj,ObjMove_GetX(tar),ObjMove_GetY(tar)-20,0);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"./Functions/img/nuclear2.png");
	ObjSprite2D_SetSourceRect(obj,0, 0, 256, 256);
	ObjRender_SetAlpha(obj,255);
	Obj_SetRenderPriorityI(obj,66);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjSprite2D_SetDestCenter(obj);	
	ObjRender_SetAngleZ(obj,Zangle);
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetPosition(obj2,ObjMove_GetX(tar),ObjMove_GetY(tar)-20,0);
	ObjPrim_SetTexture(obj2, GetCurrentScriptDirectory~"./Functions/img/nuclear3.png");
	ObjSprite2D_SetSourceRect(obj2,0, 0, 256, 256);
	ObjRender_SetAlpha(obj2,255);
	Obj_SetRenderPriorityI(obj2,65);
	ObjRender_SetBlendType(obj2,BLEND_ALPHA);
	ObjRender_SetColor(obj2,255,90,255);
	ObjSprite2D_SetDestCenter(obj2);
	Boing(pointer);
	loop(40){counter++;
	ObjRender_SetScaleXYZ(obj,Xscale,Yscale,0);
	ObjRender_SetScaleXYZ(obj2,Xscale2,Yscale2,0);
				if(Xscale<scale){Xscale+=scale/40;
		Xscale2+=scale/40;}
		if(Yscale<scale){Yscale+=scale/40;
		Yscale2+=scale/40;}
		
			Xscale2=Xscale+Xscale/10-(Xscale/20)*cos(counter*10);
			Yscale2=Yscale+Yscale/10-(Yscale/20)*cos(counter*10);
			yield;
	}
	loop(719){
		ObjRender_SetPosition(obj,ObjRender_GetX(pointer),ObjRender_GetY(pointer),0);			
		ObjRender_SetAngleZ(obj,Zangle);
		Zangle+=20;
		
		ObjRender_SetPosition(obj2,ObjRender_GetX(pointer),ObjRender_GetY(pointer),0);			
		ObjEnemy_SetIntersectionCircleToPlayer(tar, ObjRender_GetX(obj)+mx,ObjRender_GetY(obj)+my, 92*Xscale);
		if(Obj_IsDeleted(pointer)){Obj_Delete(obj);Obj_Delete(obj2);}
		yield;
	}
	loop(32){
	CreateShotA2(ObjMove_GetX(pointer)+50*cos(angle),ObjMove_GetY(pointer)+50*sin(angle),0.1,angle,0.02, 5,BALL_S_PINK,0);
	angle += 360/32;
	}
	angle += 360/64;
	loop(32){
	CreateShotA2(ObjMove_GetX(pointer)+50*cos(angle),ObjMove_GetY(pointer)+50*sin(angle),0.1,angle,0.015, 5,BALL_S_GRAY,0);
	angle += 360/32;
	}
	angle += 360/64;
	loop(32){
	CreateShotA2(ObjMove_GetX(pointer)+50*cos(angle),ObjMove_GetY(pointer)+50*sin(angle),0.1,angle,0.01, 5,BALL_S_GRAY,0);
	angle += 360/32;
	}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/barrage2.wav", 100);
	loop(255){
		ObjRender_SetAlpha(obj,alpha);
		ObjRender_SetAlpha(obj2,alpha);
		ObjMove_SetSpeed(pointer,0.01);
		alpha -= 5;
		wait(1);
		if(alpha<0){Obj_Delete(obj);Obj_Delete(obj2);Obj_Delete(pointer);return;}
	}
	Obj_Delete(obj);
	Obj_Delete(obj2);
	Obj_Delete(pointer);
	task Boing(obj){
    while(!Obj_IsDeleted(obj)){
	let spd = ObjMove_GetSpeed(obj)/2;
	let angle = rand(0,360);
	if(trunc(ObjRender_GetX(obj))>384){
		ObjMove_SetAngle(obj,180 - ObjMove_GetAngle(obj));
		boop(objBoss);
		boop(obj);
	}
	if(trunc(ObjRender_GetX(obj))<0){      
        ObjMove_SetAngle(obj,180 - ObjMove_GetAngle(obj));
		boop(objBoss);
		boop(obj);
    }
    if(trunc(ObjMove_GetY(obj))>448){
		ObjMove_SetAngle(obj,360 - ObjMove_GetAngle(obj));
		boop(objBoss);
    }
    if(trunc(ObjRender_GetY(obj))<0){
		ObjMove_SetAngle(obj,360 - ObjMove_GetAngle(obj));
		boop(objBoss);
    }
yield;
}
}
}

task boop(tar){
	let angle = rand(0,360);
	loop(22){
	CreateShotA1(ObjMove_GetX(tar)+25*cos(angle),ObjMove_GetY(tar)+25*sin(angle),1,angle,BALL_S_RED,5);
	angle += 360/22;
	}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/spirit.wav", 50);
}

task end{
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
    Obj_Delete(objBoss);
    DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
}

