diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-17 20:34:59 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-17 20:34:59 -0500 |
commit | aac57db782718bf40a7adea15baf8d6b899ea925 (patch) | |
tree | c9f32563cdb9a06e6e24343946c5bb35dc254b76 /src | |
parent | b3720c4a401f345c49eadabdb852968e273e7077 (diff) | |
download | tanetane-aac57db782718bf40a7adea15baf8d6b899ea925.tar.gz tanetane-aac57db782718bf40a7adea15baf8d6b899ea925.tar.bz2 tanetane-aac57db782718bf40a7adea15baf8d6b899ea925.zip |
Made some sprites persist between map changes
The player party characters are now initialised at the start of the game and are no longer re-created after every map change. The script system takes care of moving the player into the correct position on the new map. Deleted sprite IDs are now reused the next time a sprite is created, instead of throwing out everything between maps.
Diffstat (limited to 'src')
-rw-r--r-- | src/camera_system.cpp | 4 | ||||
-rw-r--r-- | src/camera_system.h | 2 | ||||
-rw-r--r-- | src/character_system.cpp | 34 | ||||
-rw-r--r-- | src/character_system.h | 4 | ||||
-rw-r--r-- | src/game.cpp | 75 | ||||
-rw-r--r-- | src/game.h | 6 | ||||
-rw-r--r-- | src/main.cpp | 42 | ||||
-rw-r--r-- | src/script_system.cpp | 11 | ||||
-rw-r--r-- | src/sprite.h | 1 | ||||
-rw-r--r-- | src/system.h | 2 | ||||
-rw-r--r-- | src/transform_system.cpp | 10 | ||||
-rw-r--r-- | src/transform_system.h | 2 |
12 files changed, 108 insertions, 85 deletions
diff --git a/src/camera_system.cpp b/src/camera_system.cpp index 2d7be61..c46b9dd 100644 --- a/src/camera_system.cpp +++ b/src/camera_system.cpp | |||
@@ -45,10 +45,6 @@ void CameraSystem::destroySprite(int spriteId) { | |||
45 | } | 45 | } |
46 | } | 46 | } |
47 | 47 | ||
48 | void CameraSystem::clearSpriteCache() { | ||
49 | followingSprite_ = -1; | ||
50 | } | ||
51 | |||
52 | vec2i CameraSystem::calculatePosWithCenter(vec2i center) const { | 48 | vec2i CameraSystem::calculatePosWithCenter(vec2i center) const { |
53 | const Map& map = game_.getMap(); | 49 | const Map& map = game_.getMap(); |
54 | vec2i mapBounds = map.getMapSize() * map.getTileSize(); | 50 | vec2i mapBounds = map.getMapSize() * map.getTileSize(); |
diff --git a/src/camera_system.h b/src/camera_system.h index a2dee93..8c9419c 100644 --- a/src/camera_system.h +++ b/src/camera_system.h | |||
@@ -35,8 +35,6 @@ public: | |||
35 | 35 | ||
36 | void destroySprite(int spriteId) override; | 36 | void destroySprite(int spriteId) override; |
37 | 37 | ||
38 | void clearSpriteCache() override; | ||
39 | |||
40 | private: | 38 | private: |
41 | 39 | ||
42 | vec2i calculatePosWithCenter(vec2i center) const; | 40 | vec2i calculatePosWithCenter(vec2i center) const; |
diff --git a/src/character_system.cpp b/src/character_system.cpp index d0c416e..7d456f6 100644 --- a/src/character_system.cpp +++ b/src/character_system.cpp | |||
@@ -39,6 +39,32 @@ void CharacterSystem::addSpriteToParty(int leaderId, int followerId) { | |||
39 | game_.getSystem<AnimationSystem>().setSpriteAnimation(followerId, "still"); | 39 | game_.getSystem<AnimationSystem>().setSpriteAnimation(followerId, "still"); |
40 | } | 40 | } |
41 | 41 | ||
42 | void CharacterSystem::transplantParty(int leaderId, vec2i pos, Direction dir) { | ||
43 | Sprite& leader = game_.getSprite(leaderId); | ||
44 | CharacterState oldState = leader.characterState; | ||
45 | |||
46 | std::vector<int> followers = leader.followers; | ||
47 | leader.followers.clear(); | ||
48 | |||
49 | game_.getSystem<TransformSystem>().moveSprite(leaderId, pos); | ||
50 | game_.getSystem<AnimationSystem>().setSpriteDirection(leaderId, dir); | ||
51 | |||
52 | for (int followerId : followers) { | ||
53 | Sprite& follower = game_.getSprite(followerId); | ||
54 | follower.trail.clear(); | ||
55 | |||
56 | game_.getSystem<TransformSystem>().moveSprite(followerId, pos); | ||
57 | game_.getSystem<AnimationSystem>().setSpriteDirection(followerId, dir); | ||
58 | addSpriteToParty(leaderId, followerId); | ||
59 | } | ||
60 | |||
61 | if (oldState == CharacterState::Running) { | ||
62 | startRunning(leaderId); | ||
63 | } else { | ||
64 | setPartyState(leaderId, oldState); | ||
65 | } | ||
66 | } | ||
67 | |||
42 | void CharacterSystem::moveInDirection(int spriteId, Direction dir) { | 68 | void CharacterSystem::moveInDirection(int spriteId, Direction dir) { |
43 | Sprite& sprite = game_.getSprite(spriteId); | 69 | Sprite& sprite = game_.getSprite(spriteId); |
44 | 70 | ||
@@ -266,11 +292,3 @@ void CharacterSystem::destroySprite(int spriteId) { | |||
266 | stopRunningSound(sprite); | 292 | stopRunningSound(sprite); |
267 | } | 293 | } |
268 | } | 294 | } |
269 | |||
270 | void CharacterSystem::clearSpriteCache() { | ||
271 | for (Sprite& sprite : game_.getSprites() | game_.spriteView()) { | ||
272 | if (sprite.runningSfxChannel != -1) { | ||
273 | stopRunningSound(sprite); | ||
274 | } | ||
275 | } | ||
276 | } | ||
diff --git a/src/character_system.h b/src/character_system.h index 72a2585..8c2ea54 100644 --- a/src/character_system.h +++ b/src/character_system.h | |||
@@ -23,6 +23,8 @@ public: | |||
23 | 23 | ||
24 | void addSpriteToParty(int leaderId, int followerId); | 24 | void addSpriteToParty(int leaderId, int followerId); |
25 | 25 | ||
26 | void transplantParty(int leaderId, vec2i pos, Direction dir); | ||
27 | |||
26 | void moveInDirection(int spriteId, Direction dir); | 28 | void moveInDirection(int spriteId, Direction dir); |
27 | 29 | ||
28 | void stopDirecting(int spriteId); | 30 | void stopDirecting(int spriteId); |
@@ -37,8 +39,6 @@ public: | |||
37 | 39 | ||
38 | void destroySprite(int spriteId) override; | 40 | void destroySprite(int spriteId) override; |
39 | 41 | ||
40 | void clearSpriteCache() override; | ||
41 | |||
42 | private: | 42 | private: |
43 | 43 | ||
44 | void stopRunning(int spriteId); | 44 | void stopRunning(int spriteId); |
diff --git a/src/game.cpp b/src/game.cpp index b7ca869..ffa52cf 100644 --- a/src/game.cpp +++ b/src/game.cpp | |||
@@ -5,9 +5,19 @@ | |||
5 | #include "camera_system.h" | 5 | #include "camera_system.h" |
6 | 6 | ||
7 | int Game::emplaceSprite(std::string alias) { | 7 | int Game::emplaceSprite(std::string alias) { |
8 | int id = sprites_.size(); | 8 | int id; |
9 | sprites_.emplace_back(); | 9 | |
10 | sprites_.back().alias = alias; | 10 | if (idsToReuse_.empty()) { |
11 | id = sprites_.size(); | ||
12 | sprites_.emplace_back(); | ||
13 | sprites_.back().alias = alias; | ||
14 | } else { | ||
15 | id = idsToReuse_.front(); | ||
16 | idsToReuse_.pop_front(); | ||
17 | sprites_[id] = Sprite(); | ||
18 | sprites_[id].alias = alias; | ||
19 | } | ||
20 | |||
11 | spriteIds_.insert(id); | 21 | spriteIds_.insert(id); |
12 | spritesByAlias_[alias] = id; | 22 | spritesByAlias_[alias] = id; |
13 | return id; | 23 | return id; |
@@ -19,56 +29,28 @@ void Game::destroySprite(int id) { | |||
19 | } | 29 | } |
20 | 30 | ||
21 | spriteIds_.erase(id); | 31 | spriteIds_.erase(id); |
32 | idsToReuse_.push_back(id); | ||
22 | spritesByAlias_.erase(sprites_.at(id).alias); | 33 | spritesByAlias_.erase(sprites_.at(id).alias); |
23 | } | 34 | } |
24 | 35 | ||
25 | void Game::clearSprites() { | 36 | void Game::loadMap(std::string filename) { |
26 | for (std::unique_ptr<System>& system : systems_) { | 37 | // Clear out non-persistent sprites. |
27 | system->clearSpriteCache(); | 38 | std::list<int> spritesToDelete; |
28 | } | 39 | for (int spriteId : spriteIds_) { |
40 | Sprite& sprite = sprites_.at(spriteId); | ||
29 | 41 | ||
30 | sprites_.clear(); | 42 | if (!sprite.persistent) { |
31 | spriteIds_.clear(); | 43 | spritesToDelete.push_back(spriteId); |
32 | spritesByAlias_.clear(); | 44 | } |
33 | } | 45 | } |
34 | 46 | ||
35 | void Game::loadMap(std::string filename, std::string warpPoint, Direction dir) { | 47 | for (int spriteId : spritesToDelete) { |
36 | clearSprites(); | 48 | destroySprite(spriteId); |
49 | } | ||
37 | 50 | ||
51 | // Load the new map. | ||
38 | map_ = std::make_unique<Map>(filename); | 52 | map_ = std::make_unique<Map>(filename); |
39 | 53 | ||
40 | vec2i warpLoc = map_->getWarpPoint(warpPoint); | ||
41 | |||
42 | int lucasSprite = emplaceSprite("lucas"); | ||
43 | getSystem<TransformSystem>().initSprite(lucasSprite, warpLoc); | ||
44 | getSystem<TransformSystem>().setUpCollision(lucasSprite, {-8, -8}, {12, 8}, true); | ||
45 | getSystem<AnimationSystem>().initSprite(lucasSprite, "../res/sprites/lucas_anim.txt"); | ||
46 | getSystem<AnimationSystem>().setSpriteDirection(lucasSprite, dir); | ||
47 | getSprite(lucasSprite).hasShadow = true; | ||
48 | getSprite(lucasSprite).player = true; | ||
49 | getSystem<CharacterSystem>().initSprite(lucasSprite); | ||
50 | |||
51 | int kumaSprite = emplaceSprite("kuma"); | ||
52 | getSystem<TransformSystem>().initSprite(kumaSprite, warpLoc); | ||
53 | getSystem<AnimationSystem>().initSprite(kumaSprite, "../res/sprites/kuma_anim.txt"); | ||
54 | getSystem<AnimationSystem>().setSpriteDirection(kumaSprite, dir); | ||
55 | getSprite(kumaSprite).hasShadow = true; | ||
56 | getSystem<CharacterSystem>().addSpriteToParty(lucasSprite, kumaSprite); | ||
57 | |||
58 | int dusterSprite = emplaceSprite("duster"); | ||
59 | getSystem<TransformSystem>().initSprite(dusterSprite, warpLoc); | ||
60 | getSystem<AnimationSystem>().initSprite(dusterSprite, "../res/sprites/duster_anim.txt"); | ||
61 | getSystem<AnimationSystem>().setSpriteDirection(dusterSprite, dir); | ||
62 | getSprite(dusterSprite).hasShadow = true; | ||
63 | getSystem<CharacterSystem>().addSpriteToParty(lucasSprite, dusterSprite); | ||
64 | |||
65 | int boneySprite = emplaceSprite("boney"); | ||
66 | getSystem<TransformSystem>().initSprite(boneySprite, warpLoc); | ||
67 | getSystem<AnimationSystem>().initSprite(boneySprite, "../res/sprites/boney_anim.txt"); | ||
68 | getSystem<AnimationSystem>().setSpriteDirection(boneySprite, dir); | ||
69 | getSprite(boneySprite).hasShadow = true; | ||
70 | getSystem<CharacterSystem>().addSpriteToParty(lucasSprite, boneySprite); | ||
71 | |||
72 | for (const Prototype& p : map_->getPrototypes()) { | 54 | for (const Prototype& p : map_->getPrototypes()) { |
73 | int spriteId = emplaceSprite(p.name); | 55 | int spriteId = emplaceSprite(p.name); |
74 | getSystem<TransformSystem>().initSprite(spriteId, p.pos); | 56 | getSystem<TransformSystem>().initSprite(spriteId, p.pos); |
@@ -86,7 +68,4 @@ void Game::loadMap(std::string filename, std::string warpPoint, Direction dir) { | |||
86 | getSystem<TransformSystem>().setUpCollision(spriteId, {0, 0}, t.size, false); | 68 | getSystem<TransformSystem>().setUpCollision(spriteId, {0, 0}, t.size, false); |
87 | getSprite(spriteId).walkthroughScript = t.script; | 69 | getSprite(spriteId).walkthroughScript = t.script; |
88 | } | 70 | } |
89 | |||
90 | getSystem<CameraSystem>().setFollowingSprite(lucasSprite); | ||
91 | getSystem<CameraSystem>().unlockCamera(); | ||
92 | } | 71 | } |
diff --git a/src/game.h b/src/game.h index 736fa7a..9340c65 100644 --- a/src/game.h +++ b/src/game.h | |||
@@ -1,6 +1,7 @@ | |||
1 | #ifndef GAME_H_E6F1396E | 1 | #ifndef GAME_H_E6F1396E |
2 | #define GAME_H_E6F1396E | 2 | #define GAME_H_E6F1396E |
3 | 3 | ||
4 | #include <deque> | ||
4 | #include <list> | 5 | #include <list> |
5 | #include <range/v3/all.hpp> | 6 | #include <range/v3/all.hpp> |
6 | #include <vector> | 7 | #include <vector> |
@@ -76,7 +77,7 @@ public: | |||
76 | }); | 77 | }); |
77 | } | 78 | } |
78 | 79 | ||
79 | void loadMap(std::string filename, std::string warpPoint, Direction dir); | 80 | void loadMap(std::string filename); |
80 | 81 | ||
81 | const Map& getMap() const { return *map_; } | 82 | const Map& getMap() const { return *map_; } |
82 | 83 | ||
@@ -84,8 +85,6 @@ public: | |||
84 | 85 | ||
85 | private: | 86 | private: |
86 | 87 | ||
87 | void clearSprites(); | ||
88 | |||
89 | Mixer mixer_; | 88 | Mixer mixer_; |
90 | bool shouldQuit_ = false; | 89 | bool shouldQuit_ = false; |
91 | 90 | ||
@@ -93,6 +92,7 @@ private: | |||
93 | std::map<SystemKey, System*> systemByKey_; | 92 | std::map<SystemKey, System*> systemByKey_; |
94 | 93 | ||
95 | std::set<int> spriteIds_; | 94 | std::set<int> spriteIds_; |
95 | std::deque<int> idsToReuse_; | ||
96 | std::vector<Sprite> sprites_; | 96 | std::vector<Sprite> sprites_; |
97 | std::map<std::string, int> spritesByAlias_; | 97 | std::map<std::string, int> spritesByAlias_; |
98 | std::unique_ptr<Map> map_; | 98 | std::unique_ptr<Map> map_; |
diff --git a/src/main.cpp b/src/main.cpp index 79fa306..f220dc0 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
@@ -24,8 +24,46 @@ void loop(Renderer& renderer) { | |||
24 | game.emplaceSystem<MessageSystem>(); | 24 | game.emplaceSystem<MessageSystem>(); |
25 | game.emplaceSystem<EffectSystem>(); | 25 | game.emplaceSystem<EffectSystem>(); |
26 | 26 | ||
27 | game.loadMap("map2", "debugWarp_mailboxes", Direction::down); | 27 | game.loadMap("map2"); |
28 | game.getSprite(game.getSpriteByAlias("lucas")).controllable = true; | 28 | |
29 | vec2i warpLoc = game.getMap().getWarpPoint("debugWarp_mailboxes"); | ||
30 | |||
31 | int lucasSprite = game.emplaceSprite("lucas"); | ||
32 | game.getSystem<TransformSystem>().initSprite(lucasSprite, warpLoc); | ||
33 | game.getSystem<TransformSystem>().setUpCollision(lucasSprite, {-8, -8}, {12, 8}, true); | ||
34 | game.getSystem<AnimationSystem>().initSprite(lucasSprite, "../res/sprites/lucas_anim.txt"); | ||
35 | game.getSystem<AnimationSystem>().setSpriteDirection(lucasSprite, Direction::down); | ||
36 | game.getSprite(lucasSprite).hasShadow = true; | ||
37 | game.getSprite(lucasSprite).player = true; | ||
38 | game.getSprite(lucasSprite).controllable = true; | ||
39 | game.getSprite(lucasSprite).persistent = true; | ||
40 | game.getSystem<CharacterSystem>().initSprite(lucasSprite); | ||
41 | game.getSystem<CameraSystem>().setFollowingSprite(lucasSprite); | ||
42 | game.getSystem<CameraSystem>().unlockCamera(); | ||
43 | |||
44 | int kumaSprite = game.emplaceSprite("kuma"); | ||
45 | game.getSystem<TransformSystem>().initSprite(kumaSprite, warpLoc); | ||
46 | game.getSystem<AnimationSystem>().initSprite(kumaSprite, "../res/sprites/kuma_anim.txt"); | ||
47 | game.getSystem<AnimationSystem>().setSpriteDirection(kumaSprite, Direction::down); | ||
48 | game.getSprite(kumaSprite).hasShadow = true; | ||
49 | game.getSprite(kumaSprite).persistent = true; | ||
50 | game.getSystem<CharacterSystem>().addSpriteToParty(lucasSprite, kumaSprite); | ||
51 | |||
52 | int dusterSprite = game.emplaceSprite("duster"); | ||
53 | game.getSystem<TransformSystem>().initSprite(dusterSprite, warpLoc); | ||
54 | game.getSystem<AnimationSystem>().initSprite(dusterSprite, "../res/sprites/duster_anim.txt"); | ||
55 | game.getSystem<AnimationSystem>().setSpriteDirection(dusterSprite, Direction::down); | ||
56 | game.getSprite(dusterSprite).hasShadow = true; | ||
57 | game.getSprite(dusterSprite).persistent = true; | ||
58 | game.getSystem<CharacterSystem>().addSpriteToParty(lucasSprite, dusterSprite); | ||
59 | |||
60 | int boneySprite = game.emplaceSprite("boney"); | ||
61 | game.getSystem<TransformSystem>().initSprite(boneySprite, warpLoc); | ||
62 | game.getSystem<AnimationSystem>().initSprite(boneySprite, "../res/sprites/boney_anim.txt"); | ||
63 | game.getSystem<AnimationSystem>().setSpriteDirection(boneySprite, Direction::down); | ||
64 | game.getSprite(boneySprite).hasShadow = true; | ||
65 | game.getSprite(boneySprite).persistent = true; | ||
66 | game.getSystem<CharacterSystem>().addSpriteToParty(lucasSprite, boneySprite); | ||
29 | 67 | ||
30 | renderer.render(game); | 68 | renderer.render(game); |
31 | 69 | ||
diff --git a/src/script_system.cpp b/src/script_system.cpp index 8674f97..090bf46 100644 --- a/src/script_system.cpp +++ b/src/script_system.cpp | |||
@@ -54,6 +54,7 @@ ScriptSystem::ScriptSystem(Game& game) : game_(game) { | |||
54 | 54 | ||
55 | engine_.new_usertype<CharacterSystem>( | 55 | engine_.new_usertype<CharacterSystem>( |
56 | "character", | 56 | "character", |
57 | "transplantParty", &CharacterSystem::transplantParty, | ||
57 | "startRunning", &CharacterSystem::startRunning, | 58 | "startRunning", &CharacterSystem::startRunning, |
58 | "halt", &CharacterSystem::halt); | 59 | "halt", &CharacterSystem::halt); |
59 | 60 | ||
@@ -165,8 +166,14 @@ ScriptSystem::ScriptSystem(Game& game) : game_(game) { | |||
165 | 166 | ||
166 | engine_.set_function( | 167 | engine_.set_function( |
167 | "loadMap", | 168 | "loadMap", |
168 | [&] (std::string filename, std::string warpPoint, Direction dir) { | 169 | [&] (std::string filename) { |
169 | game_.loadMap(filename, warpPoint, dir); | 170 | game_.loadMap(filename); |
171 | }); | ||
172 | |||
173 | engine_.set_function( | ||
174 | "getWarpPoint", | ||
175 | [&] (std::string warp) { | ||
176 | return game_.getMap().getWarpPoint(warp); | ||
170 | }); | 177 | }); |
171 | 178 | ||
172 | engine_.script_file("../res/scripts/common.lua"); | 179 | engine_.script_file("../res/scripts/common.lua"); |
diff --git a/src/sprite.h b/src/sprite.h index 84b161f..19dbf92 100644 --- a/src/sprite.h +++ b/src/sprite.h | |||
@@ -42,6 +42,7 @@ class Sprite { | |||
42 | public: | 42 | public: |
43 | 43 | ||
44 | std::string alias; | 44 | std::string alias; |
45 | bool persistent = false; | ||
45 | 46 | ||
46 | // Transform | 47 | // Transform |
47 | vec2i loc { 0, 0 }; | 48 | vec2i loc { 0, 0 }; |
diff --git a/src/system.h b/src/system.h index 0cdc205..01d6cec 100644 --- a/src/system.h +++ b/src/system.h | |||
@@ -18,8 +18,6 @@ public: | |||
18 | virtual void tick(double dt) {} | 18 | virtual void tick(double dt) {} |
19 | 19 | ||
20 | virtual void destroySprite(int spriteId) {} | 20 | virtual void destroySprite(int spriteId) {} |
21 | |||
22 | virtual void clearSpriteCache() {} | ||
23 | }; | 21 | }; |
24 | 22 | ||
25 | #endif /* end of include guard: SYSTEM_H_6B40E1B9 */ | 23 | #endif /* end of include guard: SYSTEM_H_6B40E1B9 */ |
diff --git a/src/transform_system.cpp b/src/transform_system.cpp index 6e04d70..144477b 100644 --- a/src/transform_system.cpp +++ b/src/transform_system.cpp | |||
@@ -225,13 +225,3 @@ void TransformSystem::destroySprite(int spriteId) { | |||
225 | removeCollidable(spriteId); | 225 | removeCollidable(spriteId); |
226 | } | 226 | } |
227 | } | 227 | } |
228 | |||
229 | void TransformSystem::clearSpriteCache() { | ||
230 | for (auto& layer : spritesByY_) { | ||
231 | layer.clear(); | ||
232 | } | ||
233 | leftCollidables_.clear(); | ||
234 | rightCollidables_.clear(); | ||
235 | upCollidables_.clear(); | ||
236 | downCollidables_.clear(); | ||
237 | } | ||
diff --git a/src/transform_system.h b/src/transform_system.h index a21b93b..4ee481e 100644 --- a/src/transform_system.h +++ b/src/transform_system.h | |||
@@ -47,8 +47,6 @@ public: | |||
47 | 47 | ||
48 | void destroySprite(int spriteId) override; | 48 | void destroySprite(int spriteId) override; |
49 | 49 | ||
50 | void clearSpriteCache() override; | ||
51 | |||
52 | private: | 50 | private: |
53 | 51 | ||
54 | Game& game_; | 52 | Game& game_; |