#include "./Default_Functions.txt"
#include "./Default_ShotFunctions.txt"

let talkHold = false;

function SetText(x, y, side,num,type,time,text,xoff,yoff, player)
	{
	let typeA = floor(type/3);
	let typeB = [48,64,typeA];	
	if(type >= 9)
		{
		typeA = min(3,type-9);
		typeB = [0,1,3];
		}
	else if(type%3 == 1)
		{
		typeB = [70,96,typeA];
		}
	else if(type%3 == 2)
		{
		typeB = [108,128,typeA];
		}

	let linelength = 0;
	let lineheight = 0;

	let objText = ObjText_Create();
	ObjText_SetText(objText, text);
	ObjText_SetFontType(objText,"Century Gothic");
	ObjText_SetFontSize(objText, 21);
	ObjText_SetFontColorTop(objText,0,0,0);
	ObjText_SetFontColorBottom(objText,0,0,0);
	ObjText_SetFontBorderType(objText,BORDER_NONE);
	ObjText_SetFontBold(objText,true);
		
	ObjText_SetLinePitch(objText, 0);
	Obj_SetRenderPriority(objText, 0.91);
	ObjText_SetAutoTransCenter(objText, false);
	linelength = ObjText_GetTotalWidth(objText);
	lineheight = ObjText_GetTotalHeight(objText);
	ObjText_SetMaxWidth(objText, linelength+1);
	//ObjRender_SetPosition(objText, 32,20,0);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_LEFT);

	let objBalloon = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	Obj_SetRenderPriority(objBalloon,0.90);
	ObjPrim_SetTexture(objBalloon,GetCurrentScriptDirectory ~ "/img/Balloon.png");
	ObjSpriteList2D_SetSourceRect(objBalloon,0,0+32*typeA,8,8+32*typeA);
	ObjSpriteList2D_SetDestRect(objBalloon,32-8,20-8,32,20);
	ObjSpriteList2D_AddVertex(objBalloon);
	ObjSpriteList2D_SetSourceRect(objBalloon,8,0+32*typeA,24,8+32*typeA);
	ObjSpriteList2D_SetDestRect(objBalloon,32,20-8,32+(linelength),20);
	ObjSpriteList2D_AddVertex(objBalloon);
	ObjSpriteList2D_SetSourceRect(objBalloon,24,0+32*typeA,32,8+32*typeA);
	ObjSpriteList2D_SetDestRect(objBalloon,32+(linelength),20-8,32+(linelength)+8,20);
	ObjSpriteList2D_AddVertex(objBalloon);
	ObjSpriteList2D_SetSourceRect(objBalloon,0,8+32*typeA,8,24+32*typeA);
	ObjSpriteList2D_SetDestRect(objBalloon,32-8,20,32,20+(lineheight));
	ObjSpriteList2D_AddVertex(objBalloon);
	ObjSpriteList2D_SetSourceRect(objBalloon,8,8+32*typeA,24,24+32*typeA);
	ObjSpriteList2D_SetDestRect(objBalloon,32,20,32+(linelength),20+(lineheight));
	ObjSpriteList2D_AddVertex(objBalloon);
	ObjSpriteList2D_SetSourceRect(objBalloon,24,8+32*typeA,32,24+32*typeA);
	ObjSpriteList2D_SetDestRect(objBalloon,32+(linelength),20,32+(linelength)+8,20+(lineheight));
	ObjSpriteList2D_AddVertex(objBalloon);
	ObjSpriteList2D_SetSourceRect(objBalloon,0,24+32*typeA,8,32+32*typeA);
	ObjSpriteList2D_SetDestRect(objBalloon,32-8,20+(lineheight),32,20+(lineheight)+8);
	ObjSpriteList2D_AddVertex(objBalloon);
	ObjSpriteList2D_SetSourceRect(objBalloon,8,24+32*typeA,24,32+32*typeA);
	ObjSpriteList2D_SetDestRect(objBalloon,32,20+(lineheight),32+(linelength),20+(lineheight)+8);
	ObjSpriteList2D_AddVertex(objBalloon);
	ObjSpriteList2D_SetSourceRect(objBalloon,24,24+32*typeA,32,32+32*typeA);
	ObjSpriteList2D_SetDestRect(objBalloon,32+(linelength),20+(lineheight),32+(linelength)+8,20+(lineheight)+8);
	ObjSpriteList2D_AddVertex(objBalloon);
	
	if(side==0)
		{
		ObjSpriteList2D_SetSourceRect(objBalloon,typeB[0],6+typeB[2]*32,typeB[1],32+typeB[2]*32);
		ObjSpriteList2D_SetDestRect(objBalloon,32,-2,48,14);
		ObjSpriteList2D_AddVertex(objBalloon);
		}
	if(side==1)
		{
		ObjSpriteList2D_SetSourceRect(objBalloon,typeB[1],6+typeB[2]*32,typeB[0],32+typeB[2]*32);
		ObjSpriteList2D_SetDestRect(objBalloon,32+(linelength)-16,-2,32+(linelength),14);
		ObjSpriteList2D_AddVertex(objBalloon);
		}
	if(side==2)
		{
		ObjSpriteList2D_SetSourceRect(objBalloon,typeB[0],6+typeB[2]*32,typeB[1],32+typeB[2]*32);
		ObjSpriteList2D_SetDestRect(objBalloon,32+(linelength/2)-16,-2,32+(linelength/2),14);
		ObjSpriteList2D_AddVertex(objBalloon);
		}

	ObjSpriteList2D_CloseVertex(objBalloon);
	
	if(side==2){
	ObjRender_SetPosition(objText,x-((30*num)*((-1)^side))-(linelength-linelength/2)*side+xoff, y-side*25+yoff, 0);
	ObjRender_SetPosition(objBalloon,x-((30*num)*((-1)^side))-32-(linelength-linelength/2)*side+xoff, y-side*25-20+yoff, 0);}
	if(side!=2){
	ObjRender_SetPosition(objText,x-((30*num)*((-1)^side))-(linelength-90)*side+xoff, y-side*25+yoff, 0);
	ObjRender_SetPosition(objBalloon,x-((30*num)*((-1)^side))-32-(linelength-90)*side+xoff, y-side*25-20+yoff, 0);}
	
	ascent(i in 0..9)
		{
		ObjRender_SetScaleY(objBalloon,i/8);
		ObjRender_SetScaleY(objText,i/8);
		if(talkHold == false || i%2 == 0)
			{
			yield;
			}
		}
	ObjRender_SetScaleY(objBalloon,1);
	ObjRender_SetScaleY(objText,1);
	if(time == 0)
		{
		while(GetVirtualKeyState(VK_SHOT)==KEY_PUSH && talkHold == false){yield;}
		while(GetVirtualKeyState(VK_SHOT)!=KEY_PUSH && talkHold == false){
		if(player == true){
		if(side==2){
		ObjRender_SetPosition(objText,GetPlayerX-((30*num)*((-1)^side))-(linelength-linelength/2)*side+xoff, GetPlayerY-side*25+yoff, 0);
		ObjRender_SetPosition(objBalloon,GetPlayerX-((30*num)*((-1)^side))-32-(linelength-linelength/2)*side+xoff, GetPlayerY-side*25-20+yoff, 0);}
		if(side!=2){
		ObjRender_SetPosition(objText,GetPlayerX-((30*num)*((-1)^side))-(linelength-90)*side+xoff, GetPlayerY-side*25+yoff, 0);
		ObjRender_SetPosition(objBalloon,GetPlayerX-((30*num)*((-1)^side))-32-(linelength-90)*side+xoff, GetPlayerY-side*25-20+yoff, 0);}		
		}		
		if(player == false){
		if(side==2){
		ObjRender_SetPosition(objText,x-((30*num)*((-1)^side))-(linelength-linelength/2)*side+xoff, y-side*25+yoff, 0);
		ObjRender_SetPosition(objBalloon,x-((30*num)*((-1)^side))-32-(linelength-linelength/2)*side+xoff, y-side*25-20+yoff, 0);}
		if(side!=2){
		ObjRender_SetPosition(objText,x-((30*num)*((-1)^side))-(linelength-90)*side+xoff, y-side*25+yoff, 0);
		ObjRender_SetPosition(objBalloon,x-((30*num)*((-1)^side))-32-(linelength-90)*side+xoff, y-side*25-20+yoff, 0);}			
		}
		yield;}
		}
	else
		{
		loop(time){
		if(player == true){
		if(side==2){
		ObjRender_SetPosition(objText,GetPlayerX-((30*num)*((-1)^side))-(linelength-linelength/2)*side+xoff, GetPlayerY-side*25+yoff, 0);
		ObjRender_SetPosition(objBalloon,GetPlayerX-((30*num)*((-1)^side))-32-(linelength-linelength/2)*side+xoff, GetPlayerY-side*25-20+yoff, 0);}
		if(side!=2){
		ObjRender_SetPosition(objText,GetPlayerX-((30*num)*((-1)^side))-(linelength-90)*side+xoff, GetPlayerY-side*25+yoff, 0);
		ObjRender_SetPosition(objBalloon,GetPlayerX-((30*num)*((-1)^side))-32-(linelength-90)*side+xoff, GetPlayerY-side*25-20+yoff, 0);}		
		}		
		if(player == false){
		if(side==2){
		ObjRender_SetPosition(objText,x-((30*num)*((-1)^side))-(linelength-linelength/2)*side+xoff, y-side*25+yoff, 0);
		ObjRender_SetPosition(objBalloon,x-((30*num)*((-1)^side))-32-(linelength-linelength/2)*side+xoff, y-side*25-20+yoff, 0);}
		if(side!=2){
		ObjRender_SetPosition(objText,x-((30*num)*((-1)^side))-(linelength-90)*side+xoff, y-side*25+yoff, 0);
		ObjRender_SetPosition(objBalloon,x-((30*num)*((-1)^side))-32-(linelength-90)*side+xoff, y-side*25-20+yoff, 0);}			
		}	
		yield;}
		}
	Obj_Delete(objBalloon);
	Obj_Delete(objText);
	}

function Spirit(x, y){
let obj = ObjEnemy_Create(OBJ_ENEMY);
ObjEnemy_Regist(obj);
ObjMove_SetPosition(obj, x, y);
Render(obj);
return obj;

	task Render(tar){
		let XY = 32;
		let obj1 =ObjPrim_Create(OBJ_SPRITE_2D);
		Obj_SetRenderPriorityI(obj1, 40);
		ObjPrim_SetTexture(obj1,GetCurrentScriptDirectory ~ "/img/Ingame/I stole this mwahaha.png");
		ObjSprite2D_SetSourceRect(obj1,0,0,32,32);
		ObjSprite2D_SetDestCenter(obj1);
		ObjRender_SetScaleXYZ(obj1, 1, 1, 1);
		ObjRender_SetBlendType(obj1,BLEND_ADD_ARGB);
		ObjRender_SetPosition(obj1,GetX(tar), GetY(tar),0);
		
		let a = 0;
		let b = 0;
		while(!Obj_IsDeleted(tar)){
		if(a <= 4*1) {ObjSprite2D_SetSourceRect(obj1,0,0,XY,XY);}
		if(a >= 4*1) {ObjSprite2D_SetSourceRect(obj1,XY,0,XY*2,XY);}
		if(a >= 4*2) {ObjSprite2D_SetSourceRect(obj1,XY*2,0,XY*3,XY);}
		if(a >= 4*3) {ObjSprite2D_SetSourceRect(obj1,XY*3,0,XY*4,XY);}
		if(a >= 4*4) {ObjSprite2D_SetSourceRect(obj1,XY*4,0,XY*5,XY);}
		if(a >= 4*5) {ObjSprite2D_SetSourceRect(obj1,XY*5,0,XY*6,XY);}
		if(a >= 4*6) {ObjSprite2D_SetSourceRect(obj1,XY*6,0,XY*7,XY);}
		if(a >= 4*7) {ObjSprite2D_SetSourceRect(obj1,XY*7,0,XY*8,XY);}
		if(a >= 4*8) {a = 0;}
		ObjRender_SetAngleZ(obj1, a*12);
		a++;
		b++;
		if(b == 4){b = 0; Spirit2(tar);}
		ObjRender_SetPosition(obj1,GetX(tar), GetY(tar),0);
		yield;
		}
		Obj_Delete(obj1);
	}
	
	task Spirit2(tar){
		let XY = 32;
		let X = GetX(tar);
		let Y = GetY(tar);
		let obj1 =ObjPrim_Create(OBJ_SPRITE_2D);
		Obj_SetRenderPriorityI(obj1, 40);
		ObjPrim_SetTexture(obj1,GetCurrentScriptDirectory ~ "/img/Ingame/I stole this mwahaha.png");
		ObjSprite2D_SetSourceRect(obj1,0,0,32,32);
		ObjSprite2D_SetDestCenter(obj1);
		ObjRender_SetScaleXYZ(obj1, 1, 1, 1);
		ObjRender_SetBlendType(obj1,BLEND_ADD_ARGB);
		ObjRender_SetPosition(obj1,X,Y,0);
		
		let a = 0;
		let b = 0;
		let c = 0;
		let cr = rand(-0.4, 0.4);
		let scale = 1;
		loop{
		if(a <= 4*1) {ObjSprite2D_SetSourceRect(obj1,0,0,XY,XY);}
		if(a >= 4*1) {ObjSprite2D_SetSourceRect(obj1,XY,0,XY*2,XY);}
		if(a >= 4*2) {ObjSprite2D_SetSourceRect(obj1,XY*2,0,XY*3,XY);}
		if(a >= 4*3) {ObjSprite2D_SetSourceRect(obj1,XY*3,0,XY*4,XY);}
		if(a >= 4*4) {ObjSprite2D_SetSourceRect(obj1,XY*4,0,XY*5,XY);}
		if(a >= 4*5) {ObjSprite2D_SetSourceRect(obj1,XY*5,0,XY*6,XY);}
		if(a >= 4*6) {ObjSprite2D_SetSourceRect(obj1,XY*6,0,XY*7,XY);}
		if(a >= 4*7) {ObjSprite2D_SetSourceRect(obj1,XY*7,0,XY*8,XY);}
		if(a >= 4*8) {a = 0;}
		ObjRender_SetAngleZ(obj1, a*12);
		a++;
		b++;
		c += cr;
		scale-=0.02;
		ObjRender_SetPosition(obj1,X+c,Y-b,0);
		ObjRender_SetScaleXYZ(obj1, scale, scale, 1);
		if(scale <= 0){Obj_Delete(obj1); break;}
		yield;
		}	
	}
}


function Enemy(x, y){
let obj = ObjEnemy_Create(OBJ_ENEMY);
ObjEnemy_Regist(obj);
ObjMove_SetPosition(obj, x, y);
return obj;
}

function Spear(x, y, sca){
let obj = ObjEnemy_Create(OBJ_ENEMY);
ObjEnemy_Regist(obj);
Obj_SetValue(obj, "Angle", 0);
let las = CreateStraightLaserA1(x, y, 90, 300, 5, 9999999999999, SS_LASER_GREY, 0);
ObjRender_SetAlpha(las, 255);
ObjShot_SetSpellResist(obj, 1);
ObjShot_SetAutoDelete(las, 0);
ObjMove_SetPosition(obj, x, y);
Render(obj);
Follow(las, obj);
return obj;

	task Follow(las, tar){
		while(!Obj_IsDeleted(tar)){
		let scale = Obj_GetValueD(tar, "Scale", 0);
		let ang = Obj_GetValueD(tar, "Angle", 0);
		ObjMove_SetPosition(las, GetX(tar)+(scale*150)*cos(ang), GetY(tar)+(scale*150)*sin(ang));
		ObjStLaser_SetAngle(las, ang+180);
		ObjLaser_SetLength(las, scale*300);
		yield;
		}
		Obj_Delete(las);
	}
	
	task Render(tar){
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		let scale = 0;
		let sc = -90;
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory ~ "./img/Ingame/spearaf.png");
		Obj_SetRenderPriorityI(obj, 55);
		ObjSprite2D_SetSourceRect(obj, 0, 0, 46, 256);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetAngleZ(obj, 0);
		ObjRender_SetScaleXYZ(obj, scale, scale, scale);
		ObjRender_SetPosition(obj, GetX(tar), GetY(tar), 0);
		let a = 0;
		while(!Obj_IsDeleted(tar)){
		//if(a < 4){a++;}
		//else{a = 0; Afterimage;}
		let ang = Obj_GetValueD(tar, "Angle", 0);
		ObjRender_SetAngleZ(obj, ang+90);
		ObjRender_SetPosition(obj, ObjMove_GetX(tar), ObjMove_GetY(tar), 0);
		ObjRender_SetColor(obj, 255, 255, 255);
		Obj_SetValue(tar, "Scale", sca*cos(sc));
		ObjRender_SetScaleXYZ(obj, sca*cos(sc), sca*cos(sc), sca*cos(sc));
		if(sc != 0){sc++;}
		yield;
		}
		Obj_Delete(tar);
		Obj_Delete(obj);
		
			task Afterimage{
			let obj = ObjPrim_Create(OBJ_SPRITE_2D);
			let scale = Obj_GetValueD(tar, "Scale", 0);
			let ang = Obj_GetValueD(tar, "Angle", 0);
			let alpha = 122;
			ObjPrim_SetTexture(obj, GetCurrentScriptDirectory ~ "./img/Ingame/spear.png");
			Obj_SetRenderPriorityI(obj, 44);
			ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
			ObjSprite2D_SetSourceRect(obj, 0, 0, 46, 256);
			ObjSprite2D_SetDestCenter(obj);
			ObjRender_SetAngleZ(obj, ang+90);
			ObjRender_SetScaleXYZ(obj, scale, scale, scale);
			ObjRender_SetPosition(obj, GetX(tar), GetY(tar), 0);
			let a = 0;
			
			while(!Obj_IsDeleted(obj)){
			ObjRender_SetColor(obj, 255, 255, 255);
			ObjRender_SetScaleXYZ(obj, scale, scale, scale);
			ObjRender_SetAlpha(obj, alpha);
			scale = (scale*1.1);
			alpha -= 12;
			if(alpha < 0){Obj_Delete(obj);}
			yield;
			}		
		}
	}
}