From ef773a1ec571f4e71054eacb461e763b5893eda2 Mon Sep 17 00:00:00 2001 From: Henry Date: Tue, 9 Dec 2025 12:00:26 +0000 Subject: [PATCH] Setup UI canvas, added shield and game start. --- enemy_bullet.gd | 1 + main.gd | 15 ++++++++ main.tscn | 52 ++++++++++++++++++--------- player.gd | 20 ++++++++++- player.tscn | 1 + score_counter.gd | 20 +++++++++++ score_counter.gd.uid | 1 + score_counter.tscn | 83 ++++++++++++++++++++++++++++++++++++++++++++ ui.gd | 12 +++++++ ui.gd.uid | 1 + ui.tscn | 34 ++++++++++++++++++ 11 files changed, 223 insertions(+), 17 deletions(-) create mode 100644 score_counter.gd create mode 100644 score_counter.gd.uid create mode 100644 score_counter.tscn create mode 100644 ui.gd create mode 100644 ui.gd.uid create mode 100644 ui.tscn diff --git a/enemy_bullet.gd b/enemy_bullet.gd index 0fe440e..b9c9546 100644 --- a/enemy_bullet.gd +++ b/enemy_bullet.gd @@ -14,3 +14,4 @@ func _on_visible_on_screen_notifier_2d_screen_exited(): func _on_area_entered(area): if area.name == "Player": queue_free() + area.shield -= 1 diff --git a/main.gd b/main.gd index 7e50620..3952496 100644 --- a/main.gd +++ b/main.gd @@ -1,9 +1,18 @@ extends Node2D +@onready var start_button = $CanvasLayer/CenterContainer/Start + var enemy = preload("res://enemy.tscn") var score = 0 func _ready(): + start_button.show() +# spawn_enemies() + +func new_game(): + score = 0 + $CanvasLayer/UI.update_score(score) + $Player.start() spawn_enemies() func spawn_enemies(): @@ -17,3 +26,9 @@ func spawn_enemies(): func _on_enemy_died(value): score += value + $CanvasLayer/UI.update_score(score) + + +func _on_start_pressed(): + start_button.hide() + new_game() diff --git a/main.tscn b/main.tscn index 6ed744d..adab708 100644 --- a/main.tscn +++ b/main.tscn @@ -1,8 +1,25 @@ -[gd_scene load_steps=7 format=3 uid="uid://cc2dnhuv4qx7m"] +[gd_scene load_steps=9 format=3 uid="uid://cc2dnhuv4qx7m"] [ext_resource type="Script" uid="uid://c51huloycn5as" path="res://main.gd" id="1_h2yge"] [ext_resource type="Texture2D" uid="uid://jj8b7vqj3ihx" path="res://Mini Pixel Pack 3/Space_BG (2 frames) (64 x 64).png" id="1_ig7tw"] [ext_resource type="PackedScene" uid="uid://pyuorpwb7lpe" path="res://player.tscn" id="2_0xm2m"] +[ext_resource type="PackedScene" uid="uid://s6wf3egdqtmh" path="res://ui.tscn" id="4_1bvp3"] +[ext_resource type="Texture2D" uid="uid://bonoqs5pisflo" path="res://Mini Pixel Pack 3/UI objects/START (48 x 8).png" id="5_lquwl"] + +[sub_resource type="Animation" id="Animation_1bvp3"] +length = 0.001 +tracks/0/type = "value" +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/path = NodePath("background:region_rect") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/keys = { +"times": PackedFloat32Array(0), +"transitions": PackedFloat32Array(1), +"update": 0, +"values": [Rect2(0, 0, 240, 320)] +} [sub_resource type="Animation" id="Animation_h2yge"] resource_name = "scroll" @@ -21,21 +38,6 @@ tracks/0/keys = { "values": [Rect2(0, 0, 240, 320), Rect2(0, -64, 240, 320)] } -[sub_resource type="Animation" id="Animation_1bvp3"] -length = 0.001 -tracks/0/type = "value" -tracks/0/imported = false -tracks/0/enabled = true -tracks/0/path = NodePath("background:region_rect") -tracks/0/interp = 1 -tracks/0/loop_wrap = true -tracks/0/keys = { -"times": PackedFloat32Array(0), -"transitions": PackedFloat32Array(1), -"update": 0, -"values": [Rect2(0, 0, 240, 320)] -} - [sub_resource type="AnimationLibrary" id="AnimationLibrary_lquwl"] _data = { &"RESET": SubResource("Animation_1bvp3"), @@ -60,3 +62,21 @@ libraries = { &"": SubResource("AnimationLibrary_lquwl") } autoplay = "scroll" + +[node name="CanvasLayer" type="CanvasLayer" parent="."] + +[node name="UI" parent="CanvasLayer" instance=ExtResource("4_1bvp3")] + +[node name="CenterContainer" type="CenterContainer" parent="CanvasLayer"] +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="Start" type="TextureButton" parent="CanvasLayer/CenterContainer"] +layout_mode = 2 +texture_normal = ExtResource("5_lquwl") + +[connection signal="shield_changed" from="Player" to="CanvasLayer/UI" method="update_shield"] +[connection signal="pressed" from="CanvasLayer/CenterContainer/Start" to="." method="_on_start_pressed"] diff --git a/player.gd b/player.gd index 6a49e43..c10e038 100644 --- a/player.gd +++ b/player.gd @@ -1,6 +1,12 @@ extends Area2D +signal died +signal shield_changed + @onready var screensize = get_viewport_rect().size +@export var max_shield = 10 +var shield = max_shield: + set = set_shield @export var speed = 150 @export var cooldown = 0.25 @export var bullet_scene : PackedScene @@ -10,9 +16,17 @@ func _ready(): start() func start(): + shield = max_shield position = Vector2(screensize.x / 2, screensize.y - 64) $GunCooldown.wait_time = cooldown +func set_shield(value): + shield = min(max_shield, value) + shield_changed.emit(max_shield, shield) + if shield <= 0: + hide() + died.emit() + func shoot(): if not can_shoot: return @@ -41,6 +55,10 @@ func _process(delta): func _on_gun_cooldown_timeout(): can_shoot = true - func _on_gun_cool_down_timeout() -> void: can_shoot = true + +func _on_area_entered(area): + if area.is_in_group("enemies"): + area.explode() + shield -= max_shield / 2 diff --git a/player.tscn b/player.tscn index 0616c6d..259f3a9 100644 --- a/player.tscn +++ b/player.tscn @@ -91,4 +91,5 @@ shape = SubResource("RectangleShape2D_op7ga") [node name="GunCooldown" type="Timer" parent="."] one_shot = true +[connection signal="area_entered" from="." to="." method="_on_area_entered"] [connection signal="timeout" from="GunCooldown" to="." method="_on_gun_cool_down_timeout"] diff --git a/score_counter.gd b/score_counter.gd new file mode 100644 index 0000000..ac10812 --- /dev/null +++ b/score_counter.gd @@ -0,0 +1,20 @@ +extends HBoxContainer + +var digit_coords = { + 1: Vector2(0, 0), + 2: Vector2(8, 0), + 3: Vector2(16, 0), + 4: Vector2(24, 0), + 5: Vector2(32, 0), + 6: Vector2(0, 8), + 7: Vector2(8, 8), + 8: Vector2(16, 8), + 9: Vector2(24, 8), + 0: Vector2(32, 8) +} + +func display_digits(n): + var s = "%08d" % n + for i in 8: + get_child(i).texture.region = Rect2(digit_coords[int(s[i])], + Vector2(8, 8)) diff --git a/score_counter.gd.uid b/score_counter.gd.uid new file mode 100644 index 0000000..7ef1bad --- /dev/null +++ b/score_counter.gd.uid @@ -0,0 +1 @@ +uid://val4c82n4krk diff --git a/score_counter.tscn b/score_counter.tscn new file mode 100644 index 0000000..f78f2f3 --- /dev/null +++ b/score_counter.tscn @@ -0,0 +1,83 @@ +[gd_scene load_steps=11 format=3 uid="uid://5qkcf1cpre32"] + +[ext_resource type="Script" uid="uid://val4c82n4krk" path="res://score_counter.gd" id="1_t7i3n"] +[ext_resource type="Texture2D" uid="uid://ddh7mk2ekhq3u" path="res://Mini Pixel Pack 3/UI objects/Number_font (8 x 8).png" id="1_terno"] + +[sub_resource type="AtlasTexture" id="AtlasTexture_t7i3n"] +atlas = ExtResource("1_terno") +region = Rect2(32, 8, 8, 8) + +[sub_resource type="AtlasTexture" id="AtlasTexture_knege"] +atlas = ExtResource("1_terno") +region = Rect2(32, 8, 8, 8) + +[sub_resource type="AtlasTexture" id="AtlasTexture_qa8aw"] +atlas = ExtResource("1_terno") +region = Rect2(32, 8, 8, 8) + +[sub_resource type="AtlasTexture" id="AtlasTexture_pd82r"] +atlas = ExtResource("1_terno") +region = Rect2(32, 8, 8, 8) + +[sub_resource type="AtlasTexture" id="AtlasTexture_j44f5"] +atlas = ExtResource("1_terno") +region = Rect2(32, 8, 8, 8) + +[sub_resource type="AtlasTexture" id="AtlasTexture_emiqc"] +atlas = ExtResource("1_terno") +region = Rect2(32, 8, 8, 8) + +[sub_resource type="AtlasTexture" id="AtlasTexture_inuy1"] +atlas = ExtResource("1_terno") +region = Rect2(32, 8, 8, 8) + +[sub_resource type="AtlasTexture" id="AtlasTexture_ke503"] +atlas = ExtResource("1_terno") +region = Rect2(32, 8, 8, 8) + +[node name="ScoreCounter" type="HBoxContainer"] +anchors_preset = 10 +anchor_right = 1.0 +grow_horizontal = 2 +size_flags_horizontal = 8 +script = ExtResource("1_t7i3n") + +[node name="Digit0" type="TextureRect" parent="."] +layout_mode = 2 +texture = SubResource("AtlasTexture_t7i3n") +stretch_mode = 5 + +[node name="Digit1" type="TextureRect" parent="."] +layout_mode = 2 +texture = SubResource("AtlasTexture_knege") +stretch_mode = 5 + +[node name="Digit2" type="TextureRect" parent="."] +layout_mode = 2 +texture = SubResource("AtlasTexture_qa8aw") +stretch_mode = 5 + +[node name="Digit3" type="TextureRect" parent="."] +layout_mode = 2 +texture = SubResource("AtlasTexture_pd82r") +stretch_mode = 5 + +[node name="Digit4" type="TextureRect" parent="."] +layout_mode = 2 +texture = SubResource("AtlasTexture_j44f5") +stretch_mode = 5 + +[node name="Digit5" type="TextureRect" parent="."] +layout_mode = 2 +texture = SubResource("AtlasTexture_emiqc") +stretch_mode = 5 + +[node name="Digit6" type="TextureRect" parent="."] +layout_mode = 2 +texture = SubResource("AtlasTexture_inuy1") +stretch_mode = 5 + +[node name="Digit7" type="TextureRect" parent="."] +layout_mode = 2 +texture = SubResource("AtlasTexture_ke503") +stretch_mode = 5 diff --git a/ui.gd b/ui.gd new file mode 100644 index 0000000..2d683c9 --- /dev/null +++ b/ui.gd @@ -0,0 +1,12 @@ +extends MarginContainer + +@onready var shield_bar = $HBoxContainer/ShieldBar +@onready var score_counter = $HBoxContainer/ScoreCounter + +func update_score(value): + score_counter.display_digits(value) + + +func update_shield(max_value, value): + shield_bar.max_value = max_value + shield_bar.value = value diff --git a/ui.gd.uid b/ui.gd.uid new file mode 100644 index 0000000..faaf54c --- /dev/null +++ b/ui.gd.uid @@ -0,0 +1 @@ +uid://b544c65halgk4 diff --git a/ui.tscn b/ui.tscn new file mode 100644 index 0000000..b0737fb --- /dev/null +++ b/ui.tscn @@ -0,0 +1,34 @@ +[gd_scene load_steps=5 format=3 uid="uid://s6wf3egdqtmh"] + +[ext_resource type="Texture2D" uid="uid://d11molrkdjjh5" path="res://bar_background.png" id="1_m6e0p"] +[ext_resource type="Script" uid="uid://b544c65halgk4" path="res://ui.gd" id="1_nltto"] +[ext_resource type="Texture2D" uid="uid://bsl3pxvxiuoqg" path="res://bar_foreground.png" id="2_27fn8"] +[ext_resource type="PackedScene" uid="uid://5qkcf1cpre32" path="res://score_counter.tscn" id="4_ibotj"] + +[node name="UI" type="MarginContainer"] +anchors_preset = 10 +anchor_right = 1.0 +offset_bottom = 20.0 +grow_horizontal = 2 +theme_override_constants/margin_left = 10 +theme_override_constants/margin_top = 10 +theme_override_constants/margin_right = 10 +theme_override_constants/margin_bottom = 10 +script = ExtResource("1_nltto") + +[node name="HBoxContainer" type="HBoxContainer" parent="."] +layout_mode = 2 + +[node name="ShieldBar" type="TextureProgressBar" parent="HBoxContainer"] +custom_minimum_size = Vector2(80, 16) +layout_mode = 2 +nine_patch_stretch = true +stretch_margin_left = 3 +stretch_margin_top = 3 +stretch_margin_right = 3 +stretch_margin_bottom = 3 +texture_under = ExtResource("1_m6e0p") +texture_progress = ExtResource("2_27fn8") + +[node name="ScoreCounter" parent="HBoxContainer" instance=ExtResource("4_ibotj")] +layout_mode = 2