From a645524d19bb9183d4eece72bf8945bba4bed3a0 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 16 Feb 2021 22:03:10 -0500 Subject: Added scriptable camera panning --- src/camera_system.cpp | 63 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 14 deletions(-) (limited to 'src/camera_system.cpp') diff --git a/src/camera_system.cpp b/src/camera_system.cpp index b7627b1..2d7be61 100644 --- a/src/camera_system.cpp +++ b/src/camera_system.cpp @@ -6,26 +6,39 @@ void CameraSystem::tick(double dt) { if (!locked_ && followingSprite_ != -1) { const Sprite& follow = game_.getSprite(followingSprite_); - const Map& map = game_.getMap(); - vec2i mapBounds = map.getMapSize() * map.getTileSize(); - pos_ = follow.loc - vec2i{0, 16} - (fov_ / 2); + pos_ = calculatePosWithCenter(follow.loc - vec2i{0, 16}); + } - if (pos_.x() < 0) { - pos_.x() = 0; - } - if (pos_.y() < 0) { - pos_.y() = 0; - } - if (pos_.x() + fov_.w() >= mapBounds.w()) { - pos_.x() = mapBounds.w() - fov_.w() - 1; - } - if (pos_.y() + fov_.h() >= mapBounds.h()) { - pos_.y() = mapBounds.h() - fov_.h() - 1; + if (panning_) { + panThus_ += dt; + + if (panThus_ >= panLength_) { + panning_ = false; + pos_ = panEnd_; + } else { + pos_.x() = static_cast((panEnd_ - panStart_).x()) / panLength_ * panThus_ + panStart_.x(); + pos_.y() = static_cast((panEnd_ - panStart_).y()) / panLength_ * panThus_ + panStart_.y(); } } } +void CameraSystem::panToSprite(int targetId, int length) { + locked_ = true; + + const Sprite& target = game_.getSprite(targetId); + + if (length > 0) { + panning_ = true; + panStart_ = pos_; + panEnd_ = calculatePosWithCenter(target.loc - vec2i{0, 16}); + panLength_ = length; + panThus_ = 0.0; + } else { + pos_ = calculatePosWithCenter(target.loc - vec2i{0, 16}); + } +} + void CameraSystem::destroySprite(int spriteId) { if (followingSprite_ == spriteId) { followingSprite_ = -1; @@ -35,3 +48,25 @@ void CameraSystem::destroySprite(int spriteId) { void CameraSystem::clearSpriteCache() { followingSprite_ = -1; } + +vec2i CameraSystem::calculatePosWithCenter(vec2i center) const { + const Map& map = game_.getMap(); + vec2i mapBounds = map.getMapSize() * map.getTileSize(); + + vec2i result = center - (fov_ / 2); + + if (result.x() < 0) { + result.x() = 0; + } + if (result.y() < 0) { + result.y() = 0; + } + if (result.x() + fov_.w() >= mapBounds.w()) { + result.x() = mapBounds.w() - fov_.w() - 1; + } + if (result.y() + fov_.h() >= mapBounds.h()) { + result.y() = mapBounds.h() - fov_.h() - 1; + } + + return result; +} -- cgit 1.4.1