
let MainSTG = GetMainStgScriptDirectory;

let img_effect = MainSTG~"ds_system/img/effect.png"; // Effect Sheet
let img_effect2 = MainSTG~"ds_system/img/effect2.png"; // Effect2 Sheet
let img_fairy = MainSTG~"ds_system/img/enemy.png"; // Fairy Sheet
let img_SpellAttack = MainSTG~"ds_system/img/effect3/effect37.png"; // Spell Declare

let GetCenterX = GetStgFrameWidth/2;
let GetCenterY = GetStgFrameHeight/2;
let LastTimeStop = 0;
let IsTimeStopped = false;
let bosstime = 99;

let IsAttackAnimPlaying = false;
let Anim_Attack = 0;
let Enemy_Move = 0;

let EnemyX = 0;
let EnemyY = 0;

let EnemyDead = false;
let quality = GetCommonData("quality", 1);

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;
	//DrawText("", GetPlayerX-24, GetPlayerY, 11, 300, "black", 0.80);
	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/timestop.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 == "timestop"){PlaySE(MainSTG~"source/Sounds/timestop.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);
	// You can remove EnemyDead or add in your own extra statement
	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 StarOnBossSingle(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.30);
	ObjPrim_SetTexture(objStar, img_effect);
	ObjSprite2D_SetSourceRect(objStar, 128, 80, 256, 208);
	ObjSprite2D_SetDestRect(objStar, -64, -64, 64, 64);
	
	
	ObjRender_SetAlpha(objStar, 185);
	
	while(!Obj_IsDeleted(SnowEnemyObject)){

		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);
		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" && CutType!="KYOUKO"){
		CutType++;
		// You are seeing this error because you put an invalid
		// cutin type
	}
	//mu
	
	if(CutType=="YOUMU"){TextYoumu;}
	if(CutType=="DAIYOUSEI"){TextDaiyousei;}
	if(CutType=="KYOUKO"){TextKyouko;}
	
	while(CutInTime<600){
		CutInTime++;					
		yield;
	}
	
	Obj_Delete(objSpellCutIn);
	while(!Obj_IsDeleted(SnowEnemyObject)){yield;}
	Obj_Delete(objTxt);
	Obj_Delete(objSpellBG);
	
	task TextYoumu{
		CutInY = -60;
		SpellTextY = GetStgFrameHeight/4*3;
		SpellTextX = 310;
		SpellTextX2 = 310;
		BorderOfSpiritEffect;
		while(SpellTextSet!=4){			
			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;}
			}			
			CutInY+=0.9;
			yield;
		}
	}
	
	task TextDaiyousei{
		CutInY = 120;
		SpellTextY = GetStgFrameHeight/6*4;
		SpellTextX = 0;
		SpellTextX2 = 0;
		SpellTextSize = 164;
		BorderOfSpiritEffect;
		ObjSprite2D_SetDestRect(objSpellCutIn, -CutX2/2, 0, CutX2/2, CutY2);
		while(CutInTime<119||CutInAlpha>0){
			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;
		}
	}
	
	task TextKyouko{
		CutInY = 120;
		SpellTextY = GetStgFrameHeight/6*4;
		SpellTextX = 0;
		SpellTextX2 = 0;
		SpellTextSize = 164;
		SpellAttackEffect;
		ObjSprite2D_SetDestRect(objSpellCutIn, -CutX2/2, 0, CutX2/2, CutY2);
		
		while(CutInTime<119||CutInAlpha>0){
			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;
		}
	}
	
	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;
	}	
	function SetSpellAttackProperties{
		let SSBPtemp = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjRender_SetBlendType(SSBPtemp, BLEND_ALPHA);
		Obj_SetRenderPriority(SSBPtemp, 0.75);
		ObjPrim_SetTexture(SSBPtemp, img_SpellAttack);
		ObjSprite2D_SetSourceRect(SSBPtemp, -768, 0, 768, 16);
		ObjSprite2D_SetDestCenter(SSBPtemp);
		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 SpellAttackEffect{
		let alpha = 0;
		Octagon(GetCenterX*2, GetCenterY*2, -1.15, 140);
		Octagon(GetCenterX*2, GetCenterY*2, 1.15, 175);
		Octagon(GetCenterX*2, GetCenterY*2, 1.15, 205);
		Octagon(GetCenterX/5, GetCenterY/5, 0.75, 140);
		
		ascent (i in 0..5){
			Line(GetCenterX, GetCenterY+i*75-75-95, 1.5);
		}
		ascent (i in 0..5){
			Line(GetCenterX, GetCenterY+i*75-37.5-95, -1.5);
		}
		while(CutInTime<180||alpha>0){
			if(CutInTime<180 && CutInTime>15 && alpha<145){alpha+=5}
			if(CutInTime>=180 && alpha>0){alpha-=5}
			yield;
		}
		
		task Line(mx, my, posinc){
			let objSpellAttack = SetSpellAttackProperties;
			let LineX = 0;
			let LineY = 0;
			ObjRender_SetAngleZ(objSpellAttack, -35);
			while(CutInTime<180 || alpha>0){
				LineX = LineX+posinc*cos(-35);
				LineY = LineY+posinc*sin(-35);
				ObjRender_SetPosition(objSpellAttack, mx+LineX, my+LineY, 0);
				ObjRender_SetAlpha(objSpellAttack, alpha);
				yield;
			}
			Obj_Delete(objSpellAttack);
		}
	
		task Octagon(mx, my, spininc, dist){
		
			let countVertex = 16;
			let listRadius = [];
			loop(countVertex){
				listRadius = listRadius ~ [0];
			}

			let objOutline = ObjPrim_Create(OBJ_PRIMITIVE_2D);
			ObjPrim_SetPrimitiveType(objOutline, PRIMITIVE_TRIANGLESTRIP);
			ObjPrim_SetVertexCount(objOutline, countVertex);
			ObjRender_SetBlendType(objOutline, BLEND_ALPHA);
			Obj_SetRenderPriority(objOutline, 0.76);
			ObjPrim_SetTexture(objOutline, img_SpellAttack);
	
			ascent (iVert in 0..countVertex/2){
				let left = iVert * 128;
				let indexVert = iVert * 2;
				ObjPrim_SetVertexUVT(objOutline, indexVert + 0, left, 0);
				ObjPrim_SetVertexUVT(objOutline, indexVert + 1, left, 16);
			}			
	
			let frame = 0;
			let rRate = 1;
			let spin = 0;
	
			while(CutInTime<180 || alpha>0){
				spin+=spininc;
				VertexSize;
				ObjRender_SetPosition(objOutline, mx, my, 0);
				ObjRender_SetAngleZ(objOutline, spin);
				ObjRender_SetAlpha(objOutline, alpha);
				frame++;
				yield;
			}
			Obj_Delete(objOutline);
			
			task VertexSize{	
				if(frame>=35){return;}
				ascent (iVert in 0..countVertex/2){
					let indexVert = iVert * 2;
					let angle = (360 / (countVertex / 2 - 1) * iVert);

					let vx1 = listRadius[indexVert] * cos(angle);
					let vy1 = listRadius[indexVert] * sin(angle);
					ObjPrim_SetVertexPosition(objOutline, indexVert + 0, vx1, vy1, 0);

					let vx2 = listRadius[indexVert+1] * cos(angle);
					let vy2 = listRadius[indexVert+1] * sin(angle);
					ObjPrim_SetVertexPosition(objOutline, indexVert + 1, vx2, vy2, 0);

					let dr = (dist * rRate - listRadius[indexVert]) / 16;
					listRadius[indexVert] = listRadius[indexVert] + dr;
					if(frame>1){
						listRadius[indexVert + 1] = listRadius[indexVert + 1] + dr;
					}
				}
			}
		}
	}
}


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 Shadow(x, y, incremental, alpha, colorR, colorG, colorB, stime){

	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let ShadowTime = 0;
	let xBend = 1;
	let yBend = 1;
	let BendInc = 0.22;
	let ShadSet = 0;
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 0.69);
	ObjRender_SetAlpha(obj, alpha);
	ObjPrim_SetTexture(obj, img_effect2);
	ObjSprite2D_SetSourceRect(obj, 0, 130, 62, 192);
	ObjSprite2D_SetDestRect(obj, -xBend, -xBend, xBend, xBend);
	ObjRender_SetPosition(obj, x, y, 0);
	ObjRender_SetColor(obj, colorR, colorG, colorB);
		while(Obj_IsDeleted(obj) == false){
		ShadowTime++;
		ObjSprite2D_SetDestRect(obj, -xBend, -xBend, xBend, xBend);
		
		if(xBend<1500&&ShadSet==0){
			xBend+=BendInc;
			BendInc+=incremental;
		}
		if(ShadowTime>stime&&ShadSet==0){
			BendInc=0;		
			ShadSet=2;
		}
		if(ShadowTime>stime&&ShadSet==2){
			xBend+=BendInc;
			BendInc-=incremental*1.75;
		}
		if(xBend<-20){Obj_Delete(obj);}
		yield;
	}
}

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 TimeStop(x, y, length){
	SetCommonData("TimeStop", 1);
	LastTimeStop = length;
	IsTimeStopped = true;
	if(x != -1 && y != -1){Shadow(x, y, 1.45, 165, 1, 1, 1, length-25);}
	ascent(time in 0..length){
		yield;
	}
	SetCommonData("TimeStop", 0);
	IsTimeStopped = false;
	ScreenFlash(-10, 50);
	ScreenShake(15, 1.75);
}

task TimeSlow(divide, length){
	SetCommonData("TimeSlow", divide);
	ascent(time in 0..length){
		yield;
	}
	SetCommonData("TimeSlow", 0);
}

task ObjShot_AddTimeCompatA1(objID, spd){
	let tspd = 1;
	while(!Obj_IsDeleted(objID)){
		tspd = GetCommonData("TimeSlow", 1);
		if(GetCommonData("TimeStop", 0) > 0){
			ObjMove_SetSpeed(objID, 0);
		}else{
			ObjMove_SetSpeed(objID, spd/tspd);
		}
		yield;
	}
}	

task ObjShot_AddTimeCompatB1(objID, spd){
	let tspd = GetCommonData("TimeSlow", 1);
	if(GetCommonData("TimeStop", 0) > 0){
		ObjMove_SetSpeed(objID, 0);
	}else{
		ObjMove_SetSpeed(objID, spd/tspd);
	}
}	

task ObjShot_AddCollisionA1(shotobjid, dX, dY, radius){
	let X = ObjRender_GetX(shotobjid);
	let Y = ObjRender_GetY(shotobjid);
	let A = ObjMove_GetAngle(shotobjid);
	while(!Obj_IsDeleted(shotobjid)){
		X = ObjRender_GetX(shotobjid);
		Y = ObjRender_GetY(shotobjid);
		A = ObjMove_GetAngle(shotobjid);
		if(X<=dX-1 || X>=dX+1){
			ObjMove_SetAngle(shotobjid, 180-A);
		}	
		if(Y<=dY-1 || Y>=dY+1){	
			ObjMove_SetAngle(shotobjid, 360-A);
		}
		yield;
	}
}

task ObjShot_AddCollisionB1(shotobjid, radius){
	let X = ObjRender_GetX(shotobjid);
	let Y = ObjRender_GetY(shotobjid);
	let A = ObjMove_GetAngle(shotobjid);
	let barray = GetShotIdInCircleA2(X, Y, radius, TARGET_ENEMY);
	while(!Obj_IsDeleted(shotobjid)){
		X = ObjRender_GetX(shotobjid);
		Y = ObjRender_GetY(shotobjid);
		A = ObjMove_GetAngle(shotobjid);
		barray = GetShotIdInCircleA2(X, Y, radius, TARGET_ENEMY);
		if(length(barray)>0){
			let b = barray[0];
			let dX = ObjRender_GetX(b);
			let dY = ObjRender_GetY(b);
			if(X<=dX-1 || X>=dX+1){
				ObjMove_SetAngle(shotobjid, 180-A);
			}
			if(Y<=dY-1 || Y>=dY+1){	
				ObjMove_SetAngle(shotobjid, 360-A);
			}
		}
		yield;
	}
}

task ObjShot_AddCollisionB2(shotobjid, radius){
	let X = ObjRender_GetX(shotobjid);
	let Y = ObjRender_GetY(shotobjid);
	let A = ObjMove_GetAngle(shotobjid);
	let barray = GetShotIdInCircleA2(X, Y, radius, TARGET_ENEMY);
	let blength = length(barray);
	while(!Obj_IsDeleted(shotobjid)){
		X = ObjRender_GetX(shotobjid);
		Y = ObjRender_GetY(shotobjid);
		A = ObjMove_GetAngle(shotobjid);
		barray = GetShotIdInCircleA2(X, Y, radius, TARGET_ENEMY);
		blength = length(barray);
		if(blength>0){
			ascent (i in 0..blength){
				let b = barray[i];
				let dX = ObjRender_GetX(b);
				let dY = ObjRender_GetY(b);
				if(X<=dX-1 || X>=dX+1){
					ObjMove_SetAngle(shotobjid, 180-A);
					ObjMove_SetAngle(b, 180+(180-A));
				}
				if(Y<=dY-1 || Y>=dY+1){	
					ObjMove_SetAngle(shotobjid, 180-A);
					ObjMove_SetAngle(b, 180+(180-A));
				}
			}
		}
		yield;
	}
}

task ImageOverImageA1(obj, path, InX, InY, radset){
	let countVertex = 64;
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
	ObjPrim_SetVertexCount(obj, countVertex);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjRender_SetAlpha(obj, 255);
	Obj_SetRenderPriority(obj, 0.3);
	ObjPrim_SetTexture(obj, path);
	ascent(iVert in 0..countVertex/2){
		let angle = (360 / (countVertex/2 - 1) * iVert);
		let x = InX+radset*cos(angle);
		let y = InY+radset*sin(angle);
		ObjPrim_SetVertexUVT(obj, iVert+0, x, y);	
		x = radset*cos(angle);
		y = radset*sin(angle);
		ObjPrim_SetVertexPosition(obj, iVert+0 , x, y, 0);
		
		x = InX+(1.15*radset)*cos(angle);
		y = InY+(1.15*radset)*sin(angle);
		ObjPrim_SetVertexUVT(obj, iVert+1, x, y);	
		x = (1.15*radset)*cos(angle);
		y = (1.15*radset)*sin(angle);
		ObjPrim_SetVertexPosition(obj, iVert+1 , x, y, 0);
		ObjPrim_SetVertexAlpha(obj, iVert+1, 0);
	}
}

task SetNewImageRender(obj, InX, InY, radset){
	let countVertex = 64;
	ascent(iVert in 0..countVertex/2){
		let angle = (360 / (countVertex/2 - 1) * iVert);
		let x = InX+radset*cos(angle);
		let y = InY+radset*sin(angle);
		ObjPrim_SetVertexUVT(obj, iVert+0, x, y);	
		x = radset*cos(angle);
		y = radset*sin(angle);
		ObjPrim_SetVertexPosition(obj, iVert+0 , x, y, 0);
		
		x = InX+(1.15*radset)*cos(angle);
		y = InY+(1.15*radset)*sin(angle);
		ObjPrim_SetVertexUVT(obj, iVert+1, x, y);	
		x = (1.15*radset)*cos(angle);
		y = (1.15*radset)*sin(angle);
		ObjPrim_SetVertexPosition(obj, iVert+1 , x, y, 0);
		ObjPrim_SetVertexAlpha(obj, iVert+1, 0);
	}
}

//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;
	
	let r = 7; // Frames yielded before moving to next frame
	let m = -6; // Initial Position - Movement Up/down 
	let x = 64; // X grid
	let y = 80; // Y grid
	
	MoveBoss(1, 0.01, 0.325);
	
	while(!Obj_IsDeleted(objID)){
		if(ObjMove_GetSpeed(objID) == 0){
			if(anim_type == 1){
				anim_count = 0;
				anim_type = 2;
			}
		}
		if(ObjMove_GetSpeed(objID) > 0){
			if(anim_type == 0){
				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(anim_type==0){ //Idle
			if(anim_count == 0){ObjSprite2D_SetSourceRect(objID, 0, yoffset, x, yoffset+y);}
			if(anim_count == r){ObjSprite2D_SetSourceRect(objID, x, yoffset, x*2, yoffset+y);}
			if(anim_count == r*2){ObjSprite2D_SetSourceRect(objID, x*2, yoffset, x*3, yoffset+y);}
			if(anim_count == r*3){ObjSprite2D_SetSourceRect(objID, x*3, yoffset, x*4, yoffset+y);}
			if(anim_count == r*4){ObjSprite2D_SetSourceRect(objID, x*3, yoffset, x*4, yoffset+y);}
			if(anim_count == r*5){ObjSprite2D_SetSourceRect(objID, x*2, yoffset, x*3, yoffset+y);}
			if(anim_count == r*6){ObjSprite2D_SetSourceRect(objID, x, yoffset, x*2, yoffset+y);}
			if(anim_count >= r*7-1){anim_count = -1;}
		}
		if(anim_type==1){ //Moving
			if(anim_count == 0){ObjSprite2D_SetSourceRect(objID, 0, yoffset+y, x, yoffset+y*2);}
			if(anim_count == r){ObjSprite2D_SetSourceRect(objID, x, yoffset+y, x*2, yoffset+y*2);}
			if(anim_count == r*2){ObjSprite2D_SetSourceRect(objID, x*2, yoffset+y, x*3, yoffset+y*2);}
			if(anim_count == r*3){ObjSprite2D_SetSourceRect(objID, x*3, yoffset+y, x*4, yoffset+y*2);}
			if(anim_count >= r*4-1){anim_count = r*3-1;}
		}
		if(anim_type==2){ //Moving to idle
			if(anim_count == 0){ObjSprite2D_SetSourceRect(objID, x*3, yoffset+y, x*4, yoffset+y*2);}
			if(anim_count == r){ObjSprite2D_SetSourceRect(objID, x*2, yoffset+y, x*3, yoffset+y*2);}
			if(anim_count == r*2){ObjSprite2D_SetSourceRect(objID, x, yoffset+y, x*2, yoffset+y*2);}
			if(anim_count == r*3){ObjSprite2D_SetSourceRect(objID, 0, yoffset+y, x, yoffset+y*2);}
			if(anim_count == r*4){anim_type=0; anim_count=0;}
		}
		
		ObjSprite2D_SetDestRect(objID, -x/2, -y/2+m, x/2, y/2+m);
		anim_count++;
		yield;
	}
	task MoveBoss(type, a, b){
		let ax2 = a;
		let bx2 = b;
		let obj_sizeset = 0;
		let sizeinc = 0;
		while(true){
			if(obj_sizeset==0){sizeinc+=ax2}
			if(obj_sizeset==1){sizeinc-=ax2}		
			if(sizeinc<-bx2){obj_sizeset=0}
			if(sizeinc>bx2){obj_sizeset=1}
	
			m+=sizeinc;
			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;
}

// Script specific functions below

task SetScriptProperties{
	if(Obj_IsDeleted(Enemy)==false){
		EnemyX = ObjMove_GetX(Enemy);
		EnemyY = ObjMove_GetY(Enemy);
		ObjEnemy_SetIntersectionCircleToShot(Enemy, EnemyX, EnemyY, 24);
		ObjEnemy_SetIntersectionCircleToPlayer(Enemy, EnemyX, EnemyY, 16);
	}
	if(EnemyDead == false){
		SpellTime(objScene);
		SetCommonData("StarX1", EnemyX);
		SetCommonData("StarY1", EnemyY);
	}
	time++;
	Enemy_Move++;
}

task FinalizePlural(FinalX, FinalY){
	
	EnemyDead = true;
	let finalframe = 0;

	ObjMove_SetDestAtWeight(Enemy, FinalX-16, FinalY-12, 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));}
			finalframe = 0;
			PlaySound("smbullet");
		}
		finalframe++;
		yield;
	}
	
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	loop(90){CreateItemA1(ITEM_POINT_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));}
	ScreenShake(30,32);
	ExplosionA1(FinalX, FinalY, 0, 0, 0);
	Obj_Delete(Enemy);
}



