Skip to content

Commit ff535f9

Browse files
committed
Use input actions
1 parent a964d6a commit ff535f9

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

src/app.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <app.hpp>
33
#include <djson/json.hpp>
44
#include <game.hpp>
5-
#include <klib/visitor.hpp>
65
#include <log.hpp>
76

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

24-
auto const event_visitor = klib::SubVisitor{
25-
[&game](le::event::CursorPos const& cursor_pos) { game.on_cursor_pos(cursor_pos); },
26-
};
27-
2823
auto delta_time = kvf::DeltaTime{};
2924
while (m_context.is_running()) {
3025
m_context.next_frame();
3126
auto const dt = delta_time.tick();
32-
for (auto const& event : m_context.event_queue()) { std::visit(event_visitor, event); }
27+
m_input_router.dispatch(m_context.event_queue());
3328
game.tick(dt);
3429
if (auto renderer = m_context.begin_render()) { game.render(renderer); }
3530
m_context.present();
@@ -44,5 +39,7 @@ void App::bind_services() {
4439
m_services.bind<le::FileDataLoader>(&m_data_loader);
4540

4641
m_services.bind(&m_asset_loader);
42+
43+
m_services.bind(&m_input_router);
4744
}
4845
} // namespace miracle

src/app.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include <le2d/context.hpp>
33
#include <le2d/file_data_loader.hpp>
4+
#include <le2d/input/router.hpp>
45
#include <le2d/service_locator.hpp>
56

67
namespace miracle {
@@ -16,6 +17,7 @@ class App {
1617
le::Context m_context;
1718
le::FileDataLoader m_data_loader{};
1819
le::AssetLoader m_asset_loader{};
20+
le::input::Router m_input_router{};
1921

2022
le::ServiceLocator m_services{};
2123
};

src/game.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@
1313
#include "util/random.hpp"
1414

1515
namespace miracle {
16-
Game::Game(gsl::not_null<le::ServiceLocator const*> services) : m_services(services), m_lighthouse(services), m_light(services) {
17-
spawn_wave();
16+
Game::Game(gsl::not_null<le::ServiceLocator const*> services)
17+
: m_services(services), m_action_mapping(&services->get<le::input::Router>()), m_lighthouse(services), m_light(services) {
1818
auto const& asset_loader = services->get<le::AssetLoader>();
1919
m_font = asset_loader.load<le::IFont>("fonts/specialElite.ttf");
2020
if (!m_font) { throw std::runtime_error{"Failed to load font"}; }
21+
22+
m_action_mapping.bind_action(&m_look_action, [this](le::input::action::Value const& v) { on_look(v); });
23+
24+
spawn_wave();
2125
}
2226

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

2832
void Game::tick([[maybe_unused]] kvf::Seconds const dt) {

src/game.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <kvf/time.hpp>
33
#include <le2d/drawable/shape.hpp>
44
#include <le2d/event.hpp>
5+
#include <le2d/input/action.hpp>
6+
#include <le2d/input/scoped_mapping.hpp>
57
#include <le2d/renderer.hpp>
68
#include <le2d/service_locator.hpp>
79
#include <memory>
@@ -16,16 +18,20 @@ class Game {
1618
public:
1719
explicit Game(gsl::not_null<le::ServiceLocator const*> services);
1820

19-
void on_cursor_pos(le::event::CursorPos const& cursor_pos);
20-
2121
void tick(kvf::Seconds dt);
2222
void render(le::Renderer& renderer) const;
2323
void update_score(int points);
2424
void update_health_text();
2525
void spawn_wave();
2626

2727
private:
28+
void on_look(le::input::action::Value value);
29+
2830
gsl::not_null<le::ServiceLocator const*> m_services;
31+
32+
le::input::ScopedActionMapping m_action_mapping;
33+
le::input::action::Cursor m_look_action{};
34+
2935
Lighthouse m_lighthouse;
3036
Light m_light;
3137

0 commit comments

Comments
 (0)