2dkg/2dkg_zad5/2dgk_zad5/KCamera.cpp
2022-01-20 08:52:14 +01:00

47 lines
1.3 KiB
C++

#include "KCamera.h"
#include "KPawn.h"
#include "Utils.h"
namespace KapitanGame
{
const SDL_FRect& KCamera::GetViewport() const
{
return Viewport;
}
float KCamera::GetScale() const
{
return Scale;
}
const KVector2D& KCamera::GetFocusPoint() const
{
return FocusPoint;
}
void KCamera::Update(const std::vector<std::shared_ptr<KPawn>>& pawns, const SDL_FRect& map)
{
FocusPoint = { 0.f,0.f };
for(const auto& pawn : pawns) {
FocusPoint += pawn->GetPosition();
}
FocusPoint *= 1.f / static_cast<float>(pawns.size());
Viewport.x = Utils::Clamp(FocusPoint.X - Viewport.w / 2.f, 0.f, map.w - Viewport.w);
Viewport.y = Utils::Clamp(FocusPoint.Y - Viewport.h / 2.f, 0.f, map.h - Viewport.h);
PlayArea.x = Utils::Clamp(FocusPoint.X - PlayArea.w / 2.f, static_cast<float>(-Constants::WINDOW_DEAD_ZONE), map.w + static_cast<float>(Constants::WINDOW_DEAD_ZONE));
PlayArea.y = Utils::Clamp(FocusPoint.Y - PlayArea.h / 2.f, static_cast<float>(-Constants::WINDOW_DEAD_ZONE), map.h + static_cast<float>(Constants::WINDOW_DEAD_ZONE));
}
void KCamera::SetDebug(const SDL_FRect& map)
{
Viewport = { 0,0,Constants::SCREEN_WIDTH, Constants::SCREEN_HEIGHT };
Scale = std::min(Constants::SCREEN_HEIGHT * 1.f / map.h, Constants::SCREEN_WIDTH * 1.f / map.w);
}
const SDL_FRect& KCamera::GetPlayArea() const
{
return PlayArea;
}
}