Skip to content
Open
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
Binary file modified assets/images/lighthouse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ext/src.zip
Binary file not shown.
9 changes: 3 additions & 6 deletions src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <app.hpp>
#include <djson/json.hpp>
#include <game.hpp>
#include <klib/visitor.hpp>
#include <log.hpp>

namespace miracle {
Expand All @@ -21,15 +20,11 @@ App::App() : m_context(context_ci), m_data_loader(le::FileDataLoader::upfind("as
void App::run() {
auto game = Game{&m_services};

auto const event_visitor = klib::SubVisitor{
[&game](le::event::CursorPos const& cursor_pos) { game.on_cursor_pos(cursor_pos); },
};

auto delta_time = kvf::DeltaTime{};
while (m_context.is_running()) {
m_context.next_frame();
auto const dt = delta_time.tick();
for (auto const& event : m_context.event_queue()) { std::visit(event_visitor, event); }
m_input_router.dispatch(m_context.event_queue());
game.tick(dt);
if (auto renderer = m_context.begin_render()) { game.render(renderer); }
m_context.present();
Expand All @@ -44,5 +39,7 @@ void App::bind_services() {
m_services.bind<le::FileDataLoader>(&m_data_loader);

m_services.bind(&m_asset_loader);

m_services.bind(&m_input_router);
}
} // namespace miracle
2 changes: 2 additions & 0 deletions src/app.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <le2d/context.hpp>
#include <le2d/file_data_loader.hpp>
#include <le2d/input/router.hpp>
#include <le2d/service_locator.hpp>

namespace miracle {
Expand All @@ -16,6 +17,7 @@ class App {
le::Context m_context;
le::FileDataLoader m_data_loader{};
le::AssetLoader m_asset_loader{};
le::input::Router m_input_router{};

le::ServiceLocator m_services{};
};
Expand Down
12 changes: 8 additions & 4 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@
#include "util/random.hpp"

namespace miracle {
Game::Game(gsl::not_null<le::ServiceLocator const*> services) : m_services(services), m_lighthouse(services), m_light(services) {
spawn_wave();
Game::Game(gsl::not_null<le::ServiceLocator const*> services)
: m_services(services), m_action_mapping(&services->get<le::input::Router>()), m_lighthouse(services), m_light(services) {
auto const& asset_loader = services->get<le::AssetLoader>();
m_font = asset_loader.load<le::IFont>("fonts/specialElite.ttf");
if (!m_font) { throw std::runtime_error{"Failed to load font"}; }

m_action_mapping.bind_action(&m_look_action, [this](le::input::action::Value const& v) { on_look(v); });

spawn_wave();
}

void Game::on_cursor_pos(le::event::CursorPos const& cursor_pos) {
void Game::on_look(le::input::action::Value const value) {
auto const framebuffer_size = m_services->get<le::Context>().framebuffer_size();
m_cursor_pos = cursor_pos.normalized.to_target(framebuffer_size);
m_cursor_pos = le::ndc::vec2{value.get<glm::vec2>()}.to_target(framebuffer_size);
}

void Game::tick([[maybe_unused]] kvf::Seconds const dt) {
Expand Down
10 changes: 8 additions & 2 deletions src/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <kvf/time.hpp>
#include <le2d/drawable/shape.hpp>
#include <le2d/event.hpp>
#include <le2d/input/action.hpp>
#include <le2d/input/scoped_mapping.hpp>
#include <le2d/renderer.hpp>
#include <le2d/service_locator.hpp>
#include <memory>
Expand All @@ -16,16 +18,20 @@ class Game {
public:
explicit Game(gsl::not_null<le::ServiceLocator const*> services);

void on_cursor_pos(le::event::CursorPos const& cursor_pos);

void tick(kvf::Seconds dt);
void render(le::Renderer& renderer) const;
void update_score(int points);
void update_health_text();
void spawn_wave();

private:
void on_look(le::input::action::Value value);

gsl::not_null<le::ServiceLocator const*> m_services;

le::input::ScopedActionMapping m_action_mapping;
le::input::action::Cursor m_look_action{};

Lighthouse m_lighthouse;
Light m_light;

Expand Down
9 changes: 1 addition & 8 deletions src/lighthouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@ Lighthouse::Lighthouse(gsl::not_null<le::ServiceLocator const*> services) : m_se

void Lighthouse::rotate_towards_cursor(glm::vec2 cursor_pos) {
auto const dist_sq = glm::length2((cursor_pos));
if (dist_sq > 0.1f) {
auto const dist = std::sqrt(dist_sq);
auto const normalized = cursor_pos / dist;
static constexpr auto up_v = glm::vec2(0.0f, 1.0f);
auto const dot = glm::dot(normalized, up_v);
auto const angle = glm::degrees(std::acos(dot));
m_sprite.transform.orientation = cursor_pos.x > 0.0f ? -angle : angle;
}
if (dist_sq > 0.1f) { m_sprite.transform.orientation = cursor_pos; }
}

void Lighthouse::check_visibility_range(Enemy& enemy) {
Expand Down