cleanup c++ code

This commit is contained in:
2022-08-26 19:27:57 -04:00
parent 8321c8a610
commit 0f4e77d4b7
19 changed files with 340 additions and 351 deletions

View File

@@ -1,8 +1,6 @@
#include "state_machine/State.h"
using namespace godot;
void State::_register_methods()
void alai::State::_register_methods()
{
register_method("set_parent", &State::set_parent);
register_method("get_parent", &State::get_parent);
@@ -11,44 +9,44 @@ void State::_register_methods()
register_method("_state_exit", &State::_state_exit);
}
State::State()
alai::State::State()
{
}
State::~State()
alai::State::~State()
{
}
void State::_init()
void alai::State::_init()
{
}
void State::_state_enter(const String state, const Array args)
void alai::State::_state_enter(const godot::String state, const godot::Array args)
{
WARN_PRINT("State " + get_state_machine()->get_current_state() + " is missing its _state_enter method!");
}
void State::_state_exit(const String state, const Array args)
void alai::State::_state_exit(const godot::String state, const godot::Array args)
{
WARN_PRINT("State " + get_state_machine()->get_current_state() + " is missing its _state_exit method!");
}
void State::set_parent(Node *parent)
void alai::State::set_parent(Node *parent)
{
this->parent = parent;
}
Node *State::get_parent()
godot::Node *alai::State::get_parent()
{
return parent;
}
void State::set_state_machine(StateMachine *state_machine)
void alai::State::set_state_machine(alai::StateMachine *state_machine)
{
this->state_machine = state_machine;
}
StateMachine *State::get_state_machine()
alai::StateMachine *alai::State::get_state_machine()
{
return this->state_machine;
}

View File

@@ -1,12 +1,11 @@
#ifndef JUEGO_STATE_H
#define JUEGO_STATE_H
#ifndef ALAI_STATE_MACHINE_STATE_H
#define ALAI_STATE_MACHINE_STATE_H
#include "state_machine/StateMachine.h"
#include <Godot.hpp>
#include <Node.hpp>
namespace godot
namespace alai
{
/**
* @brief This class provides a virtual template state that real states should extend from and override.
@@ -21,7 +20,7 @@ namespace godot
* @brief The state's parent, this is the node 1 level above the state machine.
*
*/
Node *parent;
godot::Node *parent;
/**
* @brief The state machine itself, used to handle all state related work.
*
@@ -61,7 +60,7 @@ namespace godot
* @param[in] state This will contain the previous state.
* @param[in] args The arguments passed to the state.
*/
virtual void _state_enter(const String state, const Array args = Array());
virtual void _state_enter(const godot::String state, const godot::Array args = godot::Array());
/**
* @brief This is called when a state is exited.
@@ -69,14 +68,14 @@ namespace godot
* @param[in] state The state we are going to.
* @param[in] args The arguments passed to the state.
*/
virtual void _state_exit(const String state, const Array args = Array());
virtual void _state_exit(const godot::String state, const godot::Array args = godot::Array());
/**
* @brief Set the parent object.
*
* @param[in] parent The parent of the state.
*/
virtual void set_parent(Node *parent) final;
virtual void set_parent(godot::Node *parent) final;
/**
* @brief Get the parent object.

View File

@@ -1,35 +1,33 @@
#include "state_machine/StateMachine.h"
#include "state_machine/State.h"
using namespace godot;
void StateMachine::_register_methods()
void alai::StateMachine::_register_methods()
{
register_method("_ready", &StateMachine::_ready);
register_method("_on_StateMachine_tree_entered", &StateMachine::_on_StateMachine_tree_entered);
register_method("_on_StateMachine_tree_exiting", &StateMachine::_on_StateMachine_tree_exiting);
register_property<StateMachine, String>("default_state", &StateMachine::set_default_state, &StateMachine::get_default_state, String());
register_property<StateMachine, bool>("debug", &StateMachine::set_debug, &StateMachine::get_debug, false);
register_signal<StateMachine>("state_entered", "state", GODOT_VARIANT_TYPE_STRING);
register_signal<StateMachine>("state_exited", "state", GODOT_VARIANT_TYPE_STRING);
register_signal<StateMachine>("state_restarted", "state", GODOT_VARIANT_TYPE_STRING);
godot::register_method("_ready", &StateMachine::_ready);
godot::register_method("_on_StateMachine_tree_entered", &StateMachine::_on_StateMachine_tree_entered);
godot::register_method("_on_StateMachine_tree_exiting", &StateMachine::_on_StateMachine_tree_exiting);
godot::register_property<StateMachine, godot::String>("default_state", &StateMachine::set_default_state, &StateMachine::get_default_state, godot::String());
godot::register_property<StateMachine, bool>("debug", &StateMachine::set_debug, &StateMachine::get_debug, false);
godot::register_signal<StateMachine>("state_entered", "state", GODOT_VARIANT_TYPE_STRING);
godot::register_signal<StateMachine>("state_exited", "state", GODOT_VARIANT_TYPE_STRING);
godot::register_signal<StateMachine>("state_restarted", "state", GODOT_VARIANT_TYPE_STRING);
}
StateMachine::StateMachine()
alai::StateMachine::StateMachine()
{
}
StateMachine::~StateMachine()
alai::StateMachine::~StateMachine()
{
}
void StateMachine::_init()
void alai::StateMachine::_init()
{
set_default_state(String());
set_default_state(godot::String());
set_debug(false);
}
void StateMachine::_ready()
void alai::StateMachine::_ready()
{
connect("tree_entered", this, "_on_StateMachine_tree_entered");
connect("tree_exiting", this, "_on_StateMachine_tree_exiting");
@@ -46,7 +44,7 @@ void StateMachine::_ready()
setup();
}
void StateMachine::setup()
void alai::StateMachine::setup()
{
auto children = get_children();
@@ -89,7 +87,7 @@ void StateMachine::setup()
}
}
void StateMachine::add_states()
void alai::StateMachine::add_states()
{
auto children = get_children();
for (uint8_t i = 0; i < children.size(); i++)
@@ -99,12 +97,12 @@ void StateMachine::add_states()
}
}
void StateMachine::add_state(const String state, Node *child)
void alai::StateMachine::add_state(const godot::String state, Node *child)
{
states[state] = child;
}
bool StateMachine::is_current(const String state)
bool alai::StateMachine::is_current(const godot::String state)
{
if (get_current_state() == "")
{
@@ -116,19 +114,19 @@ bool StateMachine::is_current(const String state)
}
}
bool StateMachine::has(const String state)
bool alai::StateMachine::has(const godot::String state)
{
return states.has(state);
}
void StateMachine::restart(const String state, const Array& args)
void alai::StateMachine::restart(const godot::String state, const godot::Array& args)
{
this->call("_state_exit", state, args);
this->call("_state_enter", state, args);
this->emit_signal("state_restarted", get_current_state());
}
void StateMachine::change(const String state, const Array &args)
void alai::StateMachine::change(const godot::String state, const godot::Array &args)
{
if (is_current(state))
{
@@ -143,7 +141,7 @@ void StateMachine::change(const String state, const Array &args)
auto previous_state = get_current_state();
Variant exiting;
godot::Variant exiting;
Node *state_node = Object::cast_to<Node>(this->states[previous_state]);
if (state_node)
{
@@ -171,7 +169,7 @@ void StateMachine::change(const String state, const Array &args)
this->emit_signal("state_exited", get_current_state());
if (debug)
{
Godot::print(get_current_state() + " exited!");
godot::Godot::print(get_current_state() + " exited!");
}
}
@@ -199,11 +197,11 @@ void StateMachine::change(const String state, const Array &args)
this->emit_signal("state_entered", get_current_state());
if (debug)
{
Godot::print(get_current_state() + " entered!");
godot::Godot::print(get_current_state() + " entered!");
}
}
Variant StateMachine::call(const String method, const Array &args)
godot::Variant alai::StateMachine::call(const godot::String method, const godot::Array &args)
{
auto node = Object::cast_to<Node>(states[get_current_state()].operator Object*());
if (node)
@@ -215,57 +213,57 @@ Variant StateMachine::call(const String method, const Array &args)
else
{
WARN_PRINT("The state " + get_current_state() + " doesn't contain the method " + method + "!");
return Variant();
return godot::Variant();
}
}
else
{
ERR_PRINT("Could not get current state node for " + get_current_state() + "!");
return Variant();
return godot::Variant();
}
}
Variant StateMachine::_call(const String method, const Array &args)
godot::Variant alai::StateMachine::_call(const godot::String method, const godot::Array &args)
{
return this->call(method, args);
}
void StateMachine::set_default_state(const String default_state)
void alai::StateMachine::set_default_state(const godot::String default_state)
{
this->default_state = default_state;
}
String StateMachine::get_default_state()
godot::String alai::StateMachine::get_default_state()
{
return this->default_state;
}
void StateMachine::set_current_state(const String current_sate)
void alai::StateMachine::set_current_state(const godot::String current_sate)
{
this->current_state = current_sate;
}
String StateMachine::get_current_state()
godot::String alai::StateMachine::get_current_state()
{
return this->current_state;
}
void StateMachine::set_debug(bool debug)
void alai::StateMachine::set_debug(bool debug)
{
this->debug = debug;
}
bool StateMachine::get_debug()
bool alai::StateMachine::get_debug()
{
return this->debug;
}
void StateMachine::_on_StateMachine_tree_entered()
void alai::StateMachine::_on_StateMachine_tree_entered()
{
setup();
}
void StateMachine::_on_StateMachine_tree_exiting()
void alai::StateMachine::_on_StateMachine_tree_exiting()
{
auto keys = states.keys();
for (uint8_t i = 0; i < keys.size(); i++)

View File

@@ -1,32 +1,32 @@
#ifndef JUEGO_STATE_MACHINE_H
#define JUEGO_STATE_MACHINE_H
#ifndef ALAI_STATE_MACHINE_STATE_MACHINE_H
#define ALAI_STATE_MACHINE_STATE_MACHINE_H
#include <Godot.hpp>
#include <Node.hpp>
namespace godot
namespace alai
{
/**
* @brief This class provides a finite state machine that can be used with any scene and node.
*
*/
class StateMachine : public Node
class StateMachine : public godot::Node
{
GODOT_CLASS(StateMachine, Node)
GODOT_CLASS(StateMachine, godot::Node)
private:
/**
* @brief The parent node which is one level above the state machine.
*
*/
Node *parent;
godot::Node *parent;
/**
* @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.
*/
String default_state;
godot::String default_state;
/**
* @brief If set to true the state machine will print a message showing when it enters or exits a state.
*
@@ -36,12 +36,12 @@ namespace godot
* @brief The current state the machine is in.
*
*/
String current_state;
godot::String current_state;
/**
* @brief A list of the states in the state machine.
*
*/
Dictionary states;
godot::Dictionary states;
/**
* @brief This adds all nodes of the states machine as states in the machine.
@@ -55,14 +55,14 @@ namespace godot
* @param[in] state The new state.
* @param[in] child The godot node which represents the state.
*/
void add_state(const String state, Node *child);
void add_state(const godot::String state, godot::Node *child);
/**
* @brief Set the current state object.
*
* @param[in] current_state The current state that is running.
*/
void set_current_state(const String current_state);
void set_current_state(const godot::String current_state);
public:
/**
@@ -109,7 +109,7 @@ namespace godot
* @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.
*/
bool is_current(const String state);
bool is_current(const godot::String state);
/**
* @brief Check if the state machine has a given state.
@@ -118,7 +118,7 @@ namespace godot
* @return true If the state exists.
* @return false If the state doesn't exist.
*/
bool has(const String state);
bool has(const godot::String state);
/**
* @brief Restar the running state by calling its enter and exit methods.
@@ -126,7 +126,7 @@ namespace godot
* @param[in] state The state that is being restarted.
* @param[in] args The arguments to pass to the state on exit and enter.
*/
void restart(const String state, const Array& args = Array());
void restart(const godot::String state, const godot::Array& args = godot::Array());
/**
* @brief Change to a different state.
@@ -136,7 +136,7 @@ namespace godot
* @param[in] state The state to change to.
* @param[in] args The arguments to pass to the exiting state and the entering state.
*/
void change(const String state, const Array &args = Array());
void change(const godot::String state, const godot::Array &args = godot::Array());
/**
* @brief Call a registered godot method in the class.
@@ -145,7 +145,7 @@ namespace godot
* @param[in] args The arguments to pass to the method.
* @return Variant Returns a Variant based off what the method returns.
*/
Variant call(const String method, const Array &args = Array());
godot::Variant call(const godot::String method, const godot::Array &args = godot::Array());
/**
* @brief This method is to link up a signal call back.
@@ -154,28 +154,28 @@ namespace godot
* @param[in] args The arguments to pass to the method.
* @return Variant Returns a Variant based off what the method returns.
*/
Variant _call(const String method, const Array &args = Array());
godot::Variant _call(const godot::String method, const godot::Array &args = godot::Array());
/**
* @brief Set the default state object.
*
* @param[in] default_state The new default state.
*/
void set_default_state(const String default_state);
void set_default_state(const godot::String default_state);
/**
* @brief Get the default state object.
*
* @return String The default state.
*/
String get_default_state();
godot::String get_default_state();
/**
* @brief Get the current state object.
*
* @return String The current running state.
*/
String get_current_state();
godot::String get_current_state();
/**
* @brief Set the debug object.
@@ -210,51 +210,51 @@ namespace godot
/**
* @brief Restarts the running state.
*
* @tparam Args Variable number of arguments to pass to restart.
* @param Args Variable number of arguments to pass to restart.
* @param[in] state The state being restarted.
* @param[in] args The arguments to pass when restarting.
*/
template <class ...Args> void restart(const String state, Args ...args)
template <class ...Args> void restart(const godot::String state, Args ...args)
{
return restart(state, Array::make(args...));
return restart(state, godot::Array::make(args...));
}
/**
* @brief Changes to a new state.
*
* @tparam Args Variable number of arguments to pass when chaning states.
* @param Args Variable number of arguments to pass when changing states.
* @param[in] state The state to change to.
* @param[in] args The arguments to pass to the new state.
*/
template <class ...Args> void change(const String state, Args ...args)
template <class ...Args> void change(const godot::String state, Args ...args)
{
return change(state, Array::make(args...));
return change(state, godot::Array::make(args...));
}
/**
* @brief Call a registered godot method in the class.
*
* @tparam Args The variable arguments to pass to the method.
* @param Args The variable arguments to pass to the method.
* @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.
*/
template <class ...Args> Variant call(const String method, Args ...args)
template <class ...Args> godot::Variant call(const godot::String method, Args ...args)
{
return call(method, Array::make(args...));
return call(method, godot::Array::make(args...));
}
/**
* @brief This is used to connect a callback from a signal.
*
* @tparam Args The arguments to pass to the callback method.
* @param Args The arguments to pass to the callback method.
* @param[in] method The method to call.
* @param[in] args The arguments to pass.
* @return Variant The Variant object returned by the method called.
*/
template <class ...Args> Variant _call(const String method, Args ...args)
template <class ...Args> godot::Variant _call(const godot::String method, Args ...args)
{
return _call(method, Array::make(args...));
return _call(method, godot::Array::make(args...));
}
};
}