Added two states, collected and not collected
This commit is contained in:
parent
5fe9e3340a
commit
876206c39d
@ -12,7 +12,7 @@
|
||||
extents = Vector2( 7, 12 )
|
||||
|
||||
[node name="Player" type="KinematicBody2D"]
|
||||
collision_mask = 6
|
||||
collision_mask = 2
|
||||
script = ExtResource( 5 )
|
||||
|
||||
[node name="AnimatedSprite" type="AnimatedSprite" parent="."]
|
||||
|
71
godot/collectables/coin/Coin.tscn
Normal file
71
godot/collectables/coin/Coin.tscn
Normal file
@ -0,0 +1,71 @@
|
||||
[gd_scene load_steps=10 format=2]
|
||||
|
||||
[ext_resource path="res://assets/coin.png" type="Texture" id=1]
|
||||
[ext_resource path="res://state_machine/StateMachine.gdns" type="Script" id=2]
|
||||
[ext_resource path="res://collectables/coin/states/CoinNotCollected.gdns" type="Script" id=3]
|
||||
[ext_resource path="res://collectables/coin/states/CoinCollected.gdns" type="Script" id=4]
|
||||
|
||||
[sub_resource type="CircleShape2D" id=1]
|
||||
radius = 6.0
|
||||
|
||||
[sub_resource type="AtlasTexture" id=2]
|
||||
atlas = ExtResource( 1 )
|
||||
region = Rect2( 0, 0, 18, 18 )
|
||||
|
||||
[sub_resource type="AtlasTexture" id=3]
|
||||
atlas = ExtResource( 1 )
|
||||
region = Rect2( 18, 0, 18, 18 )
|
||||
|
||||
[sub_resource type="SpriteFrames" id=4]
|
||||
animations = [ {
|
||||
"frames": [ SubResource( 2 ), SubResource( 3 ) ],
|
||||
"loop": true,
|
||||
"name": "spin",
|
||||
"speed": 5.0
|
||||
} ]
|
||||
|
||||
[sub_resource type="Animation" id=5]
|
||||
resource_name = "jump"
|
||||
length = 0.4
|
||||
tracks/0/type = "value"
|
||||
tracks/0/path = NodePath("AnimatedSprite:position")
|
||||
tracks/0/interp = 1
|
||||
tracks/0/loop_wrap = true
|
||||
tracks/0/imported = false
|
||||
tracks/0/enabled = true
|
||||
tracks/0/keys = {
|
||||
"times": PoolRealArray( 0, 0.2, 0.4 ),
|
||||
"transitions": PoolRealArray( 1, 1, 1 ),
|
||||
"update": 0,
|
||||
"values": [ Vector2( 0, 0 ), Vector2( 0, -20 ), Vector2( 0, 0 ) ]
|
||||
}
|
||||
|
||||
[node name="Coin" type="Area2D"]
|
||||
collision_layer = 4
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||
position = Vector2( 9, 9 )
|
||||
shape = SubResource( 1 )
|
||||
|
||||
[node name="AnimatedSprite" type="AnimatedSprite" parent="."]
|
||||
frames = SubResource( 4 )
|
||||
animation = "spin"
|
||||
frame = 1
|
||||
playing = true
|
||||
centered = false
|
||||
|
||||
[node name="StateMachine" type="Node" parent="."]
|
||||
script = ExtResource( 2 )
|
||||
default_state = "CoinNotCollected"
|
||||
|
||||
[node name="CoinNotCollected" type="Node" parent="StateMachine"]
|
||||
script = ExtResource( 3 )
|
||||
|
||||
[node name="CoinCollected" type="Node" parent="StateMachine"]
|
||||
script = ExtResource( 4 )
|
||||
|
||||
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
|
||||
anims/jump = SubResource( 5 )
|
||||
|
||||
[connection signal="body_entered" from="." to="StateMachine/CoinNotCollected" method="_on_body_entered" flags=6]
|
||||
[connection signal="animation_finished" from="AnimationPlayer" to="StateMachine/CoinCollected" method="_on_animation_finished" flags=6]
|
8
godot/collectables/coin/states/CoinCollected.gdns
Normal file
8
godot/collectables/coin/states/CoinCollected.gdns
Normal file
@ -0,0 +1,8 @@
|
||||
[gd_resource type="NativeScript" load_steps=2 format=2]
|
||||
|
||||
[ext_resource path="res://gdnative/alai.tres" type="GDNativeLibrary" id=1]
|
||||
|
||||
[resource]
|
||||
resource_name = "CoinCollected"
|
||||
class_name = "CoinCollected"
|
||||
library = ExtResource( 1 )
|
8
godot/collectables/coin/states/CoinNotCollected.gdns
Normal file
8
godot/collectables/coin/states/CoinNotCollected.gdns
Normal file
@ -0,0 +1,8 @@
|
||||
[gd_resource type="NativeScript" load_steps=2 format=2]
|
||||
|
||||
[ext_resource path="res://gdnative/alai.tres" type="GDNativeLibrary" id=1]
|
||||
|
||||
[resource]
|
||||
resource_name = "CoinNotCollected"
|
||||
class_name = "CoinNotCollected"
|
||||
library = ExtResource( 1 )
|
@ -4,7 +4,7 @@
|
||||
[ext_resource path="res://characters/player/Player.tscn" type="PackedScene" id=2]
|
||||
[ext_resource path="res://levels/Prototype.tmx" type="PackedScene" id=3]
|
||||
[ext_resource path="res://assets/backgrounds/mountains.png" type="Texture" id=4]
|
||||
[ext_resource path="res://Collectables/Coin/Coin.tscn" type="PackedScene" id=5]
|
||||
[ext_resource path="res://collectables/coin/Coin.tscn" type="PackedScene" id=5]
|
||||
|
||||
[node name="Prototype" type="Node2D"]
|
||||
|
||||
|
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
|
@ -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");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -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);
|
||||
};
|
||||
|
||||
}
|
@ -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>();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user