summaryrefslogtreecommitdiffhomepage
path: root/src/critter-script-walk.cpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2024-05-06 03:22:03 +0200
committerStanislaw Halik <sthalik@misaki.pl>2024-05-06 03:22:03 +0200
commit518e8cd4cc14bf04dd08f2f8db793430fea175fd (patch)
tree3e3f57b5c18bbb335b4840f0512eb57d8bc21222 /src/critter-script-walk.cpp
parentde33d8773c3357653414143b8a76b08b8aa8d149 (diff)
a?
Diffstat (limited to 'src/critter-script-walk.cpp')
-rw-r--r--src/critter-script-walk.cpp79
1 files changed, 68 insertions, 11 deletions
diff --git a/src/critter-script-walk.cpp b/src/critter-script-walk.cpp
index 9c92798c..564c3023 100644
--- a/src/critter-script-walk.cpp
+++ b/src/critter-script-walk.cpp
@@ -5,42 +5,88 @@
#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 {
-enum class walk_mode : uint8_t
+bool walk_line(point dest, const std::shared_ptr<critter>& c, size_t& i, const Ns& dt)
{
- none, line, path,
-};
+ Debug{} << "move from" << c->position() << "to" << dest;
+ auto res = c->move_toward(i, dt, dest);
+ DBG_nospace << "blocked:" << res.blocked << " moved:" << res.moved;
+ return res.blocked || c->position() == dest;
+}
struct walk_script final : critter_script
{
+ struct line_tag_t {};
+ struct path_tag_t {};
+
const void* type_id() const override;
void on_init(const std::shared_ptr<critter>& c) override;
void on_update(const std::shared_ptr<critter>& c, size_t& i, const Ns& dt) override;
void on_destroy(const std::shared_ptr<critter>& c, script_destroy_reason reason) override;
void delete_self() noexcept override;
- point to;
- path_search_result path;
+ walk_script(point dest, line_tag_t);
+ //walk_script(point dest, path_search_result path, path_tag_t);
+
+private:
+ point dest;
+ Optional<path_search_result> path;
walk_mode mode = walk_mode::none;
};
-constexpr StringView script_name = name_of<walk_script>;
-
-const void* walk_script::type_id() const
+walk_script::walk_script(point dest, line_tag_t) :
+ dest{dest}, mode{walk_mode::line}
{
- return &script_name;
}
void walk_script::on_init(const std::shared_ptr<critter>& c)
{
+ c->movement.AUTO = true;
}
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)
+ 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;
+ }
+ std::unreachable();
+ fm_assert(false);
+
+done:
+ Debug{} << " finished walking";
+ c->script.do_clear(c);
+}
+
+constexpr StringView script_name = name_of<walk_script>;
+
+const void* walk_script::type_id() const
+{
+ return &script_name;
}
void walk_script::on_destroy(const std::shared_ptr<critter>& c, script_destroy_reason reason)
@@ -54,9 +100,20 @@ void walk_script::delete_self() noexcept
} // namespace
-critter_script* critter_script::make_walk_script(point to, path_search_result path)
+Pointer<critter_script> critter_script::make_walk_script(point dest, const path_search_result& path, walk_mode mode)
{
- return new walk_script{};
+ 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);
}
} // namespace floormat