Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
Binary file added assets/images/apple.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions lib/include/chomper/collectible.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
#include "chomper/world_space.hpp"
#include "glm/ext/vector_float2.hpp"
#include "le2d/drawable/shape.hpp"
#include "le2d/renderer.hpp"
#include "le2d/resource/texture.hpp"

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_quad.transform.position);
}

private:
le::drawable::Quad m_quad{};
Comment thread
stanplayzz marked this conversation as resolved.
Outdated
};
} // namespace chomper
14 changes: 13 additions & 1 deletion lib/include/chomper/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#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/input/action.hpp>
Expand All @@ -15,19 +17,28 @@ class Player : public IController::IListener, public IDebugInspector, public kli
public:
struct Info {
bool alive = true;
size_t score{};
Comment thread
stanplayzz marked this conversation as resolved.
Outdated
};

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;
void grow();

[[nodiscard]] Info const& getInfo() const {
return m_info;
}
[[nodiscard]] std::span<le::RenderInstance const> getSegments() const {
return m_snake.getSegments();
}

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

// IController::IListener
void onSetHeading(Heading heading) final;
Expand All @@ -44,6 +55,7 @@ class Player : public IController::IListener, public IDebugInspector, public kli
std::unique_ptr<IController> m_controller{};

Snake m_snake{};
le::drawable::Text m_scoreText{};

Info m_info{};

Expand Down
11 changes: 11 additions & 0 deletions lib/include/chomper/runtimes/game.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#pragma once
#include "chomper/collectible.hpp"
#include "chomper/engine.hpp"
#include "chomper/player.hpp"
#include "chomper/runtime.hpp"
#include "chomper/world.hpp"
#include "le2d/resource/texture.hpp"
#include <klib/ptr.hpp>
#include <le2d/input/action.hpp>
#include <le2d/input/scoped_mapping.hpp>
#include <memory>
Comment thread
stanplayzz marked this conversation as resolved.
Outdated

namespace chomper::runtime {
// driven by Engine, owner (whether indirectly) of all game things.
Expand All @@ -27,6 +30,10 @@ class Game : public IRuntime, public klib::Pinned {

void bindActions();
void createPlayer();
void createCollectibleTexture();

void spawnCollectible();
void collideCollectibles();

void onGoBack();

Expand All @@ -39,5 +46,9 @@ class Game : public IRuntime, public klib::Pinned {

std::unique_ptr<Player> m_player{};
std::unique_ptr<World> m_world{};
std::vector<Collectible> m_collectibles{};
le::ITexture* m_collectibleTexture{};
Comment thread
stanplayzz marked this conversation as resolved.
Outdated

size_t m_collectibleAmount = 10;
};
} // namespace chomper::runtime
2 changes: 1 addition & 1 deletion 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{15.f};
constexpr auto worldSize_v = glm::vec2{16.f};
constexpr auto tileSize_v = viewport_v.world_size / worldSize_v;
} // namespace chomper
2 changes: 1 addition & 1 deletion lib/include/chomper/world_space.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ 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;
return gridPoint.x < 0 || gridPoint.y < 0 || gridPoint.x >= worldSize_v.x || gridPoint.y >= worldSize_v.y;
}
} // namespace chomper::worldSpace
15 changes: 15 additions & 0 deletions lib/src/collectible.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "chomper/collectible.hpp"
#include "chomper/world_size.hpp"
#include "le2d/renderer.hpp"

namespace chomper {
Collectible::Collectible(le::ITexture const& texture, glm::vec2 position) {
m_quad.create(tileSize_v);
m_quad.texture = &texture;
m_quad.transform.position = position;
}

void Collectible::draw(le::IRenderer& renderer) const {
m_quad.draw(renderer);
}
} // namespace chomper
21 changes: 18 additions & 3 deletions lib/src/player.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "chomper/player.hpp"
#include "chomper/controllers/player_controller.hpp"
#include "chomper/engine.hpp"
#include "chomper/world_size.hpp"
#include "chomper/world_space.hpp"
#include <algorithm>

Expand All @@ -13,6 +14,7 @@ constexpr auto headingToDir_v = klib::EnumArray<Heading, glm::vec2>{glm::vec2{1.

Player::Player(le::input::ScopedActionMapping& mapping, gsl::not_null<Engine const*> engine) : m_engine(engine) {
createController(mapping);
updateScoreText();
}

void Player::tick(kvf::Seconds dt) {
Expand All @@ -30,8 +32,10 @@ void Player::tick(kvf::Seconds dt) {
}
}

Player::Info const& Player::getInfo() const {
return m_info;
void Player::grow() {
m_info.score++;
updateScoreText();
m_shouldPop = false;
}

bool Player::isCollidingWithSelf(glm::vec2 const targetGrid) const {
Expand Down Expand Up @@ -78,11 +82,22 @@ void Player::move() {
m_snake.popTail();
}

m_graceMove = false;
m_shouldPop = true; // reset shouldPop
m_graceMove = false; // reset graceMove
}

void Player::updateScoreText() {
static constexpr auto textParams_v = le::drawable::Text::Params{
.height = le::TextHeight{16},
};

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};
Comment thread
stanplayzz marked this conversation as resolved.
}

void Player::draw(le::IRenderer& renderer) const {
m_snake.draw(renderer);
m_scoreText.draw(renderer);
}

void Player::debugInspect() {
Expand Down
73 changes: 73 additions & 0 deletions lib/src/runtimes/game.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
#include "chomper/runtimes/game.hpp"
#include "chomper/collectible.hpp"
#include "chomper/im_util.hpp"
#include "chomper/runtimes/entrypoint.hpp"
#include "chomper/world_size.hpp"
#include "chomper/world_space.hpp"
#include "glm/ext/vector_float2.hpp"
#include "le2d/resource/texture.hpp"
#include <array>
#include <memory>
#include <random>
#include <unordered_set>
#include <vector>

namespace chomper::runtime {
using ActionValue = le::input::action::Value;

Game::Game(gsl::not_null<Engine*> engine) : m_engine(engine), m_mapping(&engine->getInputRouter()) {
createPlayer();
m_world = std::make_unique<World>(m_engine);
createCollectibleTexture();

for (size_t i = 0; i < m_collectibleAmount; ++i) {
spawnCollectible();
}
}

void Game::tick(kvf::Seconds const dt) {
m_player->tick(dt);

collideCollectibles();

ImGui::SetNextWindowSize({300.0f, 300.0f}, ImGuiCond_Once);
if (ImGui::Begin("Debug Inspect")) {
debugInspectWindow();
Expand All @@ -29,6 +45,9 @@ 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);
}
Comment thread
stanplayzz marked this conversation as resolved.
}

void Game::debugInspectWindow() {
Expand Down Expand Up @@ -59,6 +78,60 @@ void Game::createPlayer() {
m_player = std::make_unique<Player>(m_mapping, m_engine);
}

void Game::createCollectibleTexture() {
m_collectibleTexture = m_engine->getResources().load<le::ITexture>("images/apple.png");
}

void Game::spawnCollectible() {
std::unordered_set<int> occupied;
Comment thread
stanplayzz marked this conversation as resolved.
Outdated
for (auto const& seg : m_player->getSegments()) {
auto p = worldSpace::worldToGrid(seg.transform.position);
occupied.insert(static_cast<int>((p.y * worldSize_v.x) + p.x));
}

for (auto const& c : m_collectibles) {
auto p = c.getGridPosition();
occupied.insert(static_cast<int>((p.y * worldSize_v.x) + p.x));
}

auto emptyTiles = (worldSize_v.x * worldSize_v.y) - (float)occupied.size();
if (emptyTiles <= 0) {
return;
}

std::mt19937 rng(std::random_device{}());
std::uniform_int_distribution<int> dist(0, (int)emptyTiles - 1);
auto target = dist(rng);
Comment thread
stanplayzz marked this conversation as resolved.
Outdated

auto count = 0;
for (float y = 0; y < worldSize_v.y; ++y) {
for (float x = 0; x < worldSize_v.x; ++x) {
Comment thread
stanplayzz marked this conversation as resolved.
Outdated
auto id = static_cast<int>((y * worldSize_v.x) + x);
if (!occupied.contains(id)) {
if (count == target) {
m_collectibles.emplace_back(*m_collectibleTexture, worldSpace::gridToWorld({x, y}));
return;
}
count++;
}
}
}
}

void Game::collideCollectibles() {
for (auto it = m_collectibles.begin(); it != m_collectibles.end();) {
Comment thread
stanplayzz marked this conversation as resolved.
Outdated
if (it->getGridPosition() == worldSpace::worldToGrid(m_player->getSegments().back().transform.position)) {

m_collectibles.erase(it); // erase collided collectible
m_player->grow(); // tell the player to not pop the tail/grow
spawnCollectible(); // spawn new collectible

return;
}
it++;
}
}

void Game::onGoBack() {
m_log.debug("execute 'go back' action here");
}
Expand Down
6 changes: 6 additions & 0 deletions lib/src/snake.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#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>

Expand All @@ -10,6 +12,10 @@ constexpr auto headingToDir_v = klib::EnumArray<Heading, glm::vec2>{glm::vec2{1.
} // namespace

Snake::Snake() {
le::RenderInstance instance{};
instance.tint = snakeBodyColor_v;
instance.transform.position = worldSpace::gridToWorld({0, worldSize_v.y / 2});
m_instances.push_back(instance);
while (m_instances.size() < m_baseSize) {
grow({});
}
Expand Down