Skip to content

Commit

Permalink
minor code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dedztbh committed Apr 29, 2024
1 parent d2a4537 commit c45dad9
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 19 deletions.
3 changes: 2 additions & 1 deletion game/board.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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
Expand Down
8 changes: 3 additions & 5 deletions src/basic_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<board_t>) {
return decltype(m_board_pair){ std::move(board), board_t(board.size()) };
} else {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 {
Expand Down
7 changes: 2 additions & 5 deletions src/engine_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ class EngineBase {
public:
using state_t = uint8_t;
using update_cb_t = std::function<void(size_t, size_t, uint8_t)>;
using done_cb_t = std::function<void()>;

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;
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/life_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<BasicEngine<get_ruleset()>>(std::vector<EngineBase::state_t>(w * h), w, h, update_cell_cb, update_done_cb);
m_engine = std::make_unique<BasicEngine<get_ruleset()>>(std::vector<EngineBase::state_t>(w * h), w, h, update_cell_cb);
} else if (init_board.get_type() == Variant::PACKED_BYTE_ARRAY) {
m_engine = std::make_unique<BasicEngine<get_ruleset(), PackedByteArray>>(init_board, w, h, update_cell_cb, update_done_cb);
m_engine = std::make_unique<BasicEngine<get_ruleset(), PackedByteArray>>(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<bool>(dict.get(String("wrap_around"), false));
setup_engine_with_ruleset<BasicEngineRuleset, std::tuple_size_v<decltype(tup)>>(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");
Expand All @@ -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;
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/life_driver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -29,7 +29,6 @@ class LifeDriver : public RefCounted {
private:
std::unique_ptr<lifepvp::engine::EngineBase> m_engine;
std::atomic_bool is_busy = false;

};

} //namespace godot
Expand Down

0 comments on commit c45dad9

Please sign in to comment.