

let MainSTG = GetMainStgScriptDirectory;
let BossImage = MainSTG~"source/boss/marisa.png"; 
let BossCutIn = MainSTG~"source/boss/cutin.png"; 
let img_effect = MainSTG~"cirno_system/img/effect.png"; // Effect Sheet
let img_effect2 = MainSTG~"cirno_system/img/effect2.png"; // Effect2 Sheet
let img_fairy = MainSTG~"cirno_system/img/enemy.png"; // Fairy Sheet
let GetCenterX = GetStgFrameWidth/2;
let GetCenterY = GetStgFrameHeight/2;
let spellget = 0;
let bosstime = 99;
let IsAttackAnimPlaying = false;
let Anim_Attack = 0;

function CreateBossObject(SBIimage, rec1, rec2, rec3, rec4){
	let CBOboss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(CBOboss);
	ObjPrim_SetTexture(CBOboss, SBIimage); 
	ObjSprite2D_SetSourceRect(CBOboss, rec1, rec2, rec3, rec4); 
	ObjSprite2D_SetDestCenter(CBOboss);
	return CBOboss;
}

function CreateEnemyObject(CEOlife, SBIimage, rec1, rec2, rec3, rec4){
	let CEOboss = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(CEOboss);
	ObjPrim_SetTexture(CEOboss, SBIimage); 
	ObjSprite2D_SetSourceRect(CEOboss, rec1, rec2, rec3, rec4); 
	ObjSprite2D_SetDestCenter(CEOboss);
	ObjEnemy_SetLife(CEOboss, CEOlife);
	return CEOboss;
}

function CreateFairyObject(CEOlife){
	let CEOboss = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(CEOboss);
	ObjPrim_SetTexture(CEOboss, img_fairy); 
	ObjEnemy_SetLife(CEOboss, CEOlife);
	return CEOboss;
}

function DrawText(txtStr, txtX, txtY, txtSize, txtWidth, presetTxtColor, txtPriority){
	let SnowDrawText = ObjText_Create;
	ObjText_SetText(SnowDrawText,txtStr);
	SetTextColor;
	ObjText_SetFontSize(SnowDrawText,txtSize);
	ObjText_SetHorizontalAlignment(SnowDrawText, ALIGNMENT_LEFT);
	ObjText_SetFontBorderWidth(SnowDrawText,1);
	ObjText_SetFontBorderType(SnowDrawText,BORDER_FULL);
	ObjText_SetMaxWidth(SnowDrawText, txtWidth);
	Obj_SetRenderPriority(SnowDrawText, txtPriority);
	ObjRender_SetPosition(SnowDrawText, txtX, txtY, 0);
	
	task SetTextColor{
		if(presetTxtColor=="black"){
			ObjText_SetFontColorTop(SnowDrawText,0,0,0);
			ObjText_SetFontColorBottom(SnowDrawText,0,0,0);
			ObjText_SetFontBorderColor(SnowDrawText,161,161,255);
		}
		if(presetTxtColor=="red"){
			ObjText_SetFontColorTop(SnowDrawText,175,20,111);
			ObjText_SetFontColorBottom(SnowDrawText,190,10,123);
			ObjText_SetFontBorderColor(SnowDrawText,0,0,64);
		}
		if(presetTxtColor=="blue"){
			ObjText_SetFontColorTop(SnowDrawText,20,80,175);
			ObjText_SetFontColorBottom(SnowDrawText,80,50,175);
			ObjText_SetFontBorderColor(SnowDrawText,0,0,64);
		}
	}
	
	return SnowDrawText;
}

task ConcentrationA1{

}

task LoadSoundEffects{
	LoadSound(MainSTG~"source/Sounds/alarm.wav");
	LoadSound(MainSTG~"source/Sounds/timeout.wav");
	LoadSound(MainSTG~"source/Sounds/timeout2.wav");
	LoadSound(MainSTG~"source/Sounds/spell.wav");
	LoadSound(MainSTG~"source/Sounds/spellfinal.wav");
	LoadSound(MainSTG~"source/Sounds/laser1.wav");
	LoadSound(MainSTG~"source/Sounds/laser2.wav");
	LoadSound(MainSTG~"source/Sounds/master.wav");
	LoadSound(MainSTG~"source/Sounds/final.wav");
	LoadSound(MainSTG~"source/Sounds/barrage.wav");
	LoadSound(MainSTG~"source/Sounds/charge.wav");
	LoadSound(MainSTG~"source/Sounds/chargelong.wav");
	LoadSound(MainSTG~"source/Sounds/chargepower.wav");
	LoadSound(MainSTG~"source/Sounds/spirit.wav");
	LoadSound(MainSTG~"source/Sounds/smbullet.wav");
	LoadSound(MainSTG~"source/Sounds/labullet.wav");
	LoadSound(MainSTG~"source/Sounds/death.wav");
	LoadSound(MainSTG~"source/Sounds/death2.wav");
	LoadSound(MainSTG~"source/Sounds/damage1.wav");
	LoadSound(MainSTG~"source/Sounds/damage2.wav");
	LoadSound(MainSTG~"source/Sounds/damage3.wav");
}

task PlaySound(str){
	//Player sounds
	
	if(str == "shot"){PlaySE(MainSTG~"source/Sounds/shot.wav");}
	
	//Boss sounds
	if(str == "alarm"){PlaySE(MainSTG~"source/Sounds/alarm.wav");}
	if(str == "timeout"){PlaySE(MainSTG~"source/Sounds/timeout.wav");}
	if(str == "timeout2"){PlaySE(MainSTG~"source/Sounds/timeout2.wav");}
	if(str == "spell"){PlaySE(MainSTG~"source/Sounds/spell.wav");}
	if(str == "spellfinal"){PlaySE(MainSTG~"source/Sounds/spellfinal.wav");}
	if(str == "laser1"){PlaySE(MainSTG~"source/Sounds/laser1.wav");}
	if(str == "laser2"){PlaySE(MainSTG~"source/Sounds/laser2.wav");}
	if(str == "master"){PlaySE(MainSTG~"source/Sounds/master.wav");}
	if(str == "final"){PlaySE(MainSTG~"source/Sounds/final.wav");}
	if(str == "barrage"){PlaySE(MainSTG~"source/Sounds/barrage.wav");}
	if(str == "charge"){PlaySE(MainSTG~"source/Sounds/charge.wav");}
	if(str == "chargelong"){PlaySE(MainSTG~"source/Sounds/chargelong.wav");}
	if(str == "chargepower"){PlaySE(MainSTG~"source/Sounds/chargepower.wav");}
	if(str == "spirit"){PlaySE(MainSTG~"source/Sounds/spirit.wav");}
	if(str == "smbullet"){PlaySE(MainSTG~"source/Sounds/smbullet.wav");}
	if(str == "labullet"){PlaySE(MainSTG~"source/Sounds/labullet.wav");}
	if(str == "death"){PlaySE(MainSTG~"source/Sounds/death.wav");}
	if(str == "death2"){PlaySE(MainSTG~"source/Sounds/death2.wav");}
	if(str == "damage1"){PlaySE(MainSTG~"source/Sounds/damage1.wav");}
	if(str == "damage2"){PlaySE(MainSTG~"source/Sounds/damage2.wav");}
	if(str == "damage3"){PlaySE(MainSTG~"source/Sounds/damage3.wav");}
}

task PlayEnemyHitSound(SoundHitEffectObjectID, hitsoundtype){
	let bosshp = ObjEnemy_GetInfo(SoundHitEffectObjectID, INFO_LIFE);
	let origbosshp = ObjEnemy_GetInfo(SoundHitEffectObjectID, INFO_LIFE);
	
	while(enemydead == false && Obj_IsDeleted(SoundHitEffectObjectID) == false){
		if(bosshp != ObjEnemy_GetInfo(SoundHitEffectObjectID, INFO_LIFE) && hitsoundtype == 1){
			if(ObjEnemy_GetInfo(SoundHitEffectObjectID, INFO_LIFE) > origbosshp/12){PlaySound("damage1");}
			if(ObjEnemy_GetInfo(SoundHitEffectObjectID, INFO_LIFE) < origbosshp/12){PlaySound("damage2");}
			SetAreaCommonData("CirnoPlayer", "IncreaseIce", 0.8);
		}
		if(bosshp != ObjEnemy_GetInfo(SoundHitEffectObjectID, INFO_LIFE) && hitsoundtype == 2){PlaySound("damage1");}
		if(bosshp != ObjEnemy_GetInfo(SoundHitEffectObjectID, INFO_LIFE) && hitsoundtype == 3){PlaySound("damage3");}
		
		bosshp = ObjEnemy_GetInfo(SoundHitEffectObjectID, INFO_LIFE);
		yield;
	}
}

task SetInvincibility(SnowEnemyObject, TFRAMES, SIshot, SIbomb){
	ObjEnemy_SetDamageRate(SnowEnemyObject, 0, 0);	
	loop(TFRAMES){yield;}
	ObjEnemy_SetDamageRate(SnowEnemyObject, SIshot, SIbomb);	
}

task StarOnBoss(SnowEnemyObject){ 
	
	let objStar = ObjPrim_Create(OBJ_SPRITE_2D);
	let objxpos=ObjMove_GetX(SnowEnemyObject);
	let objypos=ObjMove_GetY(SnowEnemyObject);
	let object_time = 30;
	let obj_angle = 330;
	let osize = 0;
	let obj_sizeset = 0;
	let sizeinc = 5;
	let angleinc = 5;
	let fullsize = 0;

	ObjRender_SetBlendType(objStar, BLEND_ALPHA );
	Obj_SetRenderPriority(objStar, 0.39);
	ObjPrim_SetTexture(objStar, img_effect);
	ObjSprite2D_SetSourceRect(objStar, 128, 80, 256, 208);
	ObjSprite2D_SetDestRect(objStar, -64, -64, 64, 64);
	
	
	ObjRender_SetAlpha(objStar, 205);
	
	while(!Obj_IsDeleted(SnowEnemyObject)){
		// 0 = Bigger; 1 = Smaller

		objxpos = ObjMove_GetX(SnowEnemyObject);
		objypos = ObjMove_GetY(SnowEnemyObject);

		
		StarSize;
		ObjRender_SetX(objStar, objxpos);
		ObjRender_SetY(objStar, objypos);
		ObjRender_SetScaleXYZ(objStar, osize/95, osize/95, 1);
		ObjRender_SetAngleZ(objStar, obj_angle);
		//if(BossDead==1){Obj_Delete(obj);}
		yield;
	}
	Obj_Delete(objStar);
	
	task StarSize{
		let ax2 = 0.04;
		let bx2 = 1.35;
		if(fullsize==2){
			if(obj_sizeset==0){sizeinc+=ax2}
			if(obj_sizeset==1){sizeinc-=ax2}
			
			if(sizeinc<-bx2){obj_sizeset=0}
			if(sizeinc>bx2){obj_sizeset=1}
			
			obj_angle-=angleinc;
			
			if(angleinc<5){angleinc+=0.5}else{angleinc=5;}
			
			osize+=sizeinc;
		}
		if(fullsize!=2){obj_angle-=angleinc;angleinc-=0.06;}
		if(osize<145&&fullsize==0){osize+=5;}
		if(osize>144&&fullsize==0){
			fullsize=1;
			loop(55){yield;}
			sizeinc=0;
			angleinc=0;
			fullsize=2;
		}			
	}	
}

task ExplosionA1(deX, deY, angX, angY, angZ){
	
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ALPHA );
	Obj_SetRenderPriority(obj, 0.50);
	ObjPrim_SetTexture(obj, img_effect);
	ObjSprite2D_SetSourceRect(obj, 129, 17, 191, 79);
	ObjSprite2D_SetDestRect(obj, -32, -32, 32, 32);
	ObjRender_SetAngleXYZ(obj, angX, angY, angZ);
	ObjRender_SetPosition(obj, deX, deY, 0);
	
	let alpha = 255;
	let scale = 0;
	let expo = 0.4;
	let RandZ = rand(-170,170);
	while(Obj_IsDeleted(obj)==false){
	
		if(alpha>100){scale += expo;}else{scale -= expo;}
		alpha -= 5;
		if(expo>0){expo = expo^1.07;}
		
		ObjRender_SetAlpha(obj, alpha);
		ObjRender_SetScaleXYZ(obj, scale*1.1, scale*1.1, 1);
		
		if(alpha<0){Obj_Delete(obj)}
		yield;
	}
	Obj_Delete(obj);
}

task ExplosionA2(deX, deY, angX, angY, angZ, deScale){
	
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 0.60);
	ObjPrim_SetTexture(obj, img_effect);
	ObjSprite2D_SetSourceRect(obj, 32, 228, 64, 252);
	ObjSprite2D_SetDestRect(obj, -16, -12, 16, 12);
	ObjRender_SetAngleXYZ(obj, angX, angY, angZ);
	ObjRender_SetPosition(obj, deX, deY, 0);
	
	let alpha = 125;
	let scale = deScale;
	let randX = rand(-500, 500);
	let randY = rand(-500, 500);
	let RandZ = rand(-170,170);
	//atan2(randY - deX, randX - deY);
	while(Obj_IsDeleted(obj)==false){
		scale += -0.05;
		alpha += -0.1;
		deX += randX/100;
		deY += randY/100;
		angX += randX/85;
		angY += randY/85;
		angZ += randX/135+randY/135;
		
		ObjRender_SetPosition(obj, deX, deY, 0);
		ObjRender_SetAngleXYZ(obj, angX, angY, angZ);
		ObjRender_SetAlpha(obj, alpha);
		ObjRender_SetScaleXYZ(obj, scale, scale, scale);
		
		if(scale<0.01){Obj_Delete(obj)}
		yield;
	}
	Obj_Delete(obj);
}

task ExplosionB1(deX, deY, presetColor, angX, angY, angZ){
	
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ALPHA );
	Obj_SetRenderPriority(obj, 0.50);
	ObjPrim_SetTexture(obj, img_effect);
	if(presetColor=="red"){ObjSprite2D_SetSourceRect(obj, 129, 17, 191, 79);}
	if(presetColor=="blue"){ObjSprite2D_SetSourceRect(obj, 192, 17, 259, 79);}
	if(presetColor=="yellow"){ObjSprite2D_SetSourceRect(obj, 0, 81, 65, 145);}
	if(presetColor=="green"){ObjSprite2D_SetSourceRect(obj, 64, 81, 129, 145);}
	ObjSprite2D_SetDestRect(obj, -32, -32, 32, 32);
	ObjRender_SetAngleXYZ(obj, angX, angY, angZ);
	ObjRender_SetPosition(obj, deX, deY, 0);
	
	let alpha = 155;
	let scale = 0;
	let expo = 0.4;
	let RandZ = rand(-170,170);
	while(Obj_IsDeleted(obj)==false){
	
		scale += expo/2;
		alpha -= 6.2;
		if(expo>0){expo = expo^1.07;}
		
		ObjRender_SetAlpha(obj, alpha);
		ObjRender_SetScaleXYZ(obj, scale*1.1, scale*1.1, 1);
		
		if(alpha<0){Obj_Delete(obj)}
		yield;
	}
	Obj_Delete(obj);
}

task ExplosionB2(deX, deY, colorR, colorG, colorB, deScale){
	
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let angX = rand(-360, 360);
	let angY = rand(-360, 360);
	let angZ = rand(-360, 360);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 0.60);
	ObjPrim_SetTexture(obj, img_effect);
	ObjSprite2D_SetSourceRect(obj, 0, 228, 33, 251);
	ObjSprite2D_SetDestRect(obj, -16, -12, 16, 12);
	ObjRender_SetAngleXYZ(obj, angX, angY, angZ);
	ObjRender_SetPosition(obj, deX, deY, 0);
	ObjRender_SetColor(obj, colorR, colorG, colorB);
	
	let alpha = 105;
	let scale = deScale;
	let randX = rand(-500, 500);
	let randY = rand(-500, 500);
	let RandZ = rand(-170,170);
	//atan2(randY - deX, randX - deY);
	while(Obj_IsDeleted(obj)==false){
		scale += -0.05;
		alpha += -0.1;
		deX += randX/100;
		deY += randY/100;
		angX += randX/85;
		angY += randY/85;
		angZ += randX/135+randY/135;
		
		ObjRender_SetPosition(obj, deX, deY, 0);
		ObjRender_SetAngleXYZ(obj, angX, angY, angZ);
		ObjRender_SetAlpha(obj, alpha);
		ObjRender_SetScaleXYZ(obj, scale, scale, scale);
		
		if(scale<0.01){Obj_Delete(obj)}
		yield;
	}
	Obj_Delete(obj);
}

task ClearBullets(x, y, type, Tsize, Tspd, Tmaxrad, delay){
	let stopclear = true;
	loop(delay){yield;}
	while(stopclear==true){
		DeleteShotInCircle(TYPE_SHOT,type,x,y,Tsize);
		Tsize += Tspd;
		if(Tsize>=Tmaxrad){
			stopclear = false;
		}
		yield;
	}
}

task CutIn(SnowEnemyObject, CutType, CutSpellName, CutSpellImage, CutX1, CutY1, CutX2, CutY2){
	
	let objSpellCutIn = ObjPrim_Create(OBJ_SPRITE_2D);
	let objSpellBG = ObjPrim_Create(OBJ_SPRITE_2D);
	let objTxt = ObjText_Create;
	
	let SpellTextX = 0;
	let SpellTextX2 = 0;
	let SpellTextY = 0;
	let SpellTextSet = 0;
	
	let SpellTextSize = 0;
	let SpellTextSizeInc = 0;
	
	let SpellBGAlpha = 0;
	
	let CutInAlpha = 0;
	let CutInX = 0;
	let CutInY = 0;
	let CutInPosInc = 0;
	
	let CutInTime = 0;
	let CutInitialize = false;
	
	SetTextProperties;
	SetSpellCutInProperties;
	SetSpellBGProperties;
	
	if(CutType!="YOUMU" && CutType!="DAIYOUSEI" ){
		WriteLog("Invalid string in 'CutIn'");
		CutType = "YOUMU";
	}
	//mu
	while(!Obj_IsDeleted(SnowEnemyObject)){
		CutInTime++;
		if(CutType=="YOUMU"){
			if(CutInitialize==false){
				CutInY = -60;
				SpellTextY = GetStgFrameHeight/4*3;
				SpellTextX = 310;
				SpellTextX2 = 310;
				BorderOfSpiritEffect;
				CutInitialize = true;
			}
			
			if(CutInTime<120&&CutInAlpha<255){CutInAlpha+=3;}
			if(CutInTime>119&&CutInAlpha>0){CutInAlpha-=3;}
			
			ObjRender_SetPosition(objSpellCutIn, 384, CutInY, 0);
			
			ObjRender_SetPosition(objSpellBG, 384+SpellTextX, SpellTextY, 0);
			ObjRender_SetPosition(objTxt, 70+SpellTextX2, SpellTextY+15, 0);
			
			ObjRender_SetAlpha(objSpellCutIn, CutInAlpha);
			
			if(SpellTextSet==0){
				SpellTextX-=10;
				if(SpellTextX==0){SpellTextSet=1;}
			}
			if(SpellTextSet==0 || SpellTextSet==1){
				SpellTextX2-=5;
				if(SpellTextX2==0){SpellTextSet=2;}
			}
			if(CutInTime==100){SpellTextSet=3;}
			if(SpellTextSet==3){
				SpellTextY-=10;
				if(SpellTextY<25){SpellTextSet=4;}
			}

			
			if(CutInTime>139&&CutInAlpha<1&&Obj_IsDeleted(objSpellCutIn)==false){
				Obj_Delete(objSpellCutIn);	
			}
			
			CutInY+=0.9;
		}
		
		if(CutType=="DAIYOUSEI"){
			if(CutInitialize==false){
				CutInY = 120;
				SpellTextY = GetStgFrameHeight/6*4;
				SpellTextX = 0;
				SpellTextX2 = 0;
				SpellTextSize = 164;
				BorderOfSpiritEffect;
				ObjSprite2D_SetDestRect(objSpellCutIn, -CutX2/2, 0, CutX2/2, CutY2);
				CutInitialize = true;
			}
			
			if(CutInTime<120&&CutInAlpha<255){CutInAlpha+=3;CutInPosInc+=0.03;}
			if(CutInTime>119&&CutInAlpha>0){CutInAlpha-=3;CutInPosInc-=0.028;}
			
			ObjRender_SetPosition(objSpellCutIn, GetCenterX, CutInY, 0);
			ObjRender_SetAlpha(objSpellCutIn, CutInAlpha);
			
			ObjRender_SetPosition(objSpellBG, 384+SpellTextX, SpellTextY, 0);
			ObjRender_SetPosition(objTxt, 70+SpellTextX2, SpellTextY+15, 0);
			
			
			if(SpellTextSize>0){
				SpellTextSize-=4.45;
				ObjSprite2D_SetDestRect(objSpellBG, -256-SpellTextSize, -SpellTextSize, 0, 36);
			}
			else{
				ObjSprite2D_SetDestRect(objSpellBG, -256, 0, 0, 36);
				if(SpellTextY>20 && CutInTime > 85){SpellTextY-=10;}
			}
			
			if(SpellBGAlpha<255){SpellBGAlpha+=4.5}
			ObjRender_SetAlpha(objTxt, SpellBGAlpha);
			ObjRender_SetAlpha(objSpellBG, SpellBGAlpha);
			
			
			CutInY-=CutInPosInc;
		}
	
		
		yield;
	}
	Obj_Delete(objTxt);
	Obj_Delete(objSpellCutIn);
	Obj_Delete(objSpellBG);
	
	
	function SetSpellBorderProperties{
		let SSBPtemp = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjRender_SetBlendType(SSBPtemp, BLEND_ALPHA);
		Obj_SetRenderPriority(SSBPtemp, 0.76);
		ObjPrim_SetTexture(SSBPtemp, img_effect2);
		ObjSprite2D_SetSourceRect(SSBPtemp, 0, 0, 1280, 80);
		ObjRender_SetPosition(SSBPtemp, GetStgFrameWidth, SpellTextY, 0);
		ObjSprite2D_SetDestRect(SSBPtemp, -280, -40, 1080, 40); //1.7
		return SSBPtemp;
	}	
	
	task SetSpellCutInProperties{
		ObjRender_SetBlendType(objSpellCutIn, BLEND_ALPHA);
		Obj_SetRenderPriority(objSpellCutIn, 0.77);
		ObjPrim_SetTexture(objSpellCutIn, CutSpellImage);
		ObjSprite2D_SetSourceRect(objSpellCutIn, CutX1, CutY1, CutX2, CutY2);
		ObjRender_SetPosition(objSpellCutIn, GetStgFrameWidth-30, 0, 0);
		while(CutX1>0){CutX1--;CutX2--;}
		while(CutY1>0){CutY1--;CutY2--;}
		ObjSprite2D_SetDestRect(objSpellCutIn, -CutX2, 0, 0, CutY2);
	}

	task SetSpellBGProperties{
		ObjRender_SetBlendType(objSpellBG, BLEND_ALPHA);
		Obj_SetRenderPriority(objSpellBG, 0.78);
		ObjPrim_SetTexture(objSpellBG, img_effect);
		ObjSprite2D_SetSourceRect(objSpellBG, 0, 256, 256, 292);
		ObjRender_SetPosition(objSpellBG, GetStgFrameWidth, SpellTextY, 0);
		ObjSprite2D_SetDestRect(objSpellBG, -256, 0, 0, 36);
	}
	
	task SetTextProperties{
		ObjText_SetText(objTxt,CutSpellName);
		ObjText_SetFontSize(objTxt,12);
		ObjText_SetFontColorTop(objTxt,254,254,254);
		ObjText_SetFontColorBottom(objTxt,255,119,119);
		ObjText_SetFontBorderType(objTxt,BORDER_FULL);
		ObjText_SetFontBorderColor(objTxt,0,0,64);
		ObjText_SetFontBorderWidth(objTxt,1);
		ObjText_SetHorizontalAlignment(objTxt, ALIGNMENT_RIGHT);
		ObjText_SetMaxWidth(objTxt, 300);
		ObjRender_SetPosition(objTxt, 70, SpellTextY+15, 0);
		Obj_SetRenderPriority(objTxt, 0.79);
	}

	task BorderOfSpiritEffect{
		let objSpellBorder = SetSpellBorderProperties;
		let objSpellBorder2 = SetSpellBorderProperties;
		let BorderAlpha = 0;
		let BorderPos = 0;
		ObjRender_SetAngleXYZ(objSpellBorder2, 0, 0, 90);
		while(Obj_IsDeleted(objSpellBorder) == false){
			BorderPos+=0.9;
			ObjRender_SetAlpha(objSpellBorder, BorderAlpha);
			ObjRender_SetAlpha(objSpellBorder2, BorderAlpha);
			ObjRender_SetPosition(objSpellBorder, BorderPos*2.4-500, 326-BorderPos*0.4, 0);
			ObjRender_SetPosition(objSpellBorder2, 256-BorderPos*0.5, -BorderPos*2.4, 0);
			if(CutInTime<120&&BorderAlpha<255){BorderAlpha+=3;}
			if(CutInTime>119&&BorderAlpha>0){BorderAlpha-=3;}
			if(CutInTime>119 && BorderAlpha == 0){Obj_Delete(objSpellBorder);Obj_Delete(objSpellBorder2);}
			yield;
		}
	}
}


task ScreenShake(shaketime, intensity){
	while(shaketime>0){
		Set2DCameraFocusX(Get2DCameraX + rand(-intensity, intensity));
		Set2DCameraFocusY(Get2DCameraY + rand(-intensity, intensity));
		shaketime--;
		yield;
	}
	Reset2DCamera;
	yield;
}

task ScreenFlash(decay, alpha){
	let menuscreen = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(menuscreen, img_effect);
	ObjRender_SetBlendType(menuscreen, BLEND_ALPHA);
	Obj_SetRenderPriorityI(menuscreen, 69);
	ObjRender_SetAlpha(menuscreen, alpha);
	ObjSprite2D_SetSourceRect(menuscreen, 240, 234, 252, 246);
	ObjSprite2D_SetDestRect(menuscreen, -124, -124, 524, 524);
	
	while(alpha>0){
		alpha+=decay;
		ObjRender_SetAlpha(menuscreen, alpha);
		yield;
	}
	
	Obj_Delete(menuscreen);
	
}

// snow did this
task ScreenDistort(SnowEnemyObject, AI, length){
	let renderTexture = GetReservedRenderTargetName(0);

	SetInvalidRenderPriorityA1(20, 79);

	let objCurentFrame = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(objCurentFrame, 80);

	if(length == -1){while(ObjEnemy_GetInfo(SnowEnemyObject, INFO_LIFE) > 0){PerformActions;yield;}}	
	if(length != -1){while(length>0){length--;PerformActions;yield;}}
	
	Obj_Delete(objCurentFrame);
	ClearInvalidRenderPriority;
	
	task PerformActions{
		let currentFrameTexture = renderTexture;
		RenderToTextureA1(currentFrameTexture, 20, 79, true);
		ObjPrim_SetTexture(objCurentFrame, currentFrameTexture);
		ObjSprite2D_SetSourceRect(objCurentFrame, 32, 16, 416, 464);
		ObjSprite2D_SetDestRect(objCurentFrame, rand(32, 36+AI), rand(16, 36+AI), rand(416-AI, 416), rand(464-AI, 464));
	}
}

task SpellTime(SpellTimeSceneObjectID){
	//@Mainloop
	let GetTimer = ObjEnemyBossScene_GetInfo(SpellTimeSceneObjectID, INFO_TIMER);
	if(bosstime != GetTimer && GetTimer <11){
		if(GetTimer > 4){PlaySound("timeout")}
		if(GetTimer < 5 && GetTimer>-1){PlaySound("timeout2");}
		bosstime = GetTimer;
	}
}

task PlayMusic(str){
	if(str=="3rdeye"){
		let objSound = ObjSound_Create;
		LoadSound(MainSTG~"source/snowbgm.ogg");
		ObjSound_Load(objSound, MainSTG~"source/snowbgm.ogg");
		ObjSound_SetSoundDivision(objSound, SOUND_BGM);
		ObjSound_SetLoopEnable(objSound, true);
		ObjSound_SetVolumeRate(objSound, 100);
		ObjSound_SetLoopTime(objSound, 1.8, 123);
		ObjSound_Play(objSound);
	}
	if(str=="ghostlead"){
		let objSound = ObjSound_Create;
		LoadSound(MainSTG~"source/snowbgm.ogg");
		ObjSound_Load(objSound, MainSTG~"source/snowbgm.ogg");
		ObjSound_SetSoundDivision(objSound, SOUND_BGM);
		ObjSound_SetLoopEnable(objSound, true);
		ObjSound_SetVolumeRate(objSound, 100);
		ObjSound_SetLoopTime(objSound, 8.9, 125); 
		ObjSound_Play(objSound);
	}
}


//Animation

//top left fairies 42x38 grid
//top right fairies 48x48 grid
//bottom 4 basic color fairies 32x32 grid
//bottom right; very bottom fairies 64x64 grid

task AnimateFairyA1(objID, presetColor){

	let anim_count = 0;
	let anim_type = 0;
	
	let xoffset = 0;
	let yoffset = 0;
	
	if(presetColor=="blue"){yoffset = 321}
	if(presetColor=="red"){yoffset = 353}
	if(presetColor=="green"){yoffset = 385}
	if(presetColor=="yellow"){yoffset = 417}
	
	loop{
		if(ObjMove_GetSpeed(objID) == 0){
			if(anim_type == 1){
				anim_type=2;
				anim_count = 0;
			}
		}
		if(ObjMove_GetSpeed(objID) > 0){
			if(anim_type != 1){
				anim_count = 0;
				anim_type = 1;
			}
		}
		if(CheckDirection(objID, "right") == true){ObjRender_SetScaleXYZ(objID, 1, 1, 1)}
		if(CheckDirection(objID, "left") == true){ObjRender_SetScaleXYZ(objID, -1, 1, 1)}
		//if(CheckDirection(objID, "none") == true){ObjRender_SetScaleXYZ(objID, 1, 1, 1)}
		
		if(anim_type==0){
			if(anim_count == 0){ObjSprite2D_SetSourceRect(objID, 0, yoffset, 32, yoffset+32);}
			if(anim_count == 5){ObjSprite2D_SetSourceRect(objID, 32, yoffset, 64, yoffset+32);}
			if(anim_count == 10){ObjSprite2D_SetSourceRect(objID, 64, yoffset, 96, yoffset+32);}
			if(anim_count == 15){ObjSprite2D_SetSourceRect(objID, 96, yoffset, 128, yoffset+32);}
			if(anim_count == 20){ObjSprite2D_SetSourceRect(objID, 64, yoffset, 96, yoffset+32);}
			if(anim_count == 25){ObjSprite2D_SetSourceRect(objID, 32, yoffset, 64, yoffset+32);}
			if(anim_count == 29){anim_count = 4;}
		}
		if(anim_type==1){
			if(anim_count == 0){ObjSprite2D_SetSourceRect(objID, 128, yoffset, 160, yoffset+32);}
			if(anim_count == 4){ObjSprite2D_SetSourceRect(objID, 160, yoffset, 192, yoffset+32);}
			if(anim_count == 8){ObjSprite2D_SetSourceRect(objID, 192, yoffset, 224, yoffset+32);}
			if(anim_count == 12){ObjSprite2D_SetSourceRect(objID, 224, yoffset, 256, yoffset+32);}
			if(anim_count == 16){ObjSprite2D_SetSourceRect(objID, 256, yoffset, 288, yoffset+32);}
			if(anim_count == 20){ObjSprite2D_SetSourceRect(objID, 288, yoffset, 320, yoffset+32);}
			if(anim_count == 24){ObjSprite2D_SetSourceRect(objID, 320, yoffset, 352, yoffset+32);}
			if(anim_count == 28){ObjSprite2D_SetSourceRect(objID, 352, yoffset, 384, yoffset+32);}
			if(anim_count == 31){anim_count = 11;}
		}
		if(anim_type==2){
			if(anim_count == 0){ObjSprite2D_SetSourceRect(objID, 224, yoffset, 256, yoffset+32);}
			if(anim_count == 3){ObjSprite2D_SetSourceRect(objID, 192, yoffset, 224, yoffset+32);}
			if(anim_count == 6){ObjSprite2D_SetSourceRect(objID, 160, yoffset, 192, yoffset+32);}
			if(anim_count == 9){ObjSprite2D_SetSourceRect(objID, 128, yoffset, 160, yoffset+32);}
			if(anim_count == 12){anim_count = 0;anim_type=0;}
		}
		
	
		ObjSprite2D_SetDestCenter(objID);
		anim_count++;
		yield;
	}
}

task AnimateBossA1(objID){

	let anim_count = 0;
	let anim_type = 0;
	
	let xoffset = 0;
	let yoffset = 0;
	
	// y grid = 112
	// x grid = 80
	
	loop{
		if(ObjMove_GetSpeed(objID) == 0 && Anim_Attack != 1){
			if(anim_type == 1){
				anim_type=2;
				anim_count = 0;
			}
			if(anim_type == 3){
				anim_type=4;
				anim_count = 0;
			}
		}
		if(ObjMove_GetSpeed(objID) > 0 && Anim_Attack != 1){
			if(anim_type == 0){
				anim_count = 0;
				anim_type = 1;
			}
		}
		if(Anim_Attack == 1){
			if(anim_type != 3){
				anim_type=3;
				anim_count = 0;
			}
		}
		if(Anim_Attack != 0){IsAttackAnimPlaying = true;}
		
		if(CheckDirection(objID, "right") == true){ObjRender_SetScaleXYZ(objID, 1, 1, 1)}
		if(CheckDirection(objID, "left") == true){ObjRender_SetScaleXYZ(objID, -1, 1, 1)}
		
		if(anim_type==0){
			if(anim_count == 0){ObjSprite2D_SetSourceRect(objID, 0, yoffset, 80, yoffset+112);}
			if(anim_count == 6){ObjSprite2D_SetSourceRect(objID, 80, yoffset, 160, yoffset+112);}
			if(anim_count == 12){ObjSprite2D_SetSourceRect(objID, 160, yoffset, 240, yoffset+112);}
			if(anim_count == 18){ObjSprite2D_SetSourceRect(objID, 240, yoffset, 320, yoffset+112);}
			if(anim_count == 23){anim_count = -1;}
		}
		if(anim_type==1){
			if(anim_count == 0){ObjSprite2D_SetSourceRect(objID, 0, yoffset+112, 80, yoffset+234);}
			if(anim_count == 6){ObjSprite2D_SetSourceRect(objID, 80, yoffset+112, 160, yoffset+234);}
			if(anim_count == 12){ObjSprite2D_SetSourceRect(objID, 160, yoffset+112, 240, yoffset+234);}
			if(anim_count == 18){ObjSprite2D_SetSourceRect(objID, 240, yoffset+112, 320, yoffset+234);}
			if(anim_count == 23){anim_count = 11;}
		}
		if(anim_type==2){
			if(anim_count == 0){ObjSprite2D_SetSourceRect(objID, 240, yoffset+112, 320, yoffset+234);}
			if(anim_count == 5){ObjSprite2D_SetSourceRect(objID, 160, yoffset+112, 240, yoffset+234);}
			if(anim_count == 10){ObjSprite2D_SetSourceRect(objID, 80, yoffset+112, 160, yoffset+234);}
			if(anim_count == 15){ObjSprite2D_SetSourceRect(objID, 0, yoffset+112, 80, yoffset+234);}
			if(anim_count == 19){anim_count = 0;anim_type=0;}
		}
		if(anim_type==3){
			if(anim_count == 0){ObjSprite2D_SetSourceRect(objID, 0, yoffset+224, 80, yoffset+336);}
			if(anim_count == 6){ObjSprite2D_SetSourceRect(objID, 80, yoffset+224, 160, yoffset+336);}
			if(anim_count == 12){ObjSprite2D_SetSourceRect(objID, 160, yoffset+224, 240, yoffset+336);}
			if(anim_count == 18){ObjSprite2D_SetSourceRect(objID, 240, yoffset+224, 320, yoffset+336);}
			if(anim_count == 23){anim_count = 11;}
		}
		if(anim_type==4){
			if(anim_count == 0){ObjSprite2D_SetSourceRect(objID, 240, yoffset+224, 320, yoffset+336);}
			if(anim_count == 3){ObjSprite2D_SetSourceRect(objID, 160, yoffset+224, 240, yoffset+336);}
			if(anim_count == 9){ObjSprite2D_SetSourceRect(objID, 80, yoffset+224, 160, yoffset+336);}
			if(anim_count == 15){ObjSprite2D_SetSourceRect(objID, 0, yoffset+224, 80, yoffset+336);}
			if(anim_count == 21){anim_count = 0;anim_type=0;IsAttackAnimPlaying=false;}
		}
		
	
		ObjSprite2D_SetDestCenter(objID);
		anim_count++;
		yield;
	}
}

function CheckDirection(checkid, direc){
	let direction = false;
	let enemyang = absolute(ObjMove_GetAngle(checkid));
	let enemyspd = absolute(ObjMove_GetSpeed(checkid));
	
	
	while(enemyang>360){
		enemyang = enemyang-360;
	}

	if(enemyspd > 0){
		if(direc=="right"){
			if(enemyang > 270 || enemyang-270 < 90){
				direction=true;
			}
		}
		if(direc=="left"){
			if(enemyang < 270 && enemyang > 90){
				direction=true;
			}
		}
	}
	if(direc=="none"){
		if(enemyspd == 0){
			direction=true;
		}
	}
	
	return direction;
}

function CheckOutsideScreen(objectID, distance){
	let COSvar = false;
	
	if(ObjRender_GetX(objectID)>GetStgFrameWidth+distance||ObjRender_GetX(objectID)<-distance||ObjRender_GetY(objectID)>GetStgFrameHeight+distance||ObjRender_GetY(objectID)<-distance){
		COSvar=true;
	}
	
	return COSvar;
}



task FinalizePlural(FinalX, FinalY){
	if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <= 0 && enemydead == false){
		enemydead = true;
		Anim_Attack = 0;
		let finalframe = 0;
		PlaySound("death");
		Anim_Attack = 0;
		ObjMove_SetDestAtWeight(objEnemy, FinalX-22, FinalY-16, 130, 1);	
		ExplosionA1(FinalX, FinalY, 0, 0, 0);
		loop(90){ExplosionA2(FinalX, FinalY, rand(-360, 360), rand(-360, 360), rand(-360, 360), rand(1, 3));}
		ScreenShake(110,24);
		ClearBullets(FinalX, FinalY, TYPE_ITEM, 30, 10, 500, 0);
		
		loop(110){
			if(finalframe>6){
				loop(3){ExplosionA2(FinalX, FinalY, rand(-360, 360), rand(-360, 360), rand(-360, 360), rand(2, 5));}
				PlaySound("smbullet");
				finalframe = 0;
			}
			finalframe++;
			yield;
		}
		
		DeleteShotAll(TYPE_ALL, TYPE_ITEM);
		loop(42){CreateItemA1(ITEM_POINT, FinalX+rand(-60, 60), FinalY+rand(-60, 60), rand(360, 3600));CreateItemA1(ITEM_POWER_S, FinalX+rand(-60, 60), FinalY+rand(-60, 60), rand(360, 3600));ExplosionA2(FinalX, FinalY, rand(-360, 360), rand(-360, 360), rand(-360, 360), rand(1, 7));}
		PlaySound("death");
		ScreenShake(30,32);
		ExplosionA1(FinalX, FinalY, 0, 0, 0);
		SetCommonData("StarX", -400);
		SetCommonData("StarY", -400);		
		Obj_Delete(objEnemy);
		loop(180){yield}
		CloseScript(GetOwnScriptID);
	}
}



