Explorar el Código

:truck: Move counter to a dedicated scene

Prevent duplication of counter logic.
DricomDragon hace 3 años
padre
commit
5ffe95fedf

+ 0 - 17
godot/component/ui/BallCounterLabel.gd

@@ -1,17 +0,0 @@
-extends Label
-
-
-var nb_bullet = 0
-
-
-func _ready():
-	gain_new_bullet()
-
-
-func gain_new_bullet():
-	nb_bullet += 1
-	text = str(nb_bullet)
-
-
-func _on_Launcher_gain_new_bullet():
-	gain_new_bullet()

+ 0 - 13
godot/component/ui/ScoreCounterLabel.gd

@@ -1,13 +0,0 @@
-extends Label
-
-
-var total_score = 0
-
-
-func score_point():
-	total_score += 1
-	text = str(total_score)
-
-
-func _on_Grid_kace_damaged():
-	score_point()

+ 19 - 0
godot/component/ui/number_panel/NumberPanel.gd

@@ -0,0 +1,19 @@
+class_name NumberPanel
+extends Panel
+
+
+export(String) var title = "Counter"
+
+var counter = 0
+
+onready var label = $Label
+onready var counter_label = $CounterLabel
+
+
+func _ready():
+	label.text = title
+
+
+func increase_counter():
+	counter += 1
+	counter_label.text = str(counter)

+ 32 - 0
godot/component/ui/number_panel/NumberPanel.tscn

@@ -0,0 +1,32 @@
+[gd_scene load_steps=2 format=2]
+
+[ext_resource path="res://component/ui/number_panel/NumberPanel.gd" type="Script" id=1]
+
+[node name="Panel" type="Panel"]
+margin_right = 40.0
+margin_bottom = 30.0
+script = ExtResource( 1 )
+__meta__ = {
+"_edit_use_anchors_": false
+}
+
+[node name="Label" type="Label" parent="."]
+margin_right = 40.0
+margin_bottom = 14.0
+text = "Title"
+align = 1
+valign = 1
+__meta__ = {
+"_edit_use_anchors_": false
+}
+
+[node name="CounterLabel" type="Label" parent="."]
+margin_top = 15.0
+margin_right = 40.0
+margin_bottom = 30.0
+text = "0"
+align = 1
+valign = 1
+__meta__ = {
+"_edit_use_anchors_": false
+}

+ 15 - 63
godot/main.tscn

@@ -1,11 +1,10 @@
-[gd_scene load_steps=17 format=2]
+[gd_scene load_steps=16 format=2]
 
-[ext_resource path="res://component/ui/BallCounterLabel.gd" type="Script" id=1]
 [ext_resource path="res://component/entity/launcher/TargetLine.gd" type="Script" id=2]
 [ext_resource path="res://component/entity/catcher/Catcher.gd" type="Script" id=3]
 [ext_resource path="res://component/ui/LifeContainer.gd" type="Script" id=4]
 [ext_resource path="res://component/ui/UI.gd" type="Script" id=5]
-[ext_resource path="res://component/ui/ScoreCounterLabel.gd" type="Script" id=6]
+[ext_resource path="res://component/ui/number_panel/NumberPanel.tscn" type="PackedScene" id=6]
 [ext_resource path="res://component/entity/death/DeadLine.gd" type="Script" id=7]
 [ext_resource path="res://component/entity/launcher/Launcher.gd" type="Script" id=8]
 [ext_resource path="res://component/entity/brick/brick_tileset.tres" type="TileSet" id=9]
@@ -26,7 +25,7 @@ extents = Vector2( 300, 50 )
 [sub_resource type="RectangleShape2D" id=4]
 extents = Vector2( 200, 50 )
 
-[node name="Root" type="Node"]
+[node name="Game" type="Node"]
 
 [node name="Background" type="Panel" parent="."]
 margin_right = 400.0
@@ -151,78 +150,31 @@ text = "Click to shoot a ball !"
 align = 1
 valign = 1
 
-[node name="BallCounterPanel" type="Panel" parent="UI"]
-margin_left = 10.0
-margin_top = 360.0
-margin_right = 50.0
-margin_bottom = 390.0
-__meta__ = {
-"_edit_use_anchors_": false
-}
-
-[node name="BallLabel" type="Label" parent="UI/BallCounterPanel"]
-margin_right = 40.0
-margin_bottom = 14.0
-text = "Balls"
-align = 1
-valign = 1
-__meta__ = {
-"_edit_use_anchors_": false
-}
-
-[node name="BallCounterLabel" type="Label" parent="UI/BallCounterPanel"]
-margin_top = 15.0
-margin_right = 40.0
-margin_bottom = 30.0
-text = "0"
-align = 1
-valign = 1
-script = ExtResource( 1 )
-__meta__ = {
-"_edit_use_anchors_": false
-}
-
-[node name="ScorePanel" type="Panel" parent="UI"]
+[node name="ScoreCounter" parent="UI" instance=ExtResource( 6 )]
 margin_left = 350.0
 margin_top = 360.0
 margin_right = 390.0
 margin_bottom = 390.0
-__meta__ = {
-"_edit_use_anchors_": false
-}
+title = "Score"
 
-[node name="ScoreLabel" type="Label" parent="UI/ScorePanel"]
-margin_right = 40.0
-margin_bottom = 14.0
-text = "Score"
-align = 1
-valign = 1
-__meta__ = {
-"_edit_use_anchors_": false
-}
-
-[node name="ScoreCounterLabel" type="Label" parent="UI/ScorePanel"]
-margin_top = 15.0
-margin_right = 40.0
-margin_bottom = 30.0
-text = "0"
-align = 1
-valign = 1
-script = ExtResource( 6 )
-__meta__ = {
-"_edit_use_anchors_": false
-}
+[node name="BallCounter" parent="UI" instance=ExtResource( 6 )]
+margin_left = 10.0
+margin_top = 360.0
+margin_right = 50.0
+margin_bottom = 390.0
+title = "Balls"
+[connection signal="ready" from="." to="UI/BallCounter" method="increase_counter"]
 [connection signal="body_entered" from="Catcher" to="Catcher" method="_on_Catcher_body_entered"]
 [connection signal="game_over" from="Catcher" to="UI" method="_on_Catcher_game_over"]
-[connection signal="gain_new_bullet" from="Launcher" to="UI/BallCounterPanel/BallCounterLabel" method="_on_Launcher_gain_new_bullet"]
+[connection signal="gain_new_bullet" from="Launcher" to="UI/BallCounter" method="increase_counter"]
 [connection signal="get_firing" from="Launcher" to="Launcher" method="_on_Launcher_get_firing"]
 [connection signal="get_firing" from="Launcher" to="UI" method="_on_Launcher_get_firing"]
 [connection signal="is_full" from="Launcher" to="Launcher/Grid" method="_on_Launcher_is_full"]
 [connection signal="move_visor" from="Launcher" to="Launcher/TargetLine" method="_on_Launcher_move_visor"]
-[connection signal="has_moved" from="Launcher/Grid" to="Launcher/DeadLine" method="_on_Grid_has_moved"]
 [connection signal="has_moved" from="Launcher/Grid" to="Launcher" method="_on_Grid_has_moved"]
+[connection signal="has_moved" from="Launcher/Grid" to="Launcher/DeadLine" method="_on_Grid_has_moved"]
 [connection signal="kace_broken" from="Launcher/Grid" to="Launcher" method="_on_Grid_kace_broken"]
-[connection signal="kace_damaged" from="Launcher/Grid" to="UI/ScorePanel/ScoreCounterLabel" method="_on_Grid_kace_damaged"]
+[connection signal="kace_damaged" from="Launcher/Grid" to="UI/ScoreCounter" method="increase_counter"]
 [connection signal="tween_all_completed" from="Launcher/Grid/MoveDown" to="Launcher/Grid" method="_on_MoveDown_tween_all_completed"]
 [connection signal="timeout" from="Launcher/ShootDelay" to="Launcher" method="_on_ShootDelay_timeout"]
 [connection signal="game_lost" from="Launcher/DeadLine" to="Launcher" method="_on_DeadLine_game_lost"]

+ 7 - 1
godot/project.godot

@@ -18,10 +18,16 @@ _global_script_classes=[ {
 "class": "Catcher",
 "language": "GDScript",
 "path": "res://component/entity/catcher/Catcher.gd"
+}, {
+"base": "Panel",
+"class": "NumberPanel",
+"language": "GDScript",
+"path": "res://component/ui/number_panel/NumberPanel.gd"
 } ]
 _global_script_class_icons={
 "Ball": "",
-"Catcher": ""
+"Catcher": "",
+"NumberPanel": ""
 }
 
 [application]