#include "CS_SavePoint.h"

CS_SavePoint::CS_SavePoint()
{
	ani.png = L"CS_CaveStoryMod";
	ani.sprite_index = L"ls_savepoint";
	cbRect.enableCbRect = true;
	cbRect.enableSoild = true;
	cbRect.left = cbRect.right = 5;
	cbRect.top = 6;
	cbRect.bottom = 8;
	drawBeforeOtherEntity = true;
	hp = 1;
	//调起难度
	caughtDifficulty = 0.5f;
}

void CS_SavePoint::Init(std::weak_ptr<Entity> self)
{
	stateGravity.Init(shared_from_this());
	stateResist.Init(shared_from_this());
	stateSplash.Init(shared_from_this());
	Entity::Init(self);
}

void CS_SavePoint::Step()
{
	//跟上移动平台
	Entity::BeginMovingPlatform();
	stateGravity.SetPhysice(cbRect.inWater);
	stateResist.SetPhysice(cbRect.inWater);
	if (bCaught)
	{
		vx = vy = 0;
	}
	else
	{
		//在地上掉落
		stateGravity.Step();
	}
	if (cbRect.inWater)
		drawBeforeOtherEntity = false;
	else
		drawBeforeOtherEntity = true;
	if (flee_counter > 0)
		flee_counter--;
	if (cbRect.cbBottom)
		stateResist.Step();
	stateSplash.Step();
	//跟上移动平台
	Entity::EndMovingPlatform();
	//清除最后一次碰撞结果
	Entity::ClearCollisionResult();
	//执行运动
	Entity::Step();
	//执行碰撞
	Entity::Collision();
}

std::weak_ptr<Entity> CS_SavePoint::BeginCaught(std::weak_ptr<Entity> e)
{
	if (flee_counter > 0)
		return std::weak_ptr<Entity>();
	bCaught = true;
	return shared_from_this();
}

void CS_SavePoint::EndCaught(std::weak_ptr<Entity> e)
{
	bCaught = false;
	if (cbRect.inWater)
	{
		//四处逃串
		flee_counter = 120;
		//物品跳跃
		vy += Random(-0x600, -0x200) / ts;
		vx += Random(-0x300, 0x300) / ts;
	}
}

std::shared_ptr<Entity> CreateCS_SavePoint()
{
	return std::make_shared<CS_SavePoint>();
}
