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
3 changes: 2 additions & 1 deletion lib/include/chomper/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class Player : public IController::IListener, public IDebugInspector, public kli
void draw(le::IRenderer& renderer) const;

private:
bool selfCollides() const;
[[nodiscard]] bool isCollidingWithSelf(glm::vec2 targetGrid) const;
[[nodiscard]] bool isCollidingWithWall(glm::vec2 targetGrid) const;
void move();

// IController::IListener
Expand Down
5 changes: 5 additions & 0 deletions lib/include/chomper/world_space.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "chomper/world_size.hpp"
#include "glm/common.hpp"
#include <glm/vec2.hpp>

namespace chomper::worldSpace {
Expand All @@ -16,4 +17,8 @@ constexpr auto gridToWorld(glm::vec2 gridPosition) {
constexpr auto worldToGrid(glm::vec2 worldPosition) {
return glm::floor((worldPosition - tileOffset) / tileSize_v) + halfGridSize;
}

constexpr auto isOutOfBounds(glm::vec2 gridPoint) {
return gridPoint.x <= 0 || gridPoint.y <= 0 || gridPoint.x > worldSize_v.x || gridPoint.y > worldSize_v.y;
}
} // namespace chomper::worldSpace
16 changes: 13 additions & 3 deletions lib/src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,22 @@ void Player::tick(kvf::Seconds dt) {
}
}

bool Player::selfCollides() const {
auto targetGrid = worldSpace::worldToGrid(m_snake.getSegments().back().transform.position) + headingToDir_v[m_heading];
bool Player::isCollidingWithSelf(glm::vec2 const targetGrid) const {
if (m_snake.getSegments().empty()) {
return false;
}
return std::ranges::any_of(m_snake.getSegments(), [targetGrid](le::RenderInstance const& s) {
return worldSpace::worldToGrid(s.transform.position) == targetGrid;
});
}

bool Player::isCollidingWithWall(glm::vec2 const targetGrid) const {
if (m_snake.getSegments().empty()) {
return false;
}
return worldSpace::isOutOfBounds(targetGrid);
}

void Player::move() {
// no body, no movement
if (m_snake.getSegments().empty()) {
Expand All @@ -45,7 +54,8 @@ void Player::move() {
m_headingQueue.erase(m_headingQueue.begin());
}

if (selfCollides()) {
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>();
} else {
Expand Down