add state machine and have player use it

This commit is contained in:
2022-04-11 13:30:51 -04:00
parent 6d2d1178a7
commit ecd6fe45b2
28 changed files with 1303 additions and 416 deletions

View File

@@ -7,18 +7,20 @@
#include <Texture.hpp>
using namespace godot;
using namespace player;
void Player::_register_methods()
{
register_method("_ready", &Player::_ready);
register_method("_physics_process", &Player::_physics_process);
register_method("_on_Player_player_moved", &Player::_on_Player_player_moved);
register_method("set_velocity", &Player::set_velocity);
register_method("get_velocity", &Player::get_velocity);
//register_property<Player, Ref<SpriteFrames>>("sprite_frames", &Player::set_sprite_frames, &Player::get_sprite_frames, Ref<SpriteFrames>(), GODOT_METHOD_RPC_MODE_DISABLED, GODOT_PROPERTY_USAGE_DEFAULT, GODOT_PROPERTY_HINT_RESOURCE_TYPE, String("SpriteFrames"));
register_property<Player, float>("speed", &Player::set_speed, &Player::get_speed, ALAI_PLAYER_SPEED);
register_property<Player, float>("jump_force", &Player::set_jump_force, &Player::get_jump_force, ALAI_PLAYER_JUMP_FORCE);
register_property<Player, float>("gravity", &Player::set_gravity, &Player::get_gravity, ALAI_PLAYER_GRAVITY);
register_property<Player, float>("run_speed", &Player::set_run_speed, &Player::get_run_speed, ALAI_PLAYER_RUN_SPEED);
register_signal<Player>("player_moved", "position", GODOT_VARIANT_TYPE_VECTOR2);
register_property<Player, float>("speed", &Player::set_speed, &Player::get_speed, player::speed);
register_property<Player, float>("jump_force", &Player::set_jump_force, &Player::get_jump_force, player::jump_force);
register_property<Player, float>("gravity", &Player::set_gravity, &Player::get_gravity, player::gravity);
register_property<Player, float>("run_speed", &Player::set_run_speed, &Player::get_run_speed, player::run_speed);
register_property<Player, bool>("double_jump", &Player::set_double_jump, &Player::get_double_jump, player::double_jump);
}
Player::Player()
@@ -31,21 +33,18 @@ Player::~Player()
void Player::_init()
{
_os = OS::get_singleton();
_input = Input::get_singleton();
_resource_loader = ResourceLoader::get_singleton();
//sprite_frames = _resource_loader->load(ALAI_PLAYER_SPRITE_FRAMES);
set_speed(ALAI_PLAYER_SPEED);
set_jump_force(ALAI_PLAYER_JUMP_FORCE);
set_gravity(ALAI_PLAYER_GRAVITY);
set_run_speed(ALAI_PLAYER_RUN_SPEED);
//sprite_frames = _resource_loader->load(player::sprite_frames);
set_speed(player::speed);
set_jump_force(player::jump_force);
set_gravity(player::gravity);
set_run_speed(player::run_speed);
set_double_jump(player::double_jump);
coins = 0;
velocity = Vector2();
jumping = 0;
}
void Player::_ready()
@@ -53,7 +52,7 @@ void Player::_ready()
animated_sprite = get_node<AnimatedSprite>("AnimatedSprite");
if (!animated_sprite)
{
WARN_PRINT("AnimateSprite not found!");
ERR_PRINT("AnimateSprite not found!");
animated_sprite = AnimatedSprite()._new();
}
//animated_sprite->set_sprite_frames(sprite_frames);
@@ -74,74 +73,7 @@ void Player::_ready()
void Player::_physics_process(float delta)
{
auto current_speed = get_speed();
velocity.x = 0;
if (_input->is_action_pressed("run"))
{
current_speed *= get_run_speed();
}
if (_input->is_action_pressed("right"))
{
velocity.x += current_speed;
}
if (_input->is_action_pressed("left"))
{
velocity.x += -current_speed;
}
if (velocity.x > 0)
{
animated_sprite->play("move");
animated_sprite->set_flip_h(true);
}
else if (velocity.x < 0)
{
animated_sprite->play("move");
animated_sprite->set_flip_h(false);
}
else
{
animated_sprite->stop();
animated_sprite->set_animation("idle");
}
if (jumping > 0 && is_on_floor())
{
animated_sprite->set_frame(1);
jumping = 0;
}
if (!is_on_floor())
{
animated_sprite->stop();
animated_sprite->set_animation("air");
if (jumping == 0)
{
jumping = 2;
}
}
velocity.y += get_gravity();
if (_input->is_action_just_pressed("jump") && jumping < 2)
{
velocity.y = -get_jump_force();
jumping++;
}
if (_input->is_action_just_released("jump"))
{
if (velocity.y < -100)
{
velocity.y = -100;
}
}
if (velocity.x < 0 || velocity.x > 0)
{
//emit_signal("player_moved", get_position());
}
velocity = move_and_slide(velocity, Vector2::UP, true);
velocity.x = Math::lerp((float) velocity.x, (float) 0, (float) 0.2);
@@ -170,7 +102,6 @@ void Player::_physics_process(float delta)
{
if (get_parent()->get_class() == "TileMap")
{
Godot::print("Off screen");
auto error = get_tree()->change_scene("res://Main.tscn");
if (error != Error::OK)
{
@@ -231,7 +162,22 @@ float Player::get_run_speed()
return run_speed;
}
void Player::_on_Player_player_moved(Vector2 position)
void Player::set_double_jump(bool double_jump)
{
Godot::print(position);
this->double_jump = double_jump;
}
bool Player::get_double_jump()
{
return this->double_jump;
}
void Player::set_velocity(Vector2 velocity)
{
this->velocity = velocity;
}
Vector2 Player::get_velocity()
{
return this->velocity;
}

View File

@@ -1,37 +1,12 @@
#ifndef ALAI_PLAYER_H
#define ALAI_PLAYER_H
/**
* @brief This resource is loaded by default for the AnimatedSprite node.
*/
#define ALAI_PLAYER_SPRITE_FRAMES "res://characters/player/sprites/green.tres"
/**
* @brief The speed the player should move it.
*/
#define ALAI_PLAYER_SPEED 60.0
/**
* @brief The force applied to the player when jumping.
*/
#define ALAI_PLAYER_JUMP_FORCE 300.0
/**
* @brief The gravity applied to the player.
*/
#define ALAI_PLAYER_GRAVITY 9.81
/**
* @brief The multiplier used to change the speed of the player when running.
*/
#define ALAI_PLAYER_RUN_SPEED 2.0
#include <Godot.hpp>
#include <KinematicBody2D.hpp>
#include <OS.hpp>
#include <Sprite.hpp>
#include <Vector2.hpp>
#include <Input.hpp>
#include <AnimatedSprite.hpp>
#include <SpriteFrames.hpp>
#include <ResourceLoader.hpp>
#include <PackedScene.hpp>
/**
* @brief This is the godot namespace for all the code included in the library.
@@ -39,181 +14,178 @@
*/
namespace godot
{
/**
* @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
namespace player
{
GODOT_CLASS(Player, KinematicBody2D)
const char player_sprite_frames[] = "res://characters/player/sprites/green.tres";
const float speed = 60.0;
const float jump_force = 300.0;
const float gravity = 9.81;
const float run_speed = 2.0;
const bool double_jump = true;
private:
/**
* @brief OS singleton.
*/
OS *_os;
/**
* @brief Input singleton.
*/
Input *_input;
/**
* @brief ResourceLoader singleton.
*/
ResourceLoader *_resource_loader;
/**
* @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
{
GODOT_CLASS(Player, KinematicBody2D)
/**
* @brief The animated sprite connected to the KinematicBody2D.
*/
AnimatedSprite *animated_sprite;
/**
* @brief The sprite frames used in the animated sprite.
*/
Ref<SpriteFrames> 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;
private:
/**
* @brief ResourceLoader singleton.
*/
ResourceLoader *_resource_loader;
/**
* @brief State of jumping of the player. To be replaced with a state machine in the future.
*/
uint8_t jumping;
/**
* @brief The animated sprite connected to the KinematicBody2D.
*/
AnimatedSprite *animated_sprite;
/**
* @brief The sprite frames used in the animated sprite.
*/
Ref<SpriteFrames> 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;
public:
/**
* @brief This method registers classes with Godot.
* @details This method registers methods, properties, and signals with the Godot engine.
*/
static void _register_methods();
bool double_jump;
/**
* @brief Construct a new Player object.
*/
Player();
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 Destroy the Player object.
*/
~Player();
/**
* @brief Construct a new 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 Destroy the Player object.
*/
~Player();
/**
* @brief Code to be run when ready.
* @details This method is run when all the children of this node are ready.
*/
void _ready();
/**
* @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 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.
*/
void _physics_process(float delta);
/**
* @brief Code to be run when ready.
* @details This method is run when all the children of this node are ready.
*/
void _ready();
/**
* @brief Set the sprite frames object.
*
* @param[in] new_sprite_frames The new sprite frame.
*/
void set_sprite_frames(Ref<SpriteFrames> new_sprite_frames);
/**
* @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.
*/
void _physics_process(float delta);
/**
* @brief Get the sprite frames object.
*
* @return Ref<SpriteFrames> A reference to the sprite frames object.
*/
Ref<SpriteFrames> get_sprite_frames();
/**
* @brief Set the sprite frames object.
*
* @param[in] new_sprite_frames The new sprite frame.
*/
void set_sprite_frames(Ref<SpriteFrames> new_sprite_frames);
/**
* @brief Set the speed object.
*
* @param[in] new_speed The new speed.
*/
void set_speed(float new_speed);
/**
* @brief Get the sprite frames object.
*
* @return Ref<SpriteFrames> A reference to the sprite frames object.
*/
Ref<SpriteFrames> get_sprite_frames();
/**
* @brief Get the speed object.
*
* @return float The current speed of the player.
*/
float get_speed();
/**
* @brief Set the speed object.
*
* @param[in] new_speed The new speed.
*/
void set_speed(float new_speed);
/**
* @brief Set the jump force object.
*
* @param[in] new_jump_force The new force applied to the player to make him jump.
*/
void set_jump_force(float new_jump_force);
/**
* @brief Get the speed object.
*
* @return float The current speed of the player.
*/
float get_speed();
/**
* @brief Get the jump force object.
*
* @return float The current force being applied to the player.
*/
float get_jump_force();
/**
* @brief Set the jump force object.
*
* @param[in] new_jump_force The new force applied to the player to make him jump.
*/
void set_jump_force(float new_jump_force);
/**
* @brief Set the gravity object.
*
* @param[in] new_gravity The new gravity to apply to the player.
*/
void set_gravity(float new_gravity);
/**
* @brief Get the jump force object.
*
* @return float The current force being applied to the player.
*/
float get_jump_force();
/**
* @brief Get the gravity object.
*
* @return float The current gravity applied to the player.
*/
float get_gravity();
/**
* @brief Set the gravity object.
*
* @param[in] new_gravity The new gravity to apply to the player.
*/
void set_gravity(float new_gravity);
/**
* @brief Set the run speed object.
*
* @param[in] new_run_speed The new speed for running.
*/
void set_run_speed(float new_run_speed);
/**
* @brief Get the gravity object.
*
* @return float The current gravity applied to the player.
*/
float get_gravity();
/**
* @brief Get the run speed object.
*
* @return float The current run speed of the player.
*/
float get_run_speed();
/**
* @brief Set the run speed object.
*
* @param[in] new_run_speed The new speed for running.
*/
void set_run_speed(float new_run_speed);
/**
* @brief This signal is called when the player moves.
* @details The jump action is not included in this signal, just moving left or right.
*
* @param[in] position The new position of the player.
*/
void _on_Player_player_moved(Vector2 position);
};
/**
* @brief Get the run speed object.
*
* @return float The current run speed of the player.
*/
float get_run_speed();
void set_double_jump(bool double_jump);
bool get_double_jump();
void set_velocity(Vector2 velocity);
Vector2 get_velocity();
};
}
}
#endif

View File

@@ -0,0 +1,78 @@
#include "player/states/PlayerFall.h"
#include "player/Player.h"
using namespace godot;
using namespace player;
void PlayerFall::_register_methods()
{
register_method("_state_enter", &PlayerFall::_state_enter);
register_method("_state_exit", &PlayerFall::_state_exit);
register_method("_physics_process", &PlayerFall::_physics_process);
}
PlayerFall::PlayerFall()
{
}
PlayerFall::~PlayerFall()
{
}
void PlayerFall::_init()
{
_input = Input::get_singleton();
}
void PlayerFall::_state_enter()
{
animated_sprite = get_parent()->get_node<AnimatedSprite>("AnimatedSprite");
animated_sprite->stop();
animated_sprite->set_animation("air");
}
void PlayerFall::_state_exit()
{
animated_sprite->set_animation("move");
animated_sprite->set_frame(1);
}
void PlayerFall::_physics_process(float delta)
{
auto parent = Object::cast_to<player::Player>(get_parent());
if (parent->is_on_floor())
{
get_state_machine()->change("Move");
return;
}
auto current_speed = parent->get_speed();
auto velocity = parent->get_velocity();
velocity.x = 0;
if (_input->is_action_pressed("run"))
{
current_speed *= parent->get_run_speed();
}
if (_input->is_action_pressed("right"))
{
velocity.x += current_speed;
}
if (_input->is_action_pressed("left"))
{
velocity.x += -current_speed;
}
if (velocity.x > 0)
{
animated_sprite->set_flip_h(true);
}
else if (velocity.x < 0)
{
animated_sprite->set_flip_h(false);
}
parent->set_velocity(velocity);
}

View File

@@ -0,0 +1,44 @@
#ifndef JUEGO_PLAYER_FALL_H
#define JUEGO_PLAYER_FALL_H
#include "state_machine/State.h"
#include <Godot.hpp>
#include <Input.hpp>
#include <AnimatedSprite.hpp>
namespace godot
{
namespace player
{
class PlayerFall : public State
{
GODOT_CLASS(PlayerFall, State)
private:
Input *_input;
AnimatedSprite *animated_sprite;
public:
static void _register_methods();
PlayerFall();
~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();
void _state_enter();
void _state_exit();
void _physics_process(float delta);
};
}
}
#endif

View File

@@ -0,0 +1,65 @@
#include "player/states/PlayerIdle.h"
#include "player/Player.h"
using namespace godot;
using namespace player;
void PlayerIdle::_register_methods()
{
register_method("_state_enter", &PlayerIdle::_state_enter);
register_method("_state_exit", &PlayerIdle::_state_exit);
register_method("_physics_process", &PlayerIdle::_physics_process);
}
PlayerIdle::PlayerIdle()
{
}
PlayerIdle::~PlayerIdle()
{
}
void PlayerIdle::_init()
{
_input = Input::get_singleton();
}
void PlayerIdle::_state_enter()
{
animated_sprite = get_parent()->get_node<AnimatedSprite>("AnimatedSprite");
animated_sprite->stop();
animated_sprite->set_animation("idle");
}
void PlayerIdle::_state_exit()
{
}
void PlayerIdle::_physics_process(float delta)
{
auto parent = Object::cast_to<player::Player>(get_parent());
if (_input->is_action_pressed("right"))
{
get_state_machine()->change("Move");
return;
}
if (_input->is_action_pressed("left"))
{
get_state_machine()->change("Move");
return;
}
if (_input->is_action_just_pressed("jump"))
{
get_state_machine()->change("Jump");
return;
}
if (!parent->is_on_floor())
{
get_state_machine()->change("Fall");
return;
}
}

View File

@@ -0,0 +1,45 @@
#ifndef JUEGO_PLAYER_IDLE_H
#define JUEGO_PLAYER_IDLE_H
#include "state_machine/State.h"
#include <Godot.hpp>
#include <Node.hpp>
#include <Input.hpp>
#include <AnimatedSprite.hpp>
namespace godot
{
namespace player
{
class PlayerIdle : public State
{
GODOT_CLASS(PlayerIdle, State)
private:
Input *_input;
AnimatedSprite *animated_sprite;
public:
static void _register_methods();
PlayerIdle();
~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();
void _state_enter();
void _state_exit();
void _physics_process(float delta);
};
}
}
#endif

View File

@@ -0,0 +1,105 @@
#include "player/states/PlayerJump.h"
#include "player/Player.h"
using namespace godot;
using namespace player;
void PlayerJump::_register_methods()
{
register_method("_state_enter", &PlayerJump::_state_enter);
register_method("_state_exit", &PlayerJump::_state_exit);
register_method("_physics_process", &PlayerJump::_physics_process);
}
PlayerJump::PlayerJump()
{
}
PlayerJump::~PlayerJump()
{
}
void PlayerJump::_init()
{
_input = Input::get_singleton();
}
void PlayerJump::_state_enter(const String state)
{
animated_sprite = get_parent()->get_node<AnimatedSprite>("AnimatedSprite");
animated_sprite->stop();
animated_sprite->set_animation("air");
if (state == "Jump")
{
double_jumped = true;
}
else
{
double_jumped = false;
}
auto parent = Object::cast_to<player::Player>(get_parent());
auto velocity = parent->get_velocity();
velocity.y = -parent->get_jump_force();
parent->set_velocity(velocity);
}
void PlayerJump::_state_exit()
{
animated_sprite->set_animation("move");
animated_sprite->set_frame(1);
}
void PlayerJump::_physics_process(float delta)
{
auto parent = Object::cast_to<player::Player>(get_parent());
if (parent->is_on_floor())
{
get_state_machine()->change("Move");
return;
}
auto current_speed = parent->get_speed();
auto velocity = parent->get_velocity();
velocity.x = 0;
if (_input->is_action_pressed("run"))
{
current_speed *= parent->get_run_speed();
}
if (_input->is_action_pressed("right"))
{
velocity.x += current_speed;
}
if (_input->is_action_pressed("left"))
{
velocity.x += -current_speed;
}
if (_input->is_action_just_released("jump"))
{
if (velocity.y < -100)
{
velocity.y = -100;
}
}
if (parent->get_double_jump() && !double_jumped && _input->is_action_just_pressed("jump"))
{
get_state_machine()->change("Jump");
return;
}
if (velocity.x > 0)
{
animated_sprite->set_flip_h(true);
}
else if (velocity.x < 0)
{
animated_sprite->set_flip_h(false);
}
parent->set_velocity(velocity);
}

View File

@@ -0,0 +1,45 @@
#ifndef JUEGO_PLAYER_JUMP_H
#define JUEGO_PLAYER_JUMP_H
#include "state_machine/State.h"
#include <Godot.hpp>
#include <Input.hpp>
#include <AnimatedSprite.hpp>
namespace godot
{
namespace player
{
class PlayerJump : public State
{
GODOT_CLASS(PlayerJump, State)
private:
Input *_input;
AnimatedSprite *animated_sprite;
bool double_jumped;
public:
static void _register_methods();
PlayerJump();
~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();
void _state_enter(const String state);
void _state_exit();
void _physics_process(float delta);
};
}
}
#endif

View File

@@ -0,0 +1,94 @@
#include "player/states/PlayerMove.h"
#include "player/Player.h"
using namespace godot;
using namespace player;
void PlayerMove::_register_methods()
{
register_method("_state_enter", &PlayerMove::_state_enter);
register_method("_state_exit", &PlayerMove::_state_exit);
register_method("_physics_process", &PlayerMove::_physics_process);
}
PlayerMove::PlayerMove()
{
}
PlayerMove::~PlayerMove()
{
}
void PlayerMove::_init()
{
_input = Input::get_singleton();
}
void PlayerMove::_state_enter(String state, Array args)
{
animated_sprite = get_parent()->get_node<AnimatedSprite>("AnimatedSprite");
animated_sprite->set_animation("move");
animated_sprite->play();
}
void PlayerMove::_state_exit()
{
}
void PlayerMove::_physics_process(float delta)
{
auto parent = Object::cast_to<player::Player>(get_parent());
auto direction_pressed = false;
auto current_speed = parent->get_speed();
auto velocity = parent->get_velocity();
velocity.x = 0;
if (_input->is_action_pressed("run"))
{
current_speed *= parent->get_run_speed();
}
if (_input->is_action_pressed("right"))
{
direction_pressed = true;
velocity.x += current_speed;
}
if (_input->is_action_pressed("left"))
{
direction_pressed = true;
velocity.x += -current_speed;
}
if (_input->is_action_just_pressed("jump"))
{
get_state_machine()->change("Jump");
return;
}
if (velocity.x > 0)
{
animated_sprite->set_flip_h(true);
}
else if (velocity.x < 0)
{
animated_sprite->set_flip_h(false);
}
else
{
if (!direction_pressed)
{
get_state_machine()->change("Idle");
}
return;
}
parent->set_velocity(velocity);
if (!parent->is_on_floor())
{
get_state_machine()->change("Fall");
return;
}
}

View File

@@ -0,0 +1,46 @@
#ifndef JUEGO_PLAYER_MOVE_H
#define JUEGO_PLAYER_MOVE_H
#include "state_machine/State.h"
#include <Godot.hpp>
#include <Input.hpp>
#include <AnimatedSprite.hpp>
namespace godot
{
namespace player
{
class PlayerMove : public State
{
GODOT_CLASS(PlayerMove, State)
private:
Input *_input;
AnimatedSprite *animated_sprite;
public:
static void _register_methods();
PlayerMove();
~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);
void _state_exit();
void _physics_process(float delta);
Vector2 move_player(Vector2 velocity);
};
}
}
#endif