summary refs log tree commit diff stats
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
parentc5a96f0ee23a3dbe1a6dab0a1062cf25b7a2aba4 (diff)
downloadtanetane-a645524d19bb9183d4eece72bf8945bba4bed3a0.tar.gz
tanetane-a645524d19bb9183d4eece72bf8945bba4bed3a0.tar.bz2
tanetane-a645524d19bb9183d4eece72bf8945bba4bed3a0.zip
Added scriptable camera panning
-rw-r--r--res/scripts/common.lua23
-rw-r--r--res/scripts/map2.lua19
-rw-r--r--src/camera_system.cpp63
-rw-r--r--src/camera_system.h16
-rw-r--r--src/script_system.cpp14
5 files changed, 120 insertions, 15 deletions
diff --git a/res/scripts/common.lua b/res/scripts/common.lua index caab86a..f88c4c1 100644 --- a/res/scripts/common.lua +++ b/res/scripts/common.lua
@@ -141,6 +141,29 @@ function StopShakingCamera()
141 effect():stopShakingCamera() 141 effect():stopShakingCamera()
142end 142end
143 143
144function PanToSprite(spriteName, length)
145 local spriteId = getSpriteByAlias(spriteName)
146 camera():panToSprite(spriteId, length)
147end
148
149function WaitForPan()
150 while camera():isPanning() do
151 coroutine.yield()
152 end
153end
154
155function ReturnCamera(length)
156 local playerId = getPlayerSprite()
157 camera():panToSprite(playerId, length)
158
159 while camera():isPanning() do
160 coroutine.yield()
161 end
162
163 camera():setFollowingSprite(playerId)
164 camera():unlockCamera()
165end
166
144function SetPartyDirection(spriteId, direction) 167function SetPartyDirection(spriteId, direction)
145 animation():setSpriteDirection(spriteId, direction) 168 animation():setSpriteDirection(spriteId, direction)
146 169
diff --git a/res/scripts/map2.lua b/res/scripts/map2.lua index 92c1747..c929e8d 100644 --- a/res/scripts/map2.lua +++ b/res/scripts/map2.lua
@@ -74,9 +74,26 @@ function map2.approach_doria()
74 gamestate.approached_doria = true 74 gamestate.approached_doria = true
75 75
76 StartCutscene() 76 StartCutscene()
77 PanToSprite("doria", 2000)
78 WaitForPan()
79
77 SetDirection("doria", Direction.DOWN_RIGHT) 80 SetDirection("doria", Direction.DOWN_RIGHT)
78 SetAnimation("doria", "talk") 81 SetAnimation("doria", "talk")
79 DisplayMessage("* Oh my!\n* Is that our little Kumatora I spy? `\n* Such style! Such swagger!\nWhy, just as if she were Magifolk herself! `\n* When Ionia showed us the human she was looking after, I hardly knew what to think.", "Doria", SpeakerType.MAN) 82 DisplayMessage("* Oh my!\n\f* Is that our little Kumatora I spy? `", "Doria", SpeakerType.MAN)
83 WaitForEndOfMessage()
84
85 SetAnimation("doria", "still")
86 ReturnCamera(2000)
87 Delay(1000)
88 SetAnimation("kuma", "talk")
89 DisplayMessage("* Doria...?\n\f* What the hell... You're still alive?", "Kumatora", SpeakerType.WOMAN)
90 WaitForEndOfMessage()
91
92 SetAnimation("kuma", "still")
93 Delay(1000)
94
95 SetAnimation("doria", "talk")
96 DisplayMessage("* It is! It is her!\n\f* Such style! Such swagger!\nWhy, just as if she were Magifolk herself! `\n* When Ionia showed us the human she was looking after, I hardly knew what to think.", "Doria", SpeakerType.MAN)
80 WaitForEndOfMessage() 97 WaitForEndOfMessage()
81 98
82 SetDirection("doria", Direction.DOWN) 99 SetDirection("doria", Direction.DOWN)
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}
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
33private: 40private:
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
11ScriptSystem::ScriptSystem(Game& game) : game_(game) { 12ScriptSystem::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();