diff options
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/game.cpp | 14 | ||||
-rw-r--r-- | src/game.h | 22 | ||||
-rw-r--r-- | src/main.cpp | 5 | ||||
-rw-r--r-- | src/party.cpp | 4 | ||||
-rw-r--r-- | src/renderer.cpp | 2 | ||||
-rw-r--r-- | src/system.h | 7 | ||||
-rw-r--r-- | src/transform_system.cpp | 20 | ||||
-rw-r--r-- | src/transform_system.h | 33 |
9 files changed, 82 insertions, 26 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 6357cb8..da71520 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
@@ -39,6 +39,7 @@ add_executable(tanetane | |||
39 | src/party.cpp | 39 | src/party.cpp |
40 | src/game.cpp | 40 | src/game.cpp |
41 | src/map.cpp | 41 | src/map.cpp |
42 | src/transform_system.cpp | ||
42 | ) | 43 | ) |
43 | 44 | ||
44 | set_property(TARGET tanetane PROPERTY CXX_STANDARD 17) | 45 | set_property(TARGET tanetane PROPERTY CXX_STANDARD 17) |
diff --git a/src/game.cpp b/src/game.cpp index ebb792e..42dbd64 100644 --- a/src/game.cpp +++ b/src/game.cpp | |||
@@ -3,22 +3,10 @@ | |||
3 | int Game::addSprite(Sprite sprite) { | 3 | int Game::addSprite(Sprite sprite) { |
4 | int id = sprites_.size(); | 4 | int id = sprites_.size(); |
5 | sprites_.push_back(std::move(sprite)); | 5 | sprites_.push_back(std::move(sprite)); |
6 | spritesByY_.emplace(sprite.loc().y(), id); | 6 | spriteIds_.push_back(id); |
7 | return id; | 7 | return id; |
8 | } | 8 | } |
9 | 9 | ||
10 | void Game::moveSprite(int id, vec2i newLoc) { | ||
11 | Sprite& sprite = sprites_.at(id); | ||
12 | bool changedY = (sprite.loc().y() != newLoc.y()); | ||
13 | if (changedY) { | ||
14 | spritesByY_.erase(std::make_tuple(sprite.loc().y(), id)); | ||
15 | } | ||
16 | sprite.loc() = newLoc; | ||
17 | if (changedY) { | ||
18 | spritesByY_.emplace(newLoc.y(), id); | ||
19 | } | ||
20 | } | ||
21 | |||
22 | void Game::tick() { | 10 | void Game::tick() { |
23 | if (!cameraLocked_) { | 11 | if (!cameraLocked_) { |
24 | const Sprite& follow = getSprite(followingSprite_); | 12 | const Sprite& follow = getSprite(followingSprite_); |
diff --git a/src/game.h b/src/game.h index a717d53..189cabb 100644 --- a/src/game.h +++ b/src/game.h | |||
@@ -8,6 +8,7 @@ | |||
8 | #include "sprite.h" | 8 | #include "sprite.h" |
9 | #include "map.h" | 9 | #include "map.h" |
10 | #include "consts.h" | 10 | #include "consts.h" |
11 | #include "transform_system.h" | ||
11 | 12 | ||
12 | struct Input { | 13 | struct Input { |
13 | bool left = false; | 14 | bool left = false; |
@@ -19,14 +20,20 @@ struct Input { | |||
19 | class Game { | 20 | class Game { |
20 | public: | 21 | public: |
21 | 22 | ||
23 | Game() : transform_(*this) {} | ||
24 | |||
22 | int addSprite(Sprite sprite); | 25 | int addSprite(Sprite sprite); |
23 | 26 | ||
24 | const Sprite& getSprite(int id) const { | 27 | const Sprite& getSprite(int id) const { |
25 | return sprites_.at(id); | 28 | return sprites_.at(id); |
26 | } | 29 | } |
27 | 30 | ||
28 | const std::vector<Sprite>& getSprites() { | 31 | Sprite& getSprite(int id) { |
29 | return sprites_; | 32 | return sprites_.at(id); |
33 | } | ||
34 | |||
35 | const std::vector<int>& getSprites() { | ||
36 | return spriteIds_; | ||
30 | } | 37 | } |
31 | 38 | ||
32 | auto spriteView() const { | 39 | auto spriteView() const { |
@@ -35,13 +42,8 @@ public: | |||
35 | }); | 42 | }); |
36 | } | 43 | } |
37 | 44 | ||
38 | auto getSpritesByY() const { | 45 | TransformSystem& getTransformSystem() { return transform_; } |
39 | return spritesByY_ | ranges::views::transform([] (const std::tuple<int, int>& val) { | ||
40 | return std::get<1>(val); | ||
41 | }); | ||
42 | } | ||
43 | 46 | ||
44 | void moveSprite(int id, vec2i newLoc); | ||
45 | 47 | ||
46 | void setSpriteState(int id, std::string state) { | 48 | void setSpriteState(int id, std::string state) { |
47 | sprites_[id].setState(std::move(state)); | 49 | sprites_[id].setState(std::move(state)); |
@@ -77,8 +79,10 @@ public: | |||
77 | 79 | ||
78 | private: | 80 | private: |
79 | 81 | ||
82 | TransformSystem transform_; | ||
83 | |||
84 | std::vector<int> spriteIds_; | ||
80 | std::vector<Sprite> sprites_; | 85 | std::vector<Sprite> sprites_; |
81 | std::set<std::tuple<int, int>> spritesByY_; | ||
82 | std::unique_ptr<Map> map_; | 86 | std::unique_ptr<Map> map_; |
83 | 87 | ||
84 | // Camera | 88 | // Camera |
diff --git a/src/main.cpp b/src/main.cpp index 1d1583f..d182290 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
@@ -19,7 +19,10 @@ void loop(Renderer& renderer, Mixer& mixer) { | |||
19 | int dusterSprite = game.addSprite(Sprite("../res/duster_anim.txt", renderer)); | 19 | int dusterSprite = game.addSprite(Sprite("../res/duster_anim.txt", renderer)); |
20 | int boneySprite = game.addSprite(Sprite("../res/boney_anim.txt", renderer)); | 20 | int boneySprite = game.addSprite(Sprite("../res/boney_anim.txt", renderer)); |
21 | 21 | ||
22 | game.moveSprite(lucasSprite, {32,32}); | 22 | game.getTransformSystem().initSprite(lucasSprite, {32, 32}); |
23 | game.getTransformSystem().initSprite(kumaSprite, {32, 32}); | ||
24 | game.getTransformSystem().initSprite(dusterSprite, {32, 32}); | ||
25 | game.getTransformSystem().initSprite(boneySprite, {32, 32}); | ||
23 | 26 | ||
24 | Party party; | 27 | Party party; |
25 | party.addMember(game, lucasSprite); | 28 | party.addMember(game, lucasSprite); |
diff --git a/src/party.cpp b/src/party.cpp index b7284c7..863a5d2 100644 --- a/src/party.cpp +++ b/src/party.cpp | |||
@@ -156,12 +156,12 @@ void Party::move(Game& game, Mixer& mixer, const Input& keystate) { | |||
156 | 156 | ||
157 | // Move everything | 157 | // Move everything |
158 | if (pLoc != p1.loc()) { | 158 | if (pLoc != p1.loc()) { |
159 | game.moveSprite(members_[0].spriteId, pLoc); | 159 | game.getTransformSystem().moveSprite(members_[0].spriteId, pLoc); |
160 | 160 | ||
161 | for (int i = 1; i < members_.size(); i++) { | 161 | for (int i = 1; i < members_.size(); i++) { |
162 | const Sprite& pNext = game.getSprite(members_[i].spriteId); | 162 | const Sprite& pNext = game.getSprite(members_[i].spriteId); |
163 | const Movement& posdir = members_[i].movement.front(); | 163 | const Movement& posdir = members_[i].movement.front(); |
164 | game.moveSprite(members_[i].spriteId, posdir.pos); | 164 | game.getTransformSystem().moveSprite(members_[i].spriteId, posdir.pos); |
165 | game.setSpriteDirection(members_[i].spriteId, posdir.dir); | 165 | game.setSpriteDirection(members_[i].spriteId, posdir.dir); |
166 | 166 | ||
167 | members_[i].movement.pop_front(); | 167 | members_[i].movement.pop_front(); |
diff --git a/src/renderer.cpp b/src/renderer.cpp index 03d5173..b705310 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp | |||
@@ -109,7 +109,7 @@ void Renderer::render(Game& game) { | |||
109 | 109 | ||
110 | SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, nullptr); | 110 | SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, nullptr); |
111 | 111 | ||
112 | for (const Sprite& sprite : game.getSpritesByY() | game.spriteView()) { | 112 | for (const Sprite& sprite : game.getTransformSystem().getSpritesByY() | game.spriteView()) { |
113 | const SpriteFrame& frame = sprite.getFrame(); | 113 | const SpriteFrame& frame = sprite.getFrame(); |
114 | const SDL_Rect& src = frame.srcRect; | 114 | 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() }; | 115 | SDL_Rect dest { sprite.loc().x() - frame.center.x(), sprite.loc().y() - frame.center.y(), frame.size.w(), frame.size.h() }; |
diff --git a/src/system.h b/src/system.h new file mode 100644 index 0000000..684aca9 --- /dev/null +++ b/src/system.h | |||
@@ -0,0 +1,7 @@ | |||
1 | #ifndef SYSTEM_H_6B40E1B9 | ||
2 | #define SYSTEM_H_6B40E1B9 | ||
3 | |||
4 | class System { | ||
5 | }; | ||
6 | |||
7 | #endif /* end of include guard: SYSTEM_H_6B40E1B9 */ | ||
diff --git a/src/transform_system.cpp b/src/transform_system.cpp new file mode 100644 index 0000000..e53fb7d --- /dev/null +++ b/src/transform_system.cpp | |||
@@ -0,0 +1,20 @@ | |||
1 | #include "transform_system.h" | ||
2 | #include "game.h" | ||
3 | |||
4 | void TransformSystem::initSprite(int spriteId, vec2i loc) { | ||
5 | Sprite& sprite = game_.getSprite(spriteId); | ||
6 | sprite.loc() = loc; | ||
7 | spritesByY_.emplace(loc.y(), spriteId); | ||
8 | } | ||
9 | |||
10 | void TransformSystem::moveSprite(int spriteId, vec2i newLoc) { | ||
11 | Sprite& sprite = game_.getSprite(spriteId); | ||
12 | bool changedY = (sprite.loc().y() != newLoc.y()); | ||
13 | if (changedY) { | ||
14 | spritesByY_.erase(std::make_tuple(sprite.loc().y(), spriteId)); | ||
15 | } | ||
16 | sprite.loc() = newLoc; | ||
17 | if (changedY) { | ||
18 | spritesByY_.emplace(newLoc.y(), spriteId); | ||
19 | } | ||
20 | } | ||
diff --git a/src/transform_system.h b/src/transform_system.h new file mode 100644 index 0000000..4616fc0 --- /dev/null +++ b/src/transform_system.h | |||
@@ -0,0 +1,33 @@ | |||
1 | #ifndef TRANSFORM_SYSTEM_H_BA2633BC | ||
2 | #define TRANSFORM_SYSTEM_H_BA2633BC | ||
3 | |||
4 | #include <range/v3/all.hpp> | ||
5 | #include <set> | ||
6 | #include <tuple> | ||
7 | #include "system.h" | ||
8 | #include "vector.h" | ||
9 | |||
10 | class Game; | ||
11 | |||
12 | class TransformSystem : public System { | ||
13 | public: | ||
14 | |||
15 | TransformSystem(Game& game) : game_(game) {} | ||
16 | |||
17 | void initSprite(int spriteId, vec2i loc); | ||
18 | |||
19 | void moveSprite(int spriteId, vec2i newLoc); | ||
20 | |||
21 | auto getSpritesByY() const { | ||
22 | return spritesByY_ | ranges::views::transform([] (const std::tuple<int, int>& val) { | ||
23 | return std::get<1>(val); | ||
24 | }); | ||
25 | } | ||
26 | |||
27 | private: | ||
28 | |||
29 | Game& game_; | ||
30 | std::set<std::tuple<int, int>> spritesByY_; | ||
31 | }; | ||
32 | |||
33 | #endif /* end of include guard: TRANSFORM_SYSTEM_H_BA2633BC */ | ||