From 4e7cb64d2032fdf836013032203e3f32586bfd85 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Tue, 20 Aug 2024 18:44:16 +0300 Subject: [PATCH] Support placing crossing on top of factories --- source/application.cpp | 116 +++++++++++++++++++++++------------------ 1 file changed, 66 insertions(+), 50 deletions(-) diff --git a/source/application.cpp b/source/application.cpp index c70d1d1..eda75c4 100644 --- a/source/application.cpp +++ b/source/application.cpp @@ -561,13 +561,9 @@ namespace gmtk sink(world, c, m); } - void clear_tile(map & map, location const & l) + void destroy_tile_entity(map & map, ecs::handle entity) { - auto entity = map.world->index().find(l); - if (!entity) - return; - - auto acc = map.world->get(*entity); + auto acc = map.world->get(entity); if (acc.contains() || acc.contains()) return; @@ -583,6 +579,17 @@ namespace gmtk map.put_card(transformer_to_card(t->type)); } + map.world->destroy(entity); + } + + void clear_tile(map & map, location const & l) + { + auto entity = map.world->index().find(l); + if (!entity) + return; + + destroy_tile_entity(map, *entity); + auto & index = map.world->index(); for (int y = 0; y < 3; ++y) @@ -644,8 +651,47 @@ namespace gmtk } } } + } - map.world->destroy(*entity); + bool place_card(map & map, card_type type, location const & l) + { + switch (type) + { + case card_type::mixer: + case card_type::hue_shifter: + case card_type::reshaper: + map.world->create( + vertex{l}, + transformer{card_to_transformer(type).value()} + ); + return true; + case card_type::crossing: + map.world->create( + vertex{l}, + crossing{} + ); + return true; + case card_type::zoomer: + { + map.world->create( + vertex{l}, + zoomer{} + ); + + auto c = l.down(); + + sink_belt(*map.world, c.left()); + sink_belt(*map.world, c.right()); + sink_belt(*map.world, c.bottom()); + sink_belt(*map.world, c.top()); + return true; + } + break; + default: + return false; + } + + return false; } map start_menu_map() @@ -1432,15 +1478,22 @@ namespace gmtk destroy_sound(); clear_tile(map_, *selected_); } - else if (auto t = map_.world->get(*entity).get_if()) + else { - if (auto n = card_to_transformer(*active_card_); n && n != t->type) + auto acc = map_.world->get(*entity); + auto t = acc.get_if(); + auto n = card_to_transformer(*active_card_); + + if (!acc.contains() && (!t || !n || t->type != *n)) { if (map_.take_card(*active_card_)) { built_sound(); - map_.put_card(transformer_to_card(t->type)); - t->type = *n; + + destroy_tile_entity(map_, *entity); + + place_card(map_, *active_card_, *selected_); + active_card_ = std::nullopt; auto e = map_.world->index().get(selected_->down()); @@ -1457,44 +1510,7 @@ namespace gmtk { if (map_.take_card(*active_card_)) { - bool built = false; - switch (*active_card_) - { - case card_type::mixer: - case card_type::hue_shifter: - case card_type::reshaper: - map_.world->create( - vertex{*selected_}, - transformer{card_to_transformer(*active_card_).value()} - ); - built = true; - break; - case card_type::crossing: - map_.world->create( - vertex{*selected_}, - crossing{} - ); - built = true; - break; - case card_type::zoomer: - { - map_.world->create( - vertex{*selected_}, - zoomer{} - ); - - auto c = selected_->down(); - - sink_belt(*map_.world, c.left()); - sink_belt(*map_.world, c.right()); - sink_belt(*map_.world, c.bottom()); - sink_belt(*map_.world, c.top()); - built = true; - } - break; - case card_type::eraser: - break; - } + bool built = place_card(map_, *active_card_, *selected_); if (built) { @@ -2099,7 +2115,7 @@ namespace gmtk auto n = card_to_transformer(*active_card_); auto acc = map_.world->get(*entity); auto t = acc.get_if(); - if (n && t && n != t->type) + if (*active_card_ != card_type::eraser && (!n || !t || *n != t->type) && !acc.contains()) { gfx::color_rgba color = {64, 64, 64, 255}; draw_selection(selected_->bbox(), painter_, color, true);