From 3ca5f4e7b2e7e01d4c4b57dac5bd146644cd2e45 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Thu, 1 Sep 2022 18:34:37 -0400 Subject: [PATCH] improve coin counter HUD --- godot/hud/CoinHUD.tscn | 39 ++++++++++--------- .../coin/{Counter.gdns => CoinCounter.gdns} | 0 godot/levels/Prototype.tscn | 2 - src/coin/CoinCounter.cpp | 23 +++++++---- 4 files changed, 37 insertions(+), 27 deletions(-) rename godot/hud/coin/{Counter.gdns => CoinCounter.gdns} (100%) diff --git a/godot/hud/CoinHUD.tscn b/godot/hud/CoinHUD.tscn index 512a8cd..c14b61c 100644 --- a/godot/hud/CoinHUD.tscn +++ b/godot/hud/CoinHUD.tscn @@ -1,7 +1,7 @@ [gd_scene load_steps=8 format=2] [ext_resource path="res://assets/coin.png" type="Texture" id=1] -[ext_resource path="res://hud/coin/Counter.gdns" type="Script" id=2] +[ext_resource path="res://hud/coin/CoinCounter.gdns" type="Script" id=2] [ext_resource path="res://hud/CoinHUD.gd" type="Script" id=3] [sub_resource type="StyleBoxFlat" id=1] @@ -28,31 +28,34 @@ pause_mode = 2 script = ExtResource( 3 ) [node name="Panel" type="Panel" parent="."] -margin_left = 144.0 -margin_top = 18.0 -margin_right = 288.0 -margin_bottom = 54.0 +anchor_left = 0.5 +anchor_right = 0.5 +margin_left = -44.5 +margin_top = 5.0 +margin_right = 44.5 +margin_bottom = 25.0 custom_styles/panel = SubResource( 1 ) -[node name="Label" type="Label" parent="."] -margin_left = 180.0 -margin_top = 18.0 -margin_right = 220.0 -margin_bottom = 36.0 +[node name="X" type="Label" parent="Panel"] +margin_left = 40.0 +margin_top = 4.0 +margin_right = 53.0 +margin_bottom = 24.0 text = "X" -[node name="Coins" type="Label" parent="."] -margin_left = 198.0 -margin_top = 18.0 -margin_right = 238.0 -margin_bottom = 32.0 +[node name="Coins" type="Label" parent="Panel"] +anchor_left = 0.5 +anchor_right = 0.5 +margin_left = 13.5 +margin_top = 4.0 +margin_right = 36.0 +margin_bottom = 18.0 text = "##" script = ExtResource( 2 ) -[node name="AnimatedSprite" type="AnimatedSprite" parent="."] -position = Vector2( 162, 18 ) +[node name="AnimatedSprite" type="AnimatedSprite" parent="Panel"] +position = Vector2( 12, 1 ) frames = SubResource( 4 ) animation = "spin" -frame = 1 playing = true centered = false diff --git a/godot/hud/coin/Counter.gdns b/godot/hud/coin/CoinCounter.gdns similarity index 100% rename from godot/hud/coin/Counter.gdns rename to godot/hud/coin/CoinCounter.gdns diff --git a/godot/levels/Prototype.tscn b/godot/levels/Prototype.tscn index b71e0c7..2d3dce5 100644 --- a/godot/levels/Prototype.tscn +++ b/godot/levels/Prototype.tscn @@ -55,5 +55,3 @@ position = Vector2( 72, 450 ) position = Vector2( 234, 450 ) [node name="Goal" parent="." instance=ExtResource( 8 )] - -[editable path="Coins/coin"] diff --git a/src/coin/CoinCounter.cpp b/src/coin/CoinCounter.cpp index c0f5cbe..da39b39 100644 --- a/src/coin/CoinCounter.cpp +++ b/src/coin/CoinCounter.cpp @@ -23,15 +23,24 @@ void alai::CoinCounter::_init() coins = 0; } -void alai::CoinCounter::_on_coin_collected(int amount) -{ - coins = coins + amount; - set_text(godot::String::num(coins)); -} - void alai::CoinCounter::_ready() { - set_text("0"); + set_text("00"); auto event = get_node("/root/Event"); event->connect("coin_collected", this, "_on_coin_collected"); } + + +void alai::CoinCounter::_on_coin_collected(int amount) +{ + coins = coins + amount; + godot::String coin_string = godot::String(); + if (coins <= 9) + { + coin_string = "0" + godot::String::num(coins); + } + else{ + coin_string = godot::String::num(coins); + } + set_text(coin_string); +} -- 2.30.2