#include "CS_Enemy_SandDragonBlade.h"
#include "CS_PeHurt.h"
CS_Enemy_SandDragonBlade::CS_Enemy_SandDragonBlade(std::shared_ptr<Entity> parent)
{
	type = 2;
	attackParent = parent;
	lr = parent->lr;
	//启用碰撞
	cbRect.enableCbRect = true;
	cbRect.enableSoild = true;
	cbRect.left = cbRect.right = cbRect.bottom = cbRect.top = 4;
	//设置动画
	ani.png = L"CS_CaveStoryMod";
	ani.sprite_index = L"am_sanddragon";
	//特殊属性
	speedDir = true;
	action_timecounter = 180;
}

void CS_Enemy_SandDragonBlade::Step()
{
	//清除最后一次碰撞结果
	Entity::ClearCollisionResult();
	//执行运动
	Entity::Step();
	//设置墙体碰撞矩形
	SetCbRect_Collision();
	//执行碰撞
	Collision();
	//设置实体碰撞矩形
	SetCbRect_Entity();
	if (bDisappear)
	{
		ani.sprite_index = L"am_sanddragonshot_disappear";
		if (ani.image_index >= 2)
			Destroy();
	}
	else
	{
		//碰墙消失
		if (cbRect.enableSoild)
		{
			cbRect.enableSoild = false;
		}
		else if (cbRect.cbLeft || cbRect.cbRight || cbRect.cbTop || cbRect.cbBottom)
		{
			bDisappear = true;
			ani.sprite_index = L"am_sanddragonshot_disappear";
			ani.image_index = 0;
		}
		//消失
		if (action_timecounter > 0)
		{
			action_timecounter--;
			if (action_timecounter == 0)
			{
				bDisappear = true;
				ani.sprite_index = L"am_sanddragonshot_disappear";
				ani.image_index = 0;
			}
		}
	}
}

void CS_Enemy_SandDragonBlade::Attack(std::weak_ptr<Entity> dst, int* outDamage, float* outAngle, bool* outTakedown, int* outElemType, float* outShockForce, float* outHeavy)
{
	*outDamage = 2;
	auto wdst = dst.lock();
	if (wdst)
	{
		if (wdst->lr == 0)
		{
			*outAngle = 315;
		}
		else
		{
			*outAngle = 225;
		}
	}
	*outTakedown = true;
	*outElemType = 0;
	*outShockForce = 0x200 / ts;
	*outHeavy = 2;
}

void CS_Enemy_SandDragonBlade::Feedback(std::weak_ptr<Entity> target)
{
	bDisappear = true;
	ani.sprite_index = L"am_sanddragonshot_disappear";
	ani.image_index = 0;
}

void CS_Enemy_SandDragonBlade::Collision()
{
	float srcVx = vx;
	float srcVy = vy;
	Entity::Collision();
	vx = srcVx;
	vy = srcVy;
}

void CS_Enemy_SandDragonBlade::SetCbRect_Entity()
{
	cbRect.left = cbRect.right = cbRect.top = cbRect.bottom = 5;
}

void CS_Enemy_SandDragonBlade::SetCbRect_Collision()
{
	cbRect.left = cbRect.right = cbRect.top = cbRect.bottom = 1;
}

void SetCS_Enemy_SandDragonBlade(std::shared_ptr<Entity> parent, float x, float y, float vx, float vy)
{
	auto am = std::make_shared<CS_Enemy_SandDragonBlade>(parent);
	am->x = x;
	am->y = y;
	am->vx = vx;
	am->vy = vy;
	parent->gameFunc->SetEntity(parent, am);
}
