Added two states, collected and not collected
This commit is contained in:
49
src/coin/CoinCollected.cpp
Normal file
49
src/coin/CoinCollected.cpp
Normal 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
75
src/coin/CoinCollected.h
Normal 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
|
54
src/coin/CoinNotCollected.cpp
Normal file
54
src/coin/CoinNotCollected.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
#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");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
76
src/coin/CoinNotCollected.h
Normal file
76
src/coin/CoinNotCollected.h
Normal 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
|
Reference in New Issue
Block a user