#TouhouDanmakufu[Player]
#ScriptVersion[3]
#ID["YaoSukune"]
#Title["YaoSukune"]
#ReplayName["YaoSukune"]
#Text["A duo that works like in RMI"]
#Image["./img/Player_02.png"]

#include "./Shot.txt"

let CSD = GetCurrentScriptDirectory;
let playerID = GetPlayerObjectID;
let playerPosDeath = [];

let hitboxActive = false;
let optionAlive = true;

let flash = 0;
let playerType = 0;

let centerX = GetStgFrameWidth/2;
let centerY = GetStgFrameHeight/2;
let soundLoad = [
	CreateSE(CSD~"se/se_plst00.wav"), //0
	CreateSE(CSD~"se/se_thrill.wav"),//1
	CreateSE(CSD~"se/se_bomb_oro02.wav"),//2
	CreateSE(CSD~"se/se_barrier.wav"),//3
	CreateSE(CSD~"se/se_hit.wav"),//4
	CreateSE(CSD~"se/se_spell.wav"),//5
	CreateSE(CSD~"se/se_flash.wav"),//6
	CreateSE(CSD~"se/se_charge.wav"),//7
];
function CreateSE(patfil){
	let obj = ObjSound_Create();
	ObjSound_Load(obj, patfil);
	ObjSound_SetSoundDivision(obj,SOUND_SE);
	ObjSound_SetVolumeRate(obj,50);
	return obj;
}
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(30);
	SetPlayerDownStateFrame(0);
	
	SetPlayerSpeed(4.5, 2.5); 
	SetPlayerClip(1,1,GetStgFrameWidth-1,GetStgFrameHeight-1);
	SetPlayerAutoItemCollectLine(GetStgFrameHeight/5*1.5);
	SetPlayerSpell(4);

	playerRender;
	
	shoot;
	option;
	FlashMeter;
}

@Event{
	alternative(GetEventType)
	
	case(EV_REQUEST_SPELL){
		if(IsPermitPlayerSpell && GetPlayerSpell >= 1  && (GetVirtualKeyState(VK_SLOWMOVE) != KEY_FREE  || GetPlayerState == STATE_HIT)){
			if(playerType == 0){playerType = 1;SpellSukune;}
			else{playerType = 0;SpellOrochin;}
			SetPlayerSpell(GetPlayerSpell-1);
			SetScriptResult(true);
		}
		else if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_FREE && flash == 50){
			if(playerType == 0){playerType = 1;FlashSukune;}
			else{playerType = 0;FlashOrochin;}
			flash = 0;
			SetScriptResult(false);
		}
		else{SetScriptResult(false);}
	}
		
	case(EV_GRAZE){
		ObjSound_Play(soundLoad[1]);
	}
		
	case(EV_HIT){
		ObjSound_Play(soundLoad[4]);
	}
		
	case(EV_PLAYER_SHOOTDOWN){
		playerPosDeath = [GetPlayerX,GetPlayerY];
		ObjSound_Play(soundLoad[3]);
		TExplosionA1(GetPlayerX, GetPlayerY, 128);
		if(GetPlayerSpell < 4){SetPlayerSpell(4);}
	}
		
	case(EV_PLAYER_REBIRTH){
		playerType = (playerType+1)%2;
		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%2 == 0 && playerType == 0){
				ObjSound_Play(soundLoad[0]);
				ShotBaseAOrochin();
			}
			if(count%2 == 0 && playerType == 1){
				ObjSound_Play(soundLoad[0]);
				ShotBaseASukune(GetPlayerX+8,GetPlayerY+8,0);ShotBaseASukune(GetPlayerX-8,GetPlayerY+8,0);
			}
		}
		
		count++;

		yield;
	}
}

task option{
	
	let x1 = [-60,-30,30,60];
	let	y1 = [-40,-20,-20,-40];
	
	let x2 = [-50,-20,20,50];
	let	y2 = [20,-20,-20,20];

	let ang = [30,15,-15,-30];
	
	ascent(a in 0..4){
		Fam([x1[a],x2[a]],[y1[a],y2[a]],[ang[a]]);
	}
}

task Fam(lx,ly,data){
	let obj0 = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj0, 0.46);
	ObjPrim_SetTexture(obj0, CSD ~ "img/shot.png");
	ObjSprite2D_SetSourceRect(obj0, 146,139,157,174);
	ObjSprite2D_SetDestCenter(obj0);

	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj, 0.46);
	ObjPrim_SetTexture(obj, CSD ~ "img/shot.png");
	ObjSprite2D_SetSourceRect(obj, 112,139,127,157);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetAngleZ(obj,data[0]);
	
	let count = 0;
	
	Pos(GetPlayerX,GetPlayerY);
	let xa;
	let ya;

	while(optionAlive){
		
		if(playerType == 0){ObjRender_SetAlpha(obj,0);ObjRender_SetAlpha(obj0,0);}
		else{ObjRender_SetAlpha(obj,255);ObjRender_SetAlpha(obj0,255);}

		if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_FREE){
			xa = GetPlayerX+lx[0];
			ya = GetPlayerY+ly[0];
		}
			
		else{
			xa = GetPlayerX+lx[1];
			ya = GetPlayerY+ly[1];
		}
		
		if(GetVirtualKeyState(VK_SHOT)!=KEY_FREE && IsPermitPlayerShot && !IsPlayerSpellActive && playerType == 1){
			if(count%2 == 0){
				ShotBaseASukune(ObjRender_GetX(obj0), ObjRender_GetY(obj0),data[0]);
			}
		}

		Pos(ObjRender_GetX(obj) + (xa - ObjRender_GetX(obj)) / 3, ObjRender_GetY(obj) + (ya - ObjRender_GetY(obj)) / 3);
		
		count++;
		
		yield;
	}

	task Pos(x,y){
		ObjRender_SetPosition(obj, x, y, 0);
		ObjRender_SetPosition(obj0, x, y, 0);
		ObjRender_SetAngleZ(obj0,count*10);
	}
	
	Obj_Delete(obj);
	Obj_Delete(obj0);
}


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

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

task playerRender{
	let playerObj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(playerObj, CSD ~ "img/player_snek.png");
	
	let animA = 0;
	let animB = 0;
	let animC = 0;
	
	let Uber = 0;
	let Uberblink = 0;
	
	let A = 0;
	let x = 35;
	let y = 56;
	let angy = 0;
	let ena_y = true;
	
	Obj_SetRenderPriority(playerObj, 0.45);
	
	loop{

		if(playerType == 0){
			ObjPrim_SetTexture(playerObj, CSD ~ "img/player_snek.png");
			x = 35;
			y = 56;
		}
		else{
			ObjPrim_SetTexture(playerObj, CSD ~ "img/player_sukune.png");
			x = 30;
			y = 50;
		}

		if(GetPlayerState!=STATE_DOWN && GetPlayerState!=STATE_END){ObjRender_SetAlpha(playerObj, 255);}
		else{ObjRender_SetAlpha(playerObj, 0);}
		
		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 --//
////////////////////////////////////////////////////////////////////