#include "CS_Enemy_CritterFlying.h"
#include "CS_PeSmoke.h"
#include "CS_Exp.h"

CS_Enemy_CritterFlying::CS_Enemy_CritterFlying()
{
	type = 2;
	ani.png = L"CS_CaveStoryMod";
	cbRect.enableCbRect = true;
	cbRect.enableSoild = true;
	cbRect.enableSelfSoild = true;
	cbRect.left = cbRect.right = 6;
	cbRect.top = 4;
	cbRect.bottom = 5;
	cbRect.fullWaterBlock = true;
	ani.hideLayerList.insert(0);
	ani.hideLayerList.insert(1);
	ani.hideLayerList.insert(3);
	ani.hideLayerList.insert(4);
	hp_max = hp = 5;
	dropExp = 3;
	damage_reset_counter = 60;
	attackState_reset_counter_max = 60;
	drawBeforeOtherEntity = true;
}

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

void CS_Enemy_CritterFlying::Step()
{
	//跟上移动平台
	Entity::BeginMovingPlatform();
	stateResist.apply_resistH = cbRect.cbBottom;
	stateResist.Step();
	stateSplash.Step();
	switch (act_no)
	{
	case 0:
		y += 0x600 / ts;
		act_no = 1;
		[[fallthrough]];
	case 1:
		if (gameFunc->GetPlayerDistance)
		{
			auto wplayer = gameFunc->GetPlayerDistance(gameFunc->game, x, y).lock();
			if (wplayer)
			{
				if (act_wait >= 8 && x - 0x10000 / ts < wplayer->x && x + 0x10000 / ts > wplayer->x && y - 0x10000 / ts < wplayer->y && y + 0x6000 / ts > wplayer->y)
				{
					lr = x > wplayer->x ? 0 : 1;
					ani_no = 1;
				}
				else
				{
					if (act_wait < 8)
						++act_wait;
					ani_no = 0;
				}
				if (act_wait >= 8 && x - 0xC000 / ts < wplayer->x && x + 0xC000 / ts > wplayer->x && y - 0xC000 / ts < wplayer->y && y + 0x6000 / ts > wplayer->y)
				{
					act_no = 2;
					ani_no = 0;
					act_wait = 0;
				}
			}
		}
		if (shock_counter > 0)
		{
			act_no = 2;
			ani_no = 0;
			act_wait = 0;
		}
		break;
	case 2:
		if (++act_wait > 8)
		{
			act_no = 3;
			ani_no = 2;
			vy = -1228 / ts;
			gameFunc->PlaySound(L"CS_030_critter_jump");
			if (gameFunc->GetPlayerDistance)
			{
				auto wplayer = gameFunc->GetPlayerDistance(gameFunc->game, x, y).lock();
				if (wplayer)
				{
					lr = x > wplayer->x ? 0 : 1;
				}
			}
			vx = lr == 0 ? -0x100 / ts: 0x100 / ts;
		}
		break;
	case 3:
		if (vy > 0x100 / ts)
		{
			tgt_y = y;
			act_no = 4;
			ani_no = 3;
			act_wait = 0;
			act_wait = 0;	// Pixel duplicated this line
		}
		break;
	case 4:
		if (gameFunc->GetPlayerDistance)
		{
			auto wplayer = gameFunc->GetPlayerDistance(gameFunc->game, x, y).lock();
			if (wplayer)
			{
				lr = x > wplayer->x ? 0 : 1;
			}
		}
		++act_wait;
		if (cbRect.cbLeft || cbRect.cbRight || cbRect.cbTop || act_wait > 100)
		{
			damage = 3;
			act_no = 5;
			ani_no = 2;
			vx /= 2;
		}
		else
		{
			if (act_wait % 4 == 1)
				gameFunc->PlaySound(L"CS_109_critter_fly");
			if (cbRect.cbBottom)
				vy = -0x200 / ts;
			if (++ani_wait > 0)
			{
				ani_wait = 0;
				++ani_no;
			}
			if (ani_no > 5)
				ani_no = 3;
		}
		break;
	case 5:
		if (cbRect.cbBottom)
		{
			damage = 2;
			vx = 0;
			act_wait = 0;
			ani_no = 0;
			act_no = 1;
			gameFunc->PlaySound(L"CS_023_thud");
		}
		break;
	}
	if (act_no != 4)
	{
		vy += 0x40 / ts;
		if (vy > 0x5FF / ts)
			vy = 0x5FF / ts;
	}
	else
	{
		if (gameFunc->GetPlayerDistance)
		{
			auto wplayer = gameFunc->GetPlayerDistance(gameFunc->game, x, y).lock();
			if (wplayer)
			{
				if (x < wplayer->x)
					vx += 0x20 / ts;
				else
					vx -= 0x20 / ts;
			}
		}
		if (y > tgt_y)
			vy -= 0x10 / ts;
		else
			vy += 0x10 / ts;
		if (vy > 0x200 / ts)
			vy = 0x200 / ts;
		if (vy < -0x200 / ts)
			vy = -0x200 / ts;
		if (vx > 0x200 / ts)
			vx = 0x200 / ts;
		if (vx < -0x200 / ts)
			vx = -0x200 / ts;
	}
	//跟上移动平台
	Entity::EndMovingPlatform();
	//清除最后一次碰撞结果
	Entity::ClearCollisionResult();
	//执行运动
	Entity::Step();
	//执行碰撞
	Entity::Collision();
	//维持在斜坡上
	Entity::KeepOnSlopes();
	switch (ani_no)
	{
	case 0:
	case 1:
		ani.sprite_index = lr == 0 ? L"ls_critter_stand_l" : L"ls_critter_stand_r";
		ani.image_index = ani_no;
		break;
	case 2:
		ani.sprite_index = lr == 0 ? L"ls_critter_jump_l" : L"ls_critter_jump_r";
		ani.image_index = 0;
		break;
	case 3:
	case 4:
	case 5:
		ani.sprite_index = lr == 0 ? L"ls_critter_fly_l" : L"ls_critter_fly_r";
		ani.image_index = ani_no - 3;
		break;
	}
}

void CS_Enemy_CritterFlying::SetState(int stateID, int lr)
{
	this->lr = lr;
	ani.sprite_index = lr == 0 ? L"ls_critter_stand_l" : L"ls_critter_stand_r";
}

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

bool CS_Enemy_CritterFlying::Hurt(std::weak_ptr<Entity> src, int getDamage, float angle, bool bTakedown, int elemType, float shockForce, float heavy, int* outRealDamage)
{
	if (getDamage == 0)
		return false;
	*outRealDamage = getDamage;
	shock_counter = 30;
	hp -= getDamage;
	if (hp > 0)
	{
		gameFunc->PlaySound(L"CS_051_enemy_hurt");
	}
	else
	{
		gameFunc->PlaySound(L"CS_071_little_crash");
		//创建云团
		SetCS_PeSmokeMedium(shared_from_this(), x, y);
		hp = 0;
		//掉落物品
		SetDropItem(shared_from_this(), x, y, dropExp);
		Destroy();
	}
	//伤害冲击处理
	if (shockForce > 0)
	{
		//被攻击弹反
		float radian = angle * deg2rad;
		vx = shockForce * cosf(radian);
		vy = shockForce * sinf(radian);
		vy -= 1;
	}
	//显示伤害值
	if (gameFunc->dmgNum)
		gameFunc->SetDmgNum(gameFunc->dmgNum, getDamage, x, y - 16, 0, shared_from_this());
	return true;
}

std::shared_ptr<Entity> CreateCS_Enemy_CritterFlying()
{
	return std::make_shared<CS_Enemy_CritterFlying>();
}
