summary refs log tree commit diff stats
path: root/src/camera_system.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2021-02-16 22:03:10 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2021-02-16 22:03:10 -0500
commita645524d19bb9183d4eece72bf8945bba4bed3a0 (patch)
treeca45cb80f73def5bd6bd8d7ec46bb90c826be848 /src/camera_system.cpp
parentc5a96f0ee23a3dbe1a6dab0a1062cf25b7a2aba4 (diff)
downloadtanetane-a645524d19bb9183d4eece72bf8945bba4bed3a0.tar.gz
tanetane-a645524d19bb9183d4eece72bf8945bba4bed3a0.tar.bz2
tanetane-a645524d19bb9183d4eece72bf8945bba4bed3a0.zip
Added scriptable camera panning
Diffstat (limited to 'src/camera_system.cpp')
-rw-r--r--src/camera_system.cpp63
1 files changed, 49 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 @@
6void CameraSystem::tick(double dt) { 6void 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
26void 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
29void CameraSystem::destroySprite(int spriteId) { 42void 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) {
35void CameraSystem::clearSpriteCache() { 48void CameraSystem::clearSpriteCache() {
36 followingSprite_ = -1; 49 followingSprite_ = -1;
37} 50}
51
52vec2i 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}