From b9ff12dab300bb48909b01989da6d36368cf4f49 Mon Sep 17 00:00:00 2001 From: Martin Araneda Date: Sat, 16 Jul 2022 18:36:38 -0400 Subject: [PATCH] Finished coin counter --- godot/Collectables/Coin/Coin.tscn | 35 ------------------ godot/hud/coin/Counter.gdns | 8 +++++ godot/levels/Prototype.tscn | 59 +++++++++++++++++++++---------- src/coin/CoinCollected.cpp | 5 +-- src/coin/CoinCounter.cpp | 40 +++++++++++---------- src/coin/CoinCounter.h | 32 +++++------------ src/godot.cpp | 2 ++ 7 files changed, 84 insertions(+), 97 deletions(-) delete mode 100644 godot/Collectables/Coin/Coin.tscn create mode 100644 godot/hud/coin/Counter.gdns diff --git a/godot/Collectables/Coin/Coin.tscn b/godot/Collectables/Coin/Coin.tscn deleted file mode 100644 index d496232..0000000 --- a/godot/Collectables/Coin/Coin.tscn +++ /dev/null @@ -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 diff --git a/godot/hud/coin/Counter.gdns b/godot/hud/coin/Counter.gdns new file mode 100644 index 0000000..622052d --- /dev/null +++ b/godot/hud/coin/Counter.gdns @@ -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 ) diff --git a/godot/levels/Prototype.tscn b/godot/levels/Prototype.tscn index b1f265c..e3c4608 100644 --- a/godot/levels/Prototype.tscn +++ b/godot/levels/Prototype.tscn @@ -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"] diff --git a/src/coin/CoinCollected.cpp b/src/coin/CoinCollected.cpp index 7f1f2f2..9f3a9a4 100644 --- a/src/coin/CoinCollected.cpp +++ b/src/coin/CoinCollected.cpp @@ -2,13 +2,13 @@ #include 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("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"); } diff --git a/src/coin/CoinCounter.cpp b/src/coin/CoinCounter.cpp index 365453b..22fc8e4 100644 --- a/src/coin/CoinCounter.cpp +++ b/src/coin/CoinCounter.cpp @@ -1,14 +1,12 @@ #include "coin/CoinCounter.h" -#include - +#include +#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"); + } + - +} diff --git a/src/coin/CoinCounter.h b/src/coin/CoinCounter.h index b194fb9..006aa93 100644 --- a/src/coin/CoinCounter.h +++ b/src/coin/CoinCounter.h @@ -1,28 +1,24 @@ #ifndef ALAI_COIN_COUNTER #define ALAI_COIN_COUNTER -#include "state_machine/State.h" #include #include -#include +#include 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(); }; diff --git a/src/godot.cpp b/src/godot.cpp index 2e5d9a6..620fb1d 100644 --- a/src/godot.cpp +++ b/src/godot.cpp @@ -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(); register_class(); register_class(); + register_class(); }