#東方弾幕風
#Title[リグル通常3-Easy]
#Text[]
#Image[.]
#BackGround[User]
#Player[FREE]
#ScriptVersion[2]
script_enemy_main {
    let name    = "リグル通常3-Easy";
let csd = GetCurrentScriptDirectory;
    let imgBoss =csd ~ "..\lib/dot_wriggle.png"; 
    let imgFam  = csd ~ "img\familiar.png";
SetRateScoreSystemEnable(false);
    // 初期位置
    let xIni    = GetCenterX;
    let yIni    = GetClipMinY + 128;
	let wIni=60;
    @Initialize {
SetShotAutoDeleteClip(16,16,16,16);
       LoadUserShotData(GetCurrentScriptDirectory~".\..\img\shot_All.txt");
        SetTimer(40);
        SetLife(3000);
        SetDamageRate(1, 1);
        LoadGraphic(imgBoss);
	InitializeAction();
	SetEnemyMarker(true);
        TMain;
    }

    @MainLoop {
	GetBossShadowPosition;
        SetCollisionA(GetX, GetY, 48);
        SetCollisionB(GetX, GetY, 8);
        yield;
    }

    @DrawLoop {
	DrawBossShadow( imgBoss,150,255,255,0);
	DrawBoss( imgBoss );
    }

    @Finalize {
        DeleteGraphic(imgBoss);
    }

    // メインタスク
task TMain 
{
	standBy;
	wait(180);
	Trate;
	TMove;
	shot;
	subshot;
}


task Trate
{
	wait(120);
        SetDamageRate(100, 100);
}

task TMove
{
	loop
	{
	wait(120);
	moveToPlayer(rand(40, 60), rand(-30, 30), 30,
                     GetClipMinX + 48, GetClipMinY +  32,
                     GetClipMaxX - 48, GetClipMinY + 128);
	wait(30);
	}
}

sub standBy//初期位置へ
{
	SetAction(ACT_MOVE, 60);
	SetMovePosition02(xIni, yIni, wIni);
	SetInvincibility(wIni);
	wait(wIni);
}

task shot
{
wait(60);
loop
{
	playershot;
	wait(150);
}
}

task playershot
{
let angle=GetAngleToPlayer;
	loop(8)
	{
	ascent(let i in 0..30)
	{
//	CreateShot01(GetX,GetY,3,angle+i*12,YELLOW02,20);
//		CreateShotFromScript( "shot1",GetX, GetY, 3, angle+i*12, 20, YELLOW02);
	CreateShot01ToItem(GetX,GetY,3,angle+i*12,YELLOW02,20);
	}
	wait(8);
	}
}

task subshot
{
loop
{
	Wave;
	wait(150);
}
}

task Wave
{
loop(16)
{
	ascent(let i in 0..18)
	{
	CreateSinewaveShot(GetX,GetY,2,i*20,20,3,0,YELLOW01,10);
	}
wait(5);
}
}

//正弦波弾の例
//amp:振幅, om:角速度, arg:位相
task CreateSinewaveShot(let x, let y, let speed, let angle,
            let amp, let om, let arg, let graphic, let delay) {
        let itemx=x;
	let itemy=y;
    let t = 0;
    let px = speed*t;
    let py = amp*sin(om*t + arg);
    let a = amp*om*3.1415926/(180*speed);
    
    //オブジェクト弾の作成
    let obj = Obj_Create(OBJ_SHOT);
    
    //パラメータの設定
    Obj_SetPosition(obj, x + px*cos(angle) - py*sin(angle),
                             y + px*sin(angle) + py*cos(angle));
    Obj_SetSpeed(obj, 0);
    Obj_SetAngle(obj, angle + atan(a*cos(om*t + arg)));
    ObjShot_SetGraphic(obj, graphic);
    ObjShot_SetDelay(obj, delay);
    
	loop(delay)
	{
		itemx=Obj_GetX(obj);
		itemy=Obj_GetY(obj);
		if(Obj_BeDeleted(obj))
		{
			CreateShotChengeToItem(itemx,itemy);
			return;
		}
		yield;
	}
	
    while(!Obj_BeDeleted(obj))
    {
		itemx=Obj_GetX(obj);
		itemy=Obj_GetY(obj);
        //(px,py)座標での弾の位置を計算
        px=speed*t;
        py=amp*sin(om*t + arg);
        //回転・平行移動して実際の位置に
        Obj_SetPosition(obj, x + px*cos(angle) - py*sin(angle),
                                 y + px*sin(angle) + py*cos(angle));
        //弾の方向にもangleを足す
        Obj_SetAngle(obj, angle + atan(a*cos(om*t + arg)));
        
        t++;
        yield;
    }
	CreateShotChengeToItem(itemx,itemy);
}   

function moveToPlayer(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);
	SetAction(ACT_MOVE,60);
}


#include_function ".\..\txt\data.txt"
#include_function ".\..\lib/lib_anime_Patchouli.txt"
}
#include_script ".\..\txt/EnemyShotData.txt"