summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2021-02-03 01:11:31 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2021-02-03 01:11:31 -0500
commit24918837c3ff9026d228657d14852c9cf39a5644 (patch)
treebe131a43eb30f164bd70f542cfcaec688fbc3d51 /src
parentf449345e3aeb599eb497dfeeac7027cf4d1de515 (diff)
downloadtanetane-24918837c3ff9026d228657d14852c9cf39a5644.tar.gz
tanetane-24918837c3ff9026d228657d14852c9cf39a5644.tar.bz2
tanetane-24918837c3ff9026d228657d14852c9cf39a5644.zip
Added camera system
Diffstat (limited to 'src')
-rw-r--r--src/camera_system.cpp27
-rw-r--r--src/camera_system.h38
-rw-r--r--src/game.cpp22
-rw-r--r--src/game.h48
-rw-r--r--src/main.cpp22
-rw-r--r--src/party.cpp5
-rw-r--r--src/renderer.cpp12
-rw-r--r--src/system.h8
-rw-r--r--src/transform_system.h2
9 files changed, 120 insertions, 64 deletions
diff --git a/src/camera_system.cpp b/src/camera_system.cpp new file mode 100644 index 0000000..abeba86 --- /dev/null +++ b/src/camera_system.cpp
@@ -0,0 +1,27 @@
1#include "camera_system.h"
2#include "sprite.h"
3#include "game.h"
4#include "map.h"
5
6void CameraSystem::tick(double dt) {
7 if (!locked_) {
8 const Sprite& follow = game_.getSprite(followingSprite_);
9 const Map& map = game_.getMap();
10 vec2i mapBounds = map.getMapSize() * map.getTileSize();
11
12 pos_ = follow.loc() - (fov_ / 2);
13
14 if (pos_.x() < 0) {
15 pos_.x() = 0;
16 }
17 if (pos_.y() < 0) {
18 pos_.y() = 0;
19 }
20 if (pos_.x() + fov_.w() >= mapBounds.w()) {
21 pos_.x() = mapBounds.w() - fov_.w() - 1;
22 }
23 if (pos_.y() + fov_.h() >= mapBounds.h()) {
24 pos_.y() = mapBounds.h() - fov_.h() - 1;
25 }
26 }
27}
diff --git a/src/camera_system.h b/src/camera_system.h new file mode 100644 index 0000000..3eb8374 --- /dev/null +++ b/src/camera_system.h
@@ -0,0 +1,38 @@
1#ifndef CAMERA_SYSTEM_H_D52ADAD3
2#define CAMERA_SYSTEM_H_D52ADAD3
3
4#include "consts.h"
5#include "system.h"
6#include "vector.h"
7
8class Game;
9
10class CameraSystem : public System {
11public:
12
13 static constexpr SystemKey Key = SystemKey::Camera;
14
15 CameraSystem(Game& game) : game_(game) {}
16
17 const vec2i& getCameraPosition() const { return pos_; }
18
19 const vec2i& getFieldOfView() const { return fov_; }
20
21 void setFollowingSprite(int spriteId) { followingSprite_ = spriteId; }
22
23 void lockCamera() { locked_ = true; }
24
25 void unlockCamera() { locked_ = false; }
26
27 void tick(double dt) override;
28
29private:
30 Game& game_;
31
32 vec2i pos_;
33 vec2i fov_ { CANVAS_WIDTH, CANVAS_HEIGHT };
34 int followingSprite_;
35 bool locked_ = true;
36};
37
38#endif /* end of include guard: CAMERA_SYSTEM_H_D52ADAD3 */
diff --git a/src/game.cpp b/src/game.cpp index 42dbd64..bb20d74 100644 --- a/src/game.cpp +++ b/src/game.cpp
@@ -6,25 +6,3 @@ int Game::addSprite(Sprite sprite) {
6 spriteIds_.push_back(id); 6 spriteIds_.push_back(id);
7 return id; 7 return id;
8} 8}
9
10void Game::tick() {
11 if (!cameraLocked_) {
12 const Sprite& follow = getSprite(followingSprite_);
13 vec2i mapBounds = map_->getMapSize() * map_->getTileSize();
14
15 cameraPos_ = follow.loc() - (cameraFov_ / 2);
16
17 if (cameraPos_.x() < 0) {
18 cameraPos_.x() = 0;
19 }
20 if (cameraPos_.y() < 0) {
21 cameraPos_.y() = 0;
22 }
23 if (cameraPos_.x() + cameraFov_.w() >= mapBounds.w()) {
24 cameraPos_.x() = mapBounds.w() - cameraFov_.w() - 1;
25 }
26 if (cameraPos_.y() + cameraFov_.h() >= mapBounds.h()) {
27 cameraPos_.y() = mapBounds.h() - cameraFov_.h() - 1;
28 }
29 }
30}
diff --git a/src/game.h b/src/game.h index 189cabb..f89c707 100644 --- a/src/game.h +++ b/src/game.h
@@ -1,14 +1,15 @@
1#ifndef GAME_H_E6F1396E 1#ifndef GAME_H_E6F1396E
2#define GAME_H_E6F1396E 2#define GAME_H_E6F1396E
3 3
4#include <set> 4#include <list>
5#include <range/v3/all.hpp> 5#include <range/v3/all.hpp>
6#include <vector> 6#include <vector>
7#include <memory> 7#include <memory>
8#include <map>
8#include "sprite.h" 9#include "sprite.h"
9#include "map.h" 10#include "map.h"
10#include "consts.h" 11#include "consts.h"
11#include "transform_system.h" 12#include "system.h"
12 13
13struct Input { 14struct Input {
14 bool left = false; 15 bool left = false;
@@ -20,7 +21,22 @@ struct Input {
20class Game { 21class Game {
21public: 22public:
22 23
23 Game() : transform_(*this) {} 24 template <typename T>
25 void emplaceSystem() {
26 systems_.push_back(std::make_unique<T>(*this));
27 systemByKey_[T::Key] = systems_.back().get();
28 }
29
30 template <typename T>
31 T& getSystem() {
32 return *dynamic_cast<T*>(systemByKey_.at(T::Key));
33 }
34
35 auto systems() {
36 return systems_ | ranges::views::transform([&] (std::unique_ptr<System>& systemPtr) -> System& {
37 return *systemPtr;
38 });
39 }
24 40
25 int addSprite(Sprite sprite); 41 int addSprite(Sprite sprite);
26 42
@@ -42,9 +58,6 @@ public:
42 }); 58 });
43 } 59 }
44 60
45 TransformSystem& getTransformSystem() { return transform_; }
46
47
48 void setSpriteState(int id, std::string state) { 61 void setSpriteState(int id, std::string state) {
49 sprites_[id].setState(std::move(state)); 62 sprites_[id].setState(std::move(state));
50 } 63 }
@@ -63,33 +76,14 @@ public:
63 76
64 const Map& getMap() const { return *map_; } 77 const Map& getMap() const { return *map_; }
65 78
66 // Camera
67
68 const vec2i& getCameraPosition() const { return cameraPos_; }
69
70 const vec2i& getFieldOfView() const { return cameraFov_; }
71
72 void setFollowingSprite(int spriteId) { followingSprite_ = spriteId; }
73
74 void lockCamera() { cameraLocked_ = true; }
75
76 void unlockCamera() { cameraLocked_ = false; }
77
78 void tick();
79
80private: 79private:
81 80
82 TransformSystem transform_; 81 std::list<std::unique_ptr<System>> systems_;
82 std::map<SystemKey, System*> systemByKey_;
83 83
84 std::vector<int> spriteIds_; 84 std::vector<int> spriteIds_;
85 std::vector<Sprite> sprites_; 85 std::vector<Sprite> sprites_;
86 std::unique_ptr<Map> map_; 86 std::unique_ptr<Map> map_;
87
88 // Camera
89 vec2i cameraPos_;
90 vec2i cameraFov_ { CANVAS_WIDTH, CANVAS_HEIGHT };
91 int followingSprite_;
92 bool cameraLocked_ = true;
93}; 87};
94 88
95#endif /* end of include guard: GAME_H_E6F1396E */ 89#endif /* end of include guard: GAME_H_E6F1396E */
diff --git a/src/main.cpp b/src/main.cpp index d182290..a092114 100644 --- a/src/main.cpp +++ b/src/main.cpp
@@ -6,9 +6,14 @@
6#include "timer.h" 6#include "timer.h"
7#include "map.h" 7#include "map.h"
8#include "mixer.h" 8#include "mixer.h"
9#include "transform_system.h"
10#include "camera_system.h"
9 11
10void loop(Renderer& renderer, Mixer& mixer) { 12void loop(Renderer& renderer, Mixer& mixer) {
11 Game game; 13 Game game;
14 game.emplaceSystem<TransformSystem>();
15 game.emplaceSystem<CameraSystem>();
16
12 Input keystate; 17 Input keystate;
13 18
14 auto map = std::make_unique<Map>("../res/map1.tmx", renderer); 19 auto map = std::make_unique<Map>("../res/map1.tmx", renderer);
@@ -19,10 +24,10 @@ void loop(Renderer& renderer, Mixer& mixer) {
19 int dusterSprite = game.addSprite(Sprite("../res/duster_anim.txt", renderer)); 24 int dusterSprite = game.addSprite(Sprite("../res/duster_anim.txt", renderer));
20 int boneySprite = game.addSprite(Sprite("../res/boney_anim.txt", renderer)); 25 int boneySprite = game.addSprite(Sprite("../res/boney_anim.txt", renderer));
21 26
22 game.getTransformSystem().initSprite(lucasSprite, {32, 32}); 27 game.getSystem<TransformSystem>().initSprite(lucasSprite, {32, 32});
23 game.getTransformSystem().initSprite(kumaSprite, {32, 32}); 28 game.getSystem<TransformSystem>().initSprite(kumaSprite, {32, 32});
24 game.getTransformSystem().initSprite(dusterSprite, {32, 32}); 29 game.getSystem<TransformSystem>().initSprite(dusterSprite, {32, 32});
25 game.getTransformSystem().initSprite(boneySprite, {32, 32}); 30 game.getSystem<TransformSystem>().initSprite(boneySprite, {32, 32});
26 31
27 Party party; 32 Party party;
28 party.addMember(game, lucasSprite); 33 party.addMember(game, lucasSprite);
@@ -30,9 +35,8 @@ void loop(Renderer& renderer, Mixer& mixer) {
30 party.addMember(game, dusterSprite); 35 party.addMember(game, dusterSprite);
31 party.addMember(game, boneySprite); 36 party.addMember(game, boneySprite);
32 37
33 game.setFollowingSprite(lucasSprite); 38 game.getSystem<CameraSystem>().setFollowingSprite(lucasSprite);
34 game.unlockCamera(); 39 game.getSystem<CameraSystem>().unlockCamera();
35 game.tick();
36 40
37 renderer.render(game); 41 renderer.render(game);
38 42
@@ -73,7 +77,9 @@ void loop(Renderer& renderer, Mixer& mixer) {
73 game.tickSpriteAnim(); 77 game.tickSpriteAnim();
74 } 78 }
75 79
76 game.tick(); 80 for (System& system : game.systems()) {
81 system.tick(frameTime);
82 }
77 83
78 renderer.render(game); 84 renderer.render(game);
79 } 85 }
diff --git a/src/party.cpp b/src/party.cpp index 863a5d2..b717281 100644 --- a/src/party.cpp +++ b/src/party.cpp
@@ -1,6 +1,7 @@
1#include "party.h" 1#include "party.h"
2#include "consts.h" 2#include "consts.h"
3#include "mixer.h" 3#include "mixer.h"
4#include "transform_system.h"
4 5
5void Party::addMember(Game& game, int spriteId) { 6void Party::addMember(Game& game, int spriteId) {
6 int index = members_.size(); 7 int index = members_.size();
@@ -156,12 +157,12 @@ void Party::move(Game& game, Mixer& mixer, const Input& keystate) {
156 157
157 // Move everything 158 // Move everything
158 if (pLoc != p1.loc()) { 159 if (pLoc != p1.loc()) {
159 game.getTransformSystem().moveSprite(members_[0].spriteId, pLoc); 160 game.getSystem<TransformSystem>().moveSprite(members_[0].spriteId, pLoc);
160 161
161 for (int i = 1; i < members_.size(); i++) { 162 for (int i = 1; i < members_.size(); i++) {
162 const Sprite& pNext = game.getSprite(members_[i].spriteId); 163 const Sprite& pNext = game.getSprite(members_[i].spriteId);
163 const Movement& posdir = members_[i].movement.front(); 164 const Movement& posdir = members_[i].movement.front();
164 game.getTransformSystem().moveSprite(members_[i].spriteId, posdir.pos); 165 game.getSystem<TransformSystem>().moveSprite(members_[i].spriteId, posdir.pos);
165 game.setSpriteDirection(members_[i].spriteId, posdir.dir); 166 game.setSpriteDirection(members_[i].spriteId, posdir.dir);
166 167
167 members_[i].movement.pop_front(); 168 members_[i].movement.pop_front();
diff --git a/src/renderer.cpp b/src/renderer.cpp index b705310..d9ce396 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp
@@ -2,6 +2,8 @@
2#include "consts.h" 2#include "consts.h"
3#include "game.h" 3#include "game.h"
4#include "map.h" 4#include "map.h"
5#include "transform_system.h"
6#include "camera_system.h"
5 7
6Renderer::Renderer() { 8Renderer::Renderer() {
7 win_ = window_ptr( 9 win_ = window_ptr(
@@ -109,7 +111,7 @@ void Renderer::render(Game& game) {
109 111
110 SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, nullptr); 112 SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, nullptr);
111 113
112 for (const Sprite& sprite : game.getTransformSystem().getSpritesByY() | game.spriteView()) { 114 for (const Sprite& sprite : game.getSystem<TransformSystem>().getSpritesByY() | game.spriteView()) {
113 const SpriteFrame& frame = sprite.getFrame(); 115 const SpriteFrame& frame = sprite.getFrame();
114 const SDL_Rect& src = frame.srcRect; 116 const SDL_Rect& src = frame.srcRect;
115 SDL_Rect dest { sprite.loc().x() - frame.center.x(), sprite.loc().y() - frame.center.y(), frame.size.w(), frame.size.h() }; 117 SDL_Rect dest { sprite.loc().x() - frame.center.x(), sprite.loc().y() - frame.center.y(), frame.size.w(), frame.size.h() };
@@ -119,10 +121,10 @@ void Renderer::render(Game& game) {
119 SDL_RenderCopy(ren_.get(), renLay0_.get(), nullptr, nullptr); 121 SDL_RenderCopy(ren_.get(), renLay0_.get(), nullptr, nullptr);
120 122
121 SDL_Rect cameraField { 123 SDL_Rect cameraField {
122 game.getCameraPosition().x(), 124 game.getSystem<CameraSystem>().getCameraPosition().x(),
123 game.getCameraPosition().y(), 125 game.getSystem<CameraSystem>().getCameraPosition().y(),
124 game.getFieldOfView().w(), 126 game.getSystem<CameraSystem>().getFieldOfView().w(),
125 game.getFieldOfView().h() 127 game.getSystem<CameraSystem>().getFieldOfView().h()
126 }; 128 };
127 129
128 SDL_SetRenderTarget(ren_.get(), nullptr); 130 SDL_SetRenderTarget(ren_.get(), nullptr);
diff --git a/src/system.h b/src/system.h index 684aca9..0fb1bcd 100644 --- a/src/system.h +++ b/src/system.h
@@ -1,7 +1,15 @@
1#ifndef SYSTEM_H_6B40E1B9 1#ifndef SYSTEM_H_6B40E1B9
2#define SYSTEM_H_6B40E1B9 2#define SYSTEM_H_6B40E1B9
3 3
4enum class SystemKey {
5 Transform,
6 Camera
7};
8
4class System { 9class System {
10public:
11
12 virtual void tick(double dt) {}
5}; 13};
6 14
7#endif /* end of include guard: SYSTEM_H_6B40E1B9 */ 15#endif /* end of include guard: SYSTEM_H_6B40E1B9 */
diff --git a/src/transform_system.h b/src/transform_system.h index 4616fc0..290d518 100644 --- a/src/transform_system.h +++ b/src/transform_system.h
@@ -12,6 +12,8 @@ class Game;
12class TransformSystem : public System { 12class TransformSystem : public System {
13public: 13public:
14 14
15 static constexpr SystemKey Key = SystemKey::Transform;
16
15 TransformSystem(Game& game) : game_(game) {} 17 TransformSystem(Game& game) : game_(game) {}
16 18
17 void initSprite(int spriteId, vec2i loc); 19 void initSprite(int spriteId, vec2i loc);