function wait(n){loop(n){yield;}}


task takeDamage(enemy, scene, lifeB){
    let sound = LoadEffect(GetCurrentScriptDirectory~ "./../SFX/DamageNon.WAV", 70);
	let sound2 = LoadEffect(GetCurrentScriptDirectory~ "./../SFX/DamageSpell.WAV", 70);
    let life; let lifen;
    while(ObjEnemy_GetInfo(enemy, INFO_LIFE)>0){
	    life = ObjEnemy_GetInfo(enemy, INFO_LIFE);
		yield;
		lifen = ObjEnemy_GetInfo(enemy, INFO_LIFE);
		if(lifen< life){
			while(lifen<life){
			    if(life<lifeB/4 && life> lifeB/8){ObjSound_Play(sound);}
				if(life<lifeB/8){ObjSound_Play(sound2);}
			    life = lifen;
				wait(4);
				lifen = ObjEnemy_GetInfo(enemy,INFO_LIFE);
			}
		}
	}
}

task text(msg, time){
    let objText = ObjText_Create();
	ObjText_SetText(objText, msg);
	ObjText_SetFontSize(objText, 20);
	ObjText_SetFontType(objText, "MS Gothic");
	ObjText_SetFontColorTop(objText, 128, 255, 128);
	ObjText_SetFontColorBottom(objText, 110, 255, 110);
	ObjText_SetFontBold(objText, true);
    ObjText_SetHorizontalAlignment(objText, ALIGNMENT_CENTER);
	ObjText_SetMaxWidth(objText, 320);
	Obj_SetRenderPriorityI(objText, 80);
	ObjRender_SetPosition(objText, 384/2 -150, 448/3, 1);
	wait(time);
	Obj_Delete(objText);
}

task shotSound(obj, time, volume, delay, enemy){
    ObjSound_SetVolumeRate(obj, volume);
	ObjSound_SetRestartEnable(obj, true);
	if(time<0){
		while(ObjEnemy_GetInfo(enemy, INFO_LIFE) > 0){
			ObjSound_Play(obj);
			wait(delay);
		}
	}
	else{
	    let t=0;
	    while(t<time){
		    ObjSound_Play(obj);
			wait(delay);
			t+=delay;
		}
	}
}	

task Indicator(obj){
    let objText = ObjText_Create();
	ObjText_SetText(objText, "Mario");
	ObjText_SetFontSize(objText, 15);
	ObjText_SetFontType(objText, "MS Gothic");
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 207, 254, 80);
	ObjText_SetFontColorBottom(objText, 207, 254, 80);
		
	ObjText_SetMaxWidth(objText, 50);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_CENTER);
	Obj_SetRenderPriorityI(objText, 40);
	while(ObjEnemy_GetInfo(obj, INFO_LIFE)>0){
		ObjRender_SetPosition(objText, ObjMove_GetX(obj) - 25, 430, 1);
		yield;
	}
	Obj_Delete(objText);
}

task animNon(objBoss){
    let img = GetCurrentScriptDirectory() ~ "../img/non.png";
	let i=0;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
		ObjPrim_SetTexture(objBoss, img);
		if(i%2==0){ObjSprite2D_SetSourceRect(objBoss, 166, 250, 340, 770);}
		else{ObjSprite2D_SetSourceRect(objBoss, 166+512, 250, 340+512, 770);}
		ObjSprite2D_SetDestRect(objBoss, -30, -60, 30, 60);
		i++;
		wait(20);
	}
}

task animSpell(objBoss){
    let img = GetCurrentScriptDirectory() ~ "../img/spell.png";
	ObjPrim_SetTexture(objBoss, img);
	let i=0;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    if(i%2 ==0){
			ObjSprite2D_SetSourceRect(objBoss, 168, 250, 350, 770); 
			ObjSprite2D_SetDestRect(objBoss, -30, -60, 30, 60);
		}
		else{
			ObjSprite2D_SetSourceRect(objBoss, 60+512, 240, 455+512, 790); 
			ObjSprite2D_SetDestRect(objBoss, -58, -65, 58, 60);
		}
	    i++;
		wait(30);
	}
}
	
	
	
function LoadEffect(sound, volume){
    let obj1 = ObjSound_Create();
	ObjSound_Load(obj1, sound);
	ObjSound_SetVolumeRate(obj1, volume);
	return(obj1);
}

task Spell(name, obj, objBoss){
    SpellPicture;
	SpellWarn;
	SpellName(name, obj, objBoss);
}


task SpellPicture{
    let im = GetCurrentScriptDirectory~ "../img/MarioSpell.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, im);
	ObjSprite2D_SetSourceRect(obj, 66, 70, 966, 980);
	ObjSprite2D_SetDestRect(obj, 0,0, 384,448);
	Obj_SetRenderPriorityI(obj, 70);
	ObjRender_SetPosition(obj, 384, 0, 1);
	while(ObjRender_GetX(obj)>20){
	    ObjRender_SetX(obj, ObjRender_GetX(obj) -20);
		yield;
	}
	wait(90);
	loop(30){
	    ObjRender_SetX(obj, ObjRender_GetX(obj) -20);
		yield;
	}
    Obj_Delete(obj);
}

task SpellWarn{
	let objText = ObjText_Create();
	ObjText_SetText(objText, "ALL RIGHT![r]PLAYTIME IS OVER!");
	ObjText_SetFontSize(objText, 40);
	ObjText_SetFontType(objText, "MS Gothic");
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 200, 0, 0);
	ObjText_SetFontColorBottom(objText, 100, 0, 150);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_CENTER);
	
	ObjText_SetMaxWidth(objText, 360);
	Obj_SetRenderPriorityI(objText, 80);
	ObjRender_SetPosition(objText, -320, 20, 1);
	while(ObjRender_GetX(objText)<384/2- 180){
	    ObjRender_SetX(objText, ObjRender_GetX(objText) +20);
		yield;
	}
	
	wait(90);
	loop(30){
	    ObjRender_SetX(objText, ObjRender_GetX(objText) +20);
		yield;
	}
	Obj_Delete(objText);
}


task SpellName(name, obj, objBoss){
	let objText = ObjText_Create();
	ObjText_SetText(objText, name);
	ObjText_SetFontSize(objText, 20);
	ObjText_SetFontType(objText, "MS Gothic");
	ObjText_SetFontColorTop(objText, 218, 20, 20);
	ObjText_SetFontColorBottom(objText, 218, 38, 34);
	ObjText_SetFontBold(objText, true);
    ObjText_SetHorizontalAlignment(objText, ALIGNMENT_CENTER);
	ObjText_SetMaxWidth(objText, 300);
	Obj_SetRenderPriorityI(objText, 80);
	
	ObjRender_SetPosition(objText, 3*384/4- 150, 300, 1);
	wait(120);
	
	ObjText_SetMaxWidth(objText, 260);
	ObjText_SetFontBold(objText, false);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_RIGHT);
	ObjRender_SetPosition(objText, 360-260, 15, 1);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){yield;}
	Obj_Delete(objText);
}



task failBonus{
    let objText = ObjText_Create();
	ObjText_SetText(objText, "HEHE![r] SPELL BONUS[r]FAILED!");
	Obj_SetRenderPriorityI(objText, 80);
	ObjText_SetFontSize(objText, 40);
	ObjText_SetFontType(objText, "MS Gothic");
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 200, 0, 0);
	ObjText_SetFontColorBottom(objText, 100, 0, 150);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_CENTER);
	
	ObjText_SetMaxWidth(objText, 360);
	Obj_SetRenderPriorityI(objText, 80);
	ObjRender_SetPosition(objText, 384/2-180, 20, 1);
	wait(120);
	Obj_Delete(objText);
}


task DelayLaser(x, y, ang, len, wid, graph, time){ 
	let objlaser = CreateStraightLaserA1(x, y, ang, len, wid, 1, graph, time);
	ObjStLaser_SetSource(objlaser, false);
	wait(time-1);
	Obj_Delete(objlaser);
}


task control(obj, wvel, timeToChange, delay){
    ObjRender_SetAlpha(obj, 250);
	ascent(c in 0..4){
		ObjMove_AddPatternA2(obj, timeToChange*c, NO_CHANGE, NO_CHANGE, NO_CHANGE, wvel, 3);
		wvel*= -1;;
	}
	wait(delay);
	Obj_Delete(obj);
}
	
	
	
task move(xmin, xmax, ymin, ymax, delay, objBoss){
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
	    ObjMove_SetDestAtFrame(objBoss, rand(xmin, xmax), rand(ymin, ymax), 60);
		wait(delay);
	}
}
	
	
	
task spittle(num, speed1, speed2, objBoss){
    let obja = ObjSound_Create;
	ObjSound_Load(obja, GetCurrentScriptDirectory~ "../SFX/ATTACK2.WAV");
    let vec;
	let poscol1 = [241,242,245,246];
	let poscol2 = [409, 411, 416];
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
	    let col1 = poscol1[truncate(rand(0, 4))];
		let col2 = poscol2[truncate(rand(0,3))];
	    shotSound(obja, 240, 65, 15, objBoss);
	    let time = 0;
		while(time<=240){
			vec=false;
			ascent(i in 0..num){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
				if(rand(0,1)<=0.1){vec=true;}
				if(vec){CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss)-25, rand(4,5), rand(360/num *i- 360/num, 360/num *i+360/num), rand(0, 0.02), 8, col2, 0);}
				else{CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss)-25, rand(speed1,speed2), rand(360/num *i- 360/num, 360/num *i+360/num), col1, 0);}
			}
		    yield;
			time++;
		}
		wait(30);
	}
}

	
task rain(colMin, colMax, colourArray, shootTime, delay, wvel, numBul, accel, objBoss){
    let obja = ObjSound_Create;
	ObjSound_Load(obja, GetCurrentScriptDirectory~ "../SFX/ATTACK2.WAV");
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
	    let i=1;
		let col = truncate(rand(colMin, colMax));
		if (colourArray[0]!=0){col = colourArray[truncate(rand(0, length(colourArray)))];}
		let time =0;
		shotSound(obja, shootTime, 65, 10, objBoss);
		while(time< shootTime){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0) {return;}
			let obj =CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss)-25, rand(4,6), rand(180, 360), rand(-0.15, -0.1), 0, col, 0);
			Command(obj, 10, wvel, accel, 8);
			if(i%2==0){let obj =CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss)-25, rand(4,6), rand(180, 360), rand(-0.15, -0.1), 0, col, 0);
			    Command(obj, 10, wvel, accel, 10);
			}
			i++;
		    time+= 60/numBul;
			wait(60/numBul);
	    }
		wait(delay);
	}
}


task Command(obj, delay, wvel, accel, maxspeed){
    while(ObjMove_GetSpeed(obj)>0){yield;}
	let ang = ObjMove_GetAngle(obj);
	if(ang<=270){ang = 90+ (3*180-2*ang)/4; wvel= -wvel;}
	else{ang= 360-ang;}
	ObjMove_AddPatternA2(obj, delay, 0, ang, accel, wvel, maxspeed);
}



task TFinalizeSpell(scene, objBoss) {
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
    Obj_Delete(objBoss);
    DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
	if(ObjEnemyBossScene_GetInfo(scene, INFO_PLAYER_SHOOTDOWN_COUNT)      
      +ObjEnemyBossScene_GetInfo(scene, INFO_PLAYER_SPELL_COUNT) == 0){
	    ObjSound_Play(LoadEffect(GetCurrentScriptDirectory ~"../SFX/BONUS3.WAV", 100));
        AddScore(ObjEnemyBossScene_GetInfo(scene, INFO_SPELL_SCORE));
		SetPlayerLife(GetPlayerLife()+1);
    }   
	else if(ObjEnemyBossScene_GetInfo(scene, INFO_PLAYER_SHOOTDOWN_COUNT)                 
      +ObjEnemyBossScene_GetInfo(scene, INFO_PLAYER_SPELL_COUNT) > 0){
	      ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "../SFX/ATTACK3.WAV", 80));
	      failBonus;
    }
	wait(240);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
    return;
}
	
	
task TFinalizeNon(objBoss) {
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "../SFX/ATTACK3.WAV", 100));
    DeleteShotAll(TYPE_ALL, TYPE_FADE);
	wait(30);
	Obj_Delete(objBoss);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
    return;
}
