Bomberman-OverlordEngine-x64 / BombermanGame / Prefabs / Bomberman / Butterfly.cpp
Butterfly.cpp
Raw
#include "stdafx.h"
#include "Butterfly.h"

#include "Materials/DiffuseOpacityMaterial_Skinned.h"

int Butterfly::m_Count = 0;
std::vector<UINT> Butterfly::m_MaterialIds = {};

Butterfly::Butterfly(const XMFLOAT3& startPos, Butterfly::Type type, Butterfly::Direction direction)
	: m_StartPos{ startPos }
	, m_pButterflyGo{ new GameObject() }
	, m_MaterialId{ int(type) }
	, m_RotationSign{ int(direction) }
{
	if (m_Count++ == 0)
	{
		std::wstring pathBegin{ L"Textures/Bomberman/Butterfly/butterfly" };
		for (UINT i{ 0 }; i < UINT(Butterfly::Type::Count);)
		{
			DiffuseOpacityMaterial_Skinned* pMat = MaterialManager::Get()->CreateMaterial<DiffuseOpacityMaterial_Skinned>();
			pMat->SetIgnoreLightDirection(true);
			pMat->SetDiffuseTexture(pathBegin + std::to_wstring(++i) + L".png");
			pMat->SetOpacityTexture(pathBegin + std::to_wstring(i) + L"_opacity.png");
			pMat->SetTransparencyShadows(true);
			m_MaterialIds.push_back(pMat->GetMaterialId());
		}
	}

	if (m_MaterialId >= UINT(Butterfly::Type::Count))
	{
		m_MaterialId = rand() % int(Butterfly::Type::Count);
	}

	m_MaterialId = m_MaterialIds[m_MaterialId];
}

void Butterfly::DrawImGui()
{

}

void Butterfly::Initialize(const SceneContext&)
{
	GetTransform()->Translate(m_StartPos);

	AddChild(m_pButterflyGo);
	m_pButterflyGo->GetTransform()->Translate(m_Radius, 0, 0);
	m_pButterflyGo->GetTransform()->Scale(m_Size);

	if (m_RotationSign == int(Butterfly::Direction::Clockwise))
		m_pButterflyGo->GetTransform()->Rotate(0, 180, 0);

	ModelComponent* pModel = new ModelComponent(L"Meshes/Bomberman/Butterfly/Butterfly.ovm", true);
	pModel->SetMaterial(m_MaterialId);

	m_pButterflyGo->AddComponent(pModel);
	if (const auto pAnimator = pModel->GetAnimator())
	{
		pAnimator->SetAnimation(0);
		pAnimator->Play();
	}
}

void Butterfly::Update(const SceneContext& sceneContext)
{
	const float elapsedSec = sceneContext.pGameTime->GetElapsed();
	const float totalSec = sceneContext.pGameTime->GetTotal();

	m_RotationAngle += m_RotationSpeed * m_RotationSign * elapsedSec;
	MathHelper::ClampAround<float>(m_RotationAngle, 360.f, 0.f);

	GetTransform()->Rotate(0.f, m_RotationAngle, 0.f);

	m_OffsetY = sinf(totalSec * XM_PI * m_Frequency) * m_Amplitude;
	//std::cout << "m_OffsetY = " << m_OffsetY << std::endl;
	
	auto butterflyTransform = m_pButterflyGo->GetTransform();
	auto bfPos = butterflyTransform->GetPosition();
	butterflyTransform->Translate(bfPos.x, bfPos.y + m_OffsetY, bfPos.z);
}