diff --git a/game/board.gd b/game/board.gd index 3968f49..2b56684 100644 --- a/game/board.gd +++ b/game/board.gd @@ -9,6 +9,7 @@ var init_matrix : Variant = null var cells = [] var life_driver = LifeDriver.new() var ruleset = Dictionary() +const colors = [Color.WHITE, Color.BLACK, Color.RED] # Called when the node enters the scene tree for the first time. func _ready(): @@ -32,7 +33,7 @@ func _ready(): func _update_cell(i: int, j: int, state: int): - cells[i][j].color = Color.BLACK if state else Color.WHITE + cells[i][j].color = colors[state] func _update_done(): pass diff --git a/src/basic_engine.hpp b/src/basic_engine.hpp index 0ea92f8..3c7248c 100644 --- a/src/basic_engine.hpp +++ b/src/basic_engine.hpp @@ -39,9 +39,8 @@ class BasicEngine : public EngineBase { BasicEngine(board_t &&board, const size_t w, const size_t h, - const update_cb_t update_cb, - const done_cb_t done_cb) : - EngineBase(w, h, update_cb, done_cb), m_board_pair([&]() { + const update_cb_t update_cb) : + EngineBase(w, h, update_cb), m_board_pair([&]() { if constexpr (BoardConstructibleBySize) { return decltype(m_board_pair){ std::move(board), board_t(board.size()) }; } else { @@ -76,7 +75,7 @@ class BasicEngine : public EngineBase { return 0 <= nx && nx < W && 0 <= ny && ny < H && at(curr_board, nx, ny); } }(); - + if (is_neighbor_alive) { ++count; } @@ -99,7 +98,6 @@ class BasicEngine : public EngineBase { } m_board ^= 1; - m_done_cb(); } state_t get_state(size_t i, size_t j) override { diff --git a/src/engine_base.hpp b/src/engine_base.hpp index 0b9ea50..aa2d856 100644 --- a/src/engine_base.hpp +++ b/src/engine_base.hpp @@ -10,13 +10,11 @@ class EngineBase { public: using state_t = uint8_t; using update_cb_t = std::function; - using done_cb_t = std::function; EngineBase(const size_t w, const size_t h, - const update_cb_t update_cb, - const done_cb_t done_cb) : - W(w), H(h), m_update_cb(update_cb), m_done_cb(done_cb) {} + const update_cb_t update_cb) : + W(w), H(h), m_update_cb(update_cb) {} virtual ~EngineBase() = default; virtual void next_iteration() = 0; @@ -26,7 +24,6 @@ class EngineBase { const size_t W; const size_t H; const update_cb_t m_update_cb; - const done_cb_t m_done_cb; }; } //namespace lifepvp::engine diff --git a/src/life_driver.cpp b/src/life_driver.cpp index 9e9e568..2266c97 100644 --- a/src/life_driver.cpp +++ b/src/life_driver.cpp @@ -31,27 +31,26 @@ auto setup_engine_with_ruleset(const auto &tup, const auto f) requires(0 < I && void LifeDriver::setup(const size_t w, const size_t h, const Variant &init_board, const EngineType engine, const Variant &ruleset) { const auto update_cell_cb = [&](size_t i, size_t j, uint8_t state) { emit_signal("update_cell", i, j, state); }; - const auto update_done_cb = [&]() { emit_signal("update_done"); }; if (engine == BASIC) { // workaround for not being able to pass constexpr Ruleset directly const auto make_engine = [&](const auto get_ruleset) { if (init_board.get_type() == Variant::NIL) { - m_engine = std::make_unique>(std::vector(w * h), w, h, update_cell_cb, update_done_cb); + m_engine = std::make_unique>(std::vector(w * h), w, h, update_cell_cb); } else if (init_board.get_type() == Variant::PACKED_BYTE_ARRAY) { - m_engine = std::make_unique>(init_board, w, h, update_cell_cb, update_done_cb); + m_engine = std::make_unique>(init_board, w, h, update_cell_cb); } else { ERR_PRINT("Unknown init_board type, should be either null or PackedByteArray"); } }; if (ruleset.get_type() == Variant::NIL) { - make_engine([](){ return BasicEngineRuleset{}; }); + make_engine([]() { return BasicEngineRuleset{}; }); } else if (ruleset.get_type() == Variant::DICTIONARY) { - const Dictionary& dict = ruleset; + const Dictionary &dict = ruleset; const auto tup = std::make_tuple(dict.get(String("wrap_around"), false)); setup_engine_with_ruleset>(tup, make_engine); } else { - ERR_PRINT("Unknown init_board type, should be either null or PackedByteArray"); + ERR_PRINT("Unknown ruleset type, should be either null or Dictionary"); } } else { ERR_PRINT("Unknown engine type"); @@ -62,6 +61,7 @@ void LifeDriver::setup(const size_t w, const size_t h, const Variant &init_board void LifeDriver::next_iteration() { if (m_engine && !is_busy.exchange(true)) { m_engine->next_iteration(); + emit_signal("update_done"); is_busy = false; } } diff --git a/src/life_driver.hpp b/src/life_driver.hpp index 0c8bfe0..0f841b5 100644 --- a/src/life_driver.hpp +++ b/src/life_driver.hpp @@ -19,7 +19,7 @@ class LifeDriver : public RefCounted { BASIC }; - void setup(const size_t w, const size_t h, const Variant &init_board, const EngineType engine, const Variant& ruleset); + void setup(const size_t w, const size_t h, const Variant &init_board, const EngineType engine, const Variant &ruleset); void next_iteration(); @@ -29,7 +29,6 @@ class LifeDriver : public RefCounted { private: std::unique_ptr m_engine; std::atomic_bool is_busy = false; - }; } //namespace godot