From b628c80f988c1fb250b94b17dca8e082bc4c1d0b Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 2 Feb 2021 16:51:22 -0500 Subject: Added moving camera --- src/game.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src/game.cpp') diff --git a/src/game.cpp b/src/game.cpp index ccca10f..ebb792e 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -17,4 +17,26 @@ void Game::moveSprite(int id, vec2i newLoc) { if (changedY) { spritesByY_.emplace(newLoc.y(), id); } -} \ No newline at end of file +} + +void Game::tick() { + if (!cameraLocked_) { + const Sprite& follow = getSprite(followingSprite_); + vec2i mapBounds = map_->getMapSize() * map_->getTileSize(); + + cameraPos_ = follow.loc() - (cameraFov_ / 2); + + if (cameraPos_.x() < 0) { + cameraPos_.x() = 0; + } + if (cameraPos_.y() < 0) { + cameraPos_.y() = 0; + } + if (cameraPos_.x() + cameraFov_.w() >= mapBounds.w()) { + cameraPos_.x() = mapBounds.w() - cameraFov_.w() - 1; + } + if (cameraPos_.y() + cameraFov_.h() >= mapBounds.h()) { + cameraPos_.y() = mapBounds.h() - cameraFov_.h() - 1; + } + } +} -- cgit 1.4.1