//Dedicated Spell Background Script

let CSD = GetCurrentScriptDirectory;
let MD = CSD~"../";
let array = [];
let stoptask = false;

#include"./Event_Const.dnh" //this defines EV_SPELLBG and stuff

@Initialize {
//idk what do here
}

@MainLoop {
	yield;
}

@Event {
	alternative(GetEventType)
	case(EV_SPELLBG) {
		let eventParam = GetEventArgument(0);
		CreateSpellBackground(eventParam);
	}
	case(EV_SPELLBGEND) {
		let eventParam = GetEventArgument(0);
		StopSpellBG;
	}
	
}

task CreateSpellBackground(arg){
let angle = 0;
let angle2 = 0;

let alpha = 0;
let alpha2 = 0;

let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);


if(arg==0){
	array = array ~ [obj1];
	let path1 = CSD ~ "spellbg/sakuya0.png";
	ObjRender_SetBlendType(obj1, BLEND_ALPHA);
	Obj_SetRenderPriority(obj1, 0.24);
	ObjPrim_SetTexture(obj1, path1);
	ObjSprite2D_SetSourceRect(obj1, 0, 0, 256, 256);
	ObjSprite2D_SetDestCenter(obj1);
	ObjRender_SetPosition(obj1, 384/2, 448/2, 0);
	ObjRender_SetScaleX(obj1, 2.5);
	ObjRender_SetScaleY(obj1, 2.5);
	Obj_SetValue(obj1, "maxalpha", 255);
	}
	
if(arg==1){
	array = array ~ [obj1];
	let path1 = CSD ~ "spellbg/alice0.png";
	ObjRender_SetBlendType(obj1, BLEND_ALPHA);
	Obj_SetRenderPriority(obj1, 0.24);
	ObjPrim_SetTexture(obj1, path1);
	ObjSprite2D_SetSourceRect(obj1, 0, 0, 512, 512);
	ObjSprite2D_SetDestCenter(obj1);
	ObjRender_SetPosition(obj1, 384/2, 448/2, 0);
	ObjRender_SetScaleX(obj1, 1.25);
	ObjRender_SetScaleY(obj1, 1.25);
	Obj_SetValue(obj1, "maxalpha", 150);
	
	array = array ~ [obj2];
	let path2 = CSD ~ "spellbg/alice1.png";
	ObjRender_SetBlendType(obj2, BLEND_ALPHA);
	Obj_SetRenderPriority(obj2, 0.23);
	ObjPrim_SetTexture(obj2, path2);
	ObjSprite2D_SetSourceRect(obj2, 0, 0, 512, 512);
	ObjSprite2D_SetDestCenter(obj2);
	ObjRender_SetPosition(obj2, 384/2, 448/2, 0);
	ObjRender_SetScaleX(obj2, 1);
	ObjRender_SetScaleY(obj2, 1);
	Obj_SetValue(obj2, "maxalpha", 255);
	
	}

if(arg==2){
	array = array ~ [obj1];
	let path1 = CSD ~ "spellbg/reimu0.png";
	ObjRender_SetBlendType(obj1, BLEND_ALPHA);
	Obj_SetRenderPriority(obj1, 0.23);
	ObjPrim_SetTexture(obj1, path1);
	ObjSprite2D_SetSourceRect(obj1, 0, 0, 512, 512);
	ObjSprite2D_SetDestCenter(obj1);
	ObjRender_SetPosition(obj1, 384/2, 448/2, 0);
	ObjRender_SetScaleX(obj1, 1.2);
	ObjRender_SetScaleY(obj1, 1.2);
	Obj_SetValue(obj1, "maxalpha", 255);
	
	array = array ~ [obj2];
	let path2 = CSD ~ "spellbg/reimu1.png";
	ObjRender_SetBlendType(obj2, BLEND_ALPHA);
	Obj_SetRenderPriority(obj2, 0.24);
	ObjPrim_SetTexture(obj2, path2);
	ObjSprite2D_SetSourceRect(obj2, 0, 0, 512, 512);
	ObjSprite2D_SetDestCenter(obj2);
	ObjRender_SetPosition(obj2, 384/2, 448/2, 0);
	ObjRender_SetScaleX(obj2, 1.25);
	ObjRender_SetScaleY(obj2, 1.25);
	Obj_SetValue(obj2, "maxalpha", 150);
	
	}
	
if(arg==3){
	array = array ~ [obj1];
	let path1 = CSD ~ "spellbg/mima0.png";
	ObjRender_SetBlendType(obj1, BLEND_ALPHA);
	Obj_SetRenderPriority(obj1, 0.24);
	ObjPrim_SetTexture(obj1, path1);
	ObjSprite2D_SetSourceRect(obj1, 0, 0, 512, 512);
	ObjSprite2D_SetDestCenter(obj1);
	ObjRender_SetPosition(obj1, 384/2, 448/2, 0);
	ObjRender_SetScaleX(obj1, 1);
	ObjRender_SetScaleY(obj1, 1);
	Obj_SetValue(obj1, "maxalpha", 255);
	}

if(arg==4){
	array = array ~ [obj1];
	let path1 = CSD ~ "spellbg/cirno1.png";
	ObjRender_SetBlendType(obj1, BLEND_ALPHA);
	Obj_SetRenderPriority(obj1, 0.24);
	ObjPrim_SetTexture(obj1, path1);
	ObjSprite2D_SetSourceRect(obj1, 0, 0, 512, 512);
	ObjSprite2D_SetDestCenter(obj1);
	ObjRender_SetPosition(obj1, 384/2, 448/2, 0);
	ObjRender_SetScaleX(obj1, 1);
	ObjRender_SetScaleY(obj1, 1);
	Obj_SetValue(obj1, "maxalpha", 255);
	}	

if(arg==5){
	array = array ~ [obj1];
	let path1 = CSD ~ "spellbg/letty0.png";
	ObjRender_SetBlendType(obj1, BLEND_ALPHA);
	Obj_SetRenderPriority(obj1, 0.24);
	ObjPrim_SetTexture(obj1, path1);
	ObjSprite2D_SetSourceRect(obj1, 0, 0, 512, 512);
	ObjSprite2D_SetDestCenter(obj1);
	ObjRender_SetPosition(obj1, 384/2, 448/2, 0);
	ObjRender_SetScaleX(obj1, 1);
	ObjRender_SetScaleY(obj1, 1);
	Obj_SetValue(obj1, "maxalpha", 255);
	}			
	
loop{

if(alpha<255){ alpha+=6; }

if(arg==5){
ObjRender_SetAlpha(obj1, alpha);
ObjSprite2D_SetSourceRect(obj1, 0+angle, 0-angle, 512+angle, 512-angle);
angle+=1;
}

if(arg==0){
ObjRender_SetAlpha(obj1, alpha);
ObjRender_SetAngleXYZ(obj1, 1, 1, angle);
angle+=0.5;
}

if(arg==1){
ObjRender_SetAlpha(obj1, alpha*0.58823529411);
ObjRender_SetAlpha(obj2, alpha);
ObjRender_SetAngleXYZ(obj1, 1, 1, angle);
angle-=1;
}

if(arg==2){
ObjRender_SetAlpha(obj1, alpha);
ObjRender_SetAlpha(obj2, alpha*0.58823529411);
ObjRender_SetAngleXYZ(obj2, 1, 1, angle);
ObjRender_SetAngleXYZ(obj1, 1, 1, -(angle*0.25));
angle+=0.6;
}

if(arg==3 || arg == 4){
ObjRender_SetAlpha(obj1, alpha);
}

if(stoptask == true){ return; } 
yield;

}

}

task StopSpellBG{
let alphacontrol = 34;
stoptask = true;
 loop(35){
	ascent(i in 0..length(array)){
	if(alphacontrol==0){ Obj_Delete(array[i]); erase(array, i); }
	if(!Obj_IsDeleted(array[i])){
	ObjRender_SetAlpha(array[i], alphacontrol*(Obj_GetValueD(array[i], "maxalpha", 255)/35)); 
	erase(array, i);
	}
	}
	alphacontrol--;
	yield;
	
	}
	
array = [];	
yield;
stoptask = false;
}
