diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-05-06 16:04:17 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-05-06 16:04:17 +0200 |
commit | 2085e643eb422eedba887c40ff4c6331f6f247c2 (patch) | |
tree | ffc3f75a0dfbe55f361d81166ed0ede6d43f7e36 /src/critter-script-walk.cpp | |
parent | cb484be64de45ad6529952366273978502a17b6d (diff) |
walk script wip
Diffstat (limited to 'src/critter-script-walk.cpp')
-rw-r--r-- | src/critter-script-walk.cpp | 107 |
1 files changed, 49 insertions, 58 deletions
diff --git a/src/critter-script-walk.cpp b/src/critter-script-walk.cpp index 8799aed5..bd2b00ca 100644 --- a/src/critter-script-walk.cpp +++ b/src/critter-script-walk.cpp @@ -1,16 +1,15 @@ #include "critter-script.hpp" //#include "raycast.hpp" +#include "critter.hpp" +#include "compat/failwith.hpp" #include "point.hpp" #include "critter.hpp" #include "search-result.hpp" #include "search-astar.hpp" #include "entity/name-of.hpp" -#include <cr/Optional.h> namespace floormat { -using walk_mode = critter_script::walk_mode; - namespace { bool walk_line(point dest, const std::shared_ptr<critter>& c, size_t& i, const Ns& dt) @@ -19,10 +18,19 @@ bool walk_line(point dest, const std::shared_ptr<critter>& c, size_t& i, const N return res.blocked || c->position() == dest; } +bool walk_path(point dest, const path_search_result& path, const std::shared_ptr<critter>& c, size_t& i, const Ns& dt) +{ + return {}; +} + +struct walk_script; +constexpr StringView script_name = name_of<walk_script>; +using ScriptPtr = Pointer<critter_script>; +using psr = path_search_result; + struct walk_script final : critter_script { - struct line_tag_t {}; - struct path_tag_t {}; + enum class walk_mode : uint8_t { none, line, path, }; StringView name() const override; const void* id() const override; @@ -31,91 +39,74 @@ struct walk_script final : critter_script void on_destroy(const std::shared_ptr<critter>& c, script_destroy_reason reason) override; void delete_self() noexcept override; - walk_script(point dest, line_tag_t); - //walk_script(point dest, path_search_result path, path_tag_t); + walk_script(point dest); + walk_script(psr path); private: point dest; - Optional<path_search_result> path; - walk_mode mode = walk_mode::none; + psr path; + uint32_t path_index = -1u; + walk_mode mode = failwith<walk_mode>("walk_mode not set"); }; -walk_script::walk_script(point dest, line_tag_t) : - dest{dest}, mode{walk_mode::line} -{ -} +StringView walk_script::name() const { return "walk"_s; } +const void* walk_script::id() const { return &script_name; } +void walk_script::on_destroy(const std::shared_ptr<critter>& c, script_destroy_reason) { c->clear_auto_movement(); } +void walk_script::delete_self() noexcept { delete this; } void walk_script::on_init(const std::shared_ptr<critter>& c) { - c->movement.AUTO = true; + Debug{} << "| start walking from" << c->position() << "to" << dest; + c->moves.AUTO = true; + + switch (mode) + { + case walk_mode::line: + break; + case walk_mode::path: + fm_assert(!path.empty()); + break; + default: + std::unreachable(); + fm_assert(false); + } } void walk_script::on_update(const std::shared_ptr<critter>& c, size_t& i, const Ns& dt) { - auto& m = c->movement; - - if (m.L | m.R | m.U | m.D) - m.AUTO = false; - - if (!m.AUTO) + auto& m = c->moves; + if (c->maybe_stop_auto_movement()) goto done; switch (mode) { - case walk_mode::none: - fm_abort("bad walk_mode 'none'"); case walk_mode::line: fm_assert(!path); if (walk_line(dest, c, i, dt)) goto done; return; case walk_mode::path: - fm_abort("todo!"); - //break; + if (walk_path(dest, path, c, i, dt)) + goto done; + return; + case walk_mode::none: + break; } - std::unreachable(); fm_assert(false); done: + //path = {}; Debug{} << " finished walking"; - m.AUTO = false; + c->clear_auto_movement(); c->script.do_clear(c); } -constexpr StringView script_name = name_of<walk_script>; - -StringView walk_script::name() const { return "walk"_s; } - -const void* walk_script::id() const -{ - return &script_name; -} - -void walk_script::on_destroy(const std::shared_ptr<critter>& c, script_destroy_reason reason) -{ -} - -void walk_script::delete_self() noexcept -{ - delete this; -} +walk_script::walk_script(point dest) : dest{dest}, mode{walk_mode::line} {} +walk_script::walk_script(psr path) : path{move(path)}, mode{walk_mode::path} { fm_assert(!path.empty()); } } // namespace -Pointer<critter_script> critter_script::make_walk_script(point dest, const path_search_result& path, walk_mode mode) -{ - switch (mode) - { - case walk_mode::line: - fm_assert(path.empty()); - return Pointer<critter_script>(new walk_script{dest, walk_script::line_tag_t{}}); - case walk_mode::path: - fm_abort("todo!"); - fm_assert(!path.empty()); - case walk_mode::none: - break; - } - fm_abort("bad walk_mode %d", (int)mode); -} +ScriptPtr critter_script::make_walk_script(point dest) { return ScriptPtr(new walk_script{dest}); } +ScriptPtr critter_script::make_walk_script(psr path) { return ScriptPtr(new walk_script{move(path)}); } } // namespace floormat |