Toast Engine
Loading...
Searching...
No Matches
state_machine.hpp
Go to the documentation of this file.
1
6
7#pragma once
8
9#include "toast/log.hpp"
10#include "toast/world/node.hpp"
11
12#include <functional>
13#include <lua.hpp>
14#include <luabridge3/LuaBridge/detail/LuaRef.h>
15#include <map>
16#include <memory>
17#include <optional>
18#include <string>
19#include <vector>
20
21namespace toast {
22
23struct Transition {
24 std::string to;
25 std::optional<std::function<bool()>> condition;
26 std::optional<std::function<void()>> invoke;
27};
28
29struct State {
30 std::optional<std::function<void()>> exit;
31 std::optional<std::function<void()>> entry;
32 std::optional<std::function<void()>> tick;
33 std::vector<Transition> transitions;
34};
35
36class [[ToastNode]] TOAST_API StateMachine : public Node {
37public:
38 void addState(const std::string& name, const State& state);
39
40 [[Reflect]]
41 void addState(const std::string& name, const luabridge::LuaRef& table);
42
43 [[Reflect]]
44 void setState(const std::string& name);
45
46 [[Reflect]]
47 auto getState() -> std::string_view {
48 return current_state;
49 }
50
51protected:
53 [[nodiscard]]
54 auto luaAffinity() const noexcept -> std::optional<size_t> override {
55 return m_lua_vm;
56 }
57
58private:
59 [[Reflect, ReadOnly]]
60 std::string current_state;
61
62 std::map<std::string, std::unique_ptr<State>> states;
63 State* cached_state = nullptr;
64 std::optional<size_t> m_lua_vm;
65
66 void begin();
67 void end();
68 void tick();
69};
70}
auto state() const noexcept -> NodeState
Definition node.hpp:186
auto name() const noexcept -> std::string_view
Display name. Siblings may share one - this is not an identifier.
Definition node.cpp:45
Definition reflect.hpp:244
Definition state_machine.hpp:36
auto luaAffinity() const noexcept -> std::optional< size_t > override
The callbacks are Lua functions, so the machine runs together with the scripts that gave them to it.
Definition state_machine.hpp:54
Public logging interface for the engine.
Base object type for the scene graph.
Definition state_machine.hpp:29
Definition state_machine.hpp:23