#TouhouDanmakufu[Player]
#ScriptVersion[3]
#ID["Tsuba"]
#Title["Tsubakura Enraku"]
#Text["Speed: 5 / 2.5[r]Shot Type: Forward Focused[r]Spell: \"Monochrome Ray\""]
#Image["./Image/Cutin.png"]
#ReplayName["Tsuba"]
#include "./Functions.dnh"

let current = GetCurrentScriptDirectory();

let objPlayer = GetPlayerObjectID();
let renderPlayerObject = ObjPrim_Create(OBJ_SPRITE_2D);
let playerdead = false;
let IsPlayerSpawning = false;
let IsHitboxActive = false;
let optionAlive = true;
let CanShoot = true;

let isvisible = true;

let imgPlayer = current ~ "Image/Tsuba.png";
let snowflake = current ~ "Image/Focus.png";
let imgOption = current ~ "Image/Shot.png";

let level = 1;

@Initialize{
	LoadPlayerShotData(current ~ "Shotdata.dnh");
	SetPlayerAutoItemCollectLine(130);
	ObjPlayer_AddIntersectionCircleA1(objPlayer, 0, 0, 1, 20); 

	SetPlayerSpeed(5, 2.5);
	SetPlayerClip(11,11,GetStgFrameWidth-11,GetStgFrameHeight-18);
	RenderPlayerImage(renderPlayerObject, 0);

	Shoot;
	TMagicCircle;
	Options;
}

@Event{
	alternative(GetEventType())
		case(EV_REQUEST_SPELL)
		{	
			let spell = GetPlayerSpell();
			if(spell >= 1 && IsPermitPlayerSpell()){
				Spell;
				SetScriptResult(true);
				SetPlayerSpell(spell-1);
			}else{
				SetScriptResult(false);
			}
		}
		case(EV_GRAZE){
			PlaySE(current~"Sound/thrill.wav");
			SetAreaCommonData("System", "PointValue", GetAreaCommonData("System", "PointValue", 10000) + 0.5);
			GrazePiece;
		}
		case(EV_HIT)
		{
			PlaySE(current~"Sound/j_end.wav");
			PlayerExplode(current ~ "Image/Explode.png");
		}
		case(EV_PLAYER_SHOOTDOWN){
			playerdead = true;
			IsHitboxActive = false;
			optionAlive = false;
		}
		case(EV_PLAYER_REBIRTH)
		{
			SetPlayerSpell(3 + GetAreaCommonData("System", "SpellFragmentNum", 0)*GetAreaCommonData("System", "SpellFragmentValue", [1/5, 5])[0]);
			playerdead = false;
			optionAlive = true;
			Options;
			SetPlayerInvincibilityFrame(180);
			DeleteShotAll(TYPE_SHOT, TYPE_IMMEDIATE);
		}
		case(EV_GET_ITEM)
		{
			let itemType = GetEventArgument(0);
			alternative(itemType)
				case(ITEM_1UP){
					PlaySE(current~"Sound/1up.wav");
					SetPlayerLife(GetPlayerLife + 1);
					WriteLog("ITEM_1UP");
				}
				case(ITEM_1UP_S){
					PlaySE(current~"Sound/1up.wav");
					SetPlayerLife(GetPlayerLife + 1);
					WriteLog("ITEM_1UP_S");
				}
				case(ITEM_SPELL){
					PlaySE(current~"Sound/item_00.wav");
					SetPlayerSpell(GetPlayerSpell + 1);
					WriteLog("ITEM_SPELL");
				}
				case(ITEM_SPELL_S){
					PlaySE(current~"Sound/item_00.wav");
					SetPlayerSpell(GetPlayerSpell + 1);
					WriteLog("ITEM_SPELL_S");
				}
				case(ITEM_POWER){
					PlaySE(current~"Sound/item_00.wav");
					WriteLog("ITEM_POWER");
				}
				case(ITEM_POWER_S){
					PlaySE(current~"Sound/item_00.wav");
					WriteLog("ITEM_POWER_S");
				}
				case(ITEM_POINT){
					PlaySE(current~"Sound/item_00.wav");
					WriteLog("ITEM_POWER");
				}
				case(ITEM_POINT_S){
					PlaySE(current~"Sound/item_00.wav");
					WriteLog("ITEM_POWER_S");
				}
		}
}

@Loading{
	LoadSound(current~"Sound/j_shot_00.wav");
	LoadSound(current~"Sound/j_end.wav");
	LoadSound(current~"Sound/thrill.wav");
	LoadSound(current~"Sound/item_00.wav");
	LoadSound(current~"Sound/1up.wav");
	LoadSound(current~"Sound/bom_00.wav");
	LoadSound(current~"Sound/bom_02.wav");
	LoadSound(current~"Sound/bom_04.wav");
}

@MainLoop{
	if(GetPlayerState==STATE_DOWN || IsPermitPlayerShot==false){CanShoot = false;}
	if(GetPlayerState==STATE_NORMAL && IsPermitPlayerShot==true){CanShoot = true;}

	SetPlayerClip(11,11,GetStgFrameWidth-11,GetStgFrameHeight-18);
	if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD&&!IsHitboxActive){RenderHitboxImage(objPlayer, 0, 1);}
	yield;
}

@Finalize{

}

task Shoot{
	let count = 0;
	loop{
		if(GetVirtualKeyState(VK_SHOT) != KEY_FREE && count%2 == 0){
			if(CanShoot && !IsPlayerSpellActive){
				PlaySE(current~"Sound/j_shot_00.wav");
				TShotRegular(GetPlayerX, GetPlayerY);
			}
		}
		count++;
		yield;
	}
}

task Options{
	let dir = 0;
	loop(6){
		Option(dir, 48, 24);
		dir += 360/6;
	}
}

//x/y and focx/focy are in relation to the player object
task Option(starta, rad, focrad){
	let objOption = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(objOption, 30);
	ObjRender_SetBlendType(objOption, BLEND_ALPHA);
	ObjPrim_SetTexture(objOption, imgOption);
	ObjSprite2D_SetSourceRect(objOption, 80, 0, 96, 16);
	ObjSprite2D_SetDestCenter(objOption);
	ObjRender_SetPosition(objOption, GetPlayerX, GetPlayerY, 0);

	let destX;
	let destY;
	let dist;
	let dir;

	let a = starta;

	let count = 0;
	while(!Obj_IsDeleted(objOption)){
		if(!optionAlive){Obj_Delete(objOption);}
		if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_FREE){
			destX = GetPlayerX + rad*cos(a);
			destY = GetPlayerY + rad*sin(a)
		}else{
			destX = GetPlayerX + focrad*cos(a);
			destY = GetPlayerY + focrad*sin(a);
		}

		let opX = ObjRender_GetX(objOption);
		let opY = ObjRender_GetY(objOption);
		dist = GetDist(opX, opY, destX, destY);
		dir = GetAngle(opX, opY, destX, destY);
		opX += 0.25 * dist * cos(dir);
		opY += 0.25 * dist * sin(dir);
		ObjRender_SetPosition(objOption, opX, opY, 0);

		if(GetVirtualKeyState(VK_SHOT) != KEY_FREE && count%2 == 0){
			if(CanShoot && !IsPlayerSpellActive){
				if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_FREE){
					TNeedle(ObjRender_GetX(objOption), ObjRender_GetY(objOption), 270, 1.5);
				}else{
					TNeedle(ObjRender_GetX(objOption), ObjRender_GetY(objOption), 270, 2);
				}
			}
		}
		count++;
		a += 4;
		yield;
	}
}

task RenderPlayerImage(ObjectRender, offset){
	let anim_count = 0;
	let anim_type = 0;
	let Uber = 0;
	let Uberblink = 0;
	
	let x = 35;
	let y = 52;
	let ri = 6;
	let rm = 4;
	let bn = 0;
	
	ObjPrim_SetTexture(ObjectRender, imgPlayer);
	Obj_SetRenderPriority(ObjectRender, 0.45);
	
	loop{

		if(GetPlayerState!=STATE_DOWN && isvisible){
			ObjRender_SetAlpha(ObjectRender, 255);
		}
		if(GetPlayerState==STATE_DOWN || playerdead || !isvisible){ObjRender_SetAlpha(ObjectRender, 0);}
		
		
		ObjRender_SetPosition(ObjectRender, GetPlayerX, GetPlayerY, 0);
		if(!IsPlayerSpawning){
			if(GetPlayerState!=STATE_HIT){
				Blink;
				if(GetVirtualKeyState(VK_RIGHT) == KEY_FREE && GetVirtualKeyState(VK_LEFT) == KEY_FREE){
					if(anim_type != 0){
						bn = 0;
						anim_count = 0;
						anim_type = 0;
					}
				}
				if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD){
					if(GetVirtualKeyState(VK_RIGHT) != KEY_HOLD && anim_type != 1){
						bn = 0.85;
						anim_count = 0;
						anim_type = 1;
					}
				}
				if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD){
					if(anim_type != 2){
						bn = 0.85;
						anim_count = 0;
						anim_type = 2;
					}
				}
				anim_count+=(1+bn);
				if(floor(anim_count) != anim_count && bn == 0){
					anim_count = floor(anim_count);
				}
			}
		}else{
			Blink;
			anim_count+=1;
			if(anim_type != 0){
				anim_count = 0;
				anim_type = 0;
			}
		}
		//0 - Idle; 1 - Left; 2 - Right
		
		if(anim_type==0){
			if(round(anim_count) >= 0){ObjSprite2D_SetSourceRect(ObjectRender, 0, 0+offset, x, y+offset);}
			if(round(anim_count) >= ri){ObjSprite2D_SetSourceRect(ObjectRender, x, 0+offset, x*2, y+offset);}
			if(round(anim_count) >= ri*2){ObjSprite2D_SetSourceRect(ObjectRender, x*2, 0+offset, x*3, y+offset);}
			if(round(anim_count) >= ri*3){ObjSprite2D_SetSourceRect(ObjectRender, x*3, 0+offset, x*4, y+offset);}
			if(round(anim_count) >= ri*4){ObjSprite2D_SetSourceRect(ObjectRender, x*4, 0+offset, x*5, y+offset);}
			if(round(anim_count) >= ri*5){ObjSprite2D_SetSourceRect(ObjectRender, x*5, 0+offset, x*6, y+offset);}
			if(round(anim_count) >= ri*6){ObjSprite2D_SetSourceRect(ObjectRender, x*6, 0+offset, x*7, y+offset);}
			if(round(anim_count) >= ri*7){ObjSprite2D_SetSourceRect(ObjectRender, x*7, 0+offset, x*8, y+offset);}
			if(round(anim_count) >= ri*8-1){anim_count = ri-1;}
		}
		if(anim_type==1){
			if(round(anim_count) >= 0){ObjSprite2D_SetSourceRect(ObjectRender, 0, y+offset, x, y*2+offset);}
			if(round(anim_count) >= rm){ObjSprite2D_SetSourceRect(ObjectRender, x, y+offset, x*2, y*2+offset);}
			if(round(anim_count) >= rm*2){ObjSprite2D_SetSourceRect(ObjectRender, x*2, y+offset, x*3, y*2+offset);}
			if(round(anim_count) >= rm*3){ObjSprite2D_SetSourceRect(ObjectRender, x*3, y+offset, x*4, y*2+offset);}
			if(round(anim_count) >= rm*4){ObjSprite2D_SetSourceRect(ObjectRender, x*4, y+offset, x*5, y*2+offset);bn = 0}
			if(round(anim_count) >= rm*5){ObjSprite2D_SetSourceRect(ObjectRender, x*5, y+offset, x*6, y*2+offset);}
			if(round(anim_count) >= rm*6){ObjSprite2D_SetSourceRect(ObjectRender, x*6, y+offset, x*7, y*2+offset);}
			if(round(anim_count) >= rm*7){ObjSprite2D_SetSourceRect(ObjectRender, x*7, y+offset, x*8, y*2+offset);}
			if(round(anim_count) >= rm*8-1){anim_count = rm*4-1;}
		}
		if(anim_type==2){
			if(round(anim_count) >= 0){ObjSprite2D_SetSourceRect(ObjectRender, 0, y*2+offset, x, y*3+offset);}
			if(round(anim_count) >= rm){ObjSprite2D_SetSourceRect(ObjectRender, x, y*2+offset, x*2, y*3+offset);}
			if(round(anim_count) >= rm*2){ObjSprite2D_SetSourceRect(ObjectRender, x*2, y*2+offset, x*3, y*3+offset);}
			if(round(anim_count) >= rm*3){ObjSprite2D_SetSourceRect(ObjectRender, x*3, y*2+offset, x*4, y*3+offset);}
			if(round(anim_count) >= rm*4){ObjSprite2D_SetSourceRect(ObjectRender, x*4, y*2+offset, x*5, y*3+offset);bn = 0}
			if(round(anim_count) >= rm*5){ObjSprite2D_SetSourceRect(ObjectRender, x*5, y*2+offset, x*6, y*3+offset);}
			if(round(anim_count) >= rm*6){ObjSprite2D_SetSourceRect(ObjectRender, x*6, y*2+offset, x*7, y*3+offset);}
			if(round(anim_count) >= rm*7){ObjSprite2D_SetSourceRect(ObjectRender, x*7, y*2+offset, x*8, y*3+offset);}
			if(round(anim_count) >= rm*8-1){anim_count = rm*4-1;}
		}
		
		
		ObjSprite2D_SetDestCenter(ObjectRender);
		yield;
	}	
	task Blink{
		if(GetPlayerInvincibilityFrame>0){
			if(Uber==0){ObjRender_SetColor(ObjectRender, 255, 255, 255);}
			if(Uber!=0){ObjRender_SetColor(ObjectRender, 32, 32, 255);}
			Uberblink+=1;
			if(Uberblink>1){Uberblink=0;Uber++;if(Uber>1){Uber=0}}
		}else{
			ObjRender_SetColor(ObjectRender, 255, 255, 255);
		}
	}
}

task RenderHitboxImage(plobj, offx, hitboxnum){
	if(IsHitboxActive){return;}
	while(playerdead){
		if(GetVirtualKeyState(VK_SLOWMOVE)!=KEY_FREE){return;}
		yield;
	}
	IsHitboxActive = true;
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 0.39);
	ObjRender_SetAlpha(obj, 220);
	ObjPrim_SetTexture(obj, snowflake);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 64, 64);
	ObjSprite2D_SetDestCenter(obj);
	
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj2, BLEND_ALPHA);
	Obj_SetRenderPriority(obj2, 0.69);
	ObjRender_SetAlpha(obj2, 220);
	ObjPrim_SetTexture(obj2, snowflake);
	ObjSprite2D_SetSourceRect(obj2, 0, 0, 64, 64);
	ObjSprite2D_SetDestCenter(obj2);
	
	
	let ObjTime = 0;
	
	let AlphaSet = 0;
	let ObjAlpha = 0;
	let ObjScale = 1;
	
	while(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD){
		if(!isvisible){ObjRender_SetAlpha(obj, 0); ObjRender_SetAlpha(obj2, 0);}else{ObjRender_SetAlpha(obj, 255); ObjRender_SetAlpha(obj2, 255);}

		ObjPrim_SetTexture(obj, snowflake);
		ObjPrim_SetTexture(obj2, snowflake);

		if(ObjScale<1.4&&!playerdead){
			ObjTime+=8;
			ObjScale += 0.125;
			PositionSet;
		}else{break;}
		yield;
	}
	while(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD){
		if(!isvisible){ObjRender_SetAlpha(obj, 0); ObjRender_SetAlpha(obj2, 0);}else{ObjRender_SetAlpha(obj, 255); ObjRender_SetAlpha(obj2, 255);}

		ObjPrim_SetTexture(obj, snowflake);
		ObjPrim_SetTexture(obj2, snowflake);

		if(ObjScale>1&&!playerdead){
			ObjTime+=8;
			ObjScale -= 0.1;
			PositionSet;
		}else{break;}
		yield;
	}
	ObjScale = 1;
	yield;
	while(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD){
		if(!isvisible){ObjRender_SetAlpha(obj, 0); ObjRender_SetAlpha(obj2, 0);}else{ObjRender_SetAlpha(obj, 255); ObjRender_SetAlpha(obj2, 255);}

		ObjPrim_SetTexture(obj, snowflake);
		ObjPrim_SetTexture(obj2, snowflake);

		if(!playerdead){
			ObjTime+=3;
			PositionSet;
		}else{break;}
		yield;
	}
	IsHitboxActive = false;
	while(ObjScale>0){
		if(!isvisible){ObjRender_SetAlpha(obj, 0); ObjRender_SetAlpha(obj2, 0);}else{ObjRender_SetAlpha(obj, 255); ObjRender_SetAlpha(obj2, 255);}

		ObjPrim_SetTexture(obj, snowflake);
		ObjPrim_SetTexture(obj2, snowflake);

		ObjTime+=3;
		ObjScale -= 0.25;
		PositionSet;
		yield;
	}

	Obj_Delete(obj);
	Obj_Delete(obj2);
	IsHitboxActive = false;
	
	task PositionSet{
		ObjRender_SetPosition(obj, GetPlayerX, GetPlayerY, 0);
		ObjRender_SetAngleZ(obj, ObjTime);
		ObjRender_SetPosition(obj2, GetPlayerX, GetPlayerY, 0);
		ObjRender_SetScaleXYZ(obj, ObjScale, ObjScale, ObjScale);
		ObjRender_SetScaleXYZ(obj2, ObjScale, ObjScale, ObjScale);
	}
	
}

task Spawn{
	let obj = GetPlayerObjectID;
	let y = 0;
	ObjRender_SetX(obj, GetStgFrameWidth/2);
	ObjRender_SetY(obj, GetStgFrameHeight-32);
	SetPlayerSpeed(0,0);
	IsPlayerSpawning = true;
	while(y<82){
		SetPlayerClip(11,11,GetStgFrameWidth-11,GetStgFrameHeight+64);	
		SetPlayerSpeed(0,0);
		ObjRender_SetY(obj, GetStgFrameHeight+32-y);
		y+=2;
		yield;
	}
	IsPlayerSpawning = false;
	SetPlayerSpeed(4.5, 2);
}

task Spell{
	PlaySE(current~"Sound/bom_00.wav");
	PlaySE(current~"Sound/bom_02.wav");
	CanShoot = false;
	SetPlayerInvincibilityFrame(240);
	SetPlayerSpeed(0.5, 0.5);

	let manage = GetSpellManageObject;
	
	ChargeCircle([0, 0, 0]);
	loop(30){yield;}
	ChargeCircle([256, 256, 256]);
	loop(30){yield;}
	ChargeCircle([0, 0, 0]);
	loop(30){yield;}

	PlaySE(current~"Sound/bom_04.wav");
	ScreenShakeA1(60, 5);
	loop(60){
		TLaser(GetPlayerX + rand(-32, 32), GetPlayerY + rand(-32, 32), 270, 3);
		TLaser(GetPlayerX + rand(-32, 32), GetPlayerY + rand(-32, 32), 270, 4);
		TLaser(GetPlayerX + rand(-32, 32), GetPlayerY + rand(-32, 32), 270, 3);
		TLaser(GetPlayerX + rand(-32, 32), GetPlayerY + rand(-32, 32), 270, 4);
		TLaser(GetPlayerX + rand(-32, 32), GetPlayerY + rand(-32, 32), 270, 3);
		TLaser(GetPlayerX + rand(-32, 32), GetPlayerY + rand(-32, 32), 270, 4);
		yield;
	}
	SetPlayerSpeed(5, 2.5);
	CanShoot = true;
	loop(60){yield;}
	Obj_Delete(manage);

	task ChargeCircle(c){
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory ~ "Image/circle.png");
		ObjSprite2D_SetSourceRect(obj, 0, 0, 256, 256);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetPosition(obj, GetPlayerX, GetPlayerY, 0);
		ObjRender_SetColor(obj, c[0], c[1], c[2]);
		ObjRender_SetAlpha(obj, 0);
		ObjRender_SetScaleXYZ(obj, 10, 10, 10);
		Obj_SetRenderPriorityI(obj, 30);

		let s = 10;
		let a = 0;
		loop(30){
			ObjRender_SetAlpha(obj, a);
			ObjRender_SetScaleXYZ(obj, s, s, s);

			a += 256/15;
			s -= 10/30;
			yield;
		}
		Obj_Delete(obj);
	}
}