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

#include "Player.h"
#include "Materials/DiffuseMaterial.h"
#include "Materials/DiffuseOpacityMaterial.h"

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

Crystal::Crystal(const XMFLOAT3& startPos, Crystal::Type type)
	: m_StartPos{ startPos }
	, m_pCrystalGo{ new GameObject() }
	, m_MaterialId{ int(type) }
	, m_Type{ type }
{
	if (m_Count++ == 0)
	{
		m_pPxMaterial = PxGetPhysics().createMaterial(.5f, .5f, .5f);

		std::wstring pathBegin{ L"Textures/Bomberman/Crystal/crystal" };
		for (UINT i{ 0 }; i < UINT(Crystal::Type::Count);)
		{
			DiffuseMaterial* pMat = MaterialManager::Get()->CreateMaterial<DiffuseMaterial>();
			pMat->SetDiffuseTexture(pathBegin + std::to_wstring(++i) + L".png");
			//pMat->SetOpacityTexture(pathBegin + std::to_wstring(i) + L"_opacity.png");
			m_MaterialIds.push_back(pMat->GetMaterialId());
		}

		auto pMat2 = MaterialManager::Get()->CreateMaterial<DiffuseOpacityMaterial>();
		pMat2->SetDiffuseTexture(L"Textures/Bomberman/Levels/shadow.png");
		pMat2->SetOpacityTexture(L"Textures/Bomberman/Levels/shadow_opacity.png");
		m_MaterialIds.push_back(pMat2->GetMaterialId());
	}

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

	m_MaterialId = m_MaterialIds[m_MaterialId];
}

void Crystal::DrawImGui()
{

}

/*
void Crystal::PickupCrystal(GameObject* pTriggerObject, GameObject* pOtherObject, PxTriggerAction action)
{

}
*/

void Crystal::Initialize(const SceneContext&)
{
	GetTransform()->Translate(m_StartPos);
	AddChild(m_pCrystalGo);

	ModelComponent* pModel = new ModelComponent(L"Meshes/Bomberman/Crystal/Crystal.ovm", false);
	pModel->SetMaterial(m_MaterialId);
	m_pCrystalGo->AddComponent(pModel);

	//Shadow
	m_pShadow = new GameObject();
	ModelComponent* pShadowModel = new ModelComponent(L"Meshes/Bomberman/Bomb/Bomb_Shadow.ovm", false);
	pShadowModel->SetMaterial(m_MaterialIds[3]);
	m_pShadow->AddComponent(pShadowModel);
	AddChild(m_pShadow);
	m_pShadow->GetTransform()->Scale(0.5f);
	m_pShadow->GetTransform()->Translate(0, 1, 0);

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

	PhysicsCallback pickUpCallback = [this](GameObject* pTriggerObject, GameObject* pOtherObject, PxTriggerAction action)
	{
		pTriggerObject;

		if (action == PxTriggerAction::ENTER)
		{
			Player* pPlayer = dynamic_cast<Player*>(pOtherObject);
			if (pPlayer != nullptr)
			{
				//PlaySFX
				FMOD::Sound* sound;
				auto fmodSystem = SoundManager::Get()->GetSystem();
				fmodSystem->createSound("Resources/Sounds/crystal_pickup.wav", FMOD_DEFAULT, 0, &sound);
				fmodSystem->playSound(sound, 0, false, 0);


				//Add points
				switch (this->GetType())
				{
				default:
					break;
				case Type::Yellow:
					pPlayer->AddPoints(10);
					break;
				case Type::Blue:
					pPlayer->AddPoints(50);
					break;
				case Type::Red:
					pPlayer->AddPoints(100);
					break;
				}
			}
		}

		GetScene()->LateRemoveChild(this);
	};


	SetOnTriggerCallBack(pickUpCallback);
}

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

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

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