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
9 changes: 4 additions & 5 deletions lib/include/chomper/collectible.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#pragma once
#include "chomper/world_space.hpp"
#include "glm/ext/vector_float2.hpp"
#include "le2d/drawable/sprite.hpp"
#include "le2d/renderer.hpp"
#include "le2d/resource/texture.hpp"
#include <le2d/drawable/sprite.hpp>
#include <le2d/renderer.hpp>
#include <le2d/resource/texture.hpp>

namespace chomper {
class Collectible {
Expand All @@ -19,4 +18,4 @@ class Collectible {
private:
le::drawable::Sprite m_sprite{};
};
} // namespace chomper
} // namespace chomper
4 changes: 2 additions & 2 deletions lib/include/chomper/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
#include "chomper/controller.hpp"
#include "chomper/debug_inspector.hpp"
#include "chomper/snake.hpp"
#include "le2d/drawable/text.hpp"
#include "le2d/render_instance.hpp"
#include <imgui.h>
#include <klib/log.hpp>
#include <le2d/drawable/text.hpp>
#include <le2d/input/action.hpp>
#include <le2d/input/scoped_mapping.hpp>
#include <le2d/render_instance.hpp>
#include <le2d/renderer.hpp>

namespace chomper {
Expand Down
4 changes: 2 additions & 2 deletions lib/include/chomper/runtimes/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
#include "chomper/player.hpp"
#include "chomper/runtime.hpp"
#include "chomper/world.hpp"
#include "le2d/random.hpp"
#include "le2d/resource/texture.hpp"
#include <klib/ptr.hpp>
#include <le2d/drawable/text.hpp>
#include <le2d/input/action.hpp>
#include <le2d/input/scoped_mapping.hpp>
#include <le2d/random.hpp>
#include <le2d/resource/texture.hpp>
#include <unordered_set>

namespace chomper::runtime {
Expand Down
14 changes: 9 additions & 5 deletions lib/include/chomper/world_space.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#pragma once
#include "chomper/world_size.hpp"
#include "glm/common.hpp"
#include <glm/common.hpp>
#include <glm/vec2.hpp>

namespace chomper::worldSpace {
namespace {
auto const halfGridSize = glm::ceil(worldSize_v * 0.5f);
// only add half the tilesize when that axis is even
constexpr auto tileOffset =
glm::vec2{(static_cast<int>(worldSize_v.x) % 2 == 0) ? tileSize_v.x * 0.5f : 0.0f, (static_cast<int>(worldSize_v.y) % 2 == 0) ? tileSize_v.y * 0.5f : 0.0f};

[[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)};
} // namespace

constexpr auto gridToWorld(glm::vec2 gridPosition) {
Expand All @@ -21,4 +25,4 @@ constexpr auto worldToGrid(glm::vec2 worldPosition) {
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
} // namespace chomper::worldSpace
4 changes: 2 additions & 2 deletions lib/src/collectible.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "chomper/collectible.hpp"
#include "chomper/world_size.hpp"
#include "le2d/renderer.hpp"
#include <le2d/renderer.hpp>

namespace chomper {
Collectible::Collectible(le::ITexture const& texture, glm::vec2 position) {
Expand All @@ -12,4 +12,4 @@ Collectible::Collectible(le::ITexture const& texture, glm::vec2 position) {
void Collectible::draw(le::IRenderer& renderer) const {
m_sprite.draw(renderer);
}
} // namespace chomper
} // namespace chomper
2 changes: 1 addition & 1 deletion lib/src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,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, worldSize_v.y - 1}) + glm::vec2{m_scoreText.get_size().x / 2, 0};
m_scoreText.transform.position = worldSpace::gridToWorld({0.0f, worldSize_v.y - 1.0f}) + 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/runtimes/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ void Game::tick(kvf::Seconds const dt) {

void Game::render(le::IRenderer& renderer) const {
m_world->draw(renderer);
m_player->draw(renderer);
for (auto const& collectible : m_collectibles) {
collectible.draw(renderer);
}
m_player->draw(renderer);
if (m_countdown.count() > 0) {
m_countdownText.draw(renderer);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/snake.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "chomper/snake.hpp"
#include "chomper/world_size.hpp"
#include "chomper/world_space.hpp"
#include "le2d/render_instance.hpp"
#include <imgui.h>
#include <klib/fixed_string.hpp>
#include <le2d/render_instance.hpp>

namespace chomper {
namespace {
Expand All @@ -14,7 +14,7 @@ constexpr auto headingToDir_v = klib::EnumArray<Heading, glm::vec2>{glm::vec2{1.
Snake::Snake() {
le::RenderInstance instance{};
instance.tint = snakeBodyColor_v;
instance.transform.position = worldSpace::gridToWorld({0, worldSize_v.y / 2});
instance.transform.position = worldSpace::gridToWorld({0, 0.5f * worldSize_v.y});
m_instances.push_back(instance);
while (m_instances.size() < m_baseSize) {
grow({});
Expand Down