#include "CS_TeleporterLargeHead.h"
#include "CS_TeleporterLargeHeadHotWind.h"
CS_TeleporterLargeHead::CS_TeleporterLargeHead()
{
	ani.png = L"CS_CaveStoryMod";
	ani.sprite_index = L"ls_teleporter_large_head";
	drawBeforeOtherEntity = true;
	cbRect.enableCbRect = true;
	cbRect.enableSoild = true;
	cbRect.enableSelfSoild = true;
	cbRect.enableMovePlatform = true;
	//设置矩形框
	cbRect.top = 8;
	cbRect.bottom = 0;
	cbRect.left = cbRect.right = 16;
	//图层显示
	ani.hideLayerList.insert({ 0,1,2,3,4,5,6,7,8,9,10,11 });
}

CS_TeleporterLargeHead::~CS_TeleporterLargeHead()
{
	if (soundID != -1)
	{
		gameFunc->StopLoopSound(soundID);
	}
}

void CS_TeleporterLargeHead::Init(std::weak_ptr<Entity> self)
{
	stateGravity.Init(shared_from_this());
	stateSplash.Init(shared_from_this());
	srcY = y;
	Entity::Init(self);
}

void CS_TeleporterLargeHead::Step()
{
	stateGravity.SetPhysice(cbRect.inWater);
	stateSplash.SetPhysice(cbRect.inWater);
	switch (stateID)
	{
	case 0://关机状态
		[[fallthrough]];
	case 1://待机状态 蓝灯（没有可传送地点）
		stateGravity.Step();
		stateSplash.Step();
		break;
	case 2://开机状态 并悬浮顶盖 红灯（有可传送地点）

		break;
	case 3://特殊功能 并悬浮顶盖 橙灯
		SetHotWind();
		break;
	}
	//清除最后一次碰撞结果
	Entity::ClearCollisionResult();
	//执行运动
	Entity::Step();
	//执行碰撞
	Entity::Collision();
}

void CS_TeleporterLargeHead::SetState(int stateID, int lr)
{
	this->lr = lr;
	if (lr != 0)
		stateID = 2;
	this->stateID = stateID;
	switch (stateID)
	{
	case 0://关机状态
		ani.hideLayerList.insert({ 0,1,2,3,4,5,6,7,8,9,10,11 });
		break;
	case 1://待机状态 蓝灯（没有可传送地点）
		ani.hideLayerList.insert({ 0,1,2,3,8,9,10,11 });
		ani.hideLayerList.erase(4);
		ani.hideLayerList.erase(5);
		ani.hideLayerList.erase(6);
		ani.hideLayerList.erase(7);
		break;
	case 2://开机状态 并悬浮顶盖 红灯（有可传送地点）
		ani.hideLayerList.insert({ 4,5,6,7,8,9,10,11 });
		ani.hideLayerList.erase(0);
		ani.hideLayerList.erase(1);
		ani.hideLayerList.erase(2);
		ani.hideLayerList.erase(3);
		vy = 0;
		y -= 64;
		break;
	case 3://特殊功能 并悬浮顶盖 橙灯
		ani.hideLayerList.insert({ 0,1,2,3,4,5,6,7 });
		ani.hideLayerList.erase(8);
		ani.hideLayerList.erase(9);
		ani.hideLayerList.erase(10);
		ani.hideLayerList.erase(11);
		vy = 0;
		y -= 96;
		soundID = gameFunc->PlayLoopSound(L"CS_dry");
		break;
	}
}

void CS_TeleporterLargeHead::SetState(std::wstring stateName, int lr)
{
	if (stateName == L"off")
		SetState(0, lr);
	else if(stateName == L"empty")
		SetState(1, lr);
	else if (stateName == L"on")
		SetState(2, lr);
	else if (stateName == L"special")
		SetState(3, lr);
}

void CS_TeleporterLargeHead::SetHotWind()
{
	auto pe = std::make_shared<CS_TeleporterLargeHeadHotWind>();
	pe->y = srcY - 16;
	pe->x = x + (float)Random((int)-cbRect.left, (int)cbRect.right);
	gameFunc->SetEntity(shared_from_this(), pe);
}

std::shared_ptr<Entity> CreateCS_TeleporterLargeHead()
{
	return std::make_shared<CS_TeleporterLargeHead>();
}
