alai/src/state_machine/StateMachine.h

263 lines
9.5 KiB
C
Raw Normal View History

2022-08-26 19:27:57 -04:00
#ifndef ALAI_STATE_MACHINE_STATE_MACHINE_H
#define ALAI_STATE_MACHINE_STATE_MACHINE_H
#include <Godot.hpp>
#include <Node.hpp>
2022-08-26 19:27:57 -04:00
namespace alai
{
2022-04-13 00:59:23 -04:00
/**
* @brief This class provides a finite state machine that can be used with any scene and node.
*
*/
2022-08-26 19:27:57 -04:00
class StateMachine : public godot::Node
{
2022-10-10 21:54:12 -03:00
GODOT_CLASS(StateMachine, godot::Node)
private:
2022-04-13 00:59:23 -04:00
/**
* @brief The parent node which is one level above the state machine.
*
*/
2022-08-26 19:27:57 -04:00
godot::Node *parent;
2022-04-13 00:59:23 -04:00
/**
* @brief The default state for the state machine to run.
*
* @details If this is not set, the state machine will try to run the first state node.
* If no state nodes are present, an error will be printed and the state machine will not work.
*/
2022-08-26 19:27:57 -04:00
godot::String default_state;
2022-04-13 00:59:23 -04:00
/**
* @brief If set to true the state machine will print a message showing when it enters or exits a state.
*
*/
bool debug;
2022-04-13 00:59:23 -04:00
/**
* @brief The current state the machine is in.
*
*/
2022-08-26 19:27:57 -04:00
godot::String current_state;
2022-04-13 00:59:23 -04:00
/**
* @brief A list of the states in the state machine.
*
*/
2022-08-26 19:27:57 -04:00
godot::Dictionary states;
2022-04-16 15:54:05 -04:00
/**
* @brief This adds all nodes of the states machine as states in the machine.
*
*/
void add_states();
2022-04-13 00:59:23 -04:00
/**
* @brief This adds a state to the list of states in the state machine.
*
* @param[in] state The new state.
* @param[in] child The godot node which represents the state.
*/
2022-08-26 19:27:57 -04:00
void add_state(const godot::String state, godot::Node *child);
2022-04-16 15:54:05 -04:00
/**
* @brief Set the current state object.
*
* @param[in] current_state The current state that is running.
*/
2022-08-26 19:27:57 -04:00
void set_current_state(const godot::String current_state);
2022-04-16 15:54:05 -04:00
public:
2022-04-13 00:59:23 -04:00
/**
* @brief This method registers classes with Godot.
*
* @details This method registers methods, properties, and signals with the Godot engine.
*/
static void _register_methods();
2022-04-13 00:59:23 -04:00
/**
2022-04-22 21:56:22 -04:00
* @brief Construct a new StateMachine object.
2022-04-13 00:59:23 -04:00
*/
StateMachine();
2022-04-13 00:59:23 -04:00
/**
2022-04-22 21:56:22 -04:00
* @brief Destroy the StateMachine object.
2022-04-13 00:59:23 -04:00
*/
~StateMachine();
/**
* @brief Initialize the class from Godot.
2022-04-13 00:59:23 -04:00
*
* @details This method is called just once when the Godot engine connects to the instance of the class.
*/
void _init();
/**
* @brief Code to be run when ready.
2022-04-13 00:59:23 -04:00
*
* @details This method is run when all the children of this node are ready.
*/
void _ready();
2022-04-13 00:59:23 -04:00
/**
* @brief This method will remove all no default states from the scene tree and start the default state.
*
*/
void setup();
2022-04-13 00:59:23 -04:00
/**
* @brief Check if the given state is the current running state.
*
* @param[in] state The state to compare with the running state.
* @return true If the running state and given state are the same.
* @return false If the running state and given state are not the same.
*/
2022-08-26 19:27:57 -04:00
bool is_current(const godot::String state);
2022-04-13 00:59:23 -04:00
/**
* @brief Check if the state machine has a given state.
*
* @param[in] state The state to check for.
* @return true If the state exists.
* @return false If the state doesn't exist.
*/
2022-08-26 19:27:57 -04:00
bool has(const godot::String state);
2022-04-13 00:59:23 -04:00
/**
* @brief Restar the running state by calling its enter and exit methods.
*
* @param[in] state The state that is being restarted.
* @param[in] args The arguments to pass to the state on exit and enter.
*/
2022-10-10 21:54:12 -03:00
void restart(const godot::String state, const godot::Array &args = godot::Array());
2022-04-13 00:59:23 -04:00
/**
* @brief Change to a different state.
*
* @details If the running state is the same as the state it will restart it.
*
* @param[in] state The state to change to.
* @param[in] args The arguments to pass to the exiting state and the entering state.
*/
2022-08-26 19:27:57 -04:00
void change(const godot::String state, const godot::Array &args = godot::Array());
2022-04-13 00:59:23 -04:00
/**
* @brief Call a registered godot method in the class.
*
* @param[in] method The method name to call.
* @param[in] args The arguments to pass to the method.
* @return Variant Returns a Variant based off what the method returns.
*/
2022-08-26 19:27:57 -04:00
godot::Variant call(const godot::String method, const godot::Array &args = godot::Array());
2022-04-13 00:59:23 -04:00
/**
* @brief This method is to link up a signal call back.
*
* @param[in] method The method to call for the signal.
* @param[in] args The arguments to pass to the method.
* @return Variant Returns a Variant based off what the method returns.
*/
2022-08-26 19:27:57 -04:00
godot::Variant _call(const godot::String method, const godot::Array &args = godot::Array());
2022-04-13 00:59:23 -04:00
/**
* @brief Set the default state object.
*
* @param[in] default_state The new default state.
*/
2022-08-26 19:27:57 -04:00
void set_default_state(const godot::String default_state);
2022-04-13 00:59:23 -04:00
/**
* @brief Get the default state object.
*
* @return String The default state.
*/
2022-08-26 19:27:57 -04:00
godot::String get_default_state();
2022-04-13 00:59:23 -04:00
2022-04-16 15:54:05 -04:00
/**
* @brief Get the current state object.
*
* @return String The current running state.
*/
2022-08-26 19:27:57 -04:00
godot::String get_current_state();
2022-04-16 15:54:05 -04:00
2022-04-13 00:59:23 -04:00
/**
* @brief Set the debug object.
*
* @param[in] debug Whether or not to debug.
*/
void set_debug(bool debug);
/**
* @brief Get the debug object.
*
* @return true If debugging is enabled.
* @return false If debugging is disabled.
*/
bool get_debug();
/**
* @brief This method is called when the signal tree_entered is emitted.
*
* @details This will run the setup method to prepare the state machine for use.
*/
void _on_StateMachine_tree_entered();
/**
* @brief This method is called when the signal tree_exiting is emitted.
*
* @details If the tree is in the exiting state readd all the removed state nodes to the scene tree.
* This is important because the memory won't be freed if they are no longer in the scene tree.
*/
void _on_StateMachine_tree_exiting();
/**
* @brief Restarts the running state.
*
2022-08-26 19:27:57 -04:00
* @param Args Variable number of arguments to pass to restart.
2022-04-13 00:59:23 -04:00
* @param[in] state The state being restarted.
* @param[in] args The arguments to pass when restarting.
*/
2022-10-10 21:54:12 -03:00
template<class... Args> void restart(const godot::String state, Args... args)
{
2022-08-26 19:27:57 -04:00
return restart(state, godot::Array::make(args...));
}
2022-04-13 00:59:23 -04:00
/**
* @brief Changes to a new state.
*
2022-08-26 19:27:57 -04:00
* @param Args Variable number of arguments to pass when changing states.
2022-04-13 00:59:23 -04:00
* @param[in] state The state to change to.
* @param[in] args The arguments to pass to the new state.
*/
2022-10-10 21:54:12 -03:00
template<class... Args> void change(const godot::String state, Args... args)
{
2022-08-26 19:27:57 -04:00
return change(state, godot::Array::make(args...));
}
2022-04-13 00:59:23 -04:00
/**
* @brief Call a registered godot method in the class.
*
2022-08-26 19:27:57 -04:00
* @param Args The variable arguments to pass to the method.
2022-04-13 00:59:23 -04:00
* @param[in] method The method to call.
* @param[in] args The arguments to pass to it.
* @return Variant The Variant object returned by the method called.
*/
2022-10-10 21:54:12 -03:00
template<class... Args> godot::Variant call(const godot::String method, Args... args)
{
2022-08-26 19:27:57 -04:00
return call(method, godot::Array::make(args...));
}
2022-04-13 00:59:23 -04:00
/**
* @brief This is used to connect a callback from a signal.
*
2022-08-26 19:27:57 -04:00
* @param Args The arguments to pass to the callback method.
2022-04-13 00:59:23 -04:00
* @param[in] method The method to call.
* @param[in] args The arguments to pass.
* @return Variant The Variant object returned by the method called.
*/
2022-10-10 21:54:12 -03:00
template<class... Args> godot::Variant _call(const godot::String method, Args... args)
{
2022-08-26 19:27:57 -04:00
return _call(method, godot::Array::make(args...));
}
};
2022-10-10 21:54:12 -03:00
} // namespace alai
#endif