
script_enemy_main {
    let GCSD=GetCurrentScriptDirectory;
    #include_function ".\..\ShotSheet\shot_replace.dnh"
    #include_function ".\..\functions.txt"
    #include_function ".\iddyea_functions.txt"

    SpellID=503;

    @Initialize {
	Randomize;
        SetLife(1600);
        SetTimer2(60);
	SetDamageRate(80,40);
	MagicCircle(false);
	SetEnemyMarker(true);
	IddyeaInit;
	MainTask;
	shotinit;

	StartFunctions(1,0,0);
	SetBG(20);

	SpellCircle2(6,1,4,200);
	SetInvincibility(500);
    }


function Wait(frames){loop(frames){yield;}}

let phasetimer=1;
let phasechanging=false;
let globalframe=0;

task MainTask{

	DelayBombTask;
	CutIn2(3,"Nostalgia [Fleeting Memory]");

	SetMovePositionNew(GetCenterX,100,60);
	Wait(80);
	PhaseTask;
	let k=0;
	let j=0;
	let frame=0;
	let angle=rand(0,360);

	loop(4){	//total x offset
		k=0;
		loop(3){	//position on circle
			j=0;
			loop(5){	//y offset of pattern
				MainShot(GetClipMinX-20-frame*(400/4),(400/4)*(j+0.5),  1.3,0, 0.7,k*(360/3)+angle,70, -1, RED02);
				MainShot(GetClipMinX-20-frame*(400/4),(400/4)*j,        1.3,0, -0.7,k*(360/3)+angle,55, -1, RED01);
				MainShot(GetClipMinX-20-frame*(400/4),(400/4)*(j+0.5),  1.3,0, 0.5,k*(360/3)+angle,60, -1, RED02);
				MainShot(GetClipMinX-20-frame*(400/4),(400/4)*j,        1.3,0, -0.5,k*(360/3)+angle,45, -1, RED01);
				j++;
			}
			k++;
		}
		frame++;
	}

	Wait(130);
	k=0;
	j=0;
	frame=0;

	let yoffset=[0,1,2,1,-1,-1,0];
	let xoffset=[0,1,2,3,1,3,2];
	loop(4){	//total x offset of pattern
		k=0;
		loop(6){	//number of shot in pattern
			j=0;
			loop(5){	//y offset of pattern
				MainShot(GetClipMaxX-90+frame*(400/4) + xoffset[k]*((400/4)/4),yoffset[k]*((500/5)/4) + (500/5)*j,  -1,-1, 0,0,0, 1, RED22);
				MainShot(GetClipMaxX-90+frame*(400/4) + xoffset[k]*((400/4)/4),yoffset[k]*((500/5)/4) + (500/5)*j,  -2,0.5, 0,0,0, 1, RED52);
				j++;
			}
			k++;
		}
		frame++;
	}

}

task MainShot(x,y,xv,yv,angvel,rotation,radius,phase, g){
	let collision=true;

	let obj=Obj_Create(OBJ_SHOT);
	Obj_SetAlpha(obj,0);
	Obj_SetAutoDelete(obj,false);
	ObjShot_SetBombResist(obj,true);
	Obj_SetAngle(obj,90);
	let frame=0;
	let color=-1;
	ObjShot_SetGraphic(obj,g);
	Obj_SetCollisionToPlayer(obj,false);
	let x2=x+cos(rotation)*radius;
	let y2=y+sin(rotation)*radius;
	let totalxadd=0;
	let onscreened=false;
	Obj_SetPosition(obj,x2,y2);
	while(!Obj_BeDeleted(obj)){
		if(phasetimer*phase>=0){
			x+=xv*phasetimer*phase;
			y+=yv*phasetimer*phase;

			if(y<-10&&yv<0){
				y+=500;
			}
			if(y>-10+500&&yv>0){
				y-=500;
			}

			if(radius==0){

				if(x<GetClipMinX-8&&xv<0){
					x+=400;
				}
				if(x>GetClipMinX-8+400&&xv>0){
					x-=400;
				}

				x2=x;
				y2=y;
				Obj_SetPosition(obj,x,y);
			}else{
				x2=x+cos(rotation)*radius;
				y2=y+sin(rotation)*radius;

				if(x2+totalxadd<GetClipMinX-8&&onscreened){
					totalxadd+=400;
				}
				if(x2+totalxadd>GetClipMinX-8+400){
					totalxadd-=400;
					onscreened=true;	//don't warp left to right unless it's already gone past the screen once
				}

				Obj_SetPosition(obj,x2+totalxadd,y2);
				rotation+=angvel*phasetimer*phase;
			}
		}
		if(phasetimer*phase>=0.7){
			if(!collision){
				if(distance(x2+totalxadd,y2,GetPlayerX,GetPlayerY)>30*30){
					Obj_SetCollisionToPlayer(obj,true);
					collision=true;
				}
			}
		}else if(collision){
			Obj_SetCollisionToPlayer(obj,false);
			collision=false;
		}
		x2=trunc(((y2*phase)/60+globalframe/6))%7;//new color
		if(x2!=color&&phasetimer*phase!=-1){
			color=x2;
			ObjShot_SetGraphic(obj,g+color);
		}
		if(phasechanging){
			Obj_SetAlpha(obj,127.5+127.5*phasetimer*phase);
		}
		frame++;
		yield;
	}
}
task PhaseTask{
	loop{
		SE("charge");
		phasechanging=true;
		loop(100){
			phasetimer-=2/100;
			yield;
		}
		phasechanging=false;
		phasetimer=-1;
		Wait(60);
		SE("charge");
		phasechanging=true;
		loop(100){
			phasetimer+=2/100;
			yield;
		}
		phasechanging=false;
		phasetimer=1;
		Wait(60);
	}

}


@MainLoop {
        SetCollisionA(GetX, GetY, 40);
	SetCollisionB(GetX, GetY, 20);

	globalframe++;
	IddyeaDrawLogic;

	yield;

}

@DrawLoop {
	IddyeaDrawLoop;
}

@Finalize {
	EndingFunc(6);
	IddyeaFinalize;
	PointItems(GetX,GetY,7,1);
}

}