Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/include/chomper/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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*> engine);
struct Info {
bool alive = true;
};

explicit Player(le::input::ScopedActionMapping& mapping, gsl::not_null<Engine const*> 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;
Expand All @@ -33,12 +39,14 @@ class Player : public IController::IListener, public IDebugInspector, public kli

klib::TypedLogger<Player> m_log{};

gsl::not_null<Engine*> m_engine;
gsl::not_null<Engine const*> m_engine;

std::unique_ptr<IController> m_controller{};

Snake m_snake{};

Info m_info{};

Heading m_heading{};
std::vector<Heading> m_headingQueue{};

Expand Down
13 changes: 10 additions & 3 deletions lib/src/player.cpp
Original file line number Diff line number Diff line change
@@ -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 <algorithm>

Expand All @@ -12,11 +11,15 @@ constexpr auto oppositeHeading_v = klib::EnumArray<Heading, Heading>{Heading::We
constexpr auto headingToDir_v = klib::EnumArray<Heading, glm::vec2>{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*> engine) : m_engine(engine) {
Player::Player(le::input::ScopedActionMapping& mapping, gsl::not_null<Engine const*> engine) : m_engine(engine) {
createController(mapping);
}

void Player::tick(kvf::Seconds dt) {
if (!m_info.alive) {
return;
}

m_controller->tick(dt);

m_moveTimer += dt;
Expand All @@ -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;
Expand Down Expand Up @@ -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<runtime::Entrypoint>();
m_info.alive = false;
} else {
m_graceMove = true;
}
Expand Down
6 changes: 6 additions & 0 deletions lib/src/runtimes/game.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "chomper/runtimes/game.hpp"
#include "chomper/im_util.hpp"
#include "chomper/runtimes/entrypoint.hpp"
#include <array>

namespace chomper::runtime {
Expand All @@ -18,6 +19,11 @@ void Game::tick(kvf::Seconds const dt) {
debugInspectWindow();
}
ImGui::End();

// On death
if (!m_player->getInfo().alive) {
m_engine->setNextRuntime<runtime::Entrypoint>();
}
}

void Game::render(le::IRenderer& renderer) const {
Expand Down