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

#include "Player.h"
#include "Materials/DiffuseMaterial_Skinned.h"

LevelExit::LevelExit(const XMFLOAT3& startPos)
	: m_StartPos{ startPos }
{

}

void LevelExit::DrawImGui()
{

}

void LevelExit::Initialize(const SceneContext&)
{
	GetTransform()->Translate(m_StartPos);
	GetTransform()->Rotate(0, -90, 0);

	auto pPxMaterial = PxGetPhysics().createMaterial(.5f, .5f, .5f);

	auto pExitArrowsMat = MaterialManager::Get()->CreateMaterial<DiffuseMaterial_Skinned>();
	pExitArrowsMat->SetDiffuseTexture(L"Textures/Bomberman/Levels/ExitArrows.png");

	ModelComponent* pExitArrowsModel = new ModelComponent(L"Meshes/Bomberman/Levels/ExitArrows.ovm", false);
	pExitArrowsModel->SetMaterial(pExitArrowsMat);
	AddComponent(pExitArrowsModel);

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

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

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

		if (action == PxTriggerAction::ENTER)
		{
			Player* pPlayer = dynamic_cast<Player*>(pOtherObject);
			if (pPlayer != nullptr)
			{
				//PlaySFX
				if (pPlayer->GetState() != Player::State::WIN)
				{
					pPlayer->Win();

					//sfx
					FMOD::Sound* sound;
					auto fmodSystem = SoundManager::Get()->GetSystem();
					fmodSystem->createSound("Resources/Sounds/level_clear.wav", FMOD_DEFAULT, 0, &sound);
					fmodSystem->playSound(sound, 0, false, 0);
				}
			}
		}

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


	SetOnTriggerCallBack(exitLevelCallback);
}

void LevelExit::Update(const SceneContext& /*sceneContext*/)
{

}