#include "./MS_ShotFunctions.txt"
#include "./MS_DefaultFunctions.txt"

task OxigenController{
let death = ObjEnemy_Create(OBJ_ENEMY);
ObjEnemy_Regist(death);
let switch = 0;
let drown = 0;
BubbleController;
OxygenBar;
	loop{
		if(GetCommonData("Stacks",0) < 0){switch = 0;}
		if(GetCommonData("Stacks",0) < 200){drown = 0;}
		
		if(GetCommonData("Underwater",false) == true || GetCommonData("UnderwaterSpell",false) == true){ 
			if(GetPlayerInvincibilityFrame < 1){
				if(GetCommonData("Safe",false) == false){
				SetCommonData("Stacks",GetCommonData("Stacks",0)+1);
				}
			}
		}
		
		if(GetCommonData("Underwater",false) == false || GetCommonData("UnderwaterSpell",false) == false){
			if(GetCommonData("Stacks",0) > 0){
				if(GetCommonData("Safe",false) == true){
					SetCommonData("Stacks",GetCommonData("Stacks",0)-1);
				}
				if(GetCommonData("Safe",false) == false){
					if(GetCommonData("Underwater",false) == false && GetCommonData("UnderwaterSpell",false) == false){
					SetCommonData("Stacks",GetCommonData("Stacks",0)-1);
					}
				}
			}
		}

		if(GetCommonData("Stacks",0) > 200 && drown == 0){
		PlaySound(GetCurrentScriptDirectory ~ "/se/drown.wav", 255, 0);
		drown++;
		}
		
		if(GetCommonData("Stacks",0)>=300){
		switch = 1;
		ObjEnemy_SetIntersectionCircleToPlayer(death,GetPlayerX,GetPlayerY,16);
		}
		if(switch == 1){SetCommonData("Stacks",GetCommonData("Stacks",0)-4);}
		SetCommonData("Safe",false);
	yield;
	}
}

task BubbleController{
	loop{
	if(GetCommonData("Underwater",false) == true && GetCommonData("Safe",false) == false){
	BubbleSmall(GetPlayerX, GetPlayerY);
	wait(GetCommonData("Stacks",0)/5);
	}
	yield;
	}
	
	task BubbleSmall(x, y){
	let obj = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(obj);
	ObjRender_SetPosition(obj,x,y,0);
	ObjMove_AddPatternA2(obj, 0, rand(0.2, 1), rand(270-15, 270+15), 0.01, 0, 2);
	Render(obj);

		task Render(obj){
				
			ObjPrim_SetTexture(obj, GetCurrentScriptDirectory ~ "img/Ingame/bubblesmall.png");
			Obj_SetRenderPriorityI(obj, 30);
			ObjSprite2D_SetSourceRect(obj,0,0,256,256);
			ObjRender_SetScaleXYZ(obj, 0.05, 0.05, 1);
			ObjRender_SetAlpha(obj, 90);
			ObjSprite2D_SetDestCenter(obj);	
			ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
			
			loop(60*5){
			if(GetCommonData("Underwater",false) == false){Obj_Delete(obj);}
			yield;
			}
			Obj_Delete(obj);
		}
	}
}

task OxygenBar{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory ~ "img/Ingame/color.png");
	Obj_SetRenderPriority(obj, 0.82);
	ObjSprite2D_SetSourceRect(obj,0,0,270,10);
	ObjSprite2D_SetDestCenter(obj);	
	ObjRender_SetScaleXYZ(obj, 1,1,1);
	ObjRender_SetAlpha(obj, 255);
	ObjRender_SetPosition(obj,GetCenterX+32,472+2,0);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjRender_SetColor(obj, 5, 5, 155);
	
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj2, GetCurrentScriptDirectory ~ "img/Ingame/color.png");
	Obj_SetRenderPriority(obj2, 0.81);
	ObjSprite2D_SetSourceRect(obj2,0,0,390-6,15);
	ObjSprite2D_SetDestCenter(obj2);	
	ObjRender_SetScaleXYZ(obj2, 1,1,1);
	ObjRender_SetAlpha(obj2, 255);
	ObjRender_SetPosition(obj2,GetCenterX+32,472+2,0);
	ObjRender_SetBlendType(obj2, BLEND_ALPHA);
	ObjRender_SetColor(obj2, 0, 0, 0);
	loop{
	let OxygenTotal = GetCommonData("Stacks",0);
	if(OxygenTotal > 200){ObjRender_SetColor(obj, 255, 5, 5);}
	else{ObjRender_SetColor(obj, 5, 5, 155);}
	ObjSprite2D_SetSourceRect(obj,0,0,390-OxygenTotal*1.3-6,15);
	ObjSprite2D_SetDestCenter(obj);	
	yield;
	}
}

function Bubble(x, y, sca){
	let obj = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(obj);
	ObjRender_SetPosition(obj,x,y,0);
	Render(obj);
	return obj;

		task Render(obj){
		let scale = sca;
		let scalecurrent = 5;
		let alpha = 0;
				
			ObjPrim_SetTexture(obj, GetCurrentScriptDirectory ~ "img/Ingame/bubble.png");
			Obj_SetRenderPriorityI(obj, 30);
			ObjSprite2D_SetSourceRect(obj,0,0,256,256);
			ObjRender_SetScaleXYZ(obj, scalecurrent,scalecurrent,scalecurrent);
			ObjSprite2D_SetDestCenter(obj);	
			ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
			
			while(!Obj_IsDeleted(obj)){
			ObjRender_SetAlpha(obj, alpha);
			
			let dist = GetObjectDistance(obj, GetPlayerObjectID);
			
			if(dist < scalecurrent*100){
			SetCommonData("Safe",true);
			}
			
			ObjRender_SetScaleXYZ(obj, scalecurrent, scalecurrent, scalecurrent);
			if(scalecurrent > scale){scalecurrent-=0.1;}
			if(scalecurrent < scale){scalecurrent+=0.1;}
			alpha+=2;
			yield;
			}
			Obj_Delete(obj);
		}
	}
	
function BubbleRed(x, y, sca){
	let obj = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(obj);
	ObjRender_SetPosition(obj,x,y,0);
	Render(obj);
	return obj;

		task Render(obj){
		let scale = sca;
		let scalecurrent = 5;
		let alpha = 0;
				
			ObjPrim_SetTexture(obj, GetCurrentScriptDirectory ~ "img/Ingame/bubble.png");
			Obj_SetRenderPriorityI(obj, 30);
			ObjSprite2D_SetSourceRect(obj,0,0,256,256);
			ObjRender_SetScaleXYZ(obj, scalecurrent,scalecurrent,scalecurrent);
			ObjSprite2D_SetDestCenter(obj);	
			ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
			ObjRender_SetColor(obj, 255, 10, 10);
			
			while(!Obj_IsDeleted(obj)){
			ObjRender_SetAlpha(obj, alpha);
			
			let dist = GetObjectDistance(obj, GetPlayerObjectID);
			
			if(dist < scalecurrent*100){
			SetCommonData("Safe",true);
			}
			
			ObjRender_SetScaleXYZ(obj, scalecurrent, scalecurrent, scalecurrent);
			if(scalecurrent > scale){scalecurrent-=0.1;}
			if(scalecurrent < scale){scalecurrent+=0.1;}
			alpha+=2;
			yield;
			}
			Obj_Delete(obj);
		}
	}
	
function InstantBubble(x, y, sca){
	let obj = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(obj);
	ObjRender_SetPosition(obj,x,y,0);
	Render(obj);
	return obj;

		task Render(obj){
		let scale = sca;
		let scalecurrent = scale;
		let alpha = 0;
				
			ObjPrim_SetTexture(obj, GetCurrentScriptDirectory ~ "img/Ingame/bubble.png");
			Obj_SetRenderPriorityI(obj, 30);
			ObjSprite2D_SetSourceRect(obj,0,0,256,256);
			ObjRender_SetScaleXYZ(obj, scalecurrent,scalecurrent,scalecurrent);
			ObjSprite2D_SetDestCenter(obj);	
			ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
			
			while(!Obj_IsDeleted(obj)){
			ObjRender_SetAlpha(obj, alpha);
			
			let dist = GetObjectDistance(obj, GetPlayerObjectID);
			
			if(dist < scalecurrent*100){
			SetCommonData("Safe",true);
			}
			
			ObjRender_SetScaleXYZ(obj, scalecurrent, scalecurrent, scalecurrent);
			if(scalecurrent > scale){scalecurrent-=0.1;}
			if(scalecurrent < scale){scalecurrent+=0.1;}
			alpha+=2;
			yield;
			}
			Obj_Delete(obj);
		}
	}
	
function DualShotA1(x, y, spd, ang, graph, del, blend1, blend2){
let obj = FireShotA1(x, y, spd, ang, graph, del);
let obj2 = CreateShotA1(x, y, spd, ang, graph, del);
ObjRender_SetBlendType(obj, blend1);
ObjRender_SetBlendType(obj2, blend2);
ObjShot_SetIntersectionEnable(obj, 0);
ObjShot_SetIntersectionEnable(obj2, 0);
Delete(obj, obj2);
return obj;

task Delete(obj, obj2){
while(!Obj_IsDeleted(obj) || !Obj_IsDeleted(obj2)){
if(Obj_IsDeleted(obj)){Obj_Delete(obj2);}
if(Obj_IsDeleted(obj2)){Obj_Delete(obj);}
yield;
}
Obj_Delete(obj);
Obj_Delete(obj2);
}
}