#TouhouDanmakufu[Player]
#ScriptVersion[3]
#ID["Player"]
#Title["Player"]
#ReplayName["Player"]

#include "./Shot.txt"

let speed = [4.5,2];

let CSD = GetCurrentScriptDirectory;
let playerID = GetPlayerObjectID;
let playerPosDeath = [];
let playerObj = ObjPrim_Create(OBJ_SPRITE_2D);

let hitboxActive = false;
let optionAlive = true;

let centerX = GetStgFrameWidth/2;
let centerY = GetStgFrameHeight/2;
task _PlaySE(name){NotifyEventAll(EV_USER_PACKAGE + 10, [name+19]);}
function wait(w){loop(w){yield;}}
function trun(w){let x = truncate(w);return x;}



@Initialize{
	LoadPlayerShotData(CSD ~ "Shotdata.txt");
	
	ObjPlayer_AddIntersectionCircleA1(playerID, 0, 0, 1.5, 20); 
	SetPlayerRebirthFrame(15);
	SetPlayerDownStateFrame(0);
	
	SetPlayerSpeed(speed[0], speed[1]); 
	SetPlayerClip(1,1,GetStgFrameWidth-1,GetStgFrameHeight-1);
	SetPlayerAutoItemCollectLine(GetStgFrameHeight/5*1.5);

	playerRender;
	shoot;
	option;
}

@Event{
	alternative(GetEventType)
	
	case(EV_REQUEST_SPELL){
		if(IsPermitPlayerSpell && GetPlayerSpell >= 1){
				if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD){SpellMarisa;}
				else{SpellReimu;}
				SetPlayerSpell(GetPlayerSpell-1);
				SetScriptResult(true);
		}
		else{SetScriptResult(false);}
	}
		
	case(EV_GRAZE){
		_PlaySE(1);
	}
		
	case(EV_HIT){
		_PlaySE(3);
	}

	case(EV_PLAYER_SHOOTDOWN){
		playerPosDeath = [GetPlayerX,GetPlayerY];
		_PlaySE(2);
		TExplosionA1(GetPlayerX, GetPlayerY, 128);
		if(GetPlayerSpell < 2){SetPlayerSpell(2);}
	}
		
	case(EV_PLAYER_REBIRTH){
		Spawn;
		}
}

@MainLoop{
	if(GetPlayerState==STATE_END){optionAlive = false;}
	
	if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD && GetPlayerState==STATE_NORMAL && !hitboxActive && GetPlayerState!=STATE_END){hitboxRender;}
	
	yield;
}
	
///////////////////////////////////////////////////////////////////////////
//-- Shoot --/-- Shoot --/-- Shoot --/-- Shoot --/-- Shoot --/-- Shoot --//
///////////////////////////////////////////////////////////////////////////

task shoot{
	let count = 0;

	loop{
		if(GetVirtualKeyState(VK_SHOT)!=KEY_FREE && IsPermitPlayerShot && !IsPlayerSpellActive){
			if(count%3 == 0){
				_PlaySE(0);
				if(GetVirtualKeyState(VK_SLOWMOVE)!=KEY_HOLD){
					ShotBaseAReimu(8);
					ShotBaseAReimu(-8);
				}
				else{
					ShotBaseAMarisa(8);
					ShotBaseAMarisa(-8);
				}
			}
		}

		count++;

		yield;
	}
}

task option{
	
	let x1 = [-40,-40,40,40];
	let	y1 = [40,-40,-40,40];
	
	let x2 = [36,18,-18,-36];
	let	y2 = [-15,15,15,-15];

	let angl = [-5,2,-2,5];

	ascent(a in 0..length(x1)){
		Fam([x1[a],x2[a]],[y1[a],y2[a]],[angl[a]]);
	}
}

task Fam(lx,ly,data){
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj, 0.46);
	ObjPrim_SetTexture(obj, CSD ~ "img/shot.png");

	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj2, 0.45);
	ObjPrim_SetTexture(obj2, CSD ~ "img/shot.png");
	ObjRender_SetScaleXYZ(obj2,1.4,1.4,0);
	ObjRender_SetAlpha(obj2,120);

	if(GetCommonData("Shottype",0) == 0){
		ObjSprite2D_SetSourceRect(obj, 0,0,15,15);
		ObjSprite2D_SetDestCenter(obj);
		ObjSprite2D_SetSourceRect(obj2, 0,0,15,15);
		ObjSprite2D_SetDestCenter(obj2);
	}
	else if(GetCommonData("Shottype",0) == 1){
		ObjSprite2D_SetSourceRect(obj, 19,0,33,15);
		ObjSprite2D_SetDestCenter(obj);
		ObjSprite2D_SetSourceRect(obj2, 19,0,33,15);
		ObjSprite2D_SetDestCenter(obj2);
	}
	
	let count = 0;
	
	Pos(GetPlayerX,GetPlayerY);
	let xa;
	let ya;

	while(optionAlive){
		if(GetVirtualKeyState(VK_SLOWMOVE)!=KEY_HOLD){
			xa = GetPlayerX+lx[0];
			ya = GetPlayerY+ly[0];

			ObjSprite2D_SetSourceRect(obj, 0,0,15,15);
			ObjSprite2D_SetDestCenter(obj);
			ObjSprite2D_SetSourceRect(obj2, 0,0,15,15);
			ObjSprite2D_SetDestCenter(obj2);

			ObjRender_SetAngleZ(obj,count*10);
			ObjRender_SetAngleZ(obj2,-count*10);

			ObjRender_SetScaleXYZ(obj2,1.4,1.4,0);
		}
		else{
			xa = GetPlayerX+lx[1];
			ya = GetPlayerY+ly[1];

			ObjSprite2D_SetSourceRect(obj, 19,0,33,15);
			ObjSprite2D_SetDestCenter(obj);
			ObjSprite2D_SetSourceRect(obj2, 19,0,33,15);
			ObjSprite2D_SetDestCenter(obj2);

			ObjRender_SetAngleZ(obj,0);
			ObjRender_SetAngleZ(obj2,0);

			ObjRender_SetScaleXYZ(obj2,1.4+0.1*cos(count*6),1.4+0.1*cos(count*6),0);
		}
		Pos(ObjRender_GetX(obj) + (xa - ObjRender_GetX(obj)) / 3, ObjRender_GetY(obj) + (ya - ObjRender_GetY(obj)) / 3);


		if(GetVirtualKeyState(VK_SHOT)!=KEY_FREE && IsPermitPlayerShot && !IsPlayerSpellActive){
			if(count%3 == 0 && GetVirtualKeyState(VK_SLOWMOVE)!=KEY_HOLD){
				ShotBaseBReimu(ObjRender_GetX(obj)+8,ObjRender_GetY(obj)+15,data[0]);
				ShotBaseBReimu(ObjRender_GetX(obj)-8,ObjRender_GetY(obj)+15,data[0]);
			}
			if(count%8 == 0 && GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD){
					ShotBaseBMarisa(ObjRender_GetX(obj),ObjRender_GetY(obj),1);
					ShotBaseBMarisa(ObjRender_GetX(obj),ObjRender_GetY(obj),-1);
			}
		}

		count++;
		
		yield;
	}

	task Pos(x,y){
		ObjRender_SetPosition(obj, x, y, 0);
		ObjRender_SetPosition(obj2, x, y, 0);
	}
	function GetAngA3(x1,y1,x2,y2){return atan2(y2-y1,x2-x1);}
	
	Obj_Delete(obj);
}


///////////////////////////////////////////////////////////////////////////
//-- Shoot --/-- Shoot --/-- Shoot --/-- Shoot --/-- Shoot --/-- Shoot --//
///////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////
//-- Render --/-- Render --/-- Render --/-- Render --/-- Render --//
////////////////////////////////////////////////////////////////////

task playerRender{
	
	let animA = 0;
	let animB = 0;
	let animC = 0;
	
	let Uber = 0;
	let Uberblink = 0;
	
	let A = 0;
	let x = 256/8;
	let y = 142/3;
	let angy = 0;
	let ena_y = true;
	
	Obj_SetRenderPriority(playerObj, 0.45);
	
	loop{

		if(GetPlayerState!=STATE_DOWN && GetPlayerState!=STATE_END){ObjRender_SetAlpha(playerObj, 255);}
		else{ObjRender_SetAlpha(playerObj, 0);}

		if(GetVirtualKeyState(VK_SLOWMOVE)!=KEY_HOLD){
			ObjPrim_SetTexture(playerObj, CSD ~ "img/player_sprite_00.png");
		}
		else{
			ObjPrim_SetTexture(playerObj, CSD ~ "img/player_sprite_01.png");
		}
		
		if(GetVirtualKeyState(VK_RIGHT) == KEY_FREE && GetVirtualKeyState(VK_LEFT) == KEY_FREE){
			
			if(animA < 6){A = 0;}
			else if((animA-6)%(6*7) < 6*1){A = 1;}
			else if((animA-6)%(6*7) < 6*2){A = 2;}
			else if((animA-6)%(6*7) < 6*3){A = 3;}
			else if((animA-6)%(6*7) < 6*4){A = 4;}
			else if((animA-6)%(6*7) < 6*5){A = 5;}
			else if((animA-6)%(6*7) < 6*6){A = 6;}
			else if((animA-6)%(6*7) < 6*7){A = 7;}
			
			animB = 0;
			animC = 0;
			}
			
		else if(GetVirtualKeyState(VK_RIGHT) != KEY_FREE && GetVirtualKeyState(VK_LEFT) != KEY_FREE){
			
			if(animA%(6*8) < 6){A = 0;}
			else if(animA%(6*8) < 6*2){A = 1;}
			else if(animA%(6*8) < 6*3){A = 2;}
			else if(animA%(6*8) < 6*4){A = 3;}
			else if(animA%(6*8) < 6*5){A = 4;}
			else if(animA%(6*8) < 6*6){A = 5;}
			else if(animA%(6*8) < 6*7){A = 6;}
			else if(animA%(6*8) < 6*8-1){A = 7;}
			
			animB = 0;
			animC = 0;
			}
		
		else if(GetVirtualKeyState(VK_RIGHT) == KEY_FREE && GetVirtualKeyState(VK_LEFT) != KEY_FREE){
			
			if(animB < 2){A = 8;}
			else if(animB < 2*2){A = 9;}
			else if(animB < 2*3){A = 10;}
			else if(animB < 2*4){A = 11;}
			else if((animB-8)%(4*4) < 4*1){A = 12;}
			else if((animB-8)%(4*4) < 4*2){A = 13;}
			else if((animB-8)%(4*4) < 4*3){A = 14;}
			else if((animB-8)%(4*4) < 4*4){A = 15;}
			
			
			animA = 0;
			animC = 0;
			}
			
		else{
			
			if(animC < 2){A = 16;}
			else if(animC < 2*2){A = 17;}
			else if(animC < 2*3){A = 18;}
			else if(animC < 2*4){A = 19;}
			else if((animC-8)%(4*4) < 4*1){A = 20;}
			else if((animC-8)%(4*4) < 4*2){A = 21;}
			else if((animC-8)%(4*4) < 4*3){A = 22;}
			else if((animC-8)%(4*4) < 4*4){A = 23;}
			
			animA = 0;
			animB = 0;
			}
		
		animA++;
		animB++;
		animC++;
		
		Blink;
		
		ObjSprite2D_SetSourceRect(playerObj, x*(A%8), 1+trun(A/8)*y, x*(A%8)+x, 1+trun(A/8)*y+y);
		ObjRender_SetPosition(playerObj,GetPlayerX,GetPlayerY,0);
		ObjSprite2D_SetDestCenter(playerObj);
		
		ObjRender_SetAngleY(playerObj,angy);
		
		yield;
		}
		task Blink{
			if(GetPlayerInvincibilityFrame>0){
				if(Uber==0){ObjRender_SetColor(playerObj, 255, 255, 255);}
				if(Uber!=0){ObjRender_SetColor(playerObj, 32, 32, 32);}
				Uberblink+=1;
				if(Uberblink>1){Uberblink=0;Uber++;if(Uber>1){Uber=0}}
			}else{
				ObjRender_SetColor(playerObj, 255, 255, 255);
			}
		}
}

////////////////////////////////////////////////////////////////////
//-- Render --/-- Render --/-- Render --/-- Render --/-- Render --//
////////////////////////////////////////////////////////////////////