#include "CS_KanpaGrapplingHookBlade.h"
#include "CS_Kanpa.h"
#include "PeSmog.h"
#include "CS_PeSmoke.h"
CS_KanpaGrapplingHookBlade::CS_KanpaGrapplingHookBlade(std::shared_ptr<Entity> parent)
{
	type = 2;
	attackParent = parent;
	//初始动画设置
	ani.png = L"CS_CaveStoryMod";
	ani.sprite_index = L"am_fishhook_270";
	//启用碰撞
	cbRect.enableCbRect = true;
	cbRect.fullWaterBlock = true;
	//设置碰撞框
	cbRect.left = cbRect.right = cbRect.top = cbRect.bottom = 4;
	damage_reset_counter = 30;
	attackState_reset_counter_max = 30;
	el.name = L"fishhook";
}

void CS_KanpaGrapplingHookBlade::Init(std::weak_ptr<Entity> self)
{
	stateGravity.Init(shared_from_this());
	stateGravity.SetGravityScale(0.5f);
	Entity::Init(self);
}

void CS_KanpaGrapplingHookBlade::Step()
{
	auto wattackParent = attackParent.lock();
	if (!wattackParent)
		return;

	auto mychar = std::static_pointer_cast<CS_Kanpa>(wattackParent);
	if (!bHooked)
	{
		//钩还在向上抛的情况
		stateGravity.Step();
		//钩住目标，目标只能是墙体
		if (cbRect.cbTop && cbRect.movingPlatformTop.expired())
		{
			vx = 0;
			vy = 0;
			bHooked = true;
			//产生烟雾
			for (size_t i = 0; i < 3; i++)
			{
				float offset_x = (float)Random((int)-cbRect.left, (int)cbRect.right);
				float offset_y = (float)Random((int)-cbRect.top, (int)cbRect.bottom);
				auto e = std::make_shared<CS_PeSmoke>();
				e->x = x + offset_x;
				e->y = y + offset_y;
				gameFunc->SetEntity(shared_from_this(), e);
			}
			gameFunc->PlaySound(L"CS_012_block_destroy");
		}
		else if (vy > 0)
			Leave();
	}
	//如果离开挂钩状态
	if (!mychar->skillGrapplingHook.GetEnable())
		Leave();
	//清除最后一次碰撞结果
	Entity::ClearCollisionResult();
	//执行运动
	Entity::Step();
	//执行碰撞
	Entity::Collision();
	last_lineX = lineX;
	last_lineY = lineY;
	lineX = wattackParent->x - x;
	lineY = wattackParent->y - y;
}

void CS_KanpaGrapplingHookBlade::DeltaTime(float deltaTime)
{
	auto wattackParent = attackParent.lock();
	if (!wattackParent)
		return;

	float nowPlayerX = lerp(last_lineX, lineX, deltaTime);
	float nowPlayerY = lerp(last_lineY, lineY, deltaTime);
	AddFillLine(fillList, 0, 1, 4, RGBA(0xff, 0xff, 0xff, 0xff), nowPlayerX, nowPlayerY - 18, RGBA(0xff, 0xff, 0xff, 0xff), 1);
	Entity::DeltaTime(deltaTime);
}

void CS_KanpaGrapplingHookBlade::Leave()
{
	auto wattackParent = attackParent.lock();
	if (!wattackParent)
		return;

	auto mychar = std::static_pointer_cast<CS_Kanpa>(wattackParent);
	auto pe = std::make_shared<PeSmog>();
	pe->x = x;
	pe->y = y;
	gameFunc->SetEntity(shared_from_this(), pe);
	mychar->skillGrapplingHook.QuitHook();
	Destroy();
}
