diff --git a/src/Main.h b/src/Main.h index af61ec8..4d86783 100644 --- a/src/Main.h +++ b/src/Main.h @@ -8,18 +8,37 @@ /** * @brief This is the godot namespace for all the code included in the library. + * * @details This namespace is used a prefix when the Godot engine looks for classes, methods, and properties. */ namespace godot { + /** + * @brief This namespace houses some global variables and the main class. + * + */ namespace main { + /** + * @brief The default value for if the game should start in full screen. + * + */ const bool full_screen = false; + /** + * @brief The default resolution for the game window. + * + */ const Vector2 window_size = Vector2(1280, 720); + /** + * @brief The default screen the the game should open on. + * + * @details -1 opens it on the currently active screen. And 0 and above are the screens to use. + */ const int8_t launch_screen = -1; /** * @brief This class controls the Main node. + * * @details The main node is responsible for controling the window and the game iteself is a child of it. */ class Main : public Node @@ -29,57 +48,68 @@ namespace godot private: /** * @brief OS singleton. + * */ OS *_os; /** * @brief Input singleton. + * */ Input *_input; /** * @brief If the window is full screen or not. + * */ bool full_screen; /** * @brief The size of the window. + * */ Vector2 window_size; /** * @brief The screen to launch the game on. + * */ int8_t launch_screen; public: /** * @brief This method registers classes with Godot. + * * @details This method registers methods, properties, and signals with the Godot engine. */ static void _register_methods(); /** * @brief Construct a new Main object. + * */ Main(); /** * @brief Destroy the Main object. + * */ ~Main(); /** * @brief Initialize the class from Godot. + * * @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. + * * @details This method is run when all the children of this node are ready. */ void _ready(); /** * @brief This class handles the physics processing. + * * @details Every delta time, this function is called to check for input and update positioning. * * @param[in] delta The difference in time that passed since the last call to this method. diff --git a/src/godot.cpp b/src/godot.cpp index b449e46..76e07ec 100644 --- a/src/godot.cpp +++ b/src/godot.cpp @@ -11,11 +11,19 @@ using namespace godot; +/** + * @brief This function connects the gdnative init function. + * + */ extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o) { Godot::gdnative_init(o); } +/** + * @brief This function connects the gdnative terminate function. + * + */ extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *o) { // This next line is a workaround to fix bug: @@ -24,6 +32,10 @@ extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_opt Godot::gdnative_terminate(o); } +/** + * @brief This function connects the init methods in the classes to godot's gdnative. + * + */ extern "C" void GDN_EXPORT godot_nativescript_init(void *handle) { Godot::nativescript_init(handle); diff --git a/src/player/Player.h b/src/player/Player.h index 78bc986..fd2474a 100644 --- a/src/player/Player.h +++ b/src/player/Player.h @@ -10,21 +10,51 @@ /** * @brief This is the godot namespace for all the code included in the library. + * * @details This namespace is used a prefix when the Godot engine looks for classes, methods, and properties. */ namespace godot { + /** + * @brief This namespace contains the global variables related to the player and its states. + * + */ namespace player { + /** + * @brief The default sprite resource of the player. + * + */ const char player_sprite_frames[] = "res://characters/player/sprites/green.tres"; + /** + * @brief The default speed of the player. + * + */ const float speed = 60.0; + /** + * @brief The default jump force applied when jumping. + * + */ const float jump_force = 300.0; + /** + * @brief The default gravity applied to the player. + * + */ const float gravity = 9.81; + /** + * @brief The default run speed multiplier. + * + */ const float run_speed = 2.0; + /** + * @brief The default double jump activation state. + * + */ const bool double_jump = true; /** * @brief This class is used to control the player. + * * @details This class allows the player to move, run, and jump as well as controls the sprite displayed for those actions. */ class Player : public KinematicBody2D @@ -34,75 +64,93 @@ namespace godot private: /** * @brief ResourceLoader singleton. + * */ ResourceLoader *_resource_loader; /** * @brief The animated sprite connected to the KinematicBody2D. + * */ AnimatedSprite *animated_sprite; /** * @brief The sprite frames used in the animated sprite. + * */ Ref sprite_frames; /** * @brief The coins the player has collected. + * */ uint8_t coins; /** * @brief The velocity at which moves the player moves. + * */ Vector2 velocity; /** * @brief The speed that the player moves in. + * */ float speed; /** * @brief The force applied to the player when jumping. + * */ float jump_force; /** * @brief The gravity applied to the player. + * */ float gravity; /** * @brief The speed multiplier used to make the player move faster. + * */ float run_speed; - + /** + * @brief If double jump is enabled or not. + * + */ bool double_jump; public: /** * @brief This method registers classes with Godot. + * * @details This method registers methods, properties, and signals with the Godot engine. */ static void _register_methods(); /** * @brief Construct a new Player object. + * */ Player(); /** * @brief Destroy the Player object. + * */ ~Player(); /** * @brief Initialize the class from Godot. + * * @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. + * * @details This method is run when all the children of this node are ready. */ void _ready(); /** * @brief This class handles the physics processing. + * * @details Every delta time, this function is called to check for input and update positioning. * * @param[in] delta The difference in time that passed since the last call to this method. @@ -179,10 +227,33 @@ namespace godot */ float get_run_speed(); + /** + * @brief Set the double jump object. + * + * @param[in] double_jump The new double dump value. + */ void set_double_jump(bool double_jump); + + /** + * @brief Get the double jump object. + * + * @return true If double jump is enabled. + * @return false If double jump is disabled. + */ bool get_double_jump(); + /** + * @brief Set the velocity object. + * + * @param[in] velocity The new velocity of the player. + */ void set_velocity(Vector2 velocity); + + /** + * @brief Get the velocity object. + * + * @return Vector2 Returns the velocity of the player. + */ Vector2 get_velocity(); }; } diff --git a/src/player/states/PlayerFall.h b/src/player/states/PlayerFall.h index 4fcbf83..d53fd4f 100644 --- a/src/player/states/PlayerFall.h +++ b/src/player/states/PlayerFall.h @@ -7,35 +7,80 @@ #include #include +/** + * @brief This is the godot namespace for all the code included in the library. + * + * @details This namespace is used a prefix when the Godot engine looks for classes, methods, and properties. + */ namespace godot { namespace player { + /** + * @brief This class controls what happens when the player is in a falling state. + * + */ class PlayerFall : public State { GODOT_CLASS(PlayerFall, State) private: + /** + * @brief Input singleton. + * + */ Input *_input; + + /** + * @brief The animated sprite connected to the player. + * + */ AnimatedSprite *animated_sprite; public: + /** + * @brief This method registers classes with Godot. + * + * @details This method registers methods, properties, and signals with the Godot engine. + */ static void _register_methods(); + /** + * @brief Construct a new Player Fall object. + * + */ PlayerFall(); + /** + * @brief Destroy the Player Fall object. + * + */ ~PlayerFall(); /** * @brief Initialize the class from Godot. + * * @details This method is called just once when the Godot engine connects to the instance of the class. */ void _init(); + /** + * @brief Called when the fall state is entered. + * + */ void _state_enter(); + /** + * @brief Called when the fall state is exited. + * + */ void _state_exit(); + /** + * @brief The physics processed every delta time. + * + * @param[in] delta The time since the method was last run. + */ void _physics_process(float delta); }; } diff --git a/src/player/states/PlayerIdle.h b/src/player/states/PlayerIdle.h index 631aef4..76e4685 100644 --- a/src/player/states/PlayerIdle.h +++ b/src/player/states/PlayerIdle.h @@ -12,31 +12,70 @@ namespace godot { namespace player { + /** + * @brief This class controls what happens when the player is in the idle state. + * + */ class PlayerIdle : public State { GODOT_CLASS(PlayerIdle, State) private: + /** + * @brief Input singleton. + * + */ Input *_input; + /** + * @brief The animated sprite of the player. + * + */ AnimatedSprite *animated_sprite; public: + /** + * @brief This method registers classes with Godot. + * + * @details This method registers methods, properties, and signals with the Godot engine. + */ static void _register_methods(); + /** + * @brief Construct a new Player Idle object. + * + */ PlayerIdle(); + /** + * @brief Destroy the Player Idle object. + * + */ ~PlayerIdle(); /** * @brief Initialize the class from Godot. + * * @details This method is called just once when the Godot engine connects to the instance of the class. */ void _init(); + /** + * @brief Called when the idle state is entered. + * + */ void _state_enter(); + /** + * @brief Called when the idle state is exited. + * + */ void _state_exit(); + /** + * @brief The physics processed every delta time. + * + * @param[in] delta The time since the method was last run. + */ void _physics_process(float delta); }; } diff --git a/src/player/states/PlayerJump.h b/src/player/states/PlayerJump.h index 7458832..aee1059 100644 --- a/src/player/states/PlayerJump.h +++ b/src/player/states/PlayerJump.h @@ -11,32 +11,78 @@ namespace godot { namespace player { + /** + * @brief This class control what happens when the player is in the jump state. + * + */ class PlayerJump : public State { GODOT_CLASS(PlayerJump, State) private: + /** + * @brief Input singleton. + * + */ Input *_input; + /** + * @brief The animated sprite connected to the player. + * + */ AnimatedSprite *animated_sprite; + /** + * @brief If the player has already performed a double jump or not. + * + */ bool double_jumped; public: + /** + * @brief This method registers classes with Godot. + * + * @details This method registers methods, properties, and signals with the Godot engine. + */ static void _register_methods(); + /** + * @brief Construct a new Player Jump object. + * + */ PlayerJump(); + /** + * @brief Destroy the Player Jump object. + * + */ ~PlayerJump(); /** * @brief Initialize the class from Godot. + * * @details This method is called just once when the Godot engine connects to the instance of the class. */ void _init(); + /** + * @brief Called when the player enters the jump state. + * + * @details If the previous state was the jump state, a double jump was performed. + * + * @param[in] state The previous state before this one was entered. + */ void _state_enter(const String state); + /** + * @brief Called when the player exits the jump state. + * + */ void _state_exit(); + /** + * @brief The physics processed every delta time. + * + * @param[in] delta The time since the method was last run. + */ void _physics_process(float delta); }; } diff --git a/src/player/states/PlayerMove.cpp b/src/player/states/PlayerMove.cpp index 787db1d..77df13e 100644 --- a/src/player/states/PlayerMove.cpp +++ b/src/player/states/PlayerMove.cpp @@ -24,7 +24,7 @@ void PlayerMove::_init() _input = Input::get_singleton(); } -void PlayerMove::_state_enter(String state, Array args) +void PlayerMove::_state_enter() { animated_sprite = get_parent()->get_node("AnimatedSprite"); animated_sprite->set_animation("move"); diff --git a/src/player/states/PlayerMove.h b/src/player/states/PlayerMove.h index a41e6d4..0df3078 100644 --- a/src/player/states/PlayerMove.h +++ b/src/player/states/PlayerMove.h @@ -11,34 +11,71 @@ namespace godot { namespace player { + /** + * @brief This class controls what happens when the player is in the move state. + * + */ class PlayerMove : public State { GODOT_CLASS(PlayerMove, State) private: + /** + * @brief Input singleton. + * + */ Input *_input; + /** + * @brief The animated sprite of the player. + * + */ AnimatedSprite *animated_sprite; public: + /** + * @brief This method registers classes with Godot. + * + * @details This method registers methods, properties, and signals with the Godot engine. + */ static void _register_methods(); + /** + * @brief Construct a new Player Move object. + * + */ PlayerMove(); + /** + * @brief Destroy the Player Move object. + * + */ ~PlayerMove(); /** * @brief Initialize the class from Godot. + * * @details This method is called just once when the Godot engine connects to the instance of the class. */ void _init(); - void _state_enter(String state, Array args); + /** + * @brief Called when the player enters the move state. + * + */ + void _state_enter(); + /** + * @brief Called when the player exists the move state. + * + */ void _state_exit(); + /** + * @brief The physics processed every delta time. + * + * @param[in] delta The time since the method was last run. + */ void _physics_process(float delta); - - Vector2 move_player(Vector2 velocity); }; } } diff --git a/src/state_machine/State.cpp b/src/state_machine/State.cpp index 24d8b6d..29efa48 100644 --- a/src/state_machine/State.cpp +++ b/src/state_machine/State.cpp @@ -1,4 +1,4 @@ -#include "State.h" +#include "state_machine/State.h" using namespace godot; diff --git a/src/state_machine/State.h b/src/state_machine/State.h index bd8d787..46ebe38 100644 --- a/src/state_machine/State.h +++ b/src/state_machine/State.h @@ -8,37 +8,95 @@ namespace godot { + /** + * @brief This class provides a virtual template state that real states should extend from and override. + * + */ class State : public StateMachine { GODOT_CLASS(State, Node) private: + /** + * @brief The state's parent, this is the node 1 level above the state machine. + * + */ + Node *parent; + /** + * @brief The state machine itself, used to handle all state related work. + * + */ StateMachine *state_machine; public: - Node *parent; + /** + * @brief This method registers classes with Godot. + * + * @details This method registers methods, properties, and signals with the Godot engine. + */ static void _register_methods(); + /** + * @brief Construct a new State object. + * + */ State(); + /** + * @brief Destroy the State object. + * + */ ~State(); /** * @brief Initialize the class from Godot. + * * @details This method is called just once when the Godot engine connects to the instance of the class. */ virtual void _init(); + /** + * @brief This is called when a state is entered. + * + * @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()); + /** + * @brief This is called when a state is exited. + * + * @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()); + /** + * @brief Set the parent object. + * + * @param[in] parent The parent of the state. + */ virtual void set_parent(Node *parent) final; + /** + * @brief Get the parent object. + * + * @return Node* The parent of the state. + */ virtual Node *get_parent() final; + /** + * @brief Set the state machine object. + * + * @param[in] state_machine The state machine. + */ virtual void set_state_machine(StateMachine *state_machine) final; + /** + * @brief Get the state machine object. + * + * @return StateMachine* The state machine. + */ virtual StateMachine *get_state_machine() final; }; } diff --git a/src/state_machine/StateMachine.cpp b/src/state_machine/StateMachine.cpp index 6556431..4370842 100644 --- a/src/state_machine/StateMachine.cpp +++ b/src/state_machine/StateMachine.cpp @@ -1,5 +1,5 @@ -#include "StateMachine.h" -#include "State.h" +#include "state_machine/StateMachine.h" +#include "state_machine/State.h" using namespace godot; diff --git a/src/state_machine/StateMachine.h b/src/state_machine/StateMachine.h index 5f0cea5..02789ba 100644 --- a/src/state_machine/StateMachine.h +++ b/src/state_machine/StateMachine.h @@ -6,89 +6,250 @@ namespace godot { + /** + * @brief This class provides a finite state machine that can be used with any scene and node. + * + */ class StateMachine : public Node { GODOT_CLASS(StateMachine, Node) private: + /** + * @brief The parent node which is one level above the state machine. + * + */ 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; + /** + * @brief If set to true the state machine will print a message showing when it enters or exits a state. + * + */ bool debug; + /** + * @brief The current state the machine is in. + * + */ String current_state; + /** + * @brief A list of the states in the state machine. + * + */ Dictionary states; + /** + * @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. + */ void add_state(const String state, Node *child); public: + /** + * @brief This method registers classes with Godot. + * + * @details This method registers methods, properties, and signals with the Godot engine. + */ static void _register_methods(); + /** + * @brief Construct a new State Machine object. + */ StateMachine(); + /** + * @brief Destroy the State Machine object. + */ ~StateMachine(); /** * @brief Initialize the class from Godot. + * * @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. + * * @details This method is run when all the children of this node are ready. */ void _ready(); + /** + * @brief This method will remove all no default states from the scene tree and start the default state. + * + */ void setup(); - bool is_current(); - + /** + * @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. + */ bool is_current(const String state); + /** + * @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. + */ bool has(const String state); + /** + * @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. + */ void restart(const String state, const Array& args = Array()); + /** + * @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. + */ void change(const String state, const Array &args = Array()); + /** + * @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. + */ Variant call(const String method, const Array &args = Array()); + /** + * @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. + */ Variant _call(const String method, const Array &args = Array()); - template Variant restart(Args ...args) + /** + * @brief Set the default state object. + * + * @param[in] default_state The new default state. + */ + void set_default_state(const String default_state); + + /** + * @brief Get the default state object. + * + * @return String The default state. + */ + String get_default_state(); + + /** + * @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 Set the current state object. + * + * @param[in] current_state The current state that is running. + */ + void set_current_state(const String current_state); + + /** + * @brief Get the current state object. + * + * @return String The current running state. + */ + String get_current_state(); + + /** + * @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. + * + * @tparam 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 void restart(const String state, Args ...args) { - return restart(Array::make(args...)); + return restart(state, Array::make(args...)); } - template Variant change(const String state, Args ...args) + /** + * @brief Changes to a new state. + * + * @tparam Args Variable number of arguments to pass when chaning states. + * @param[in] state The state to change to. + * @param[in] args The arguments to pass to the new state. + */ + template void change(const String state, Args ...args) { return change(state, Array::make(args...)); } + /** + * @brief Call a registered godot method in the class. + * + * @tparam 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 Variant call(const String method, Args ...args) { return call(method, 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[in] method The method to call. + * @param[in] args The arguments to pass. + * @return Variant The Variant object returned by the method called. + */ template Variant _call(const String method, Args ...args) { return _call(method, Array::make(args...)); } - - void set_default_state(const String default_state); - - String get_default_state(); - - void set_debug(bool debug); - - bool get_debug(); - - void set_current_state(const String current_state); - - String get_current_state(); - - void _on_StateMachine_tree_entered(); - - void _on_StateMachine_tree_exiting(); }; }