BubbleBobbleRemake / BubbleBobble / BubbleStates.cpp
BubbleStates.cpp
Raw
#include "BubbleComponent.h"
#include "BubbleStates.h"
#include "AnimatedTextureComponent.h"
#include "ISoundSystem.h"
#include "ColliderComponent.h"
#include "RigidbodyComponent.h"
#include "LevelComponent.h"
#include "GameObject.h"
#include "ZenChanComponent.h"

namespace dae
{
	//*****************************
	//Bubble INITIAL MOVEMENT STATE
	//*****************************

	void BubbleInitialMovementState::Enter(BubbleComponent* pBubbleComp)
	{
		m_MovementTimer = 0;
		m_pBubbleGo = pBubbleComp->GetGameObject();

		pBubbleComp->m_pAnimTexComp->SetSpritesPerSecond(12);
		pBubbleComp->m_pAnimTexComp->Play(0, 5, true);
	}

	void BubbleInitialMovementState::Update(BubbleComponent* pBubbleComp, float deltaTime)
	{
		m_MovementTimer += deltaTime;

		if (m_MovementTimer < m_MovementDuration)
		{
			float colX{}, colY{};
			m_pBubbleGo->GetComponent<RigidbodyComponent>()->Move(pBubbleComp->m_DirX * pBubbleComp->m_MoveSpeed * deltaTime, 0, pBubbleComp->m_pLevelComponent->GetBorderColliders());
			if (m_pBubbleGo->GetComponent<RigidbodyComponent>()->DidCollide())
			{
				pBubbleComp->ChangeState(FLOATING_UP);
			}
			else if (m_pBubbleGo->GetComponent<RigidbodyComponent>()->IsCollidingWith(pBubbleComp->m_pLevelComponent->GetPlayersColliders(), colX, colY, true))
			{
				GameObject* pCollidedGameObject = m_pBubbleGo->GetComponent<RigidbodyComponent>()->GetLastCollidedGameObject();
				if (pCollidedGameObject != nullptr)
				{
					switch (pBubbleComp->m_SpriteId)
					{
					case 1:
						if (pCollidedGameObject->GetName() != "Player1Go")
							pBubbleComp->ChangeState(FLOATING_UP);
						break;
					case 2:
						if (pCollidedGameObject->GetName() != "Player2Go")
							pBubbleComp->ChangeState(FLOATING_UP);
						break;
					default:
						break;
					}
				}
			}
			else if (m_pBubbleGo->GetComponent<RigidbodyComponent>()->IsCollidingWith(pBubbleComp->m_pEnemyColliders, colX, colY, true))
			{
				GameObject* pCollidedGameObject = m_pBubbleGo->GetComponent<RigidbodyComponent>()->GetLastCollidedGameObject();
				if (pCollidedGameObject != nullptr)
				{
					if (pCollidedGameObject->GetComponent<ZenChanComponent>() != nullptr)
					{
						if (!pCollidedGameObject->GetComponent<ColliderComponent>()->GetIgnoreFlag())
						{
							pBubbleComp->m_pTrappedGameObject = pCollidedGameObject;
							pBubbleComp->m_pTrappedGameObject->GetComponent<ZenChanComponent>()->EnterBubble();
							pBubbleComp->m_TrappedGameObjectId = ZENCHAN;
							pBubbleComp->ChangeState(FLOATING_UP);
						}
					}
				}
			}
		}
		else
		{
			pBubbleComp->ChangeState(FLOATING_UP);
		}
	}

	void BubbleInitialMovementState::Exit(BubbleComponent*)
	{
	}
	
	//************************
	//Bubble FLOATING UP STATE
	//************************

	void BubbleFloatingUpState::Enter(BubbleComponent* pBubbleComp)
	{
		m_pBubbleGo = pBubbleComp->GetGameObject();
		
		if (pBubbleComp->m_pTrappedGameObject)
		{
			switch (pBubbleComp->m_TrappedGameObjectId)
			{
				default: break;
			case ZENCHAN:
				pBubbleComp->m_pAnimTexComp->SetSpritesPerSecond(6);
				pBubbleComp->m_pAnimTexComp->Play(18, 20, false, true);
				break;
			}
		}
		else
		{
			pBubbleComp->m_pAnimTexComp->SetSpritesPerSecond(1);
			pBubbleComp->m_pAnimTexComp->Play(5, 5, true);
		}
	}
	void BubbleFloatingUpState::Update(BubbleComponent* pBubbleComp, float deltaTime)
	{
		pBubbleComp->HandlePlayerCollisions();

		m_pBubbleGo->GetComponent<RigidbodyComponent>()->Move(0, -1 * pBubbleComp->m_MoveSpeed / 4.f * deltaTime, pBubbleComp->m_pLevelComponent->GetTopLevelTileColliders());
		if (m_pBubbleGo->GetComponent<RigidbodyComponent>()->DidCollide())
		{
			pBubbleComp->ChangeState(FLOATING_SIDEWAYS);
		}
	}
	void BubbleFloatingUpState::Exit(BubbleComponent*)
	{
	}

	//******************************
	//Bubble FLOATING SIDEWAYS STATE
	//******************************

	void BubbleFloatingSidewaysState::Enter(BubbleComponent* pBubbleComp)
	{
		m_pBubbleGo = pBubbleComp->GetGameObject();

		if (pBubbleComp->m_pTrappedGameObject)
		{
			switch (pBubbleComp->m_TrappedGameObjectId)
			{
			default: break;
			case ZENCHAN:
				pBubbleComp->m_pAnimTexComp->SetSpritesPerSecond(6);
				pBubbleComp->m_pAnimTexComp->Play(21, 23, false, true);
				break;
			}
		}
		else
		{
			pBubbleComp->m_pAnimTexComp->SetSpritesPerSecond(1);
			pBubbleComp->m_pAnimTexComp->Play(6, 6, true);
		}

		pBubbleComp->m_isRed = true;
	}
	void BubbleFloatingSidewaysState::Update(BubbleComponent* pBubbleComp, float deltaTime)
	{
		pBubbleComp->HandlePlayerCollisions();

		m_FloatingTimer += deltaTime;
		if (m_FloatingTimer >= m_FloatingDuration)
		{
			pBubbleComp->ChangeState(BLINKING);
		}
		else
		{
			m_pBubbleGo->GetComponent<RigidbodyComponent>()->Move(-pBubbleComp->m_DirX * pBubbleComp->m_MoveSpeed / 8.f * deltaTime, 0, pBubbleComp->m_pLevelComponent->GetBorderColliders());
		}
	}
	void BubbleFloatingSidewaysState::Exit(BubbleComponent*)
	{
	}

	//*********************
	//Bubble BLINKING STATE
	//*********************

	void BubbleBlinkingState::Enter(BubbleComponent* pBubbleComp)
	{
		m_pBubbleGo = pBubbleComp->GetGameObject();

		if (pBubbleComp->m_pTrappedGameObject)
		{
			switch (pBubbleComp->m_TrappedGameObjectId)
			{
			default: break;
			case ZENCHAN:
				pBubbleComp->m_pAnimTexComp->SetSpritesPerSecond(12);
				pBubbleComp->m_pAnimTexComp->Play(24, 29, false, true);
				break;
			}
		}
		else
		{
			pBubbleComp->m_pAnimTexComp->SetSpritesPerSecond(9);
			pBubbleComp->m_pAnimTexComp->Play(6, 7);
		}
	}
	void BubbleBlinkingState::Update(BubbleComponent* pBubbleComp, float deltaTime)
	{
		pBubbleComp->HandlePlayerCollisions();

		m_BlinkingTimer += deltaTime;
		if (m_BlinkingTimer >= m_BlinkingDuration)
		{
			pBubbleComp->ChangeState(POPPING);
		}
		else
		{
			m_pBubbleGo->GetComponent<RigidbodyComponent>()->Move(-pBubbleComp->m_DirX * pBubbleComp->m_MoveSpeed / 8.f * deltaTime, 0, pBubbleComp->m_pLevelComponent->GetBorderColliders());
		}
	}
	void BubbleBlinkingState::Exit(BubbleComponent*)
	{

	}

	//********************
	//Bubble POPPING STATE
	//********************

	void BubblePoppingState::Enter(BubbleComponent* pBubbleComp)
	{
		m_pBubbleGo = pBubbleComp->GetGameObject();

		m_pBubbleGo->RemoveComponent<RigidbodyComponent>();
		m_pBubbleGo->RemoveComponent<ColliderComponent>();

		pBubbleComp->m_pAnimTexComp->SetSpritesPerSecond(6);
		pBubbleComp->m_pAnimTexComp->Play(8, 13, true);

		if (pBubbleComp->m_pTrappedGameObject)
		{
			switch (pBubbleComp->m_TrappedGameObjectId)
			{
			default: break;
			case ZENCHAN:
				pBubbleComp->m_pTrappedGameObject->GetComponent<ZenChanComponent>()->ExitBubble(m_pBubbleGo->GetWorldPosition().x, m_pBubbleGo->GetWorldPosition().y);
				break;
			}

			pBubbleComp->m_pTrappedGameObject = nullptr;
		}
	}
	void BubblePoppingState::Update(BubbleComponent*, float deltaTime)
	{
		m_PoppingTimer += deltaTime;
		if (m_PoppingTimer >= m_PoppingDuration)
		{
			m_pBubbleGo->SetActive(false);
			m_pBubbleGo->Destroy();
		}
	}
	void BubblePoppingState::Exit(BubbleComponent*)
	{
	}

	//********************************
	//Bubble SQUISHED HORIZONTAL STATE
	//********************************

	void BubbleSquishedHorizontalState::Enter(BubbleComponent* pBubbleComp)
	{
		m_pBubbleGo = pBubbleComp->GetGameObject();

		if (pBubbleComp->m_isRed)
		{
			pBubbleComp->m_pAnimTexComp->SetSpritesPerSecond(1);
			pBubbleComp->m_pAnimTexComp->Play(17, 17);
		}
		else
		{
			pBubbleComp->m_pAnimTexComp->SetSpritesPerSecond(1);
			pBubbleComp->m_pAnimTexComp->Play(15, 15);
		}
	}
	void BubbleSquishedHorizontalState::Update(BubbleComponent* pBubbleComp, float deltaTime)
	{
		m_SquishTimer += deltaTime;
		if (m_SquishTimer >= m_SquishDuration)
		{
			pBubbleComp->ChangeState(POPPING);
		}
	}
	void BubbleSquishedHorizontalState::Exit(BubbleComponent*)
	{
	}

	//******************************
	//Bubble SQUISHED VERTICAL STATE
	//******************************

	void BubbleSquishedVerticalState::Enter(BubbleComponent* pBubbleComp)
	{
		m_pBubbleGo = pBubbleComp->GetGameObject();

		if (pBubbleComp->m_isRed)
		{
			pBubbleComp->m_pAnimTexComp->SetSpritesPerSecond(1);
			pBubbleComp->m_pAnimTexComp->Play(16, 16);
		}
		else
		{
			pBubbleComp->m_pAnimTexComp->SetSpritesPerSecond(1);
			pBubbleComp->m_pAnimTexComp->Play(14, 14);
		}
	}
	void BubbleSquishedVerticalState::Update(BubbleComponent* pBubbleComp, float deltaTime)
	{
		m_SquishTimer += deltaTime;
		if (m_SquishTimer >= m_SquishDuration)
		{
			pBubbleComp->ChangeState(POPPING);
		}
	}
	void BubbleSquishedVerticalState::Exit(BubbleComponent*)
	{
	}
}