Finished coin counter

This commit is contained in:
Martin Araneda 2022-07-16 18:36:38 -04:00
parent b25e1f040c
commit b9ff12dab3
7 changed files with 84 additions and 97 deletions

View File

@ -1,35 +0,0 @@
[gd_scene load_steps=6 format=2]
[ext_resource path="res://assets/coin.png" type="Texture" id=1]
[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
} ]
[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"
playing = true
centered = false

View 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 = "CoinCounter"
class_name = "CoinCounter"
library = ExtResource( 1 )

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=8 format=2]
[gd_scene load_steps=12 format=2]
[ext_resource path="res://CameraLimit.gdns" type="Script" id=1]
[ext_resource path="res://characters/player/Player.tscn" type="PackedScene" id=2]
@ -6,10 +6,27 @@
[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://assets/coin.png" type="Texture" id=6]
[ext_resource path="res://hud/coin/Counter.gdns" type="Script" id=7]
[sub_resource type="StyleBoxFlat" id=1]
bg_color = Color( 0, 0, 0, 0.541176 )
[sub_resource type="AtlasTexture" id=2]
atlas = ExtResource( 6 )
region = Rect2( 0, 0, 18, 18 )
[sub_resource type="AtlasTexture" id=3]
atlas = ExtResource( 6 )
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
} ]
[node name="Prototype" type="Node2D"]
[node name="Player" parent="." instance=ExtResource( 2 )]
@ -46,12 +63,6 @@ centered = false
[node name="Prototype" parent="Map" instance=ExtResource( 3 )]
script = ExtResource( 1 )
[node name="coin" parent="." instance=ExtResource( 5 )]
position = Vector2( 72, 450 )
[node name="coin2" parent="." instance=ExtResource( 5 )]
position = Vector2( 234, 450 )
[node name="CoinHUD" type="CanvasLayer" parent="."]
[node name="Panel" type="Panel" parent="CoinHUD"]
@ -61,25 +72,35 @@ margin_right = 288.0
margin_bottom = 54.0
custom_styles/panel = SubResource( 1 )
[node name="TextureRect" type="TextureRect" parent="CoinHUD"]
margin_left = 144.0
margin_top = 18.0
margin_right = 184.0
margin_bottom = 58.0
texture = ExtResource( 6 )
[node name="Label" type="Label" parent="CoinHUD"]
margin_left = 198.0
margin_left = 180.0
margin_top = 18.0
margin_right = 238.0
margin_right = 220.0
margin_bottom = 36.0
text = "X"
[node name="Coins" type="Label" parent="CoinHUD"]
margin_left = 216.0
margin_left = 198.0
margin_top = 18.0
margin_right = 256.0
margin_right = 238.0
margin_bottom = 32.0
text = "##"
script = ExtResource( 7 )
[connection signal="ready" from="CoinHUD" to="Player" method="_on_CoinHUD_ready"]
[node name="AnimatedSprite" type="AnimatedSprite" parent="CoinHUD"]
position = Vector2( 162, 18 )
frames = SubResource( 4 )
animation = "spin"
frame = 1
playing = true
centered = false
[node name="Coins" type="Node" parent="."]
[node name="coin" parent="Coins" instance=ExtResource( 5 )]
position = Vector2( 72, 450 )
[node name="coin2" parent="Coins" instance=ExtResource( 5 )]
position = Vector2( 234, 450 )
[editable path="Coins/coin"]

View File

@ -2,13 +2,13 @@
#include <AnimationPlayer.hpp>
using namespace godot;
int coin = 0;
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);
register_signal<CoinCollected>("coin_collected", "amount", GODOT_VARIANT_TYPE_INT);
}
CoinCollected::CoinCollected()
@ -26,7 +26,6 @@ void CoinCollected::_init()
void CoinCollected::_state_enter()
{
coin = coin + 1;
auto node = get_parent()->find_node("AnimationPlayer");
if (node != nullptr)
@ -44,8 +43,10 @@ void CoinCollected::_state_exit()
void CoinCollected::_on_animation_finished(String anim_name)
{
emit_signal("coin_collected", 1);
this->get_parent()->queue_free();
// get_state_machine()->change("CoinCounter");
}

View File

@ -1,14 +1,12 @@
#include "coin/CoinCounter.h"
#include <Control.hpp>
#include <String.hpp>
#include "coin/CoinCollected.h"
using namespace godot;
void CoinCounter::_register_methods()
{
register_method("_state_enter", &CoinCounter::_state_enter);
register_method("_state_exit", &CoinCounter::_state_exit);
register_method("_on_body_entered", &CoinCounter::_on_CoinHUD_ready);
register_method("_on_coin_collected", &CoinCounter::_on_coin_collected);
register_method("_ready", &CoinCounter::_ready);
}
CoinCounter::CoinCounter()
@ -24,20 +22,26 @@ void CoinCounter::_init()
}
void CoinCounter::_state_enter()
{
}
void CoinCounter::_state_exit()
{
}
void CoinCounter::_on_CoinHUD_ready()
{
get_node("Coins").text() = "hola";
}
void CoinCounter::_on_coin_collected(int amount)
{
coins = coins + amount;
set_text(String::num(coins));
}
void CoinCounter::_ready()
{
set_text("0");
auto coins_node = get_node("../../Coins");
auto children_count = coins_node->get_child_count();
for(int64_t i = 0; i < children_count;i++)
{
auto child = coins_node->get_child(i);
child->get_node("StateMachine/CoinCollected")->connect("coin_collected",this,"_on_coin_collected");
}
}

View File

@ -1,28 +1,24 @@
#ifndef ALAI_COIN_COUNTER
#define ALAI_COIN_COUNTER
#include "state_machine/State.h"
#include <Godot.hpp>
#include <Node.hpp>
#include <AnimatedSprite.hpp>
#include <Label.hpp>
namespace godot
{
/**
* @brief This class controls what happens when the Coin is in the collected state.
* @brief This class controls what happens when the Coin is in the collected .
*
*/
class CoinCounter : public State
class CoinCounter : public Label
{
GODOT_CLASS(CoinCounter, State)
GODOT_CLASS(CoinCounter, Label)
private:
/**
* @brief The animated sprite of the Coin.
*
*/
AnimatedSprite *animated_sprite;
int coins = 0;
public:
/**
@ -52,23 +48,13 @@ namespace godot
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.
* @brief Called when the collected of the coin is entered.
*
*/
void _on_CoinHUD_ready();
void _on_coin_collected(int amount);
void _ready();
};

View File

@ -11,6 +11,7 @@
#include "player/states/PlayerFall.h"
#include "coin/CoinNotCollected.h"
#include "coin/CoinCollected.h"
#include "coin/CoinCounter.h"
using namespace godot;
@ -53,4 +54,5 @@ extern "C" void GDN_EXPORT godot_nativescript_init(void *handle)
register_class<player::PlayerFall>();
register_class<CoinNotCollected>();
register_class<CoinCollected>();
register_class<CoinCounter>();
}