#include "./enm/animation.txt"
#include "./AllStarShot_Const.dnh"

function CreateEnemy(x, y, type){
let enemy=ObjEnemy_Create(OBJ_ENEMY);
ObjMove_SetPosition(enemy, x, y);
ObjEnemy_Regist(enemy);
DrawEnemy(enemy, type);
return enemy;
}

task PlaySoundEffect(sound){
let se=ObjSound_Create;
ObjSound_Load(se, GetCurrentScriptDirectory~"system/se/"~sound~".wav");
ObjSound_SetVolumeRate(se, 40);
ObjSound_Play(se);
}



task CollisionDetect(id, radius){
while(!Obj_IsDeleted(id)){ObjEnemy_SetIntersectionCircleToShot(id, ObjMove_GetX(id), ObjMove_GetY(id), radius); 
ObjEnemy_SetIntersectionCircleToPlayer(id, ObjMove_GetX(id), ObjMove_GetY(id), radius/2);
yield;}
}

task TEndEnemy(id, clear){
	while(ObjEnemy_GetInfo(id, INFO_LIFE)>0){yield;}
	if (!Obj_IsDeleted(id)){if (clear==true){DeleteShotAll(TYPE_SHOT, TYPE_ITEM);}
	NotifyEventAll(EV_USER+2, [ObjMove_GetX(id), ObjMove_GetY(id), 55, 155, 255]);
	PlaySoundEffect("se_enep00");}
	Obj_Delete(id);
	}
	


task Enemy01(x, y){
let c=0;
if (GetCommonData("rank", "Normal")=="Normal"){c=2;} else{c=3;}
let obj=CreateEnemy(x, y, 0);
CollisionDetect(obj, 20);
let r=30; let ang=0; let ang2=0; let a=0; let t=1; let k=0;
let graphic=[L_LASER_RED, L_LASER_ORANGE, L_LASER_YELLOW, L_LASER_GREEN, L_LASER_AQUA, L_LASER_BLUE, L_LASER_PURPLE];
ObjMove_SetDestAtWeight(obj, x, y+120, 10, 3);
ObjEnemy_SetLife(obj, 2000);
ObjEnemy_SetDamageRate(obj, 100, 100);
TEndEnemy(obj, true);
loop(120){yield;}
loop(2){
ang=rand(0,360); ang2=rand(0,360);
loop(90){
if (!Obj_IsDeleted(obj)){
PlaySoundEffect("se_tan00");
a+=180/90;
r=100*sin(a);
ang+=t*40; ang2+=t*6; k++; if (k>6){k=0;}
ascent(i in 0..c){CreateShotA2(ObjMove_GetX(obj)+r*cos(ang+i*360/c)-r*cos(ang2+i*360/c)/2, ObjMove_GetY(obj)+r*sin(ang+i*360/c)-r*sin(ang2+i*360/c)/2, 1, ang2+i*360/c, 0.05, 2, graphic[k], 30);}
}
loop(4){yield;}}
t*=-1;
loop(60){yield;}}
ObjMove_SetDestAtWeight(obj, x, y-150, 10, 3);
loop(120){yield;}
Obj_Delete(obj);
}


task Enemy05(x, y, t){
let c=0;
if (GetCommonData("rank", "Normal")=="Normal"){c=2;} else{c=3;}
let obj=CreateEnemy(x, y, 4);
CollisionDetect(obj, 20);
let r=30; let ang=0; let ang2=0; let a=0; let k=0;
let graphic=SMALL_BLUE;
ObjMove_SetDestAtWeight(obj, x, y+120, 10, 3);
ObjEnemy_SetLife(obj, 1200);
ObjEnemy_SetDamageRate(obj, 100, 100);
TEndEnemy(obj, false);
loop(120){yield;}
loop(1){
loop(90){
if (!Obj_IsDeleted(obj)){
PlaySoundEffect("se_tan00");
a+=180/90;
r=100*sin(a);
ang+=t*40; ang2+=t*6; k++; if (k>6){k=0;}
ascent(i in 0..c){CreateShotA2(ObjMove_GetX(obj)+r*cos(ang+i*360/c)-r*cos(ang2+i*360/c)/2, ObjMove_GetY(obj)+r*sin(ang+i*360/c)-r*sin(ang2+i*360/c)/2, 1, ang2+i*360/c, 0.05, 2, graphic, 30);}
}
loop(4){yield;}}
t*=-1;
loop(60){yield;}}
ObjMove_SetDestAtWeight(obj, x, y-150, 10, 3);
loop(120){yield;}
Obj_Delete(obj);
}


task Enemy04(x, y){
let c=0; let c2=3;
if (GetCommonData("rank", "Normal")=="Normal"){c=5; c2=2;} else{c=6; c2=3;}
let obj=CreateEnemy(x, y, 0);
CollisionDetect(obj, 20);
let r=30; let ang=0; let ang2=0; let a=0; let t=1;
ObjMove_SetDestAtWeight(obj, x, y+120, 10, 3);
ObjEnemy_SetLife(obj, 2000);
ObjEnemy_SetDamageRate(obj, 100, 100);
TEndEnemy(obj, true);
loop(120){yield;}

if (!Obj_IsDeleted(obj)){
ang=rand(0,360);
		ascent(i in 0..c){
		Shot(obj, ang+i*360/c, 2, 3, 0);
		Shot(obj, ang+i*360/c, 2, 3, 0);
		}
}

loop(120){yield;}
ObjMove_SetDestAtWeight(obj, x, y-150, 10, 3);
loop(120){yield;}

	task Shot(id, angle, t, speed, speed2){
	PlaySoundEffect("se_kira00");
	let graphic=[AMULET_BLUE, AMULET_GREEN, AMULET_RED];
	let shot=CreateShotA2(ObjMove_GetX(id), ObjMove_GetY(id), speed, angle, -speed/60, 0, graphic[t], 0);
	if (t==0){ObjMove_AddPatternA1(shot, 0, speed2, angle);}
	loop(60){yield;}
	if (t>0){
	ascent(i in -c2..c2+1){Shot(shot, angle+180+i*90/c2, t-1, 2+1*(absolute(i)%2), 1.5+0.5*(absolute(i)%2));} Obj_Delete(shot);}
	}
	
Obj_Delete(obj);
}


task Enemy02(x, y, x2, y2, angv){
let c=0; let ang=0; let c2=0;
if (GetCommonData("rank", "Normal")=="Normal"){c=1; c2=3;} else{c=3; c2=4;}
let obj=CreateEnemy(x, y, 2);
CollisionDetect(obj, 20);
ObjEnemy_SetLife(obj, 200);
ObjMove_SetDestAtWeight(obj, x2, y2, 10, 4);
TEndEnemy(obj, false);
loop(120){yield;}
if (!Obj_IsDeleted(obj)){
PlaySoundEffect("se_tan01");
ang=GetAngleToPlayer(obj);
ascent(i in -c..c+1){
	ascent(v in 0..c2){CreateShotA2(ObjMove_GetX(obj), ObjMove_GetY(obj), 3+v/2, ang+i*45/c, -2/30, 1+v/2, KUNAI_YELLOW, 10);}
}}
loop(120){yield;}
let angle=90;
let v=0;
loop(60){angle+=angv; v+=0.05; ObjMove_SetSpeed(obj, v); ObjMove_SetAngle(obj, angle); yield;}
loop(60){ObjMove_SetSpeed(obj, v); ObjMove_SetAngle(obj, angle); yield;}
Obj_Delete(obj);
}


task Enemy03(x, y){
let ang=0; let c=0; let alpha=0; let c2=0; let angle=90; let angv=((round(rand(0,1))*2-1)); let v=0;
if (GetCommonData("rank", "Normal")=="Normal"){c=1; c2=3;} else{c=3; c2=4;}
let obj=CreateEnemy(x, y, 0);
loop(51){alpha+=5; ObjRender_SetAlpha(obj, alpha); yield;}
CollisionDetect(obj, 20);
ObjEnemy_SetLife(obj, 200);
loop(10){yield;}

PlaySoundEffect("se_tan01");
ang=GetAngleToPlayer(obj);
ascent(i in -c..c+1){
	ascent(v in 0..c2){CreateShotA2(ObjMove_GetX(obj), ObjMove_GetY(obj), 2+v/3, ang+i*4, 0, 2+v/3, SMALL_BLUE, 10);}
}

loop(180){yield;}
loop(120){angle+=angv; if (v<3){v+=0.05;} ObjMove_SetSpeed(obj, v); ObjMove_SetAngle(obj, angle); yield;}
TEndEnemy(obj, false);
loop(60){yield;}
}