#include "./lib_common.txt"

///
let DELETE_ITEM = 1; 

let POWER_ITEM = 2; 
let POINT_ITEM = 3; 

let LIFE_UP_ITEM = 4; 
let BOMB_UP_ITEM = 5; 
///

///
let power_max = false;
///

@Initialize{
	SetAutoDeleteObject(true);
	SetItemRenderPriorityI(40);
	SetDefaultBonusItemEnable(false);
	LoadItemData(GetCurrentScriptDirectory ~ "item/lib_item.txt");
}

@MainLoop{
	CollectItemsInCircle(GetPlayerX, GetPlayerY, 48);
	
	if(GetPlayerPower > 400){SetPlayerPower(400);}
	
	yield;
}

@Event{
	alternative(GetEventType)
	case(EV_DELETE_SHOT_TO_ITEM){
		AnimBulletDestroy(GetEventArgument(0),GetEventArgument(1));
		BulletDeleteItem(GetEventArgument(1));
	}
	case(EV_GRAZE){
		AddScore(1000);
	}
	case(EV_GET_ITEM){
		let obj = GetEventArgument(1);

		alternative(GetEventArgument(0))
		
		case(1){
			_PlaySE(10);
			AddScore((GetGraze+GetPoint)/10000*100);
		}

		case(2){
			_PlaySE(10);
			AddScore((GetGraze+GetPoint)/10000*1000);
			
			if(GetPlayerPower < 400){
				SetPlayerPower(GetPlayerPower+1);
			}
			
			if(GetPlayerPower == 100 || GetPlayerPower == 200 || GetPlayerPower == 300){}
			else if(GetPlayerPower == 400 && !power_max){
				power_max = true;
			}
			else{power_max = false;}
		}
		
		case(3){
			_PlaySE(10);
			AddScore((GetGraze+GetPoint)/10000*1000);
			AddPoint(1);
		}
		
		case(4){
			_PlaySE(10);
			AddScore((GetGraze+GetPoint)/10000*500000);
			SetPlayerLife(GetPlayerLife+1);
			NotifyEventAll(EV_USER_SYSTEM + 5,NULL);
		}
		
		case(5){
			_PlaySE(10);
			AddScore((GetGraze+GetPoint)/10000*500000);
			SetPlayerSpell(GetPlayerSpell+1);
			NotifyEventAll(EV_USER_SYSTEM + 6,NULL);
		}
	}
}

task BulletDeleteItem(xy){
	let obj	= CreateItemU2(DELETE_ITEM, xy[0], xy[1], xy[0], xy[1], 10);
	ObjItem_SetRenderScoreEnable(obj, false);
		
	loop(20){yield;}
	
	ObjItem_SetDefinedMovePatternA1(obj, ITEM_MOVE_TOPLAYER);
}

task AnimBulletDestroy(bullet,xy){
	let c = GetShotDataInfoA1(ObjShot_GetImageID(bullet), TARGET_ENEMY, INFO_DELAY_COLOR);
	
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, GetMainStgScriptDirectory ~ "img/effect/deletebullet.png");
	Obj_SetRenderPriority(obj, 0.65);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	ObjRender_SetPosition(obj, xy[0], xy[1], 0);
	ObjRender_SetColor(obj,c[0],c[1],c[2]);
		
	ascent(a in 0..8){
		ObjSprite2D_SetSourceRect(obj, 32*a, 0, 32*(a+1), 32);
		ObjSprite2D_SetDestCenter(obj);
			
		wait(5);
	}
		
	Obj_Delete(obj);
}

task AnimItemAppear(xy,c){
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, GetMainStgScriptDirectory ~ "img/effect/part_spawn.png");
	Obj_SetRenderPriority(obj, 0.49);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	ObjRender_SetPosition(obj, xy[0], xy[1], 0);
	ObjRender_SetColor(obj,c[0],c[1],c[2]);
	ObjSprite2D_SetSourceRect(obj, 32, 32, 64, 64);
	ObjSprite2D_SetDestCenter(obj);
		
	ascent(a in 0..16){
		ObjRender_SetScaleXYZ(obj,1+0.1*a/16,1+0.1*a/16,0);
		ObjRender_SetAngleZ(obj,-a*8);
		ObjRender_SetAlpha(obj,255/16*(16-a));
			
		yield;;
	}
		
	Obj_Delete(obj);
}