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

#include "Materials/DiffuseOpacityMaterial.h"
#include "Materials/DiffuseAnimatedMaterial.h"

int Bush::m_Count = 0;
std::vector<UINT> Bush::m_MaterialIds = {};
PxMaterial* Bush::m_pPxMaterial{ nullptr };

Bush::Bush(const XMFLOAT3& startPos)
	: m_StartPos{ startPos }
{
	if (m_Count++ == 0)
	{
		m_pPxMaterial = PxGetPhysics().createMaterial(.5f, .5f, .5f);

		DiffuseOpacityMaterial* pMat = MaterialManager::Get()->CreateMaterial<DiffuseOpacityMaterial>();
		pMat->SetDiffuseTexture(L"Textures/Bomberman/Bush/sm011al2.png");
		pMat->SetOpacityTexture(L"Textures/Bomberman/Bush/sm011al2_opacity.png");
		pMat->SetIgnoreLightDirection(true);
		m_MaterialIds.push_back(pMat->GetMaterialId());

		pMat = MaterialManager::Get()->CreateMaterial<DiffuseOpacityMaterial>();
		pMat->SetDiffuseTexture(L"Textures/Bomberman/Bush/sm011al.png");
		pMat->SetOpacityTexture(L"Textures/Bomberman/Bush/sm011al_opacity.png");
		pMat->SetIgnoreLightDirection(true);
		pMat->SetTransparencyShadows(true);
		m_MaterialIds.push_back(pMat->GetMaterialId());
	}

	m_pAnimatedMaterial = MaterialManager::Get()->CreateMaterial<DiffuseAnimatedMaterial>();
	m_pAnimatedMaterial->SetDiffuseTexture(L"Textures/Bomberman/Bush/b900_transparent.png");
	m_pAnimatedMaterial->SetVariable_Scalar(L"gNrSprites", 1);
	m_pAnimatedMaterial->SetVariable_Scalar(L"gAnimSpeed", 1);

	m_ScaleDelay = MathHelper::randF(0, 2);
}

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

	m_pShadow = new GameObject();
	ModelComponent* pShadowModel = new ModelComponent(L"Meshes/Bomberman/Bush/Bush_Shadow.ovm", false);
	pShadowModel->SetMaterial(m_MaterialIds[0]);
	m_pShadow->AddComponent(pShadowModel);

	AddChild(m_pShadow);

	m_pFoliage = new GameObject();
	ModelComponent* pFoliageModel = new ModelComponent(L"Meshes/Bomberman/Bush/Bush.ovm", false);
	pFoliageModel->SetMaterial(m_MaterialIds[1]);
	m_pFoliage->AddComponent(pFoliageModel);

	AddChild(m_pFoliage);

	//Collider
	auto pActor = AddComponent(new RigidBodyComponent(true));
	const auto pPxConvexMesh = ContentManager::Load<PxConvexMesh>(L"Meshes/Bomberman/Tile_Collision.ovpc");
	pActor->AddCollider(PxConvexMeshGeometry(pPxConvexMesh, PxMeshScale({ 1.f, 1.f, 1.f })), *m_pPxMaterial);
}

void Bush::Explode()
{
	if (m_DidExplode)
		return;

	// Delete models
	RemoveChild(m_pShadow, true);
	RemoveChild(m_pFoliage, true);
	m_pShadow = nullptr;
	m_pFoliage = nullptr;

	// Explosion Smoke Particle System
	ParticleEmitterSettings settings{};
	settings.velocity = { 0.f,30.f,0.f };
	settings.minSize = 40.f;
	settings.maxSize = 40.f;
	settings.minEnergy = 0.55f;
	settings.maxEnergy = 0.55f;
	settings.minScale = 1.f;
	settings.maxScale = 1.f;
	settings.minEmitterRadius = 0.f;
	settings.maxEmitterRadius = 0.f;
	settings.color = { 1.f,1.f,1.f,.5f };

	GameObject* pExplosionContainer = new GameObject();
	AddChild(pExplosionContainer);
	pExplosionContainer->GetTransform()->Translate(0, 10.f, 0);
	pExplosionContainer->AddComponent(new ParticleEmitterComponent(L"Textures/Bomberman/Bush/s001.png", settings, 1));

	// Grass Blades Particle System
	settings.velocity = { -40.f,80.f,20.f };
	settings.minSize = 12.5f;
	settings.maxSize = 15.f;
	settings.minEnergy = 0.3f;
	settings.maxEnergy = 0.15f;
	settings.minScale = .5f;
	settings.maxScale = 1.0f;
	settings.minEmitterRadius = 0.f;
	settings.maxEmitterRadius = 1.f;
	settings.color = { 1.f,1.f,1.f,.75f };

	GameObject* pGrassParticlesContainer = new GameObject();
	AddChild(pGrassParticlesContainer);
	pGrassParticlesContainer->GetTransform()->Translate(-2.5f, 15.f, 0);
	pGrassParticlesContainer->AddComponent(new ParticleEmitterComponent(L"Textures/Bomberman/Bush/s101_centered.png", settings, 2));

	// Grass Blades Particle System
	settings.velocity = { 40.f,80.f,0.f };

	pGrassParticlesContainer = new GameObject();
	AddChild(pGrassParticlesContainer);
	pGrassParticlesContainer->GetTransform()->Translate(2.5f, 15.f, 0);
	pGrassParticlesContainer->AddComponent(new ParticleEmitterComponent(L"Textures/Bomberman/Bush/s101_centered.png", settings, 2));
	
	// Explosion base quad
	m_pExplosionQuad = new GameObject();
	AddChild(m_pExplosionQuad);
	ModelComponent* pExplosionQuadModel = new ModelComponent(L"Meshes/Bomberman/Bush/BushExplosion_Base.ovm", false);
	pExplosionQuadModel->SetMaterial(m_pAnimatedMaterial);
	m_pExplosionQuad->AddComponent(pExplosionQuadModel);

	m_DidExplode = true;
	//GetScene()->RemoveChild(this, true);
}

void Bush::DrawImGui()
{

}

void Bush::Update(const SceneContext& sceneContext)
{
	if (m_DidExplode)
	{
		if (m_ExplosionTime > 0)
		{
			const float elapsedSec = sceneContext.pGameTime->GetElapsed();
			m_ExplosionTime -= elapsedSec;

			//m_pExplosionQuad->GetTransform()->Scale(1 / 1.5f + m_ExplosionTime / 1.5f, 1, 1 / 1.5f + m_ExplosionTime / 1.5f);
			m_pAnimatedMaterial->SetVariable_Scalar(L"gOpacityAmount", m_ExplosionTime * 2);

			if (m_ExplosionTime < 0.25f)
			{
				m_pAnimatedMaterial->SetVariable_Scalar(L"gOpacityAmount", m_ExplosionTime * 4);
			}
		}
		else
		{
			GetScene()->LateRemoveChild(this);
		}
	}
	else if (m_pShadow != nullptr && m_pFoliage != nullptr)
	{
		const float totalSec = sceneContext.pGameTime->GetTotal() + m_ScaleDelay;

		m_OffsetY = sinf(totalSec * XM_PI * m_Frequency) * m_Amplitude;
		//std::cout << "m_OffsetY = " << m_OffsetY << std::endl;

		auto fTransform = m_pFoliage->GetTransform();
		auto fScale = fTransform->GetScale();
		fTransform->Scale(fScale.x, (1 - m_Amplitude) + m_OffsetY, fScale.z);

		auto sTransform = m_pShadow->GetTransform();
		auto sScale = sTransform->GetScale();
		sTransform->Scale((1 - m_Amplitude / 2) + m_OffsetY / 2, sScale.y, (1 - m_Amplitude / 2) + m_OffsetY / 2);
	}
}