//Item Creation Script

let CSD = GetCurrentScriptDirectory;
let MD = GetModuleDirectory;

let POWER = 1;
let POINT = 2;
let BALANCE = 3;
let POWER_FULL = 4;
let BONUS = 5;
let LIFE_PIECE = 6;
let LIFE = 7;
let SPELL_PIECE = 8;
let SPELL = 9;
let DELETE = 10;

#include"./../LIB/Function_Audio.dnh"
#include"./../LIB/SE_Const.dnh"
#include"./../LIB/Function_Misc.dnh"

@Initialize {
	SetDefaultBonusItemEnable(false);
	LoadItemData(CSD ~ "LIB/Item_Data.dnh");
	LoadTexture(PSD ~ "IMG/Effect/BulletBreak.png");
}

@MainLoop {
	SetCommonData("PointOfCollection",160+GetCommonData("Balance",0)/10000*130);
	if(GetPlayerState==STATE_DOWN) { CancelCollectItems; }
	yield;
}

@Event {
	alternative(GetEventType)
	case(EV_GET_ITEM) {
		let itemType = GetEventArgument(0);
		let objItem = GetEventArgument(1);
		AddScore(Obj_GetValueD(objItem,"Score",0));
		
		alternative(itemType)
		case(SPELL){
			PlaySoundEffect(item,100);
			PlaySoundEffect(cardget2,100);
			SetPlayerSpell(GetPlayerSpell+1);
			NotifyEventAll(EV_GET_SPELL,0);
		}
		case(SPELL_PIECE){
			PlaySoundEffect(item,100);
			SetCommonData("SpellPieces",GetCommonData("SpellPieces",0)+1);
			if(GetCommonData("SpellPieces",0)>=8) {
				PlaySoundEffect(cardget2,100);
				SetPlayerSpell(GetPlayerSpell+1);
				NotifyEventAll(EV_GET_SPELL,0);
				SetCommonData("SpellPieces",0);
			}
		}
		case(LIFE){
			PlaySoundEffect(item,100);
			PlaySoundEffect(extend,100);
			SetPlayerLife(GetPlayerLife+1);
			NotifyEventAll(EV_EXTEND,0);
		}
		case(LIFE_PIECE){
			PlaySoundEffect(item,100);
			SetCommonData("LifePieces",GetCommonData("LifePieces",0)+1);
			if(GetCommonData("LifePieces",0)>=8) {
				PlaySoundEffect(extend,100);
				SetPlayerLife(GetPlayerLife+1);
				NotifyEventAll(EV_EXTEND,0);
				SetCommonData("LifePieces",0);
			}
		}
		case(POWER_FULL){
			PlaySoundEffect(item,100);
			SetPlayerPower(4);
			//if(GetPlayerPower==2 || GetPlayerPower==3 || GetPlayerPower==4) { PlaySE(powerup); }
			if(GetPlayerPower>4) { SetPlayerPower(4); }
		}
		case(POWER){
			PlaySoundEffect(item,100);
			SetPlayerPower(GetPlayerPower+0.01);
			if(GetPlayerPower<4) { AddScore(100); }
			else { AddScore(10000); }
			//if(GetPlayerPower==2 || GetPlayerPower==3 || GetPlayerPower==4) { PlaySE(powerup); }
			if(GetPlayerPower>4) { SetPlayerPower(4); }
		}
		case(POINT){
			PlaySoundEffect(item,100);
		}
		case(BONUS){
			PlaySoundEffect(item,100);
		}
		case(BALANCE){
			PlaySoundEffect(item,100);
			SetCommonData("Balance",GetCommonData("Balance",0)+100);
			if(GetCommonData("Balance",0)==10000) {
				PlaySoundEffect(bonus3,100);
				NotifyEventAll(EV_MAX_BALANCE,0);
			}
			if(GetCommonData("Balance",0)>10000) { SetCommonData("Balance",10000); }
		}
		case(DELETE){
			PlaySoundEffect(item,100);
			SetCommonData("ItemValue",GetCommonData("ItemValue",10000)+2+(GetGraze/10));
		}
	}
	case(EV_CREATE_ITEM) {
		let itemParam = GetEventArgument(0);
		let objEnemy = itemParam[0];
		let type = itemParam[1];
		if(objEnemy!=ID_INVALID) { EnemyItems(objEnemy,type,itemParam[2],itemParam[3],itemParam[4]); }
		else {
			if(type==POINT) { CreatePointItem(itemParam[2],itemParam[3],itemParam[4]); }
			else if(type==BALANCE) {
				if(GetCommonData("Balance",0)<10000) { CreateBalanceItem(itemParam[2],itemParam[3],itemParam[4]); }
				else { CreateBonusItem(itemParam[2],itemParam[3],itemParam[4]); }
			}
			else if(type==BONUS) { CreateBonusItem(itemParam[2],itemParam[3],itemParam[4]); }
			else { CreateItem(type,itemParam[2],itemParam[3],itemParam[4]); }
		}
	}
	case(EV_DELETE_ITEM) {
		let itemParam = GetEventArgument(0);
		CreateDeleteItem(itemParam[0],itemParam[1]);
	}
	case(EV_DELETE_SHOT_TO_ITEM) {
		let objShot = GetEventArgument(0);
		let objPos = GetEventArgument(1);
		if(objPos[0]>0 && objPos[0]<GetStgFrameWidth && objPos[1]>0 && objPos[1]<GetStgFrameHeight) {
			CreateDeleteItem(objPos[0], objPos[1]);
			BulletDeleteEffect(objPos[0],objPos[1],ObjShot_GetImageID(objShot));
		}
	}
}

function BulletDeleteEffect(xpos,ypos,graphic) {
	let DelayColor = GetShotDataInfoA1(graphic,TARGET_ENEMY,INFO_DELAY_COLOR);
	if(xpos<GetStgFrameWidth+20 && xpos>-20 && ypos<GetStgFrameHeight+20 && ypos>-20) {
		if(graphic>=284 && graphic<=291) {
			loop(3) { BulletBreak(xpos+rand(-10,10),ypos+rand(-8,8),DelayColor[0],DelayColor[1],DelayColor[2]); }
		}
		if(graphic>=265 && graphic<=272) {
			loop(3) { BulletBreak(xpos+rand(-10,10),ypos+rand(-8,8),DelayColor[0],DelayColor[1],DelayColor[2]); }
		}
		if(graphic>=324 && graphic<=331) {
			loop(3) { BulletBreak(xpos+rand(-10,10),ypos+rand(-8,8),DelayColor[0],DelayColor[1],DelayColor[2]); }
		}
		if(graphic>=292 && graphic<=307) {
			loop(8) { BulletBreak(xpos+rand(-20,20),ypos+rand(-23,23),DelayColor[0],DelayColor[1],DelayColor[2]); }
		}
		if(graphic>=383 && graphic<=390) {
			loop(5) { BulletBreak(xpos+rand(-20,20),ypos+rand(-18,18),DelayColor[0],DelayColor[1],DelayColor[2]); }
		}
		if(graphic>=399 && graphic<=408) {
			loop(5) { BulletBreak(xpos+rand(-20,20),ypos+rand(-18,18),DelayColor[0],DelayColor[1],DelayColor[2]); }
		}
		if(graphic>=272 && graphic<287) {
			loop(5) { BulletBreak(xpos+rand(-20,20),ypos+rand(-18,18),DelayColor[0],DelayColor[1],DelayColor[2]); }
		}
		if(graphic>=355 && graphic<=370) {
			loop(8) { BulletBreak(xpos+rand(-25,25),ypos+rand(-23,23),DelayColor[0],DelayColor[1],DelayColor[2]); }
		}
		else { BulletBreak(xpos,ypos,DelayColor[0],DelayColor[1],DelayColor[2]); }
	}
}
task BulletBreak(xpos,ypos,RED,GREEN,BLUE) {
	let objbreak = ObjPrim_Create(OBJ_SPRITE_2D);
	let w = 3;
	let count = 0;
	let speed = rand(1.3,2);
	let dir = rand(0,360);
	
	Obj_SetRenderPriorityI(objbreak,49);
	ObjPrim_SetTexture(objbreak,PSD ~ "IMG/Effect/BulletBreak.png");
	ObjRender_SetBlendType(objbreak,BLEND_ADD_RGB);
	ObjRender_SetPosition(objbreak,xpos,ypos,0);
	ObjRender_SetAngleXYZ(objbreak,0,0,rand(0,360));
	ObjRender_SetColor(objbreak,RED,GREEN,BLUE);
	
	loop(24) {
		if(count>=w*0) { ObjSprite2D_SetSourceRect(objbreak,0,0,32,32); }
		if(count>=w*1) { ObjSprite2D_SetSourceRect(objbreak,32,0,64,32); }
		if(count>=w*2) { ObjSprite2D_SetSourceRect(objbreak,64,0,96,32); }
		if(count>=w*3) { ObjSprite2D_SetSourceRect(objbreak,96,0,128,32); }
		if(count>=w*4) { ObjSprite2D_SetSourceRect(objbreak,128,0,160,32); }
		if(count>=w*5) { ObjSprite2D_SetSourceRect(objbreak,160,0,192,32); }
		if(count>=w*6) { ObjSprite2D_SetSourceRect(objbreak,192,0,224,32); }
		if(count>=w*7) { ObjSprite2D_SetSourceRect(objbreak,224,0,256,32); }
		ObjSprite2D_SetDestCenter(objbreak);
	
		ObjRender_SetPosition(objbreak,xpos,ypos,0);
		if(speed>0) { speed-=0.10; }
		xpos+=speed*cos(dir);
		ypos+=speed*sin(dir);
		count++;
		yield;
	}
	Obj_Delete(objbreak);
}

task EnemyItems(objEnemy,type,x,y,disperse) {
	let xpos = 0;
	let ypos = 0;
	while(!Obj_IsDeleted(objEnemy)) {
		xpos = ObjMove_GetX(objEnemy)+x;
		ypos = ObjMove_GetY(objEnemy)+y;
		yield;
	}
	if(type==LIFE_PIECE || type==SPELL_PIECE) { PlaySoundEffect(bonus,100); }
	if(type==POINT) { CreatePointItem(xpos,ypos,disperse); }
	else if(type==BALANCE) { CreateBalanceItem(xpos,ypos,disperse); }
	else if(type==BONUS) { CreateBonusItem(xpos,ypos,disperse); }
	else { CreateItem(type,xpos,ypos,disperse); }
}

task CreateDeleteItem(x,y) {
	let obj = CreateItemU1(DELETE,x,y,0);
	ObjItem_SetRenderScoreEnable(obj,false);
	ObjMove_SetAngle(obj, 270+rand(-5,5));
	let speed = rand(1,2);
	ObjRender_SetScaleXYZ(obj,0.8,0.8,0.8);
	descent(i in 0..60) { ObjMove_SetSpeed(obj,speed*i/60); yield; }
	ObjItem_SetDefinedMovePatternA1(obj,ITEM_MOVE_TOPLAYER);
}
task CreateItem(type,x,y,disperse) {
	let objItem = CreateItemU1(type,x+rand(-disperse,disperse),y+rand(-disperse,disperse),0);
	ObjItem_SetRenderScoreEnable(objItem,false);
	
	let collect = false;
	
	Animate;
	RenderScore(objItem);
	ItemCollect(objItem);
	task ItemCollect(ItemID) {
		let radius = 0;
		let distCollect = 0;
		let balanceBonus = 0;
		while(!Obj_IsDeleted(ItemID)) {
			if(GetVirtualKeyState(VK_SLOWMOVE)!= KEY_FREE) { radius = 55; } else { radius = 35; }
			if(GetPlayerState!=STATE_DOWN && GetPlayerState!=STATE_END) {
				if(GetDistance(GetPlayerX,GetPlayerY,ObjMove_GetX(ItemID),ObjMove_GetY(ItemID))<=radius && collect == false) {
					ObjMove_SetSpeed(ItemID,2.5);
					ObjMove_SetAcceleration(ItemID,0.15);
					ObjMove_SetMaxSpeed(ItemID,6);
					collect = true;
				}
			}
			if(collect == true) { ObjMove_SetAngle(ItemID,GetAngleToPlayer(ItemID)); }
			yield;
		}
	}
	task RenderScore(objItem) {
		let xpos = 0;
		let ypos = 0;
		let itemScore = 0;
		let distCollect = 0;
		while(!Obj_IsDeleted(objItem)) {
			itemScore = Obj_GetValueD(objItem,"Score",0);
			xpos = ObjRender_GetX(objItem);
			ypos = ObjRender_GetY(objItem);
			yield;
		}
		if(itemScore!=0) {
			RenderScoreText(xpos,ypos,IntToString(itemScore),255,255,255);
		}
	}
	task Animate {
		let angle = 0;
		loop(15) {
			angle+=360/15*40;
			ObjRender_SetAngleZ(objItem,angle);
			yield;
		}
	}
	function GetDistance(x1,y1,x2,y2) {
		return ((x1-x2)^2+(y1-y2)^2)^(1/2);
	}
}
task CreatePointItem(x,y,disperse) {
	let objItem = CreateItemU1(POINT,x+rand(-disperse,disperse),y+rand(-disperse,disperse),0);
	ObjItem_SetRenderScoreEnable(objItem,false);
	
	let score = round(GetCommonData("ItemValue",10000));
	let distCollect = max(0,ObjMove_GetY(objItem)-GetCommonData("PointOfCollection",100));
	let balanceBonus = 10000*(GetCommonData("Balance",0)/10000);
	let collect = false;
	
	Animate;
	RenderScore(objItem,x,y,round((score-score*(distCollect/400)+balanceBonus)/10)*10);
	ItemCollect(objItem);
	
	task ItemCollect(ItemID) {
		let radius = 0;
		while(!Obj_IsDeleted(ItemID)) {
			score = round(GetCommonData("ItemValue",10000));
			distCollect = max(0,ObjMove_GetY(ItemID)-GetCommonData("PointOfCollection",100));
			balanceBonus = 10000*(GetCommonData("Balance",0)/10000);
			
			Obj_SetValue(objItem,"Score",round((score-score*(distCollect/400)+balanceBonus)/10)*10);
			
			if(GetPlayerY<=GetCommonData("PointOfCollection",100)) { Obj_SetValue(objItem,"Score",round((score+balanceBonus)/10)*10); distCollect = 0; return; }
			if(GetVirtualKeyState(VK_SLOWMOVE)!= KEY_FREE) { radius = 55; } else { radius = 35; }
			if(GetPlayerState!=STATE_DOWN && GetPlayerState!=STATE_END) {
				if(GetDistance(GetPlayerX,GetPlayerY,ObjMove_GetX(ItemID),ObjMove_GetY(ItemID))<=radius && collect == false) {
					ObjMove_SetSpeed(ItemID,2.5);
					ObjMove_SetAcceleration(ItemID,0.15);
					ObjMove_SetMaxSpeed(ItemID,6);
					collect = true;
				}
			}
			if(collect == true) { ObjMove_SetAngle(ItemID,GetAngleToPlayer(ItemID)); }
			yield;
		}
	}
	task RenderScore(objItem,xpos,ypos,itemScore) {
		while(!Obj_IsDeleted(objItem)) {
			itemScore = Obj_GetValueD(objItem,"Score",0);
			xpos = ObjRender_GetX(objItem);
			ypos = ObjRender_GetY(objItem);
			yield;
		}
		if(itemScore!=0) {
			if(distCollect<=0) { RenderScoreText(xpos,ypos,IntToString(itemScore),255,255,0); }
			else { RenderScoreText(xpos,ypos,IntToString(itemScore),255,255,255); }
		}
	}
	task Animate {
		let angle = 0;
		loop(15) {
			angle+=360/15*40;
			ObjRender_SetAngleZ(objItem,angle);
			yield;
		}
	}
	function GetDistance(x1,y1,x2,y2) {
		return ((x1-x2)^2+(y1-y2)^2)^(1/2);
	}
}
task CreateBonusItem(x,y,disperse) {
	let objItem = CreateItemU1(BONUS,x+rand(-disperse,disperse),y+rand(-disperse,disperse),0);
	ObjItem_SetRenderScoreEnable(objItem,false);
	
	let score = GetCommonData("ItemValue",10000);
	let distCollect = max(0,ObjMove_GetY(objItem)-GetCommonData("PointOfCollection",100));
	let balanceBonus = 10000*(GetCommonData("Balance",0)/10000);
	let collect = false;
	
	Animate;
	RenderScore(objItem,x,y,round((score+balanceBonus)/10)*10);
	ItemCollect(objItem);
	
	task ItemCollect(ItemID) {
		let radius = 0;
		while(!Obj_IsDeleted(ItemID)) {
			score = GetCommonData("ItemValue",10000);
			distCollect = max(0,ObjMove_GetY(ItemID)-GetCommonData("PointOfCollection",100));
			balanceBonus = 10000*(GetCommonData("Balance",0)/10000);
			
			Obj_SetValue(objItem,"Score",round((score+balanceBonus)/10)*10);
			
			if(GetVirtualKeyState(VK_SLOWMOVE)!= KEY_FREE) { radius = 55; } else { radius = 35; }
			if(GetPlayerState!=STATE_DOWN && GetPlayerState!=STATE_END) {
				if(GetDistance(GetPlayerX,GetPlayerY,ObjMove_GetX(ItemID),ObjMove_GetY(ItemID))<=radius && collect == false) {
					ObjMove_SetSpeed(ItemID,2.5);
					ObjMove_SetAcceleration(ItemID,0.15);
					ObjMove_SetMaxSpeed(ItemID,6);
					collect = true;
				}
			}
			if(collect == true) { ObjMove_SetAngle(ItemID,GetAngleToPlayer(ItemID)); }
			yield;
		}
	}
	task RenderScore(objItem,xpos,ypos,itemScore) {
		while(!Obj_IsDeleted(objItem)) {
			itemScore = Obj_GetValueD(objItem,"Score",0);
			xpos = ObjRender_GetX(objItem);
			ypos = ObjRender_GetY(objItem);
			yield;
		}
		if(itemScore!=0) {
			RenderScoreText(xpos,ypos,IntToString(itemScore),255,255,0);
		}
	}
	task Animate {
		let angle = 0;
		loop(15) {
			angle+=360/15*40;
			ObjRender_SetAngleZ(objItem,angle);
			yield;
		}
	}
	function GetDistance(x1,y1,x2,y2) {
		return ((x1-x2)^2+(y1-y2)^2)^(1/2);
	}
}
task CreateBalanceItem(x,y,disperse) {
	let objItem = CreateItemU1(BALANCE,x+rand(-disperse,disperse),y+rand(-disperse,disperse),0);
	ObjItem_SetRenderScoreEnable(objItem,false);
	
	let collect = false;
	
	Animate;
	RenderScore(objItem,x,y);
	ItemCollect(objItem);
	task ItemCollect(ItemID) {
		let radius = 0;
		while(!Obj_IsDeleted(ItemID)) {
			if(GetVirtualKeyState(VK_SLOWMOVE)!= KEY_FREE) { radius = 55; } else { radius = 35; }
			if(GetPlayerState!=STATE_DOWN && GetPlayerState!=STATE_END) {
				if(GetDistance(GetPlayerX,GetPlayerY,ObjMove_GetX(ItemID),ObjMove_GetY(ItemID))<=radius && collect == false) {
					ObjMove_SetSpeed(ItemID,2.5);
					ObjMove_SetAcceleration(ItemID,0.15);
					ObjMove_SetMaxSpeed(ItemID,6);
					collect = true;
				}
			}
			if(collect == true) { ObjMove_SetAngle(ItemID,GetAngleToPlayer(ItemID)); }
			if(GetCommonData("Balance",0)==10000) {
				ItemEffect(ObjMove_GetX(objItem),ObjMove_GetY(objItem));
				CreateBonusItem(ObjMove_GetX(objItem),ObjMove_GetY(objItem),0);
				Obj_Delete(objItem);
				return;
			}
			yield;
		}
	}
	task RenderScore(objItem,xpos,ypos) {
		let distCollect = 0;
		while(!Obj_IsDeleted(objItem)) {
			xpos = ObjRender_GetX(objItem);
			ypos = ObjRender_GetY(objItem);
			yield;
		}
		if(GetCommonData("Balance",0)<10000) { RenderScoreText(xpos,ypos,"100",0,168,255); }
	}
	task Animate {
		let angle = 0;
		loop(15) {
			angle+=360/15*40;
			ObjRender_SetAngleZ(objItem,angle);
			yield;
		}
	}
	function GetDistance(x1,y1,x2,y2) {
		return ((x1-x2)^2+(y1-y2)^2)^(1/2);
	}
}
task RenderScoreText(x,y,text,R,G,B) {
	let obj = ObjText_Create;
	ObjText_SetText(obj, text);
	ObjText_SetFontSize(obj, 13);
	ObjText_SetFontType(obj, "Crystal clear");
	//ObjText_SetFontBold(obj, true);
	ObjText_SetFontColorTop(obj, R, G, B);
	ObjText_SetFontColorBottom(obj, R, G, B);
	// ObjText_SetFontBorderType(obj, BORDER_SHADOW);
	// ObjText_SetFontBorderWidth(obj, 1);
	// ObjText_SetFontBorderColor(obj, 0, 0, 0);
	ObjText_SetHorizontalAlignment(obj,ALIGNMENT_CENTER);
	ObjText_SetMaxWidth(obj,40);
	Obj_SetRenderPriorityI(obj, 61);
	
	let alpha = 200;
	
	loop(20) {
		ObjRender_SetPosition(obj,x-20,y,0);
		ObjRender_SetAlpha(obj,alpha);
		y-=0.6;
		yield;
	}
	loop(10) {
		ObjRender_SetPosition(obj,x-20,y,0);
		ObjRender_SetAlpha(obj,alpha);
		alpha-=200/10;
		y-=0.6;
		yield;
	}
	Obj_Delete(obj);
}
task ItemEffect(x,y) {
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	Obj_SetRenderPriorityI(obj,61);
	ObjPrim_SetTexture(obj, MD ~ "IMG/Effect/Item_Change.png");
	ObjSprite2D_SetSourceRect(obj, 0, 0, 32, 32);
	ObjSprite2D_SetDestCenter(obj);
	

	let alpha = 255;
	let scale = 0;
	let dscale = 0.2;
	loop(10){
		scale += dscale;
		alpha -= 255/10;
		if(dscale>0){ dscale = dscale^1.10; }
		ObjRender_SetColor(obj,96*(alpha/255),255*(alpha/255),96*(alpha/255));
		ObjRender_SetPosition(obj, x, y, 0);
		ObjRender_SetScaleXYZ(obj, scale, scale, 1);
		yield;
	}
	Obj_Delete(obj);
}