Toast Engine
Toggle main menu visibility
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
21
namespace
toast {
22
23
struct
Transition
{
24
std::string to;
25
std::optional<std::function<bool()>> condition;
26
std::optional<std::function<void()>> invoke;
27
};
28
29
struct
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
36
class
[[ToastNode]] TOAST_API
StateMachine
:
public
Node {
37
public
:
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
51
protected
:
53
[[nodiscard]]
54
auto
luaAffinity
() const noexcept -> std::optional<
size_t
>
override
{
55
return
m_lua_vm;
56
}
57
58
private
:
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
}
toast::Node::state
auto state() const noexcept -> NodeState
Definition
node.hpp:186
toast::Node::name
auto name() const noexcept -> std::string_view
Display name. Siblings may share one - this is not an identifier.
Definition
node.cpp:45
toast::Reflect
Definition
reflect.hpp:244
toast::StateMachine
Definition
state_machine.hpp:36
toast::StateMachine::luaAffinity
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
log.hpp
Public logging interface for the engine.
node.hpp
Base object type for the scene graph.
toast::State
Definition
state_machine.hpp:29
toast::Transition
Definition
state_machine.hpp:23
engine
src
toast
world
state_machine.hpp
Generated by
1.17.0