diff --git a/lib/include/chomper/collectible.hpp b/lib/include/chomper/collectible.hpp deleted file mode 100644 index 5059c62..0000000 --- a/lib/include/chomper/collectible.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once -#include "chomper/world_space.hpp" -#include -#include -#include - -namespace chomper { -class Collectible { - public: - explicit Collectible(le::ITexture const& texture, glm::vec2 position); - - void draw(le::IRenderer& renderer) const; - - [[nodiscard]] glm::vec2 getGridPosition() const { - return worldSpace::worldToGrid(m_sprite.transform.position); - } - - private: - le::drawable::Sprite m_sprite{}; -}; -} // namespace chomper diff --git a/lib/include/chomper/player.hpp b/lib/include/chomper/player.hpp index b507e1b..0e5f60d 100644 --- a/lib/include/chomper/player.hpp +++ b/lib/include/chomper/player.hpp @@ -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(); diff --git a/lib/include/chomper/viewport.hpp b/lib/include/chomper/viewport.hpp index 2c6307e..a308c35 100644 --- a/lib/include/chomper/viewport.hpp +++ b/lib/include/chomper/viewport.hpp @@ -2,5 +2,5 @@ #include 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 diff --git a/lib/include/chomper/world_size.hpp b/lib/include/chomper/world_size.hpp index 3339213..6b0c8e8 100644 --- a/lib/include/chomper/world_size.hpp +++ b/lib/include/chomper/world_size.hpp @@ -3,6 +3,6 @@ #include 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 diff --git a/lib/include/chomper/world_space.hpp b/lib/include/chomper/world_space.hpp index 6b002f0..bfce10a 100644 --- a/lib/include/chomper/world_space.hpp +++ b/lib/include/chomper/world_space.hpp @@ -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(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 diff --git a/lib/src/collectible.cpp b/lib/src/collectible.cpp deleted file mode 100644 index ba7da42..0000000 --- a/lib/src/collectible.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "chomper/collectible.hpp" -#include "chomper/world_size.hpp" -#include - -namespace chomper { -Collectible::Collectible(le::ITexture const& texture, glm::vec2 position) { - m_sprite.set_base_size(tileSize_v); - m_sprite.set_texture(&texture); - m_sprite.transform.position = position; -} - -void Collectible::draw(le::IRenderer& renderer) const { - m_sprite.draw(renderer); -} -} // namespace chomper diff --git a/lib/src/collectibles.cpp b/lib/src/collectibles.cpp index 391b1dd..be815e9 100644 --- a/lib/src/collectibles.cpp +++ b/lib/src/collectibles.cpp @@ -28,7 +28,7 @@ void Collectibles::spawn(Player const& player) { return v == m_emptyTiles[random]; }); // place the collectible on the tile - auto width = static_cast(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}); } @@ -36,8 +36,8 @@ void Collectibles::spawn(Player const& player) { void Collectibles::findEmptyTiles(Player const& player) { m_emptyTiles.clear(); - m_emptyTiles.reserve(static_cast(worldSize_v.x * worldSize_v.y)); - for (auto i = 0; i < static_cast(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); } @@ -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((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((p.y * worldSize_v.x) + p.x)); + removeTile((p.y * worldSize_v.x) + p.x); } } diff --git a/lib/src/player.cpp b/lib/src/player.cpp index c345ce7..f536de3 100644 --- a/lib/src/player.cpp +++ b/lib/src/player.cpp @@ -10,7 +10,7 @@ namespace chomper { namespace { constexpr auto moveSpeed_v = kvf::Seconds{0.135f}; constexpr auto oppositeHeading_v = klib::EnumArray{Heading::West, Heading::South, Heading::East, Heading::North}; -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}}; +constexpr auto headingToDir_v = klib::EnumArray{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) : m_engine(engine) { @@ -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; } @@ -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; } @@ -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 { diff --git a/lib/src/snake.cpp b/lib/src/snake.cpp index 2f71cf4..7c24c01 100644 --- a/lib/src/snake.cpp +++ b/lib/src/snake.cpp @@ -8,7 +8,7 @@ namespace chomper { namespace { -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}}; +constexpr auto headingToDir_v = klib::EnumArray{glm::ivec2{1, 0}, glm::ivec2{0, 1}, glm::ivec2{-1, 0}, glm::ivec2{0, -1}}; } // namespace Snake::Snake() { diff --git a/lib/src/world.cpp b/lib/src/world.cpp index 3ce6f09..3eeee7d 100644 --- a/lib/src/world.cpp +++ b/lib/src/world.cpp @@ -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