#include "script/DP01/System/Index.txt"

let current = GetCurrentScriptDirectory;

let imgEffect = GetCurrentScriptDirectory~"img/effect.png";
let imgAura = GetCurrentScriptDirectory~"img/eff_aura.png";

task BBullet(x, y, v, angle,graphic,delay) {
     let obj=ObjShot_Create(OBJ_SHOT);

     ObjMove_SetPosition(obj, x, y);
     ObjMove_SetAngle(obj, angle);
     ObjMove_SetSpeed(obj, v);
     ObjShot_SetGraphic(obj,graphic);
     ObjShot_SetDelay  (obj, delay);

     while(Obj_IsDeleted(obj)==false) {

             if(ObjMove_GetX(obj)<GetStgFrameLeft()) {
                    ObjMove_SetAngle(obj, 180 - ObjMove_GetAngle(obj) );
                    ObjMove_SetX(obj,  ObjMove_GetX(obj) + 0.1);
             }

             if(ObjMove_GetX(obj)>GetStgFrameWidth()) {
                    ObjMove_SetAngle(obj, 180 - ObjMove_GetAngle(obj) );
                    ObjMove_SetX(obj,  ObjMove_GetX(obj) - 0.1);
             }

             yield;
      }
}




task MagicCircleV2() {
/*placed on the main loop
Creates a Magic Circle that follows the boss around
Pre-DDC styled circle, rotates and changes size
*/
let angle = 0;
let scalecount = 0;
let scale = 1;
let scale2 = 1;



let circle = ObjPrim_Create(OBJ_SPRITE_2D);
ObjPrim_SetTexture(circle, imgEffect);
Obj_SetRenderPriority(circle,0.3);
let enmx = ObjMove_GetX(objEnemy);
let enmy = ObjMove_GetY(objEnemy);
ObjSprite2D_SetDestRect (circle, enmx, enmy, enmx+128, enmy+125);

//ObjRender_SetAlpha(circle, 150);

	while(!Obj_IsDeleted(objEnemy)) {
		ObjSprite2D_SetSourceRect(circle, 127, 81, 255, 206);
		
		ObjSprite2D_SetDestCenter(circle);

		ObjRender_SetX(circle, ObjMove_GetX(objEnemy));
		ObjRender_SetY(circle, ObjMove_GetY(objEnemy));

		
		ObjRender_SetAngleZ(circle, angle);
		
	if(scalecount>=0 && scalecount<=60){
		scale+= 0.01;
		}
	if(scalecount>=61 && scalecount<=121){
		scale-=0.01;
		}
	if(scalecount==122){
		scalecount = 0;
		}
		
		ObjRender_SetScaleX(circle,scale);
		ObjRender_SetScaleY(circle,scale);
		
		angle+=8;
		scalecount ++;

		yield;
	}
	if (Obj_IsDeleted(objEnemy)){
	Obj_Delete(circle);
	}

}




task MagicCircleV3() {
/*placed on the main loop
Creates a Magic Circle that follows the boss around
DDC styled circle, rotates in XYZ slowly
*/
let angle = 0;
let scalecount = 0;
let scale = 1;
let scale2 = 1;
let angle2 = 0;
let angle3 =0;



let circle = ObjPrim_Create(OBJ_SPRITE_2D);
ObjPrim_SetTexture(circle, imgEffect);

let enmx = ObjMove_GetX(objEnemy);
let enmy = ObjMove_GetY(objEnemy);
ObjSprite2D_SetDestRect (circle, enmx, enmy, enmx+128, enmy+125);

//ObjRender_SetAlpha(circle, 150);

	while(!Obj_IsDeleted(objEnemy)) {
		ObjSprite2D_SetSourceRect(circle, 127, 81, 255, 206);
		
		ObjSprite2D_SetDestCenter(circle);

		ObjRender_SetX(circle, ObjMove_GetX(objEnemy));
		ObjRender_SetY(circle, ObjMove_GetY(objEnemy));

		
		ObjRender_SetAngleZ(circle, 0);
		ObjRender_SetAngleX(circle, 45);
		ObjRender_SetAngleY(circle, angle3);
		
	/* if(scalecount>=0 && scalecount<=60){
		scale+= 0.01;
		}
	if(scalecount>=61 && scalecount<=121){
		scale-=0.01;
		}
	if(scalecount==122){
		scalecount = 0;
		}
		*/
		//ObjRender_SetScaleX(circle,scale);
		//ObjRender_SetScaleY(circle,scale);
		
		angle++;
		angle2++;
		angle3++;
		scalecount ++;

		yield;
	}


}



task charge1() {
	let angle=0;
	let anglecount=0;
	let petal = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(petal, imgEffect);
	ObjSprite2D_SetDestRect (petal, ObjMove_GetX(objEnemy)+700, ObjMove_GetY(objEnemy)-700, ObjMove_GetX(objEnemy)+27, ObjMove_GetY(objEnemy)+719);
	let delete=false;
			

//ObjRender_SetAlpha(petal, 150);


		
		angle=angle+round(rand(0,5));
		
		ObjRender_SetAngleX(petal, angle);
		ObjRender_SetAngleY(petal, angle);
		ObjRender_SetAngleZ(petal, angle);
		
		
		ObjSprite2D_SetSourceRect(petal, 194, 230, 221, 249);
		
		ObjSprite2D_SetDestCenter(petal);

		
		

		ObjRender_SetDestAtSpeed(petal, ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), 5);

		
		if (ObjRender_GetX(petal) == ObjMove_GetX(objEnemy) ){
			Obj_Delete(petal);
		}
		
}

task aura () {
	let frame=0;
	let aura = ObjPrim_Create(OBJ_SPRITE_2D);
	let a=0;
	let b=0;
	ObjPrim_SetTexture(aura, imgAura);
	ObjSprite2D_SetDestRect (aura, ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), ObjMove_GetX(objEnemy)+32, ObjMove_GetY(objEnemy)+40);
	ObjRender_SetColor(aura, 234, 31,74);
	ObjRender_SetAngleZ(aura, 180);
	ObjRender_SetAlpha(aura, 200);
	

	
	
	while(!Obj_IsDeleted(objEnemy)) {
	
	ObjSprite2D_SetSourceRect(aura, 9, 4, 41, 44);
	ObjSprite2D_SetDestCenter(aura);
	
	
	
	ObjRender_SetX(aura, ObjMove_GetX(objEnemy)+b);
	

	
	ObjRender_SetY(aura, ObjMove_GetY(objEnemy)+a);
	
	
	a=round(rand(-40,-20));
	b=round(rand(-5,5));
	
	if (frame==31) {
	Obj_Delete(aura);
	frame=0;
	}
	
	yield;
	
	}
	

	
}

task ObjRender_SetDestAtSpeed(obj, x, y, s){
    let ox = ObjRender_GetX(obj);
    let oy = ObjRender_GetY(obj);
    let dist = ((x-ox)^2 + (y-oy)^2)^0.5;
    let dx = (x-ox)/dist*s;
    let dy = (y-oy)/dist*s;
    loop(floor(dist/s)){
        ox += dx;
        oy += dy;
        ObjRender_SetX(obj, ox);
        ObjRender_SetY(obj, oy);
        yield;
    }
    ObjRender_SetX(obj, x);
    ObjRender_SetY(obj, y);
}


	task Aura{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=GetCurrentScriptDirectory~"img/Aura.png";
	let color = [96,0,32];
	let value = 96;
	let dir = rand(0,359);
	let c = 0;
	let s = 2;
	ObjPrim_SetTexture(obj,img);
	ObjPrim_SetPrimitiveType(obj,PRIMITIVE_TRIANGLESTRIP);
	ObjSprite2D_SetSourceRect(obj,48,0,96,64);
	ObjSprite2D_SetDestCenter(obj);
	ObjPrim_SetVertexCount(obj,4);
	ObjPrim_SetVertexPosition(obj,0,-24,-32,0);
	ObjPrim_SetVertexPosition(obj,1,-24,32,0);
	ObjPrim_SetVertexPosition(obj,2,24,-32 ,0);
	ObjPrim_SetVertexPosition(obj,3,24, 32,0);
	ObjPrim_SetVertexUVT(obj,0,0,0);
	ObjPrim_SetVertexUVT(obj,1,0,64);
	ObjPrim_SetVertexUVT(obj,2,48,0);
	ObjPrim_SetVertexUVT(obj,3,48,64);
	ObjRender_SetAngleXYZ(obj,0,0,dir);
	ObjRender_SetBlendType(obj,BLEND_ADD_RGB);
	ObjRender_SetColor(obj,color[0],color[1],color[2]);
	Obj_SetRenderPriority(obj,0.21);
	while(s>0 && !Obj_IsDeleted(objEnemy)){
	ObjRender_SetPosition(obj,ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy)-10,0);
	ObjRender_SetScaleXYZ(obj,s,s,s);
	s -= 0.03;
	ObjRender_SetColor(obj,color[0]-c,color[1],color[2]+c);
	if(c==7){Aura;}
	c++;
	yield;
	}
	Obj_Delete(obj);
	}
	
	function TCharge01(x,y,size,frame,r,g,b){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=GetCurrentScriptDirectory~"img/charge.png";
	ObjPrim_SetTexture(obj,img);
	ObjSprite2D_SetSourceRect(obj,0,0,128,128);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,x,y,0);
	ObjRender_SetAlpha(obj,0);
	ObjRender_SetScaleXYZ(obj,size,size,0);
	ObjRender_SetColor(obj,r,g,b);
	TAlpha;
	TShrink;
	return obj;
	task TAlpha{
		let l=10;
		let alpha=255/l;
		loop(l){
			ObjRender_SetAlpha(obj,alpha);
			alpha+=255/(l*2);
			yield;
			}
		}
	task TShrink{
		let l=frame;
		let a = size*2/l/l;
		ascent(i in 0..l){
			ObjRender_SetScaleXYZ(obj,size,size,0);
			size = size-i*a;
			yield;
			}
		Obj_Delete(obj);
		
	}
}

function TCharge02(x,y,size,frame,r,g,b){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=GetCurrentScriptDirectory~"img/charge.png";
	ObjPrim_SetTexture(obj,img);
	ObjSprite2D_SetSourceRect(obj,0,0,128,128);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,x,y,0);
	ObjRender_SetAlpha(obj,0);
	ObjRender_SetScaleXYZ(obj,size,size,0);
	ObjRender_SetColor(obj,r,g,b);
	TAlpha;
	TShrink;
	return obj;
	task TAlpha{
		let l=10;
		let alpha=255/l;
		loop(l){
			ObjRender_SetAlpha(obj,alpha);
			alpha+=255/(l*2);
			yield;
			}
		}
	task TShrink{
			let a = size/frame;
			loop(frame){
			ObjRender_SetScaleXYZ(obj,size,size,0);
			size = size-a;
			yield;
			}
		Obj_Delete(obj);
		
	}
}

task maple{
	ascent(i in 0..20){
	etama(150+i*10,i);
	etama(150+i*10,i);
	yield;
	}
	}
	
	task etama(r,a){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=GetCurrentScriptDirectory~"img/eff.png";
	r = round(r);
	let angle = [0,0,0];
	let dir = rand(0,359);
	let c = [0,0,rand(0,359)];
	let v = [-7.5,-5,-2.5,2.5,5,7.5];
	let size = r/120+1;
	let m = -10;
	let vr = rand(0,5.4);
	vr = round(vr);
	angle[2] = v[vr];
	let s = r;
	ObjPrim_SetTexture(obj,img);
	ObjSprite2D_SetSourceRect(obj,32,226,64,254);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,size,size,0);
	ObjRender_SetAngleXYZ(obj,0,0,c[2]);
	ObjRender_SetBlendType(obj,BLEND_ADD_RGB);
	while(r>0){
	if(r>0){ObjRender_SetPosition(obj,ObjMove_GetX(objEnemy)+r*cos(dir),ObjMove_GetX(objEnemy)+r*sin(dir),0);}
	ObjRender_SetAngleXYZ(obj,c[0],c[1],c[2]);
	ObjRender_SetScaleXYZ(obj,size,size,0);
	ObjRender_SetColor(obj,200-r,200-r,200-r);
	size -= 3/s*7;
	r -= (12-a/5)-m/10;
	m++;
	ascent(i in 0..3){c[i] = c[i] + angle[i];}
	yield;
	}
	Obj_Delete(obj);
}


task TExplosionA(x, y, dAlpha, dScale)
{
	let path = GetCurrentScriptDirectory() ~ "img/Default_Effect.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, path);
	Obj_SetRenderPriority(obj, 0.65);
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	ObjSprite2D_SetSourceRect(obj, 1, 1, 63, 63);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj, x, y, 0);

	let scale = 0;
	let alpha = 255;
	while(alpha > 0)
	{
		ObjRender_SetColor(obj, alpha, alpha, alpha);
		ObjRender_SetScaleXYZ(obj, scale, scale, 1);

		scale += dScale;
		alpha -= dAlpha;
		yield;
	}
	Obj_Delete(obj);
}


