diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/camera_system.cpp | 63 | ||||
| -rw-r--r-- | src/camera_system.h | 16 | ||||
| -rw-r--r-- | src/script_system.cpp | 14 |
3 files changed, 79 insertions, 14 deletions
| 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 @@ | |||
| 6 | void CameraSystem::tick(double dt) { | 6 | void CameraSystem::tick(double dt) { |
| 7 | if (!locked_ && followingSprite_ != -1) { | 7 | if (!locked_ && followingSprite_ != -1) { |
| 8 | const Sprite& follow = game_.getSprite(followingSprite_); | 8 | const Sprite& follow = game_.getSprite(followingSprite_); |
| 9 | const Map& map = game_.getMap(); | ||
| 10 | vec2i mapBounds = map.getMapSize() * map.getTileSize(); | ||
| 11 | 9 | ||
| 12 | pos_ = follow.loc - vec2i{0, 16} - (fov_ / 2); | 10 | pos_ = calculatePosWithCenter(follow.loc - vec2i{0, 16}); |
| 11 | } | ||
| 13 | 12 | ||
| 14 | if (pos_.x() < 0) { | 13 | if (panning_) { |
| 15 | pos_.x() = 0; | 14 | panThus_ += dt; |
| 16 | } | 15 | |
| 17 | if (pos_.y() < 0) { | 16 | if (panThus_ >= panLength_) { |
| 18 | pos_.y() = 0; | 17 | panning_ = false; |
| 19 | } | 18 | pos_ = panEnd_; |
| 20 | if (pos_.x() + fov_.w() >= mapBounds.w()) { | 19 | } else { |
| 21 | pos_.x() = mapBounds.w() - fov_.w() - 1; | 20 | pos_.x() = static_cast<double>((panEnd_ - panStart_).x()) / panLength_ * panThus_ + panStart_.x(); |
| 22 | } | 21 | pos_.y() = static_cast<double>((panEnd_ - panStart_).y()) / panLength_ * panThus_ + panStart_.y(); |
| 23 | if (pos_.y() + fov_.h() >= mapBounds.h()) { | ||
| 24 | pos_.y() = mapBounds.h() - fov_.h() - 1; | ||
| 25 | } | 22 | } |
| 26 | } | 23 | } |
| 27 | } | 24 | } |
| 28 | 25 | ||
| 26 | void CameraSystem::panToSprite(int targetId, int length) { | ||
| 27 | locked_ = true; | ||
| 28 | |||
| 29 | const Sprite& target = game_.getSprite(targetId); | ||
| 30 | |||
| 31 | if (length > 0) { | ||
| 32 | panning_ = true; | ||
| 33 | panStart_ = pos_; | ||
| 34 | panEnd_ = calculatePosWithCenter(target.loc - vec2i{0, 16}); | ||
| 35 | panLength_ = length; | ||
| 36 | panThus_ = 0.0; | ||
| 37 | } else { | ||
| 38 | pos_ = calculatePosWithCenter(target.loc - vec2i{0, 16}); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 29 | void CameraSystem::destroySprite(int spriteId) { | 42 | void CameraSystem::destroySprite(int spriteId) { |
| 30 | if (followingSprite_ == spriteId) { | 43 | if (followingSprite_ == spriteId) { |
| 31 | followingSprite_ = -1; | 44 | followingSprite_ = -1; |
| @@ -35,3 +48,25 @@ void CameraSystem::destroySprite(int spriteId) { | |||
| 35 | void CameraSystem::clearSpriteCache() { | 48 | void CameraSystem::clearSpriteCache() { |
| 36 | followingSprite_ = -1; | 49 | followingSprite_ = -1; |
| 37 | } | 50 | } |
| 51 | |||
| 52 | vec2i CameraSystem::calculatePosWithCenter(vec2i center) const { | ||
| 53 | const Map& map = game_.getMap(); | ||
| 54 | vec2i mapBounds = map.getMapSize() * map.getTileSize(); | ||
| 55 | |||
| 56 | vec2i result = center - (fov_ / 2); | ||
| 57 | |||
| 58 | if (result.x() < 0) { | ||
| 59 | result.x() = 0; | ||
| 60 | } | ||
| 61 | if (result.y() < 0) { | ||
| 62 | result.y() = 0; | ||
| 63 | } | ||
| 64 | if (result.x() + fov_.w() >= mapBounds.w()) { | ||
| 65 | result.x() = mapBounds.w() - fov_.w() - 1; | ||
| 66 | } | ||
| 67 | if (result.y() + fov_.h() >= mapBounds.h()) { | ||
| 68 | result.y() = mapBounds.h() - fov_.h() - 1; | ||
| 69 | } | ||
| 70 | |||
| 71 | return result; | ||
| 72 | } | ||
| diff --git a/src/camera_system.h b/src/camera_system.h index 37ff8b4..a2dee93 100644 --- a/src/camera_system.h +++ b/src/camera_system.h | |||
| @@ -24,6 +24,13 @@ public: | |||
| 24 | 24 | ||
| 25 | void unlockCamera() { locked_ = false; } | 25 | void unlockCamera() { locked_ = false; } |
| 26 | 26 | ||
| 27 | // Pans over to the provided sprite over the provided amount of time. | ||
| 28 | // - length is in milliseconds | ||
| 29 | // Automatically locks the camera. | ||
| 30 | void panToSprite(int targetId, int length); | ||
| 31 | |||
| 32 | bool isPanning() const { return panning_; } | ||
| 33 | |||
| 27 | void tick(double dt) override; | 34 | void tick(double dt) override; |
| 28 | 35 | ||
| 29 | void destroySprite(int spriteId) override; | 36 | void destroySprite(int spriteId) override; |
| @@ -31,12 +38,21 @@ public: | |||
| 31 | void clearSpriteCache() override; | 38 | void clearSpriteCache() override; |
| 32 | 39 | ||
| 33 | private: | 40 | private: |
| 41 | |||
| 42 | vec2i calculatePosWithCenter(vec2i center) const; | ||
| 43 | |||
| 34 | Game& game_; | 44 | Game& game_; |
| 35 | 45 | ||
| 36 | vec2i pos_; | 46 | vec2i pos_; |
| 37 | vec2i fov_ { CANVAS_WIDTH, CANVAS_HEIGHT }; | 47 | vec2i fov_ { CANVAS_WIDTH, CANVAS_HEIGHT }; |
| 38 | int followingSprite_ = -1; | 48 | int followingSprite_ = -1; |
| 39 | bool locked_ = true; | 49 | bool locked_ = true; |
| 50 | |||
| 51 | bool panning_ = false; | ||
| 52 | vec2i panStart_; | ||
| 53 | vec2i panEnd_; | ||
| 54 | double panLength_ = 0.0; | ||
| 55 | double panThus_ = 0.0; | ||
| 40 | }; | 56 | }; |
| 41 | 57 | ||
| 42 | #endif /* end of include guard: CAMERA_SYSTEM_H_D52ADAD3 */ | 58 | #endif /* end of include guard: CAMERA_SYSTEM_H_D52ADAD3 */ |
| diff --git a/src/script_system.cpp b/src/script_system.cpp index cbbd473..3ac04ac 100644 --- a/src/script_system.cpp +++ b/src/script_system.cpp | |||
| @@ -6,6 +6,7 @@ | |||
| 6 | #include "character_system.h" | 6 | #include "character_system.h" |
| 7 | #include "transform_system.h" | 7 | #include "transform_system.h" |
| 8 | #include "effect_system.h" | 8 | #include "effect_system.h" |
| 9 | #include "camera_system.h" | ||
| 9 | #include "vector.h" | 10 | #include "vector.h" |
| 10 | 11 | ||
| 11 | ScriptSystem::ScriptSystem(Game& game) : game_(game) { | 12 | ScriptSystem::ScriptSystem(Game& game) : game_(game) { |
| @@ -63,6 +64,13 @@ ScriptSystem::ScriptSystem(Game& game) : game_(game) { | |||
| 63 | "shakeCamera", &EffectSystem::shakeCamera, | 64 | "shakeCamera", &EffectSystem::shakeCamera, |
| 64 | "stopShakingCamera", &EffectSystem::stopShakingCamera); | 65 | "stopShakingCamera", &EffectSystem::stopShakingCamera); |
| 65 | 66 | ||
| 67 | engine_.new_usertype<CameraSystem>( | ||
| 68 | "camera", | ||
| 69 | "panToSprite", &CameraSystem::panToSprite, | ||
| 70 | "isPanning", &CameraSystem::isPanning, | ||
| 71 | "unlockCamera", &CameraSystem::unlockCamera, | ||
| 72 | "setFollowingSprite", &CameraSystem::setFollowingSprite); | ||
| 73 | |||
| 66 | engine_.new_usertype<Mixer>( | 74 | engine_.new_usertype<Mixer>( |
| 67 | "mixer", | 75 | "mixer", |
| 68 | "playSound", &Mixer::playSound, | 76 | "playSound", &Mixer::playSound, |
| @@ -100,6 +108,12 @@ ScriptSystem::ScriptSystem(Game& game) : game_(game) { | |||
| 100 | }); | 108 | }); |
| 101 | 109 | ||
| 102 | engine_.set_function( | 110 | engine_.set_function( |
| 111 | "camera", | ||
| 112 | [&] () -> CameraSystem& { | ||
| 113 | return game_.getSystem<CameraSystem>(); | ||
| 114 | }); | ||
| 115 | |||
| 116 | engine_.set_function( | ||
| 103 | "mixer", | 117 | "mixer", |
| 104 | [&] () -> Mixer& { | 118 | [&] () -> Mixer& { |
| 105 | return game_.getMixer(); | 119 | return game_.getMixer(); |
