/**
 * アイテム定数
 */
let UI_POINT   = 1;	//ポイント
let UI_BOMB    = 2;	//ボム
let UI_LIFE    = 3;	//1UP
let UI_GAUGE_S = 4;	//ゲージ(小)
let UI_GAUGE_L = 5;	//ゲージ(大)

//アイテム自動取得境界線
let ItemBorder = 160;

//アイテム回収範囲
let ItemRadius = 24;

//アイテム回転関係
let MaxRoll = 360 * 2;
let RollSpeed = 24;

//点アイテム取得時最高点
let ItemScoreMax = 1000000;
let ItemScoreMin =  100000;

/**
 * 指定した位置にアイテムを作成する
 * 
 * @param {Integer} x    x座標
 * @param {Integer} x    y座標
 * @param {Integer} type 種類
 */
function CreateUserItem( x, y, type ){
	let itemlist = GetCommonDataDefaultEx( "OFT", "Item", [] );
	itemlist = itemlist ~ [ x, y, type ];
	SetCommonDataEx( "OFT", "Item", itemlist );
}

/**
 * 共通空間を監視し、アイテムを出現させる
 */
task ItemTask(){
	let itemlist = [];
	let leng = 0;
	
	loop{
		//ボムエクステンドシグナル監視
		if ( GetCommonDataDefaultEx( "OFT", "BombExtendSignal", false ) ) {
			CreateUserItem( GetPlayerX(), GetPlayerY(), UI_BOMB );
			SetCommonDataEx( "OFT", "BombExtendSignal", false );
		}
		
		itemlist = GetCommonDataDefaultEx( "OFT", "Item", [] );
		leng = truncate( length( itemlist ) / 3 );
		
		//配列長/3の数だけアイテム作成
		ascent( i in 0..leng ){
			UserItem( itemlist[ i * 3 + 0 ], itemlist[ i * 3 + 1 ], itemlist[ i * 3 + 2 ] );
		}
		
		//出し終わったので空に
		SetCommonDataEx( "OFT", "Item", [] );
		
		yield;
	}
}

/**
 * ユーザーアイテム
 * 
 * @param {Integer} x    x座標
 * @param {Integer} x    y座標
 * @param {Integer} type 種類
 */
task UserItem( x, y, type ){
	let collect = false;
	let px = 0;
	let py = 0;
	let speed = -2.8;
	let roll = 0;
	let rect = 20;
	let rectx = 0;
	let recty = 0;
	
	alternative( type )
		case( UI_POINT ){
			rect = 12;
			rectx = 16;
			recty = 176;
		}
		case( UI_BOMB ){
			rect = 16;
			rectx = 16;
			recty = 192;
		}
		case( UI_LIFE ){
			rect = 16;
			rectx = 32;
			recty = 192;
		}
		case( UI_GAUGE_S ){
			rect = 12;
			rectx = 0;
			recty = 176;
		}
		case( UI_GAUGE_L ){
			rect = 16;
			rectx = 0;
			recty = 192;
		}
	
	let obj = CreateObjEffect( imgItem, x, y, rectx, recty, rect, rect, 3, 255, 255, 255, 255 );
	
	while( !Obj_BeDeleted( obj ) ){
		px = GetPlayerX();
		py = GetPlayerY();
		
		if( collect ){
			//自機を狙って移動
			let angle = GetGapAngle( x, y, px, py );
			x = GetGapX( x, speed, angle );
			y = GetGapY( y, speed, angle );
		}else{
			//自動回収開始
			if( GetPlayerY() < ItemBorder || GetCommonDataDefaultEx( "OFT", "CollectUserItem", false ) ){
				collect = true;
				speed = 10;
			}
			
			//加速
			if( speed < 2 ){
				speed += 0.06;
			}
			
			//回転
			if( roll < MaxRoll ){
				roll += RollSpeed;
				ObjEffect_SetAngle( obj, 0, 0, roll );
			}
			
			y += speed;
		}
		
		//位置設定
		Obj_SetPosition( obj, x, y );
		
		//アイテム回収
		if( GetGapLength( px, py, x, y ) < ItemRadius ){
			GetItem( type, collect );
			Obj_Delete( obj );
		}
		
		//画面外消失
		if( y > MaxY + 32 ){
			Obj_Delete( obj );
		}
		
		yield;
	}
}

/**
 * アイテムゲット時の処理
 * 
 * @param {Integer} type    種類
 * @param {Integer} collect 自動回収フラグ
 */
task GetItem( type, collect ){
	alternative( type )
		case( UI_POINT ){
			//点アイテム
			let score = 0;
			if( collect || GetPlayerY() <= ItemBorder ){
				score = ItemScoreMax;
			}else{
				score = ( MaxY -  GetPlayerY() ) / ( MaxY - ItemBorder ) * ( ItemScoreMax - ItemScoreMin ) + ItemScoreMin;
			}
			
			AddScore( score );
			PlaySE( sePoint );
		}
		case( UI_BOMB ){
			//ボムアイテム
			AddBomb( 1 );
			PlaySE( seBomb );
		}
		case( UI_LIFE ){
			//1UPアイテム
			ExtendPlayer( 1 );
			PlaySE( se1up );
		}
		case( UI_GAUGE_S ){
			//ゲージ(小)アイテム
			AddShieldGauge( 25 );
			PlaySE( seGauge );
		}
		case( UI_GAUGE_L ){
			//ゲージ(大)アイテム
			AddShieldGauge( 200 );
			PlaySE( seGauge );
		}
}