BubbleBobbleRemake / BubbleBobble / BubbleComponent.cpp
BubbleComponent.cpp
Raw
#include "BubbleComponent.h"
#include "PlayerComponent.h"
#include "ColliderComponent.h"
#include "RigidbodyComponent.h"
#include "LevelComponent.h"
#include "PlayerScoreComponent.h"
#include "AnimatedTextureComponent.h"
#include "BubbleStates.h"
#include "Scene.h"
#include "ZenChanComponent.h"

#include "GameObject.h"
#include "GameEvents.h"

dae::BubbleComponent::BubbleComponent(LevelComponent* pLevelComponent, const std::vector<ColliderComponent*>& pEnemyColliders, unsigned direction, int spriteId, float startX, float startY)
	: m_pLevelComponent{ pLevelComponent }
	, m_pEnemyColliders{ pEnemyColliders }
	, m_SpriteId{ spriteId }
	, m_StartX{ startX }
	, m_StartY{ startY }
{
	if (direction == FacingDirection::RIGHT)
	{
		m_DirX = 1;
	}
	else
	{
		m_DirX = -1;
	}

	m_pStates[INITIAL_MOVEMENT] = std::make_shared<BubbleInitialMovementState>();
	m_pStates[FLOATING_UP] = std::make_shared<BubbleFloatingUpState>();
	m_pStates[FLOATING_SIDEWAYS] = std::make_shared<BubbleFloatingSidewaysState>();
	m_pStates[BLINKING] = std::make_shared<BubbleBlinkingState>();
	m_pStates[POPPING] = std::make_shared<BubblePoppingState>();
	m_pStates[SQUISHED_HORIZONTAL] = std::make_shared<BubbleSquishedHorizontalState>();
	m_pStates[SQUISHED_VERTICAL] = std::make_shared<BubbleSquishedVerticalState>();
}

dae::BubbleComponent::~BubbleComponent()
{
	
}

void dae::BubbleComponent::Initialize()
{
	GameObject* pBubbleGo = GetGameObject();
	pBubbleGo->SetName("bubble");

	GameObject* pPlayer1ScoreGo = SceneManager::GetInstance().GetActiveScene()->FindChild("Player1ScoreGo");
	if (pPlayer1ScoreGo != nullptr)
		AddObserver(pPlayer1ScoreGo->GetComponent<PlayerScoreComponent>());

	GameObject* pPlayer2ScoreGo = SceneManager::GetInstance().GetActiveScene()->FindChild("Player2ScoreGo");
	if (pPlayer2ScoreGo != nullptr)
		AddObserver(pPlayer2ScoreGo->GetComponent<PlayerScoreComponent>());

	m_pAnimTexComp = pBubbleGo->AddComponent(new AnimatedTextureComponent("bubbles_player" + std::to_string(m_SpriteId) + ".png", 32, 32, 4, 5));
	pBubbleGo->SetPosition(m_StartX + m_pAnimTexComp->GetSpriteWidth() / 2.f, m_StartY + m_pAnimTexComp->GetSpriteHeight() / 1.f);

	pBubbleGo->AddComponent(new ColliderComponent(32, 32, false, 0, 0));
	pBubbleGo->AddComponent(new RigidbodyComponent());

	ChangeState(INITIAL_MOVEMENT);
}

void dae::BubbleComponent::Update(float deltaTime)
{
	reinterpret_cast<IBubbleState*>(m_pCurrentState.get())->Update(this, deltaTime);
}

void dae::BubbleComponent::HandlePlayerCollisions()
{
	GameObject* pBubbleGo = GetGameObject();

	float colX{}; float colY{};
	if (pBubbleGo->GetComponent<RigidbodyComponent>()->IsCollidingWith(m_pLevelComponent->GetPlayersColliders(), colX, colY, true))
	{
		float colliderCenterX = pBubbleGo->GetWorldPosition().x + pBubbleGo->GetComponent<ColliderComponent>()->GetColliderData().width / 2.f;
		float colliderCenterY = pBubbleGo->GetWorldPosition().y + pBubbleGo->GetComponent<ColliderComponent>()->GetColliderData().height / 2.f;

		float distanceX = fabs(colX - colliderCenterX);
		float distanceY = fabs(colY - colliderCenterY);

		GameObject* pCollidedGameObject = pBubbleGo->GetComponent<RigidbodyComponent>()->GetLastCollidedGameObject();
		if (pCollidedGameObject != nullptr)
		{
			if (pCollidedGameObject->GetName() == "Player1Go")
			{
				if (m_pTrappedGameObject)
				{
					if (m_TrappedGameObjectId == ZENCHAN)
					{
						NotifyObservers(BubbleEvents::P1_POPPED_ZENCHAN_BUBBLE);
						m_pTrappedGameObject->SetPosition(GetGameObject()->GetWorldPosition().x, GetGameObject()->GetWorldPosition().y);
						m_pTrappedGameObject->GetComponent<ZenChanComponent>()->Die();
					}

					m_pTrappedGameObject = nullptr;
				}
				else
					NotifyObservers(BubbleEvents::P1_POPPED_EMPTY_BUBBLE);
			}
			else if (pCollidedGameObject->GetName() == "Player2Go")
			{
				if (m_pTrappedGameObject)
				{
					if (m_TrappedGameObjectId == ZENCHAN)
					{
						NotifyObservers(BubbleEvents::P2_POPPED_ZENCHAN_BUBBLE);
						m_pTrappedGameObject->SetPosition(GetGameObject()->GetWorldPosition().x, GetGameObject()->GetWorldPosition().y);
						m_pTrappedGameObject->GetComponent<ZenChanComponent>()->Die();
					}

					m_pTrappedGameObject = nullptr;
				}
				else
					NotifyObservers(BubbleEvents::P2_POPPED_EMPTY_BUBBLE);
			}
		}

		(distanceY > distanceX) ? ChangeState(SQUISHED_VERTICAL) : ChangeState(SQUISHED_HORIZONTAL);
	}
}

void dae::BubbleComponent::FixedUpdate(float)
{

}