diff options
Diffstat (limited to 'src/game')
-rw-r--r-- | src/game/cursor_listener.cpp | 18 | ||||
-rw-r--r-- | src/game/cursor_listener.hpp | 19 | ||||
-rw-r--r-- | src/game/game.cpp | 15 |
3 files changed, 47 insertions, 5 deletions
diff --git a/src/game/cursor_listener.cpp b/src/game/cursor_listener.cpp new file mode 100644 index 0000000..c13ce44 --- /dev/null +++ b/src/game/cursor_listener.cpp @@ -0,0 +1,18 @@ +#include "cursor_listener.hpp" + +#include "strings.hpp" + +#include <fmt/core.h> +#include <utility> + +CursorListener::CursorListener(std::shared_ptr<IScene> scene) noexcept + : _scene(std::move(scene)) +{ +} + +void CursorListener::update(const Vector2 &context) noexcept +{ + _scene->write_status(fmt::format(STATUS_BAR_COORDINATES, + fmt::arg("x", context.get_x()), + fmt::arg("y", context.get_y()))); +} diff --git a/src/game/cursor_listener.hpp b/src/game/cursor_listener.hpp new file mode 100644 index 0000000..c74bb3d --- /dev/null +++ b/src/game/cursor_listener.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include "interfaces/scene.hpp" +#include "interfaces/subscriber.hpp" + +#include "engine/data/vector2.hpp" + +#include <memory> + +class CursorListener : public ISubscriber<Vector2> +{ +public: + explicit CursorListener(std::shared_ptr<IScene> scene) noexcept; + + void update(const Vector2 &context) noexcept override; + +private: + std::shared_ptr<IScene> _scene; +}; diff --git a/src/game/game.cpp b/src/game/game.cpp index 5df3935..055e212 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -3,6 +3,7 @@ #include "commands/insert_cell.hpp" #include "commands/move_cursor.hpp" #include "commands/quit.hpp" +#include "game/cursor_listener.hpp" #include "strings.hpp" #include <fmt/core.h> @@ -16,6 +17,10 @@ Game::Game(const std::shared_ptr<IWindow> &window, const std::shared_ptr<IScene> void Game::on_start() noexcept { + auto cursor_listener = std::make_shared<CursorListener>(_scene); + + _cursor_controller->subscribe(CursorEvent::POSITION_CHANGE, cursor_listener); + const auto window_size = _window->size(); const auto center_position = @@ -50,11 +55,11 @@ Game::get_input_config() const noexcept return {{'q', std::make_shared<QuitCommand>()}, {'i', std::make_shared<InsertCellCommand>(_cursor_controller, _scene)}, {'k', std::make_shared<MoveCursorCommand>(Vector2::up(), _cursor_controller, - _scene, _window)}, + _window)}, {'j', std::make_shared<MoveCursorCommand>(Vector2::down(), _cursor_controller, - _scene, _window)}, + _window)}, {'h', std::make_shared<MoveCursorCommand>(Vector2::left(), _cursor_controller, - _scene, _window)}, - {'l', std::make_shared<MoveCursorCommand>( - Vector2::right(), _cursor_controller, _scene, _window)}}; + _window)}, + {'l', std::make_shared<MoveCursorCommand>(Vector2::right(), + _cursor_controller, _window)}}; } |