Added two states, collected and not collected

This commit is contained in:
2022-06-11 00:26:11 -04:00
parent 5fe9e3340a
commit 876206c39d
12 changed files with 238 additions and 12 deletions

View File

@@ -0,0 +1,49 @@
#include "coin/CoinCollected.h"
#include <AnimationPlayer.hpp>
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)
{
this->get_parent()->queue_free();
}

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

View File

@@ -1,4 +1,5 @@
#include "Coin/CoinNotCollected.h"
#include "coin/CoinNotCollected.h"
#include <Area2D.hpp>
using namespace godot;
@@ -6,7 +7,7 @@ void CoinNotCollected::_register_methods()
{
register_method("_state_enter", &CoinNotCollected::_state_enter);
register_method("_state_exit", &CoinNotCollected::_state_exit);
register_method("_physics_process", &CoinNotCollected::_physics_process);
register_method("_on_body_entered", &CoinNotCollected::_on_body_entered);
}
CoinNotCollected::CoinNotCollected()
@@ -34,8 +35,20 @@ void CoinNotCollected::_state_exit()
}
void CoinNotCollected::_physics_process(float delta)
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

@@ -1,5 +1,5 @@
#ifndef JUEGO_COIN_COINNOTCOLLECTED
#define JUEGO_COIN_COINNOTCOLLECTED
#ifndef ALAI_COIN_NOT_COLLECTED
#define ALAI_COIN_NOT_COLLECTED
#include "state_machine/State.h"
@@ -64,11 +64,11 @@ namespace godot
void _state_exit();
/**
* @brief The physics processed every delta time.
* @brief Method called on body entered.
*
* @param[in] delta The time since the method was last run.
* @param[in] node Node interacting with whoever
*/
void _physics_process(float delta);
void _on_body_entered(Node *node);
};
}

View File

@@ -9,7 +9,8 @@
#include "player/states/PlayerMove.h"
#include "player/states/PlayerJump.h"
#include "player/states/PlayerFall.h"
#include "Coin/CoinNotCollected.h"
#include "coin/CoinNotCollected.h"
#include "coin/CoinCollected.h"
using namespace godot;
@@ -50,5 +51,6 @@ extern "C" void GDN_EXPORT godot_nativescript_init(void *handle)
register_class<player::PlayerMove>();
register_class<player::PlayerJump>();
register_class<player::PlayerFall>();
register_class<coin::CoinNotCollected>();
register_class<CoinNotCollected>();
register_class<CoinCollected>();
}