Merge branch 'develop' into feature/cleanup

# Conflicts:
#	src/godot.cpp
#	src/player/Player.cpp
This commit is contained in:
2022-08-28 00:38:08 -04:00
33 changed files with 1197 additions and 29 deletions

23
src/Event.cpp Normal file
View File

@@ -0,0 +1,23 @@
#include "Event.h"
void alai::Event::_register_methods()
{
godot::register_signal<Event>("object_created", "name", GODOT_VARIANT_TYPE_STRING, "state", GODOT_VARIANT_TYPE_STRING, "position", GODOT_VARIANT_TYPE_VECTOR2, "velocity", GODOT_VARIANT_TYPE_VECTOR2);
godot::register_signal<Event>("object_updated", "name", GODOT_VARIANT_TYPE_STRING, "state", GODOT_VARIANT_TYPE_STRING, "position", GODOT_VARIANT_TYPE_VECTOR2, "velocity", GODOT_VARIANT_TYPE_VECTOR2);
godot::register_signal<Event>("object_removed", "name", GODOT_VARIANT_TYPE_STRING);
godot::register_signal<Event>("coin_collected", "amount", GODOT_VARIANT_TYPE_INT);
godot::register_signal<Event>("player_died");
}
alai::Event::Event()
{
}
alai::Event::~Event()
{
}
void alai::Event::_init()
{
}

47
src/Event.h Normal file
View File

@@ -0,0 +1,47 @@
#ifndef ALAI_EVENT_H
#define ALAI_EVENT_H
#include <Godot.hpp>
#include <Node.hpp>
namespace alai
{
/**
* @brief This class provides an event bus for the project.
*
* @details This class should be an auto-loaded singleton for the project.
* To use this use Event.connect() and Event.emit_signal() to have global signals.
*/
class Event : public godot::Node
{
GODOT_CLASS(Event, godot::Node)
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 Event object.
*
*/
Event();
/**
* @brief Destroy the Event object.
*
*/
~Event();
/**
* @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();
};
}
#endif

View File

@@ -0,0 +1,55 @@
#include "coin/CoinCollected.h"
#include <AnimationPlayer.hpp>
#include "Event.h"
using namespace godot;
void CoinCollected::_register_methods()
{
register_method("_state_enter", &CoinCollected::_state_enter);
register_method("_state_exit", &CoinCollected::_state_exit);
register_method("_on_animation_finished", &CoinCollected::_on_animation_finished);
}
CoinCollected::CoinCollected()
{
}
CoinCollected::~CoinCollected()
{
}
void CoinCollected::_init()
{
}
void CoinCollected::_state_enter()
{
auto node = get_parent()->find_node("AnimationPlayer");
if (node != nullptr)
{
auto animation_player = Object::cast_to<AnimationPlayer>(node);
animation_player->play("jump");
}
}
void CoinCollected::_state_exit()
{
}
void CoinCollected::_on_animation_finished(String anim_name)
{
auto event = get_node<alai::Event>("/root/Event");
event->emit_signal("coin_collected", 1);
this->get_parent()->queue_free();
// get_state_machine()->change("CoinCounter");
}

75
src/coin/CoinCollected.h Normal file
View File

@@ -0,0 +1,75 @@
#ifndef ALAI_COIN_COLLECTED
#define ALAI_COIN_COLLECTED
#include "state_machine/State.h"
#include <Godot.hpp>
#include <Node.hpp>
#include <AnimatedSprite.hpp>
namespace godot
{
/**
* @brief This class controls what happens when the Coin is in the collected state.
*
*/
class CoinCollected : public State
{
GODOT_CLASS(CoinCollected, State)
private:
/**
* @brief The animated sprite of the Coin.
*
*/
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 CoinCollected object.
*
*/
CoinCollected();
/**
* @brief Destroy the CoinCollected object.
*
*/
~CoinCollected();
/**
* @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 collected state of the coin is entered.
*
*/
void _state_enter();
/**
* @brief Called when the collected state of the coin is exited.
*
*/
void _state_exit();
/**
* @brief Called when the animation of the collected coin has finished.
*
*/
void _on_animation_finished(String anim_name);
};
}
#endif

43
src/coin/CoinCounter.cpp Normal file
View File

@@ -0,0 +1,43 @@
#include "coin/CoinCounter.h"
#include <String.hpp>
#include "coin/CoinCollected.h"
#include "Event.h"
using namespace godot;
void CoinCounter::_register_methods()
{
register_method("_on_coin_collected", &CoinCounter::_on_coin_collected);
register_method("_ready", &CoinCounter::_ready);
}
CoinCounter::CoinCounter()
{
}
CoinCounter::~CoinCounter()
{
}
void CoinCounter::_init()
{
}
void CoinCounter::_on_CoinHUD_ready()
{
}
void CoinCounter::_on_coin_collected(int amount)
{
coins = coins + amount;
set_text(String::num(coins));
}
void CoinCounter::_ready()
{
set_text("0");
auto event = get_node<alai::Event>("/root/Event");
event->connect("coin_collected", this, "_on_coin_collected");
}

63
src/coin/CoinCounter.h Normal file
View File

@@ -0,0 +1,63 @@
#ifndef ALAI_COIN_COUNTER
#define ALAI_COIN_COUNTER
#include <Godot.hpp>
#include <Node.hpp>
#include <Label.hpp>
namespace godot
{
/**
* @brief This class controls what happens when the Coin is in the collected .
*
*/
class CoinCounter : public Label
{
GODOT_CLASS(CoinCounter, Label)
private:
int coins = 0;
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 CoinCounter object.
*
*/
CoinCounter();
/**
* @brief Destroy the CoinCounter object.
*
*/
~CoinCounter();
/**
* @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 collected of the coin is entered.
*
*/
void _on_CoinHUD_ready();
void _on_coin_collected(int amount);
void _ready();
};
}
#endif

View File

@@ -0,0 +1,56 @@
#include "coin/CoinNotCollected.h"
#include <Area2D.hpp>
using namespace godot;
void CoinNotCollected::_register_methods()
{
register_method("_state_enter", &CoinNotCollected::_state_enter);
register_method("_state_exit", &CoinNotCollected::_state_exit);
register_method("_on_body_entered", &CoinNotCollected::_on_body_entered);
}
CoinNotCollected::CoinNotCollected()
{
}
CoinNotCollected::~CoinNotCollected()
{
}
void CoinNotCollected::_init()
{
}
void CoinNotCollected::_state_enter()
{
animated_sprite = get_parent()->get_node<AnimatedSprite>("AnimatedSprite");
animated_sprite->set_animation("spin");
animated_sprite->play();
}
void CoinNotCollected::_state_exit()
{
}
void CoinNotCollected::_on_body_entered(Node *node)
{
Godot::print("Coin touched");
auto parent_node = get_parent();
if (parent_node != nullptr)
{
auto coin = Object::cast_to<Area2D>(parent_node);
coin->set_collision_mask_bit(0, false);
}
get_state_machine()->change("CoinCollected");
}

View File

@@ -0,0 +1,76 @@
#ifndef ALAI_COIN_NOT_COLLECTED
#define ALAI_COIN_NOT_COLLECTED
#include "state_machine/State.h"
#include <Godot.hpp>
#include <Node.hpp>
#include <AnimatedSprite.hpp>
namespace godot
{
/**
* @brief This class controls what happens when the Coin is in the not collected state.
*
*/
class CoinNotCollected : public State
{
GODOT_CLASS(CoinNotCollected, State)
private:
/**
* @brief The animated sprite of the Coin.
*
*/
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 CoinNotCollected object.
*
*/
CoinNotCollected();
/**
* @brief Destroy the CoinNotCollected object.
*
*/
~CoinNotCollected();
/**
* @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 not collected state of the coin is entered.
*
*/
void _state_enter();
/**
* @brief Called when the not collected state of the coin is exited.
*
*/
void _state_exit();
/**
* @brief Method called on body entered.
*
* @param[in] node Node interacting with whoever
*/
void _on_body_entered(Node *node);
};
}
#endif

View File

@@ -0,0 +1,55 @@
#include "goal/GoalNotReached.h"
#include <Area2D.hpp>
using namespace godot;
void GoalNotReached::_register_methods()
{
register_method("_state_enter", &GoalNotReached::_state_enter);
register_method("_state_exit", &GoalNotReached::_state_exit);
register_method("_on_Goal_body_entered", &GoalNotReached::_on_Goal_body_entered);
}
GoalNotReached::GoalNotReached()
{
}
GoalNotReached::~GoalNotReached()
{
}
void GoalNotReached::_init()
{
}
void GoalNotReached::_state_enter()
{
animated_sprite = get_parent()->get_node<AnimatedSprite>("AnimatedSprite");
animated_sprite->set_animation("flagmove");
animated_sprite->play();
}
void GoalNotReached::_state_exit()
{
}
void GoalNotReached::_on_Goal_body_entered(Node *node)
{
auto parent_node = get_parent();
if (parent_node != nullptr)
{
auto goal = Object::cast_to<Area2D>(parent_node);
goal->set_collision_mask_bit(0, false);
}
get_state_machine()->change("GoalReached");
}

76
src/goal/GoalNotReached.h Normal file
View File

@@ -0,0 +1,76 @@
#ifndef ALAI_GOAL_NOT_REACHED
#define ALAI_GOAL_NOT_REACHED
#include "state_machine/State.h"
#include <Godot.hpp>
#include <Node.hpp>
#include <AnimatedSprite.hpp>
namespace godot
{
/**
* @brief This class controls what happens when the Coin is in the not collected state.
*
*/
class GoalNotReached : public State
{
GODOT_CLASS(GoalNotReached, State)
private:
/**
* @brief The animated sprite of the Coin.
*
*/
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 GoalNotReached object.
*
*/
GoalNotReached();
/**
* @brief Destroy the GoalNotReached object.
*
*/
~GoalNotReached();
/**
* @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 not collected state of the coin is entered.
*
*/
void _state_enter();
/**
* @brief Called when the not collected state of the coin is exited.
*
*/
void _state_exit();
/**
* @brief Method called on body entered.
*
* @param[in] node Node interacting with whoever
*/
void _on_Goal_body_entered(Node *node);
};
}
#endif

36
src/goal/GoalReached.cpp Normal file
View File

@@ -0,0 +1,36 @@
#include "goal/GoalReached.h"
#include <Area2D.hpp>
using namespace godot;
void GoalReached::_register_methods()
{
register_method("_state_enter", &GoalReached::_state_enter);
register_method("_state_exit", &GoalReached::_state_exit);
}
GoalReached::GoalReached()
{
}
GoalReached::~GoalReached()
{
}
void GoalReached::_init()
{
}
void GoalReached::_state_enter()
{
Godot::print("Flag touched");
}
void GoalReached::_state_exit()
{
}

74
src/goal/GoalReached.h Normal file
View File

@@ -0,0 +1,74 @@
#ifndef ALAI_GOAL_REACHED
#define ALAI_GOAL_REACHED
#include "state_machine/State.h"
#include <Godot.hpp>
#include <Node.hpp>
#include <AnimatedSprite.hpp>
namespace godot
{
/**
* @brief This class controls what happens when the goal flag is in the reached state.
*
*/
class GoalReached : public State
{
GODOT_CLASS(GoalReached, State)
private:
/**
* @brief The animated sprite of the Coin.
*
*/
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 GoalReached object.
*
*/
GoalReached();
/**
* @brief Destroy the GoalReached object.
*
*/
~GoalReached();
/**
* @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 collected state of the coin is entered.
*
*/
void _state_enter();
/**
* @brief Called when the collected state of the coin is exited.
*
*/
void _state_exit();
/**
* @brief Called when the animation of the collected coin has finished.
*
*/
};
}
#endif

View File

@@ -1,14 +1,21 @@
#include <Godot.hpp>
#include "CameraLimit.h"
#include "Main.h"
#include "state_machine/State.h"
#include "Event.h"
#include "state_machine/StateMachine.h"
#include "state_machine/State.h"
#include "Main.h"
#include "CameraLimit.h"
#include "player/Player.h"
#include "player/states/PlayerIdle.h"
#include "player/states/PlayerMove.h"
#include "player/states/PlayerJump.h"
#include "player/states/PlayerFall.h"
#include "coin/CoinNotCollected.h"
#include "coin/CoinCollected.h"
#include "coin/CoinCounter.h"
#include "goal/GoalReached.h"
#include "goal/GoalNotReached.h"
#include "gui/game_over/GameOverScreen.h"
/**
* @brief This function connects the gdnative init function.
@@ -39,6 +46,7 @@ extern "C" void GDN_EXPORT godot_nativescript_init(void *handle)
{
godot::Godot::nativescript_init(handle);
godot::register_class<alai::Event>();
godot::register_class<alai::StateMachine>();
godot::register_class<alai::State>();
godot::register_class<alai::Main>();
@@ -48,4 +56,10 @@ extern "C" void GDN_EXPORT godot_nativescript_init(void *handle)
godot::register_class<alai::player::PlayerMove>();
godot::register_class<alai::player::PlayerJump>();
godot::register_class<alai::player::PlayerFall>();
godot::register_class<godot::CoinNotCollected>();
godot::register_class<godot::CoinCollected>();
godot::register_class<godot::CoinCounter>();
godot::register_class<godot::GoalReached>();
godot::register_class<godot::GoalNotReached>();
godot::register_class<godot::GameOverScreen>();
}

View File

@@ -0,0 +1,98 @@
#include "gui/game_over/GameOverScreen.h"
#include "Event.h"
#include <Resource.hpp>
#include <Ref.hpp>
#include <Node.hpp>
#include <SceneTree.hpp>
#include <PackedScene.hpp>
#include <Viewport.hpp>
using namespace godot;
void GameOverScreen::_register_methods()
{
register_method("_on_botonreiniciar_pressed", &GameOverScreen::_on_botonreiniciar_pressed);
register_method("_ready", &GameOverScreen::_ready);
register_method("connect_signal", &GameOverScreen::connect_signal);
register_method("_on_player_died", &GameOverScreen::_on_player_died);
}
GameOverScreen::GameOverScreen()
{
}
GameOverScreen::~GameOverScreen()
{
}
void GameOverScreen::_init()
{
_resource_loader = ResourceLoader::get_singleton();
}
void GameOverScreen::_ready()
{
connect_signal();
}
void GameOverScreen::_on_botonreiniciar_pressed()
{
if (_resource_loader->exists("res://levels/Prototype.tscn")) //CAMBIAR A DINAMICO
{
Ref<PackedScene> level_scene = _resource_loader->load("res://levels/Prototype.tscn");
auto level = level_scene->instance();
auto level_node = get_tree()->get_root()->get_node("Main")->find_node("Level");
if (level_node != nullptr)
{
level_node->add_child(level);
set_visible(false);
call_deferred("connect_signal");
}
else
{
WARN_PRINT("Node level not found!");
}
}
}
void GameOverScreen::_on_player_died() //eliminar nivel
{
auto event = get_node<alai::Event>("/root/Event");
event->disconnect("player_died", this, "_on_player_died");
Godot::print("player ded");
set_visible(true);
auto level_node = get_tree()->get_root()->get_node("Main")->find_node("Level");
if (level_node != nullptr)
{
auto child = level_node->get_child(0);
if (child != nullptr)
{
child->queue_free();
}
else
{
WARN_PRINT("Child not found!");
}
}
else
{
WARN_PRINT("Node level not found!");
}
}
void GameOverScreen::connect_signal()
{
auto event = get_node<alai::Event>("/root/Event");
event->connect("player_died", this, "_on_player_died");
}

View File

@@ -0,0 +1,62 @@
#ifndef ALAI_GAME_OVER_SCREEN_H
#define ALAI_GAME_OVER_SCREEN_H
#include <Godot.hpp>
#include <CanvasLayer.hpp>
#include <ResourceLoader.hpp>
namespace godot
{
/**
* @brief This class controls what happens when the Coin is in the collected .
*
*/
class GameOverScreen : public CanvasLayer
{
GODOT_CLASS(GameOverScreen, CanvasLayer)
private:
ResourceLoader *_resource_loader;
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 GameOverScreen object.
*
*/
GameOverScreen();
/**
* @brief Destroy the GameOverScreen object.
*
*/
~GameOverScreen();
/**
* @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 collected of the coin is entered.
*
*/
void _ready();
void _on_player_died();
void _on_botonreiniciar_pressed();
void connect_signal();
};
}
#endif

View File

@@ -1,5 +1,7 @@
#include "player/Player.h"
#include "Event.h"
#include <Camera2D.hpp>
#include <KinematicCollision2D.hpp>
#include <RayCast2D.hpp>
@@ -24,9 +26,6 @@ void alai::player::Player::_register_methods()
godot::register_property<Player, float>("gravity", &Player::set_gravity, &Player::get_gravity, player::gravity);
godot::register_property<Player, float>("run_speed", &Player::set_run_speed, &Player::get_run_speed, player::run_speed);
godot::register_property<Player, bool>("double_jump", &Player::set_double_jump, &Player::get_double_jump, player::double_jump);
godot::register_signal<Player>("object_created", "name", GODOT_VARIANT_TYPE_STRING, "state", GODOT_VARIANT_TYPE_STRING, "position", GODOT_VARIANT_TYPE_VECTOR2, "velocity", GODOT_VARIANT_TYPE_VECTOR2);
godot::register_signal<Player>("object_updated", "name", GODOT_VARIANT_TYPE_STRING, "state", GODOT_VARIANT_TYPE_STRING, "position", GODOT_VARIANT_TYPE_VECTOR2, "velocity", GODOT_VARIANT_TYPE_VECTOR2);
godot::register_signal<Player>("object_removed", "name", GODOT_VARIANT_TYPE_STRING);
}
alai::player::Player::Player()
@@ -78,28 +77,16 @@ void alai::player::Player::_ready()
}
void alai::player::Player::_on_monitor_loaded() {
auto object_node = get_tree()->get_root()->find_node("Monitor", true, false);
if (object_node != nullptr)
auto state = get_node("StateMachine")->get_child(0);
if (state != nullptr)
{
auto state = get_node("StateMachine")->get_child(0);
if (state != nullptr)
{
connect("object_created", object_node, "_object_created");
connect("object_updated", object_node, "_object_updated");
connect("object_removed", object_node, "_object_removed");
emit_signal("object_created", this->get_name(), state->get_name(), get_global_position(), velocity);
}
else
{
WARN_PRINT("State not found!");
}
auto event = get_node<alai::Event>("/root/Event");
event->emit_signal("object_created", this->get_name(), state->get_name(), get_global_position(), velocity);
}
#ifndef NDEBUG
else
{
WARN_PRINT("Monitor not found!");
WARN_PRINT("State not found!");
}
#endif
}
void alai::player::Player::_physics_process(float delta)
@@ -176,21 +163,24 @@ void alai::player::Player::_physics_process(float delta)
{
if (!notifier->is_on_screen())
{
if (get_parent()->get_class() == "TileMap")
auto event = get_node<alai::Event>("/root/Event");
event->emit_signal("player_died");
/*if (get_parent()->get_class() == "TileMap")
{
auto error = get_tree()->change_scene("res://Main.tscn");
if (error != godot::Error::OK)
{
ERR_PRINT(godot::String().num((int) error) + " Could not load scene!");
}
}
}*/
}
}
auto state = get_node("StateMachine")->get_child(0);
if (state != nullptr)
{
emit_signal("object_updated", this->get_name(), state->get_name(), get_global_position(), velocity);
auto event = get_node<alai::Event>("/root/Event");
event->emit_signal("object_updated", this->get_name(), state->get_name(), get_global_position(), velocity);
}
else
{