aboutsummaryrefslogtreecommitdiff
path: root/src/game
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-03-05 13:26:55 +0100
committerHampusM <hampus@hampusmat.com>2022-06-13 17:56:54 +0200
commit6119f1428b3615e4738a121c6acf343942107fd9 (patch)
tree47eb5537674fe58ae8ad388521203b92bfb43529 /src/game
parentd27f1a89266e141a944f88e9080bdeca95c49da3 (diff)
refactor: move input config to own file & improve cleanup
Diffstat (limited to 'src/game')
-rw-r--r--src/game/game.cpp41
-rw-r--r--src/game/game.hpp9
-rw-r--r--src/game/input_configurator.cpp33
-rw-r--r--src/game/input_configurator.hpp36
-rw-r--r--src/game/input_configurator.tpp17
5 files changed, 94 insertions, 42 deletions
diff --git a/src/game/game.cpp b/src/game/game.cpp
index 559f9a0..0773f2c 100644
--- a/src/game/game.cpp
+++ b/src/game/game.cpp
@@ -2,46 +2,13 @@
#include <utility>
-Game::Game(std::shared_ptr<CursorController> cursor_controller)
- : _cursor_controller(std::move(cursor_controller))
+Game::Game(std::shared_ptr<IInputConfigurator> input_configurator)
+ : _input_configurator(std::move(input_configurator))
{
}
-void Game::run(IScene &scene, IInputHandler &input_handler)
+void Game::run(IScene & /*scene*/, IInputHandler &input_handler)
{
- input_handler.attach('q',
- [&input_handler, &scene]()
- {
- input_handler.leave_raw_mode();
- scene.leave();
- exit(EXIT_SUCCESS);
- });
-
- auto cursor_controller = _cursor_controller;
-
- input_handler.attach('k',
- [&cursor_controller]()
- {
- cursor_controller->move<Direction::UP>(1U);
- });
-
- input_handler.attach('j',
- [&cursor_controller]()
- {
- cursor_controller->move<Direction::DOWN>(1U);
- });
-
- input_handler.attach('h',
- [&cursor_controller]()
- {
- cursor_controller->move<Direction::LEFT>(1U);
- });
-
- input_handler.attach('l',
- [&cursor_controller]()
- {
- cursor_controller->move<Direction::RIGHT>(1U);
- });
-
+ _input_configurator->configure(input_handler);
input_handler.listen();
}
diff --git a/src/game/game.hpp b/src/game/game.hpp
index c52399d..ea8145a 100644
--- a/src/game/game.hpp
+++ b/src/game/game.hpp
@@ -3,19 +3,18 @@
#include "DI/auto_wirable.hpp"
#include "interfaces/game.hpp"
#include "interfaces/input.hpp"
+#include "interfaces/input_configurator.hpp"
#include "interfaces/scene.hpp"
-#include "engine/user/cursor.hpp"
-
#include <memory>
-class Game : public IGame, public AutoWirable<IGame, Game, CursorController>
+class Game : public IGame, public AutoWirable<IGame, Game, IInputConfigurator>
{
public:
- explicit Game(std::shared_ptr<CursorController> cursor_controller);
+ explicit Game(std::shared_ptr<IInputConfigurator> input_configurator);
void run(IScene &scene, IInputHandler &input_handler) override;
private:
- std::shared_ptr<CursorController> _cursor_controller;
+ std::shared_ptr<IInputConfigurator> _input_configurator;
};
diff --git a/src/game/input_configurator.cpp b/src/game/input_configurator.cpp
new file mode 100644
index 0000000..4daee21
--- /dev/null
+++ b/src/game/input_configurator.cpp
@@ -0,0 +1,33 @@
+#include "input_configurator.hpp"
+
+namespace InputActions
+{
+
+void exit_success()
+{
+ exit(EXIT_SUCCESS);
+}
+
+} // namespace InputActions
+
+InputConfigurator::InputConfigurator(std::shared_ptr<CursorController> cursor_controller)
+ : _cursor_controller(std::move(cursor_controller))
+{
+}
+
+void InputConfigurator::configure(IInputHandler &input_handler)
+{
+ input_handler.attach('q', InputActions::exit_success);
+
+ input_handler.attach('k',
+ InputActions::move_cursor<Direction::UP>(*_cursor_controller));
+
+ input_handler.attach('j',
+ InputActions::move_cursor<Direction::DOWN>(*_cursor_controller));
+
+ input_handler.attach('h',
+ InputActions::move_cursor<Direction::LEFT>(*_cursor_controller));
+
+ input_handler.attach(
+ 'l', InputActions::move_cursor<Direction::RIGHT>(*_cursor_controller));
+}
diff --git a/src/game/input_configurator.hpp b/src/game/input_configurator.hpp
new file mode 100644
index 0000000..217c21e
--- /dev/null
+++ b/src/game/input_configurator.hpp
@@ -0,0 +1,36 @@
+#pragma once
+
+#include "DI/auto_wirable.hpp"
+#include "interfaces/direction.hpp"
+#include "interfaces/input.hpp"
+#include "interfaces/input_configurator.hpp"
+
+#include "engine/user/cursor.hpp"
+
+#include <array>
+#include <memory>
+
+namespace InputActions
+{
+
+template <Direction::value_type direction>
+auto move_cursor(CursorController cursor_controller);
+
+void exit_success();
+
+} // namespace InputActions
+
+class InputConfigurator
+ : public IInputConfigurator,
+ public AutoWirable<IInputConfigurator, InputConfigurator, CursorController>
+{
+public:
+ explicit InputConfigurator(std::shared_ptr<CursorController> cursor_controller);
+
+ void configure(IInputHandler &input_handler) override;
+
+private:
+ std::shared_ptr<CursorController> _cursor_controller;
+};
+
+#include "input_configurator.tpp"
diff --git a/src/game/input_configurator.tpp b/src/game/input_configurator.tpp
new file mode 100644
index 0000000..f3c9482
--- /dev/null
+++ b/src/game/input_configurator.tpp
@@ -0,0 +1,17 @@
+#pragma once
+
+#include "input_configurator.hpp"
+
+namespace InputActions
+{
+
+template <Direction::value_type direction>
+auto move_cursor(CursorController cursor_controller)
+{
+ return [cursor_controller]()
+ {
+ cursor_controller.move<direction>(1U);
+ };
+}
+
+} // namespace InputActions