//Shoots a square

script_enemy_main{

let GRenemy=("\script\Images\Enemies\Fairy1-05.png");

let SEshots7=("script\SoundEffects\shots7.wav");
let SEdeath=("script\SoundEffects\enemydeath1.wav");

let miny=GetClipMinY; let maxy=GetClipMaxY; let minx=GetClipMinX; let maxx=GetClipMaxX;
let cx=GetCenterX; let cy=GetCenterY;
let startx=GetX; let starty=GetY;
let frame=0; let time=0; let animation=0;
let ani=4;
let scale=0;
let shots=0;
let shotcolor=0; let shotspeed=0;
let oldX=0; let oldY=0;
let invincible=125;

SetAngle(270);
SetSpeed(0);

@Initialize{
	LoadGraphic("\script\Images\Enemies\Fairy1-05.png");

	LoadSE("script\SoundEffects\shots7.wav");

	SetInvincibility(40);
	SetLife(15);
	SetDamageRate(10,3); 
	SetScore(1000);
	SetShotColor(255,255,255);
}

@MainLoop{

if(GetX<=minx-32 || GetX>=maxx+32 || GetY<=miny-32 || GetY>=maxy+32 && frame>=60){ VanishEnemy; }

SetCollisionA(GetX,GetY,32*scale);
SetCollisionB(GetX,GetY,32*scale);


if(time==50){
	ShootShape(GetX,GetY,GetArgument[1],GetArgument[2],4,GetArgument[0],60+GetArgument[3],0);
	PlaySE(SEshots7);
}

function ShootShape(spawnX,spawnY,speed,angle,corners,bullets,graphic,delay){
	ascent(i in 0..corners){
		let sx=spawnX+cos(angle+i*360/corners);
		let sy=spawnY+sin(angle+i*360/corners);
		let sxN=spawnX+cos(angle+(i+1)*360/corners);
		let syN=spawnY+sin(angle+(i+1)*360/corners);
		CreateShot01(spawnX,spawnY,speed*(((sx-spawnX)^2+(sy-spawnY)^2)^0.5),atan2(sy-spawnY,sx-spawnX),graphic,delay);
			ascent(j in 0..bullets){
			let toAngle=atan2(syN-sy,sxN-sx);
			let toDist=(((sxN-sx)^2+(syN-sy)^2)^0.5);
			let sx2=sx+toDist/bullets*j*cos(toAngle);
			let sy2=sy+toDist/bullets*j*sin(toAngle);
			CreateShot01(spawnX,spawnY,speed*(((sx2-spawnX)^2+(sy2-spawnY)^2)^0.5),atan2(sy2-spawnY,sx2-spawnX),graphic,delay);
		}
	}
}

if(scale<0.75){ scale+=0.02; }
if(time>50){ SetSpeed(GetSpeed+0.02); }

oldX=GetX;
oldY=GetY;
frame++;
time++;
animation++;
if(animation>=ani*6){ animation=0; }
if(GetTimeOfInvincibility<25 && invincible>0){ invincible=GetTimeOfInvincibility*5; }

}

@DrawLoop{
	let side=0;
	SetTexture(GRenemy);
		if(oldX<GetX || oldX>GetX){ side=128; }
		if(animation>=ani*0 && animation<ani*1){ SetGraphicRect(1,side,256,128+side); }
		if(animation>=ani*1 && animation<ani*2){ SetGraphicRect(257,side,512,128+side); }
		if(animation>=ani*2 && animation<ani*3){ SetGraphicRect(513,side,768,128+side); }
		if(animation>=ani*3 && animation<ani*4){ SetGraphicRect(769,side,1024,128+side); }
		if(animation>=ani*4 && animation<ani*5){ SetGraphicRect(513,side,768,128+side); }
		if(animation>=ani*5 && animation<ani*6){ SetGraphicRect(257,side,512,128+side); }
	SetGraphicAngle(0,0,0);
	if(oldX<GetX){ SetGraphicAngle(180,0,0); }
	SetGraphicScale(scale,scale);
	SetColor(255-invincible,255-invincible,255);
	DrawGraphic(GetX,GetY);
}

@Finalize{
	if(BeVanished==false){
	PlaySE(SEdeath);
		loop(6){ CreateEnemyFromFile("script\Functions\itempoint.txt",GetX+rand(-40,40),GetY+rand(-40,40),0,0,0); }
		loop(2){ CreateEnemyFromFile("script\Functions\itempower.txt",GetX+rand(-40,40),GetY+rand(-40,40),0,0,0); }
	#include_function "script/Functions/PlayerTypeBonus.txt";
	}
}

}