diff --git a/lib/include/chomper/player.hpp b/lib/include/chomper/player.hpp index 065b01c..0005964 100644 --- a/lib/include/chomper/player.hpp +++ b/lib/include/chomper/player.hpp @@ -13,11 +13,17 @@ class Engine; class Player : public IController::IListener, public IDebugInspector, public klib::Pinned { public: - explicit Player(le::input::ScopedActionMapping& mapping, gsl::not_null engine); + struct Info { + bool alive = true; + }; + + explicit Player(le::input::ScopedActionMapping& mapping, gsl::not_null engine); void tick(kvf::Seconds dt); void draw(le::IRenderer& renderer) const; + [[nodiscard]] Info const& getInfo() const; + private: [[nodiscard]] bool isCollidingWithSelf(glm::vec2 targetGrid) const; [[nodiscard]] bool isCollidingWithWall(glm::vec2 targetGrid) const; @@ -33,12 +39,14 @@ class Player : public IController::IListener, public IDebugInspector, public kli klib::TypedLogger m_log{}; - gsl::not_null m_engine; + gsl::not_null m_engine; std::unique_ptr m_controller{}; Snake m_snake{}; + Info m_info{}; + Heading m_heading{}; std::vector m_headingQueue{}; diff --git a/lib/src/player.cpp b/lib/src/player.cpp index 9cd6621..aaf5d0f 100644 --- a/lib/src/player.cpp +++ b/lib/src/player.cpp @@ -1,7 +1,6 @@ #include "chomper/player.hpp" #include "chomper/controllers/player_controller.hpp" #include "chomper/engine.hpp" -#include "chomper/runtimes/entrypoint.hpp" #include "chomper/world_space.hpp" #include @@ -12,11 +11,15 @@ constexpr auto oppositeHeading_v = klib::EnumArray{Heading::We constexpr auto headingToDir_v = klib::EnumArray{glm::vec2{1.f, 0.f}, glm::vec2{0.f, 1.f}, glm::vec2{-1.f, 0.f}, glm::vec2{0.f, -1.f}}; } // namespace -Player::Player(le::input::ScopedActionMapping& mapping, gsl::not_null engine) : m_engine(engine) { +Player::Player(le::input::ScopedActionMapping& mapping, gsl::not_null engine) : m_engine(engine) { createController(mapping); } void Player::tick(kvf::Seconds dt) { + if (!m_info.alive) { + return; + } + m_controller->tick(dt); m_moveTimer += dt; @@ -27,6 +30,10 @@ void Player::tick(kvf::Seconds dt) { } } +Player::Info const& Player::getInfo() const { + return m_info; +} + bool Player::isCollidingWithSelf(glm::vec2 const targetGrid) const { if (m_snake.getSegments().empty()) { return false; @@ -57,7 +64,7 @@ void Player::move() { auto const targetGrid = worldSpace::worldToGrid(m_snake.getSegments().back().transform.position) + headingToDir_v[m_heading]; if (isCollidingWithSelf(targetGrid) || isCollidingWithWall(targetGrid)) { if (m_graceMove) { - m_engine->setNextRuntime(); + m_info.alive = false; } else { m_graceMove = true; } diff --git a/lib/src/runtimes/game.cpp b/lib/src/runtimes/game.cpp index 5c2d27a..740158b 100644 --- a/lib/src/runtimes/game.cpp +++ b/lib/src/runtimes/game.cpp @@ -1,5 +1,6 @@ #include "chomper/runtimes/game.hpp" #include "chomper/im_util.hpp" +#include "chomper/runtimes/entrypoint.hpp" #include namespace chomper::runtime { @@ -18,6 +19,11 @@ void Game::tick(kvf::Seconds const dt) { debugInspectWindow(); } ImGui::End(); + + // On death + if (!m_player->getInfo().alive) { + m_engine->setNextRuntime(); + } } void Game::render(le::IRenderer& renderer) const {