#東方弾幕風
#Title[道標「北を示す星と針」]
#Text[試作]
#ScriptVersion[2]

script_enemy_main
{//Enemy01
	let count=0;
	let cx=GetCenterX();//STGシーンの中心x座標を取得
	let cy=GetCenterY();//STGシーンの中心y座標を取得
	let imgExRumia=GetCurrentScriptDirectory~"img\ExRumia.png";
	let compus=GetCurrentScriptDirectory~"img\背景.png";
	let forest=GetCurrentScriptDirectory~"img\背景2.png";
	LoadUserShotData(GetCurrentScriptDirectory~"shot.txt");
	@Initialize
	{
		SetScore(30000);
		SetLife(500);
		SetDamageRate(10,10);
		SetMovePosition02(cx,cy-120,60);
		SetTimer(60);
		SetGraphicRect(1,1,64,64);
		SetInvincibility(60);
		LoadGraphic(imgExRumia);
		SetTexture(imgExRumia);	
		LoadGraphic(compus);
		LoadGraphic(forest);
		shottask;
		shottask2;
		movetask;
		CutIn(YOUMU,"道標「北を示す星と針」",0,0,0,0,0);
	}
	@MainLoop
	{
		yield;
		SetCollisionA(GetX(),GetY(),24);//当たり判定(自弾)登録
		SetCollisionB(GetX(),GetY(),24);//当たり判定(体当たり)登録
	}
	@Finalize
	{
		loop(8)
		{
			CreateItem(ITEM_SCORE,cx+rand(-100,100),rand(20,100));
		}
	}
	@DrawLoop
	{
		SetTexture(imgExRumia);	
		SetGraphicRect(1,1,64,64);
		SetGraphicScale(1,1);
		SetGraphicAngle(0,0,0);
		SetAlpha(255);
		DrawGraphic(GetX,GetY);
	}
	@BackGround
	{
		SetTexture(forest);	
		SetGraphicRect(1,1,450,294);
		SetAlpha(255);
		SetGraphicScale(1.55,1.55);
		SetGraphicAngle(0,0,0);
		DrawGraphic(cx,cy);
		SetTexture(compus);	
		SetGraphicRect(1,1,750,720);
		SetAlpha(255);
		SetGraphicScale(0.5,0.5);
		SetGraphicAngle(0,0,count);
		DrawGraphic(cx,cy);
		count++;
	}
	task shottask
	{
		let s=0;
		let r=0;
		let r2=0;
		wait(120);
		loop{
			r=rand(GetClipMinX,GetClipMaxX);
			r2=rand_int(0,1);
			if(r2==0)
			{
				r2=-1
			}
			wait(90);
			CreateObjLaser(r,0,0,64,7,WHITE01,0,r2);
			CreateObjLaser(r,0,90,64,7,WHITE01,0,r2);
			CreateObjLaser(r,0,180,64,7,RED01,0,r2);
			CreateObjLaser(r,0,270,64,7,WHITE01,0,r2);
		}
	}
	task shottask2
	{
		wait(120);
		loop
		{
			CreateObjShot(0,0,131,0);
			wait(4);
		}
	}
	task movetask
	{
		yield;
		loop
		{
			wait(180);
			move(rand(40, 80),rand(-40, 40),60,GetClipMinX+32,GetClipMinY+64,GetClipMaxX-32,GetClipMinY+160);
		}
	}
	task CreateObjLaser(let x, let y, let angle, let length,let width, let graphic, let delay, let r)
	{
		let obj = Obj_Create(OBJ_LASER);
		Obj_SetPosition(obj, x, y);
		Obj_SetSpeed(obj, 0);
		Obj_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetDelay(obj, delay);
		ObjLaser_SetLength(obj, length);
		ObjLaser_SetWidth(obj, width);
		ObjLaser_SetSource(obj, false);
		ObjShot_SetBombResist(obj, false);
	        loop(delay){ yield; }
		while(!Obj_BeDeleted(obj))
		{
			Obj_SetY(obj,Obj_GetY(obj)+1);
			Obj_SetAngle(obj,atan2(Obj_GetY(obj)-GetY,Obj_GetX(obj)-GetX)+angle);
			yield;
		}
	}
	task CreateObjShot(let speed,let angle, let graphic, let delay)
	{
		let count=0;
		let rimit=rand(0,400);
		let angle=rand(0,360);
		let alpha=0;
		let obj = Obj_Create(OBJ_SHOT);
		Obj_SetPosition(obj,GetX+100*cos(angle), GetY+100*sin(angle));
		Obj_SetSpeed(obj, 0);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetDelay(obj, delay);
		ObjShot_SetBombResist(obj, true);
		Obj_SetAutoDelete(obj,false);
		while(!Obj_BeDeleted(obj))
		{
			Obj_SetPosition(obj,GetX+rimit*cos(angle), GetY+rimit*sin(angle));
			angle+=1;
			count+=1;
			if(count<=60)
			{
				alpha+=5;
				Obj_SetAlpha(obj,alpha);
				Obj_SetCollisionToPlayer(obj,false);
			}
			if(count>60&&count<540)
			{
				Obj_SetCollisionToPlayer(obj,true);
			}
			if(count>=540)
			{
				alpha-=5;
				Obj_SetAlpha(obj,alpha);
				Obj_SetCollisionToPlayer(obj,false);
			}
			if(count==600)
			{
				Obj_Delete(obj);
			}
			yield;
		}
	}
	function move(xMove, yAdd, frame, left, top, right, bottom)
	{
    		let x;
    		let y;
		if(GetPlayerX < GetX) 
		{
			x = GetX - xMove;
			if(x < left) 
			{
				x = GetX + xMove;
			}
		} 
		else {
			x = GetX + xMove;
			if(right < x)
			{
				x = GetX - xMove;
			}
		}
		y = GetY + yAdd;
		if(y < top)
		{
			y = top;
		}
		else if(bottom < y)
		{
			y = bottom;
		}
  			SetMovePosition02(x, y, frame);
	}
	function wait(w) 
	{
		loop(w)
		{
			yield;
		}
	}
}