#include "CS_CurlyMachineGun.h"
#include "CS_Curly.h"
#include "CS_PeOutOfAmmo.h"
#include "CS_CurlyMachineGunBlade.h"
const int CS_CurlySTATENAME_MACHINEGUN = GetNextEntityStateID();
CS_CurlyMachineGun::CS_CurlyMachineGun()
{
	stateID = CS_CurlySTATENAME_MACHINEGUN;
	cd_counter = 0;
	cd_counter_max = 7;
	iconIndex = 0;
}

const wchar_t* CS_CurlyMachineGun::GetSaveName()
{
	return L"MachineGun";
}

const wchar_t* CS_CurlyMachineGun::GetFuncName()
{
	return L"CurlySkill_MachineGun";
}

const wchar_t* CS_CurlyMachineGun::GetCheckFuncName()
{
	return L"CurlySkill_MachineGun_Check";
}

void CS_CurlyMachineGun::SubStep()
{
	auto we = e.lock();
	if (!we)
		return;

	auto mychar = std::static_pointer_cast<CS_Curly>(we);
	bUIShowDisableSkill = !(
		mychar->lastGun != this &&
		!lock
		);
	bool a1 = mychar->lastGun != this &&
		!lock;
	bool b1 = (mychar->input.key & keyMask);
	bool c1 = mychar->mp >= mp_cost;

	bool a = a1;
	bool b = b1;
	bool c = c1;
	if (a && b && c)
	{
		mychar->lastSkill = this;
		mychar->ChangeState(GetStateID());
	}
	//如果玩家有机枪转轮模块，可以使用格林机枪
	openGatling = false;
	if (we->gameFunc->PlayerHaveItem)
		if (we->gameFunc->PlayerHaveItem(we->gameFunc->game, L"MachinegunWheelModule"))
			openGatling = true;
	//技能冷却时间
	if (cd_counter > 0)
		cd_counter--;
	//子弹条
	fcounter = (float)ammo_count / ammo_count_max;
	//子弹耗尽效果时间
	if (outOfAmmo_counter > 0)
		outOfAmmo_counter--;
	//子弹恢复
	if (!we->input.GetAtk())
	{
		if (shootingToRefill_counter > 0)
			shootingToRefill_counter--;
		if (shootingToRefill_counter == 0)
		{
			ammoRefill_counter_max = 2;
			if (ammoRefill_counter > 0)
				ammoRefill_counter--;
			if (ammoRefill_counter == 0 && ammo_count < ammo_count_max)
			{
				ammo_count++;
				ammoRefill_counter = ammoRefill_counter_max;
			}
		}
	}
	if (ammo_count < ammo_count_max)
	{
		if (ammoBar.expired())
		{
			auto wammoBar = std::make_shared<PeReadyBar>(mychar, &fcounter, RGBA(0xff, 0xff, 0xff, 0xff), 0, -16,
				&CS_CurlyMachineGun::ReadyBar_Leave,
				&CS_CurlyMachineGun::ReadyBar_Destroy);
			ammoBar = wammoBar;
			we->gameFunc->SetEntity(we, wammoBar);
		}
	}
	EntitySkillState::SubStep();
}

void CalculateShotgunAngles(std::vector<float>& angles, int num, float aveAngle, float centerAngle)
{
	angles.clear();

	if (num <= 0)
		return;

	// 计算总散布角度范围
	float totalSpread = aveAngle * (num - 1);

	// 计算起始角度(最左侧子弹的角度)
	float startAngle = centerAngle - totalSpread / 2.0f;

	// 生成每个子弹的角度
	for (int i = 0; i < num; ++i)
	{
		float angle = startAngle + i * aveAngle;
		angles.push_back(angle);
	}
}

void CS_CurlyMachineGun::ChangeStateMsg(int stateID)
{
	auto we = e.lock();
	if (!we)
		return;
	auto mychar = std::static_pointer_cast<CS_Curly>(we);

	if (stateID == GetStateID())
	{
		//初始化技能
		enable = true;
		//切换到这把枪
		mychar->lastGun = this;
		we->gameFunc->PlaySound(L"CS_004_switchweapon");
	}
	else if (stateID == CS_CurlySTATENAME_FIRE && cd_counter == 0 &&
		mychar->lastGun == this)
	{
		enable = false;
		//消耗子弹
		if (ammo_count > 0)
		{
			ammo_count--;
			shootingToRefill_counter = shootingToRefill_counter_max;
			//开火
			if (openGatling)
			{
				int num = 5;
				float aveAngle = 5;
				float centerAngle = 0;
				//散射
				if (we->ud == 0)
					centerAngle = we->lr == 0 ? 180.0f : 0.0f;
				else
					centerAngle = we->ud == 1 ? 90.0f : 270.0f;
				centerAngle += Random(-1, 1);
				CalculateShotgunAngles(angleBuffer, num, aveAngle, centerAngle);
				for (int i = 0; i < num; i++)
				{
					auto am = std::make_shared<CS_CurlyMachineGunBlade>(we);
					float speed = 0x1000 / ts;
					speed += Random(-0x300, 0x300) / ts;
					am->vx = speed * cosf(angleBuffer[i] * deg2rad);
					am->vy = speed * sinf(angleBuffer[i] * deg2rad);
					we->gameFunc->SetEntity(we, am);
				}
				//锁住按键
				cd_counter = cd_counter_max;
				//后坐力
				if (we->ud == 1)
				{
					if (we->vy > 0)
						we->vy /= 2.0f;
					if (we->vy - 2 > -4)
						we->vy -= 2;
					else
						we->vy = -4;
				}
				else if (we->ud == -1)
				{
					if (we->vy + 1.0f < 4)
						we->vy += 1.0f;
					else
						we->vy = 4;
				}
			}
			else
			{
				auto am = std::make_shared<CS_CurlyMachineGunBlade>(we);
				we->gameFunc->SetEntity(we, am);
				//锁住按键
				cd_counter = cd_counter_max;
				//后坐力
				if (we->ud == 1)
				{
					if (we->vy > 0)
						we->vy /= 2.0f;
					if (we->vy - 1 > -2)
						we->vy--;
					else
						we->vy = -2;
				}
				else if (we->ud == -1)
				{
					if (we->vy + 0.5f < 2)
						we->vy += 0.5f;
					else
						we->vy = 2;
				}
			}
		}
		else
		{
			if (outOfAmmo_counter == 0)
			{
				SetCS_PeOutOfAmmo(we, we->x, we->y);
				outOfAmmo_counter = outOfAmmo_counter_max;
				we->gameFunc->PlaySound(L"CS_037_gun_click");
			}
		}
	}
	else
		enable = false;
}

bool CS_CurlyMachineGun::ReadyBar_Leave(std::weak_ptr<Entity> e)
{
	auto we = e.lock();
	if (!we)
		return true;

	auto mychar = std::static_pointer_cast<CS_Curly>(we);
	auto& skillMachineGun = mychar->skillMachineGun;
	bool bLeave = skillMachineGun.ammo_count == skillMachineGun.ammo_count_max;
	if (bLeave)
		skillMachineGun.ammoBar.reset();
	return bLeave;
}

void CS_CurlyMachineGun::ReadyBar_Destroy(std::weak_ptr<Entity> e)
{
	auto we = e.lock();
	if (!we)
		return;

	auto mychar = std::static_pointer_cast<CS_Curly>(we);
	auto& skillMachineGun = mychar->skillMachineGun;
	skillMachineGun.ammoBar.reset();
}
