From aac57db782718bf40a7adea15baf8d6b899ea925 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Wed, 17 Feb 2021 20:34:59 -0500 Subject: 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. --- src/camera_system.cpp | 4 --- src/camera_system.h | 2 -- src/character_system.cpp | 34 ++++++++++++++++------ src/character_system.h | 4 +-- src/game.cpp | 75 +++++++++++++++++------------------------------- src/game.h | 6 ++-- src/main.cpp | 42 +++++++++++++++++++++++++-- src/script_system.cpp | 11 +++++-- src/sprite.h | 1 + src/system.h | 2 -- src/transform_system.cpp | 10 ------- src/transform_system.h | 2 -- 12 files changed, 108 insertions(+), 85 deletions(-) (limited to 'src') 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) { } } -void CameraSystem::clearSpriteCache() { - followingSprite_ = -1; -} - vec2i CameraSystem::calculatePosWithCenter(vec2i center) const { const Map& map = game_.getMap(); 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: void destroySprite(int spriteId) override; - void clearSpriteCache() override; - private: 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) { game_.getSystem().setSpriteAnimation(followerId, "still"); } +void CharacterSystem::transplantParty(int leaderId, vec2i pos, Direction dir) { + Sprite& leader = game_.getSprite(leaderId); + CharacterState oldState = leader.characterState; + + std::vector followers = leader.followers; + leader.followers.clear(); + + game_.getSystem().moveSprite(leaderId, pos); + game_.getSystem().setSpriteDirection(leaderId, dir); + + for (int followerId : followers) { + Sprite& follower = game_.getSprite(followerId); + follower.trail.clear(); + + game_.getSystem().moveSprite(followerId, pos); + game_.getSystem().setSpriteDirection(followerId, dir); + addSpriteToParty(leaderId, followerId); + } + + if (oldState == CharacterState::Running) { + startRunning(leaderId); + } else { + setPartyState(leaderId, oldState); + } +} + void CharacterSystem::moveInDirection(int spriteId, Direction dir) { Sprite& sprite = game_.getSprite(spriteId); @@ -266,11 +292,3 @@ void CharacterSystem::destroySprite(int spriteId) { stopRunningSound(sprite); } } - -void CharacterSystem::clearSpriteCache() { - for (Sprite& sprite : game_.getSprites() | game_.spriteView()) { - if (sprite.runningSfxChannel != -1) { - stopRunningSound(sprite); - } - } -} 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: void addSpriteToParty(int leaderId, int followerId); + void transplantParty(int leaderId, vec2i pos, Direction dir); + void moveInDirection(int spriteId, Direction dir); void stopDirecting(int spriteId); @@ -37,8 +39,6 @@ public: void destroySprite(int spriteId) override; - void clearSpriteCache() override; - private: 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 @@ #include "camera_system.h" int Game::emplaceSprite(std::string alias) { - int id = sprites_.size(); - sprites_.emplace_back(); - sprites_.back().alias = alias; + int id; + + if (idsToReuse_.empty()) { + id = sprites_.size(); + sprites_.emplace_back(); + sprites_.back().alias = alias; + } else { + id = idsToReuse_.front(); + idsToReuse_.pop_front(); + sprites_[id] = Sprite(); + sprites_[id].alias = alias; + } + spriteIds_.insert(id); spritesByAlias_[alias] = id; return id; @@ -19,56 +29,28 @@ void Game::destroySprite(int id) { } spriteIds_.erase(id); + idsToReuse_.push_back(id); spritesByAlias_.erase(sprites_.at(id).alias); } -void Game::clearSprites() { - for (std::unique_ptr& system : systems_) { - system->clearSpriteCache(); - } +void Game::loadMap(std::string filename) { + // Clear out non-persistent sprites. + std::list spritesToDelete; + for (int spriteId : spriteIds_) { + Sprite& sprite = sprites_.at(spriteId); - sprites_.clear(); - spriteIds_.clear(); - spritesByAlias_.clear(); -} + if (!sprite.persistent) { + spritesToDelete.push_back(spriteId); + } + } -void Game::loadMap(std::string filename, std::string warpPoint, Direction dir) { - clearSprites(); + for (int spriteId : spritesToDelete) { + destroySprite(spriteId); + } + // Load the new map. map_ = std::make_unique(filename); - vec2i warpLoc = map_->getWarpPoint(warpPoint); - - int lucasSprite = emplaceSprite("lucas"); - getSystem().initSprite(lucasSprite, warpLoc); - getSystem().setUpCollision(lucasSprite, {-8, -8}, {12, 8}, true); - getSystem().initSprite(lucasSprite, "../res/sprites/lucas_anim.txt"); - getSystem().setSpriteDirection(lucasSprite, dir); - getSprite(lucasSprite).hasShadow = true; - getSprite(lucasSprite).player = true; - getSystem().initSprite(lucasSprite); - - int kumaSprite = emplaceSprite("kuma"); - getSystem().initSprite(kumaSprite, warpLoc); - getSystem().initSprite(kumaSprite, "../res/sprites/kuma_anim.txt"); - getSystem().setSpriteDirection(kumaSprite, dir); - getSprite(kumaSprite).hasShadow = true; - getSystem().addSpriteToParty(lucasSprite, kumaSprite); - - int dusterSprite = emplaceSprite("duster"); - getSystem().initSprite(dusterSprite, warpLoc); - getSystem().initSprite(dusterSprite, "../res/sprites/duster_anim.txt"); - getSystem().setSpriteDirection(dusterSprite, dir); - getSprite(dusterSprite).hasShadow = true; - getSystem().addSpriteToParty(lucasSprite, dusterSprite); - - int boneySprite = emplaceSprite("boney"); - getSystem().initSprite(boneySprite, warpLoc); - getSystem().initSprite(boneySprite, "../res/sprites/boney_anim.txt"); - getSystem().setSpriteDirection(boneySprite, dir); - getSprite(boneySprite).hasShadow = true; - getSystem().addSpriteToParty(lucasSprite, boneySprite); - for (const Prototype& p : map_->getPrototypes()) { int spriteId = emplaceSprite(p.name); getSystem().initSprite(spriteId, p.pos); @@ -86,7 +68,4 @@ void Game::loadMap(std::string filename, std::string warpPoint, Direction dir) { getSystem().setUpCollision(spriteId, {0, 0}, t.size, false); getSprite(spriteId).walkthroughScript = t.script; } - - getSystem().setFollowingSprite(lucasSprite); - getSystem().unlockCamera(); } 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 @@ #ifndef GAME_H_E6F1396E #define GAME_H_E6F1396E +#include #include #include #include @@ -76,7 +77,7 @@ public: }); } - void loadMap(std::string filename, std::string warpPoint, Direction dir); + void loadMap(std::string filename); const Map& getMap() const { return *map_; } @@ -84,8 +85,6 @@ public: private: - void clearSprites(); - Mixer mixer_; bool shouldQuit_ = false; @@ -93,6 +92,7 @@ private: std::map systemByKey_; std::set spriteIds_; + std::deque idsToReuse_; std::vector sprites_; std::map spritesByAlias_; std::unique_ptr 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) { game.emplaceSystem(); game.emplaceSystem(); - game.loadMap("map2", "debugWarp_mailboxes", Direction::down); - game.getSprite(game.getSpriteByAlias("lucas")).controllable = true; + game.loadMap("map2"); + + vec2i warpLoc = game.getMap().getWarpPoint("debugWarp_mailboxes"); + + int lucasSprite = game.emplaceSprite("lucas"); + game.getSystem().initSprite(lucasSprite, warpLoc); + game.getSystem().setUpCollision(lucasSprite, {-8, -8}, {12, 8}, true); + game.getSystem().initSprite(lucasSprite, "../res/sprites/lucas_anim.txt"); + game.getSystem().setSpriteDirection(lucasSprite, Direction::down); + game.getSprite(lucasSprite).hasShadow = true; + game.getSprite(lucasSprite).player = true; + game.getSprite(lucasSprite).controllable = true; + game.getSprite(lucasSprite).persistent = true; + game.getSystem().initSprite(lucasSprite); + game.getSystem().setFollowingSprite(lucasSprite); + game.getSystem().unlockCamera(); + + int kumaSprite = game.emplaceSprite("kuma"); + game.getSystem().initSprite(kumaSprite, warpLoc); + game.getSystem().initSprite(kumaSprite, "../res/sprites/kuma_anim.txt"); + game.getSystem().setSpriteDirection(kumaSprite, Direction::down); + game.getSprite(kumaSprite).hasShadow = true; + game.getSprite(kumaSprite).persistent = true; + game.getSystem().addSpriteToParty(lucasSprite, kumaSprite); + + int dusterSprite = game.emplaceSprite("duster"); + game.getSystem().initSprite(dusterSprite, warpLoc); + game.getSystem().initSprite(dusterSprite, "../res/sprites/duster_anim.txt"); + game.getSystem().setSpriteDirection(dusterSprite, Direction::down); + game.getSprite(dusterSprite).hasShadow = true; + game.getSprite(dusterSprite).persistent = true; + game.getSystem().addSpriteToParty(lucasSprite, dusterSprite); + + int boneySprite = game.emplaceSprite("boney"); + game.getSystem().initSprite(boneySprite, warpLoc); + game.getSystem().initSprite(boneySprite, "../res/sprites/boney_anim.txt"); + game.getSystem().setSpriteDirection(boneySprite, Direction::down); + game.getSprite(boneySprite).hasShadow = true; + game.getSprite(boneySprite).persistent = true; + game.getSystem().addSpriteToParty(lucasSprite, boneySprite); renderer.render(game); 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) { engine_.new_usertype( "character", + "transplantParty", &CharacterSystem::transplantParty, "startRunning", &CharacterSystem::startRunning, "halt", &CharacterSystem::halt); @@ -165,8 +166,14 @@ ScriptSystem::ScriptSystem(Game& game) : game_(game) { engine_.set_function( "loadMap", - [&] (std::string filename, std::string warpPoint, Direction dir) { - game_.loadMap(filename, warpPoint, dir); + [&] (std::string filename) { + game_.loadMap(filename); + }); + + engine_.set_function( + "getWarpPoint", + [&] (std::string warp) { + return game_.getMap().getWarpPoint(warp); }); 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 { public: std::string alias; + bool persistent = false; // Transform 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: virtual void tick(double dt) {} virtual void destroySprite(int spriteId) {} - - virtual void clearSpriteCache() {} }; #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) { removeCollidable(spriteId); } } - -void TransformSystem::clearSpriteCache() { - for (auto& layer : spritesByY_) { - layer.clear(); - } - leftCollidables_.clear(); - rightCollidables_.clear(); - upCollidables_.clear(); - downCollidables_.clear(); -} 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: void destroySprite(int spriteId) override; - void clearSpriteCache() override; - private: Game& game_; -- cgit 1.4.1