aboutsummaryrefslogtreecommitdiff
path: root/src/game/game.hpp
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-06-08 18:31:58 +0200
committerHampusM <hampus@hampusmat.com>2022-06-13 17:57:01 +0200
commit6d66d5675d0fb78827bc47c49f9d4a1852c7255d (patch)
treee8cbf56d895c6d4acc496fffb076938e822dba40 /src/game/game.hpp
parent7e84d664079d9c407bdf94861825bb05ccf1b0f7 (diff)
feat: implement command mode
Diffstat (limited to 'src/game/game.hpp')
-rw-r--r--src/game/game.hpp52
1 files changed, 49 insertions, 3 deletions
diff --git a/src/game/game.hpp b/src/game/game.hpp
index 8363c4d..a35c0ce 100644
--- a/src/game/game.hpp
+++ b/src/game/game.hpp
@@ -14,14 +14,41 @@
#include <chrono>
#include <cstddef>
+#include <functional>
#include <list>
#include <memory>
+#include <optional>
+#include <string>
+#include <string_view>
+#include <vector>
constexpr auto DEFAULT_MIN_TIME_SINCE_LAST_GEN_MILLIS = 200;
constexpr auto MIN_TIME_SINCE_LAST_GEN_INCREMENT = 50;
constexpr auto MIN_TIME_SINCE_LAST_GEN_DECREMENT = 50;
+constexpr auto CURSOR_FALLBACK_POS_X = 10;
+constexpr auto CURSOR_FALLBACK_POS_Y = 10;
+
+constexpr std::string_view ERASE_ENTIRE_LINE = "{esc}[2K";
+constexpr std::string_view ERASE_LINE_FROM_CURSOR = "{esc}[0K";
+
+enum Mode
+{
+ NORMAL,
+ COMMAND
+};
+
+class CommandInfo
+{
+public:
+ using Options = const std::vector<std::string> &;
+ using CommandFunction = std::function<void(Options)>;
+
+ std::size_t option_cnt;
+ CommandFunction function;
+};
+
class Game : public IGame
{
public:
@@ -50,19 +77,38 @@ private:
std::shared_ptr<IUserInputObserver> _user_input_observer;
std::shared_ptr<ICellHelper> _cell_helper;
- using TimePoint = std::chrono::system_clock::time_point;
+ Mode _current_mode;
- TimePoint _last_update_time;
- TimePoint _last_gen_update_time;
+ std::optional<Vector2> _last_pos_before_command_mode;
+
+ std::string _command_mode_input;
+
+ std::unordered_map<std::string, CommandInfo> _commands;
+
+ std::chrono::system_clock::time_point _last_gen_update_time;
int32_t _min_time_since_last_gen_millis = DEFAULT_MIN_TIME_SINCE_LAST_GEN_MILLIS;
std::list<Vector2> _living_cell_positions;
+ void _on_normal_mode_update() noexcept;
+
+ void _on_command_mode_update() noexcept;
+
+ void _return_to_normal_mode() noexcept;
+
+ void _run_command(const std::string &command) noexcept;
+
+ void _show_command_error(const std::string_view &error_message) noexcept;
+
auto _move_cursor(const Vector2 &direction) noexcept -> bool;
void _set_space(
const std::shared_ptr<IMatrix<IScene::MatrixElement>> &matrix,
const Vector2 &position,
char character) noexcept;
+
+ static void _erase_entire_line() noexcept;
+
+ static void _erase_line_from_cursor() noexcept;
};