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/game.cpp | |
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/game.cpp')
-rw-r--r-- | src/game.cpp | 75 |
1 files changed, 27 insertions, 48 deletions
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 | } |