#TouhouDanmakufu[Player]
#ScriptVersion[3]
#ID["Reimu_ultimadragon"]
#Title["ultimadragon Reimu"]
#Text["A Homing/Needle fusion player"]
#Image["./img_Cutin2.png"]
#ReplayName["Reimu_ultimadragon"]	

#include"./sfx.txt"
#include"./ReimuBombs.txt"

let Popularity=0;
let objPlayer = GetPlayerObjectID();
let csd = GetCurrentScriptDirectory;
let Hyper=0;
let objtext = ObjText_Create();
let CanShoot=true;


@Initialize{

	let path = csd ~ "shots_random.txt";
	LoadPlayerShotData(path);

	SetPlayerLife(3);
	SetPlayerSpell(2);

	ObjPlayer_AddIntersectionCircleA1(objPlayer, 0, 0, 0, 20);
	SetPlayerSpeed(3.4, 1.6);
	SetPlayerRebirthFrame(16);
	SetPlayerRebirthLossFrame(0);
	SetPlayerDownStateFrame(45);
	


	THitbox;
	TImage;
	TShot;
	MainShot;
	}
	
@MainLoop
{
	yield;
}

@Finalize
{
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_SPELL)
	{
		//スペルカード要求
		let spell = GetPlayerSpell();//残りスペル数
		if(spell >= 1){
			
			Bomb;
			SetScriptResult(true);//スペル発動可能
			SetPlayerSpell(spell-1);//スペル数を1減らす

			}

		else
		{
			SetScriptResult(false);//スペル発動不可
		}
	}
	case(EV_HIT)
	{
		TExplosion();
		Pichuun;
	}
	case(EV_PLAYER_REBIRTH)
	{
		//復帰
		SetPlayerInvincibilityFrame(180);
		SetPlayerSpell(2);
		MCircle(175);
	}
	case(EV_GET_ITEM)
	{
		//アイテム取得通知
		let itemType = GetEventArgument(0);
		alternative(itemType)
		case(ITEM_SPELL)
		{
			//WriteLog("ITEM_SPELL");
		}
		case(ITEM_SPELL_S)
		{
			//WriteLog("ITEM_SPELL_S");
		}
		case(ITEM_POWER)
		{
			//WriteLog("ITEM_POWER");
		}
		case(ITEM_POWER_S)
		{
			//WriteLog("ITEM_POWER_S");
		}
	}
	
	case(EV_GRAZE){
		Graze;
		}
	}
	
task THitbox{
	let angle = 0;
	let scale = 0;
	let hitbox = ObjPrim_Create(OBJ_SPRITE_2D);
	let imgHitbox = GetCurrentScriptDirectory ~ "eff_base.png";
	ObjPrim_SetTexture(hitbox, imgHitbox);
	ObjRender_SetBlendType(hitbox,BLEND_ALPHA);
	ObjSprite2D_SetSourceRect(hitbox,0,16,64,79);//69,507
	ObjSprite2D_SetDestCenter(hitbox);
	
	loop{
		if (GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH){
			scale=0;
			angle=0;
			}
		if (GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD && scale < 100){
			scale+=10;
			}
		if (GetVirtualKeyState(VK_SLOWMOVE) == KEY_PULL || GetVirtualKeyState(VK_SLOWMOVE) == KEY_FREE && scale > 0){
			scale-=10;
			}
			
		ObjRender_SetPosition(hitbox,GetPlayerX(),GetPlayerY(),0);
		ObjRender_SetAngleZ(hitbox,angle);
		ObjRender_SetScaleXYZ(hitbox,scale*0.01,scale*0.01,1);
		angle+=2;
		yield;
		}
	}
	
task TShot
{
	
	let shotCount = -1;//
	let bNextShot = false;//
	let objShot = ID_INVALID;//

	loop
	{
	if(CanShoot==true){
		if((GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD || bNextShot==true) && shotCount==-1)
		{
			shotCount = 0;
			bNextShot = false;
		}
		if(GetVirtualKeyState(VK_SHOT)==KEY_HOLD && shotCount>0)
		{
			bNextShot = true;
		}
		if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD)
		{	//????????

			if(shotCount%4 == 0){
				HomingShot(GetPlayerX()-10, GetPlayerY()-20, 10, 270, 1.5, 1, 37);
				HomingShot(GetPlayerX()+10, GetPlayerY()-20, 10, 270, 1.5, 1, 37);
			
				HomingShot(GetPlayerX()-10, GetPlayerY()-20, 10, 270, 0, 1, 37);
				HomingShot(GetPlayerX()+10, GetPlayerY()-20, 10, 270, 0, 1, 37);
			
				HomingShot(GetPlayerX()-19, GetPlayerY()-16, 10, 270, 1.5, 1, 37);
				HomingShot(GetPlayerX()+19, GetPlayerY()-16, 10, 270, 1.5, 1, 37);
			
				HomingShot(GetPlayerX()-19, GetPlayerY()-16, 10, 270, 0, 1, 37);
				HomingShot(GetPlayerX()+19, GetPlayerY()-16, 10, 270, 0, 1, 37);
			}


		}
		else
		{
			if(shotCount%5 == 0)
			{	
			
			CreatePlayerShotA1(GetPlayerX()-34, GetPlayerY()-5, 25, 270, 1.5, 1.5, 20);
			CreatePlayerShotA1(GetPlayerX()-28, GetPlayerY()-5, 25, 270, 1.5, 1.5, 20);	
			CreatePlayerShotA1(GetPlayerX()+34, GetPlayerY()-5, 25, 270, 0, 1.5, 20);
			CreatePlayerShotA1(GetPlayerX()+28, GetPlayerY()-5, 25, 270, 0, 1.5, 20);	

		
			CreatePlayerShotA1(GetPlayerX()-16, GetPlayerY()-15, 25, 270, 1.5, 1.5, 20);
			CreatePlayerShotA1(GetPlayerX()-10, GetPlayerY()-15, 25, 270, 1.5, 1.5, 20);	
			CreatePlayerShotA1(GetPlayerX()+16, GetPlayerY()-15, 25, 270, 0, 1.5, 20);
			CreatePlayerShotA1(GetPlayerX()+10, GetPlayerY()-15, 25, 270, 0, 1.5, 20);
			}		
		}

		if(shotCount >= 0)
		{
			shotCount++;
		}
		if(shotCount == 30)
		{
			shotCount=-1;
		}
		}
		yield;
	
	}
	}
	
task TImage {

	let path = csd ~ "pl00.png";
	ObjPrim_SetTexture(objPlayer, path);
	loop
	{
		if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD)
		{
			ObjSprite2D_SetSourceRect(objPlayer, 0+32*6, 48, 32*7, 48*2);
			}
		else if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD)
		{
			ObjSprite2D_SetSourceRect(objPlayer, 0+32*6, 48*2, 32*7, 48*3);
		}
		else
		{
			
			ObjSprite2D_SetSourceRect(objPlayer, 0, 0, 32, 48);
		}
		ObjSprite2D_SetDestCenter(objPlayer);
		yield;
	}
	}
task HomingShot(X, Y, speed, angle, damage, penetration, graphic){
	let obj = CreatePlayerShotA1(X, Y, speed, angle, damage, penetration, graphic);
	ObjRender_SetScaleXYZ(obj, 1, 0.8, NULL);
	let ENEMY = false;
		
	while(!Obj_IsDeleted(obj)){

		let sx = ObjMove_GetX(obj);
		let sy = ObjMove_GetY(obj);
		let tx = X;
		let ty = Y;
		let pos = GetEnemyIntersectionPosition(sx, sy, 1);
		
		if(length(pos) > 0)
		{
			tx = pos[0][0];
			ty = pos[0][1];
			ENEMY = true;
			}
		else
        {
            ENEMY = false;
        }
		let toAngle=atan2(ty - sy, tx - sx);
		
		if(ENEMY==true){

			ObjMove_SetAngle(obj, toAngle);
			}
			
		else{
			ObjMove_SetAngle(obj, angle);
			}
		

		yield;
		}
	}
	
task TExplosion
{
	let path = GetCurrentScriptDirectory() ~ "Default_Player_Rumia.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	Obj_SetRenderPriority(obj, 0.60);
	ObjPrim_SetTexture(obj, path);
	ObjSprite2D_SetSourceRect(obj, 192, 0, 255, 63);
	ObjSprite2D_SetDestRect(obj, -32, -32, 32, 32);

	let alpha = 255;
	let scale = 0;
	loop(30)
	{
		scale += 0.2;
		alpha -= 8;
		ObjRender_SetColor(obj, alpha, alpha, alpha);
		ObjRender_SetPosition(obj, GetPlayerX, GetPlayerY, 0);
		ObjRender_SetScaleXYZ(obj, scale, scale, 1);
		yield;
	}
	Obj_Delete(obj);
}

task MainShot
{
	
	let shotCount = -1;//
	let bNextShot = false;//
	let objShot = ID_INVALID;//

	loop
	{
	if(CanShoot==true){
		if((GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD || bNextShot==true) && shotCount==-1)
		{
			shotCount = 0;
			bNextShot = false;
		}
		if(GetVirtualKeyState(VK_SHOT)==KEY_HOLD && shotCount>0)
		{
			bNextShot = true;
		}

			if(shotCount%4 == 0){
					let obj1 = CreatePlayerShotA1(GetPlayerX()-8, GetPlayerY()-8, 16, 270, 2, 1.2, 30);
					let obj2 = CreatePlayerShotA1(GetPlayerX()+5, GetPlayerY()-8, 16, 270, 2, 1.2, 30);
					
					ObjRender_SetScaleXYZ(obj1, 0.75, 0.75, NULL);
					ObjRender_SetScaleXYZ(obj2, 0.75, 0.75, NULL);
					}

		if(shotCount >= 0)
		{
			shotCount++;
		}
		if(shotCount == 30)
		{
			shotCount=-1;
		}
		}
		yield;
	}
	}
	
