From 24918837c3ff9026d228657d14852c9cf39a5644 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Wed, 3 Feb 2021 01:11:31 -0500 Subject: Added camera system --- src/camera_system.cpp | 27 +++++++++++++++++++++++++++ src/camera_system.h | 38 ++++++++++++++++++++++++++++++++++++++ src/game.cpp | 22 ---------------------- src/game.h | 48 +++++++++++++++++++++--------------------------- src/main.cpp | 22 ++++++++++++++-------- src/party.cpp | 5 +++-- src/renderer.cpp | 12 +++++++----- src/system.h | 8 ++++++++ src/transform_system.h | 2 ++ 9 files changed, 120 insertions(+), 64 deletions(-) create mode 100644 src/camera_system.cpp create mode 100644 src/camera_system.h (limited to 'src') 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 @@ +#include "camera_system.h" +#include "sprite.h" +#include "game.h" +#include "map.h" + +void CameraSystem::tick(double dt) { + if (!locked_) { + const Sprite& follow = game_.getSprite(followingSprite_); + const Map& map = game_.getMap(); + vec2i mapBounds = map.getMapSize() * map.getTileSize(); + + pos_ = follow.loc() - (fov_ / 2); + + if (pos_.x() < 0) { + pos_.x() = 0; + } + if (pos_.y() < 0) { + pos_.y() = 0; + } + if (pos_.x() + fov_.w() >= mapBounds.w()) { + pos_.x() = mapBounds.w() - fov_.w() - 1; + } + if (pos_.y() + fov_.h() >= mapBounds.h()) { + pos_.y() = mapBounds.h() - fov_.h() - 1; + } + } +} 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 @@ +#ifndef CAMERA_SYSTEM_H_D52ADAD3 +#define CAMERA_SYSTEM_H_D52ADAD3 + +#include "consts.h" +#include "system.h" +#include "vector.h" + +class Game; + +class CameraSystem : public System { +public: + + static constexpr SystemKey Key = SystemKey::Camera; + + CameraSystem(Game& game) : game_(game) {} + + const vec2i& getCameraPosition() const { return pos_; } + + const vec2i& getFieldOfView() const { return fov_; } + + void setFollowingSprite(int spriteId) { followingSprite_ = spriteId; } + + void lockCamera() { locked_ = true; } + + void unlockCamera() { locked_ = false; } + + void tick(double dt) override; + +private: + Game& game_; + + vec2i pos_; + vec2i fov_ { CANVAS_WIDTH, CANVAS_HEIGHT }; + int followingSprite_; + bool locked_ = true; +}; + +#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) { spriteIds_.push_back(id); return id; } - -void Game::tick() { - if (!cameraLocked_) { - const Sprite& follow = getSprite(followingSprite_); - vec2i mapBounds = map_->getMapSize() * map_->getTileSize(); - - cameraPos_ = follow.loc() - (cameraFov_ / 2); - - if (cameraPos_.x() < 0) { - cameraPos_.x() = 0; - } - if (cameraPos_.y() < 0) { - cameraPos_.y() = 0; - } - if (cameraPos_.x() + cameraFov_.w() >= mapBounds.w()) { - cameraPos_.x() = mapBounds.w() - cameraFov_.w() - 1; - } - if (cameraPos_.y() + cameraFov_.h() >= mapBounds.h()) { - cameraPos_.y() = mapBounds.h() - cameraFov_.h() - 1; - } - } -} 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 @@ #ifndef GAME_H_E6F1396E #define GAME_H_E6F1396E -#include +#include #include #include #include +#include #include "sprite.h" #include "map.h" #include "consts.h" -#include "transform_system.h" +#include "system.h" struct Input { bool left = false; @@ -20,7 +21,22 @@ struct Input { class Game { public: - Game() : transform_(*this) {} + template + void emplaceSystem() { + systems_.push_back(std::make_unique(*this)); + systemByKey_[T::Key] = systems_.back().get(); + } + + template + T& getSystem() { + return *dynamic_cast(systemByKey_.at(T::Key)); + } + + auto systems() { + return systems_ | ranges::views::transform([&] (std::unique_ptr& systemPtr) -> System& { + return *systemPtr; + }); + } int addSprite(Sprite sprite); @@ -42,9 +58,6 @@ public: }); } - TransformSystem& getTransformSystem() { return transform_; } - - void setSpriteState(int id, std::string state) { sprites_[id].setState(std::move(state)); } @@ -63,33 +76,14 @@ public: const Map& getMap() const { return *map_; } - // Camera - - const vec2i& getCameraPosition() const { return cameraPos_; } - - const vec2i& getFieldOfView() const { return cameraFov_; } - - void setFollowingSprite(int spriteId) { followingSprite_ = spriteId; } - - void lockCamera() { cameraLocked_ = true; } - - void unlockCamera() { cameraLocked_ = false; } - - void tick(); - private: - TransformSystem transform_; + std::list> systems_; + std::map systemByKey_; std::vector spriteIds_; std::vector sprites_; std::unique_ptr map_; - - // Camera - vec2i cameraPos_; - vec2i cameraFov_ { CANVAS_WIDTH, CANVAS_HEIGHT }; - int followingSprite_; - bool cameraLocked_ = true; }; #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 @@ #include "timer.h" #include "map.h" #include "mixer.h" +#include "transform_system.h" +#include "camera_system.h" void loop(Renderer& renderer, Mixer& mixer) { Game game; + game.emplaceSystem(); + game.emplaceSystem(); + Input keystate; auto map = std::make_unique("../res/map1.tmx", renderer); @@ -19,10 +24,10 @@ void loop(Renderer& renderer, Mixer& mixer) { int dusterSprite = game.addSprite(Sprite("../res/duster_anim.txt", renderer)); int boneySprite = game.addSprite(Sprite("../res/boney_anim.txt", renderer)); - game.getTransformSystem().initSprite(lucasSprite, {32, 32}); - game.getTransformSystem().initSprite(kumaSprite, {32, 32}); - game.getTransformSystem().initSprite(dusterSprite, {32, 32}); - game.getTransformSystem().initSprite(boneySprite, {32, 32}); + game.getSystem().initSprite(lucasSprite, {32, 32}); + game.getSystem().initSprite(kumaSprite, {32, 32}); + game.getSystem().initSprite(dusterSprite, {32, 32}); + game.getSystem().initSprite(boneySprite, {32, 32}); Party party; party.addMember(game, lucasSprite); @@ -30,9 +35,8 @@ void loop(Renderer& renderer, Mixer& mixer) { party.addMember(game, dusterSprite); party.addMember(game, boneySprite); - game.setFollowingSprite(lucasSprite); - game.unlockCamera(); - game.tick(); + game.getSystem().setFollowingSprite(lucasSprite); + game.getSystem().unlockCamera(); renderer.render(game); @@ -73,7 +77,9 @@ void loop(Renderer& renderer, Mixer& mixer) { game.tickSpriteAnim(); } - game.tick(); + for (System& system : game.systems()) { + system.tick(frameTime); + } renderer.render(game); } 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 @@ #include "party.h" #include "consts.h" #include "mixer.h" +#include "transform_system.h" void Party::addMember(Game& game, int spriteId) { int index = members_.size(); @@ -156,12 +157,12 @@ void Party::move(Game& game, Mixer& mixer, const Input& keystate) { // Move everything if (pLoc != p1.loc()) { - game.getTransformSystem().moveSprite(members_[0].spriteId, pLoc); + game.getSystem().moveSprite(members_[0].spriteId, pLoc); for (int i = 1; i < members_.size(); i++) { const Sprite& pNext = game.getSprite(members_[i].spriteId); const Movement& posdir = members_[i].movement.front(); - game.getTransformSystem().moveSprite(members_[i].spriteId, posdir.pos); + game.getSystem().moveSprite(members_[i].spriteId, posdir.pos); game.setSpriteDirection(members_[i].spriteId, posdir.dir); 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 @@ #include "consts.h" #include "game.h" #include "map.h" +#include "transform_system.h" +#include "camera_system.h" Renderer::Renderer() { win_ = window_ptr( @@ -109,7 +111,7 @@ void Renderer::render(Game& game) { SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, nullptr); - for (const Sprite& sprite : game.getTransformSystem().getSpritesByY() | game.spriteView()) { + for (const Sprite& sprite : game.getSystem().getSpritesByY() | game.spriteView()) { const SpriteFrame& frame = sprite.getFrame(); const SDL_Rect& src = frame.srcRect; 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) { SDL_RenderCopy(ren_.get(), renLay0_.get(), nullptr, nullptr); SDL_Rect cameraField { - game.getCameraPosition().x(), - game.getCameraPosition().y(), - game.getFieldOfView().w(), - game.getFieldOfView().h() + game.getSystem().getCameraPosition().x(), + game.getSystem().getCameraPosition().y(), + game.getSystem().getFieldOfView().w(), + game.getSystem().getFieldOfView().h() }; 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 @@ #ifndef SYSTEM_H_6B40E1B9 #define SYSTEM_H_6B40E1B9 +enum class SystemKey { + Transform, + Camera +}; + class System { +public: + + virtual void tick(double dt) {} }; #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; class TransformSystem : public System { public: + static constexpr SystemKey Key = SystemKey::Transform; + TransformSystem(Game& game) : game_(game) {} void initSprite(int spriteId, vec2i loc); -- cgit 1.4.1