#include "camera_system.h" #include "sprite.h" #include "game.h" #include "map.h" void CameraSystem::tick(double dt) { if (game_.isGameplayPaused()) return; if (!locked_ && followingSprite_ != -1) { const Sprite& follow = game_.getSprite(followingSprite_); pos_ = calculatePosWithCenter(follow.loc - vec2i{0, 16}); } 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::panToWarpPoint(std::string_view warpPoint, int length) { locked_ = true; vec2i center = game_.getMap().getWarpPoint(warpPoint); if (length > 0) { panning_ = true; panStart_ = pos_; panEnd_ = calculatePosWithCenter(center); panLength_ = length; panThus_ = 0.0; } else { pos_ = calculatePosWithCenter(center); } } void CameraSystem::destroySprite(int spriteId) { if (followingSprite_ == spriteId) { 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; }