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
21 changes: 0 additions & 21 deletions lib/include/chomper/collectible.hpp

This file was deleted.

4 changes: 2 additions & 2 deletions lib/include/chomper/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class Player : public IController::IListener, public IDebugInspector, public kli
}

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

Expand Down
2 changes: 1 addition & 1 deletion lib/include/chomper/viewport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
#include <le2d/viewport.hpp>

namespace chomper {
constexpr auto viewport_v = le::viewport::Letterbox{.world_size = glm::vec2{800.f}};
constexpr auto viewport_v = le::viewport::Letterbox{.world_size = glm::vec2{800}};
} // namespace chomper
4 changes: 2 additions & 2 deletions lib/include/chomper/world_size.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
#include <glm/vec2.hpp>

namespace chomper {
constexpr auto worldSize_v = glm::vec2{16.f};
constexpr auto tileSize_v = viewport_v.world_size / worldSize_v;
constexpr auto worldSize_v = glm::ivec2{16};
constexpr auto tileSize_v = glm::ivec2{viewport_v.world_size} / worldSize_v;
} // namespace chomper
14 changes: 7 additions & 7 deletions lib/include/chomper/world_space.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@

namespace chomper::worldSpace {
namespace {
auto const halfGridSize = glm::ceil(worldSize_v * 0.5f);
constexpr glm::ivec2 halfGridSize = (worldSize_v + glm::ivec2{1}) / 2;

[[nodiscard]] constexpr auto toTileOffset(float const worldSize, float const tileSize) -> float {
// only add half the tilesize when that axis is even
return (static_cast<int>(worldSize) % 2 == 0) ? tileSize * 0.5f : 0.0f;
}

constexpr auto tileOffset = glm::vec2{toTileOffset(worldSize_v.x, tileSize_v.x), toTileOffset(worldSize_v.y, tileSize_v.y)};
constexpr auto tileOffset = glm::ivec2{toTileOffset(worldSize_v.x, tileSize_v.x), toTileOffset(worldSize_v.y, tileSize_v.y)};
} // namespace

constexpr auto gridToWorld(glm::vec2 gridPosition) {
return ((gridPosition - halfGridSize) * tileSize_v) + tileOffset;
constexpr auto gridToWorld(glm::ivec2 gridPosition) {
return glm::vec2(((gridPosition - halfGridSize) * tileSize_v) + tileOffset);
}
constexpr auto worldToGrid(glm::vec2 worldPosition) {
return glm::floor((worldPosition - tileOffset) / tileSize_v) + halfGridSize;
constexpr auto worldToGrid(glm::ivec2 worldPosition) {
return glm::ivec2((worldPosition - tileOffset) / tileSize_v) + halfGridSize;
}

constexpr auto isOutOfBounds(glm::vec2 gridPoint) {
constexpr auto isOutOfBounds(glm::ivec2 gridPoint) {
return gridPoint.x < 0 || gridPoint.y < 0 || gridPoint.x >= worldSize_v.x || gridPoint.y >= worldSize_v.y;
}
} // namespace chomper::worldSpace
15 changes: 0 additions & 15 deletions lib/src/collectible.cpp

This file was deleted.

10 changes: 5 additions & 5 deletions lib/src/collectibles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ void Collectibles::spawn(Player const& player) {
return v == m_emptyTiles[random];
});
// place the collectible on the tile
auto width = static_cast<int>(worldSize_v.x);
auto width = worldSize_v.x;
m_sprites.instances.emplace_back();
m_sprites.instances.back().transform.position = worldSpace::gridToWorld({tile % width, tile / width});
}
}

void Collectibles::findEmptyTiles(Player const& player) {
m_emptyTiles.clear();
m_emptyTiles.reserve(static_cast<int>(worldSize_v.x * worldSize_v.y));
for (auto i = 0; i < static_cast<int>(worldSize_v.x * worldSize_v.y); i++) {
m_emptyTiles.reserve(worldSize_v.x * worldSize_v.y);
for (auto i = 0; i < worldSize_v.x * worldSize_v.y; i++) {
m_emptyTiles.push_back(i);
}

Expand All @@ -51,12 +51,12 @@ void Collectibles::findEmptyTiles(Player const& player) {

for (auto const& seg : player.getSegments()) {
auto p = worldSpace::worldToGrid(seg.transform.position);
removeTile(static_cast<int>((p.y * worldSize_v.x) + p.x));
removeTile((p.y * worldSize_v.x) + p.x);
}

for (auto const& c : m_sprites.instances) {
auto p = worldSpace::worldToGrid(c.transform.position);
removeTile(static_cast<int>((p.y * worldSize_v.x) + p.x));
removeTile((p.y * worldSize_v.x) + p.x);
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace chomper {
namespace {
constexpr auto moveSpeed_v = kvf::Seconds{0.135f};
constexpr auto oppositeHeading_v = klib::EnumArray<Heading, Heading>{Heading::West, Heading::South, Heading::East, Heading::North};
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}};
constexpr auto headingToDir_v = klib::EnumArray<Heading, glm::ivec2>{glm::ivec2{1, 0}, glm::ivec2{0, 1}, glm::ivec2{-1, 0}, glm::ivec2{0, -1}};
} // namespace

Player::Player(le::input::ScopedActionMapping& mapping, gsl::not_null<Engine const*> engine) : m_engine(engine) {
Expand Down Expand Up @@ -40,7 +40,7 @@ void Player::grow() {
m_shouldPop = false;
}

bool Player::isCollidingWithSelf(glm::vec2 const targetGrid) const {
bool Player::isCollidingWithSelf(glm::ivec2 const targetGrid) const {
if (m_snake.getSegments().empty()) {
return false;
}
Expand All @@ -49,7 +49,7 @@ bool Player::isCollidingWithSelf(glm::vec2 const targetGrid) const {
});
}

bool Player::isCollidingWithWall(glm::vec2 const targetGrid) const {
bool Player::isCollidingWithWall(glm::ivec2 const targetGrid) const {
if (m_snake.getSegments().empty()) {
return false;
}
Expand Down Expand Up @@ -95,7 +95,7 @@ void Player::updateScoreText() {
};

m_scoreText.set_string(m_engine->getResources().getMainFont(), std::format("Score: {}", m_info.score), textParams_v);
m_scoreText.transform.position = worldSpace::gridToWorld({0.0f, worldSize_v.y - 1.0f}) + glm::vec2{0.5f * m_scoreText.get_size().x, 0.0f};
m_scoreText.transform.position = worldSpace::gridToWorld({0, worldSize_v.y - 1}) + glm::vec2{0.5f * m_scoreText.get_size().x, 0.0f};
}

void Player::draw(le::IRenderer& renderer) const {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/snake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace chomper {
namespace {
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}};
constexpr auto headingToDir_v = klib::EnumArray<Heading, glm::ivec2>{glm::ivec2{1, 0}, glm::ivec2{0, 1}, glm::ivec2{-1, 0}, glm::ivec2{0, -1}};
} // namespace

Snake::Snake() {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void World::createGrid() {

m_gridQuad.texture = m_gridTexture.get();
auto const rect = kvf::Rect<>::from_size(viewport_v.world_size);
m_gridQuad.create(rect, kvf::UvRect{.rb = worldSize_v * 0.5f}); // * 0.5f, since each texture is 2x2 tiles
m_gridQuad.create(rect, kvf::UvRect{.rb = glm::vec2(worldSize_v) * 0.5f}); // * 0.5f, since each texture is 2x2 tiles
}

} // namespace chomper