SpelunkyRemake / Platform.cpp
Platform.cpp
Raw
#include "pch.h"
#include "Platform.h"

Platform::Platform(const Point2f& bottomLeft)
	: m_Texture{ "Resources/Images/platform.png" }
	, m_Shape{ bottomLeft.x, bottomLeft.y, 0, 0 }
{
	m_Shape.width = m_Texture.GetWidth();
	m_Shape.height = m_Texture.GetHeight();
}

void Platform::Draw() const
{
	m_Texture.Draw(m_Shape);
}

void Platform::HandleCollision(Rectf& actorShape, Vector2f& actorVelocity) const
{
	if (actorVelocity.y < 0)
	{
		Point2f actorTopCenter{ actorShape.left + actorShape.width / 2, actorShape.bottom + actorShape.height };
		Point2f actorBtmCenter{ actorTopCenter.x, actorShape.bottom };

		std::vector<Point2f> platformVertices;
		platformVertices.push_back(Point2f{ m_Shape.left, m_Shape.bottom + m_Shape.height });
		platformVertices.push_back(Point2f{ m_Shape.left + m_Shape.width, m_Shape.bottom + m_Shape.height });

		utils::HitInfo hitInfo{};
		if (utils::Raycast(platformVertices, actorTopCenter, actorBtmCenter, hitInfo))
		{
			actorShape.bottom = hitInfo.intersectPoint.y;
			actorVelocity.y = 0;
		}
	}
}

bool Platform::IsOnGround(const Rectf& actorShape, Vector2f& actorVelocity) const
{
	if (actorVelocity.y <= 0)
	{
		Point2f actorTopCenter{ actorShape.left + actorShape.width / 2, actorShape.bottom + actorShape.height };
		Point2f actorBtmCenter{ actorTopCenter.x, actorShape.bottom - 1 };

		std::vector<Point2f> platformVertices;
		platformVertices.push_back(Point2f{ m_Shape.left, m_Shape.bottom + m_Shape.height });
		platformVertices.push_back(Point2f{ m_Shape.left + m_Shape.width, m_Shape.bottom + m_Shape.height });

		utils::HitInfo hitInfo{};
		return utils::Raycast(platformVertices, actorTopCenter, actorBtmCenter, hitInfo);
	}

	return false;
}