1 #ifndef HRK_SCRIPTED_STATE_MACHINE_HPP
2 #define HRK_SCRIPTED_STATE_MACHINE_HPP
12 #include <luabind/luabind.hpp>
27 : owner_(owner), lua_(lua)
39 luabind::object global = luabind::globals(lua_);
40 luabind::object state = global[state_name];
42 check_state_is_table(state, state_name);
43 previous_state_ = current_state_;
44 current_state_ = state;
45 current_state_name_ = state_name;
54 return previous_state_;
61 if (!current_state_.is_valid()) {
65 if (luabind::type(current_state_[
"Execute"]) != LUA_TFUNCTION) {
66 std::string state_name = current_state_name_ +
"['Execute']";
67 std::cerr <<
"Not found function: " << state_name << std::endl;
70 current_state_[
"Execute"](owner_);
79 if (current_state_.is_valid()) {
80 if (luabind::type(current_state_[
"Exit"]) != LUA_TFUNCTION) {
81 std::cerr <<
"Not found function: "
82 << current_state_name_ <<
"['Exit']"
86 current_state_[
"Exit"](owner_);
90 previous_state_ = current_state_;
91 current_state_name_ = next_state_name;
93 luabind::object global = luabind::globals(lua_);
94 luabind::object next_state = global[next_state_name];
95 check_state_is_table(next_state, next_state_name);
96 current_state_ = next_state;
97 if (luabind::type(current_state_[
"Enter"]) != LUA_TFUNCTION) {
98 std::cerr <<
"Not found function: "
99 << next_state_name <<
"['Enter']" << std::endl;
102 current_state_[
"Enter"](owner_);
109 void check_state_is_table(
const luabind::object& state,
110 const std::string& state_name)
112 if (luabind::type(state) != LUA_TTABLE) {
113 std::cerr <<
"'" << state_name
114 <<
"' variable is not table." << std::endl;
115 if (luabind::type(state) == LUA_TNIL) {
116 std::cerr <<
"'" << state_name <<
"' is nil." << std::endl;
124 luabind::object current_state_;
125 luabind::object previous_state_;
126 std::string current_state_name_;
void set_current_state(const std::string &state_name)
現在の状態を設定する
Definition: Scripted_state_machine.hpp:37
Scripted_state_machine(T *owner, lua_State *lua)
Definition: Scripted_state_machine.hpp:26
bool update(void)
状態を更新する
Definition: Scripted_state_machine.hpp:59
bool change_to(const std::string &next_state_name)
次の状態に変更する
Definition: Scripted_state_machine.hpp:77
luabind::object change_to_previous(void) const
直前の状態を取得する
Definition: Scripted_state_machine.hpp:52
状態遷移を実現するためのテンプレート
Definition: Scripted_state_machine.hpp:19