Setup UI canvas, added shield and game start.

This commit is contained in:
Henry 2025-12-09 12:00:26 +00:00
parent e582ed7ec3
commit ef773a1ec5
11 changed files with 223 additions and 17 deletions

View file

@ -14,3 +14,4 @@ func _on_visible_on_screen_notifier_2d_screen_exited():
func _on_area_entered(area): func _on_area_entered(area):
if area.name == "Player": if area.name == "Player":
queue_free() queue_free()
area.shield -= 1

15
main.gd
View file

@ -1,9 +1,18 @@
extends Node2D extends Node2D
@onready var start_button = $CanvasLayer/CenterContainer/Start
var enemy = preload("res://enemy.tscn") var enemy = preload("res://enemy.tscn")
var score = 0 var score = 0
func _ready(): func _ready():
start_button.show()
# spawn_enemies()
func new_game():
score = 0
$CanvasLayer/UI.update_score(score)
$Player.start()
spawn_enemies() spawn_enemies()
func spawn_enemies(): func spawn_enemies():
@ -17,3 +26,9 @@ func spawn_enemies():
func _on_enemy_died(value): func _on_enemy_died(value):
score += value score += value
$CanvasLayer/UI.update_score(score)
func _on_start_pressed():
start_button.hide()
new_game()

View file

@ -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="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="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://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"] [sub_resource type="Animation" id="Animation_h2yge"]
resource_name = "scroll" resource_name = "scroll"
@ -21,21 +38,6 @@ tracks/0/keys = {
"values": [Rect2(0, 0, 240, 320), Rect2(0, -64, 240, 320)] "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"] [sub_resource type="AnimationLibrary" id="AnimationLibrary_lquwl"]
_data = { _data = {
&"RESET": SubResource("Animation_1bvp3"), &"RESET": SubResource("Animation_1bvp3"),
@ -60,3 +62,21 @@ libraries = {
&"": SubResource("AnimationLibrary_lquwl") &"": SubResource("AnimationLibrary_lquwl")
} }
autoplay = "scroll" 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"]

View file

@ -1,6 +1,12 @@
extends Area2D extends Area2D
signal died
signal shield_changed
@onready var screensize = get_viewport_rect().size @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 speed = 150
@export var cooldown = 0.25 @export var cooldown = 0.25
@export var bullet_scene : PackedScene @export var bullet_scene : PackedScene
@ -10,9 +16,17 @@ func _ready():
start() start()
func start(): func start():
shield = max_shield
position = Vector2(screensize.x / 2, screensize.y - 64) position = Vector2(screensize.x / 2, screensize.y - 64)
$GunCooldown.wait_time = cooldown $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(): func shoot():
if not can_shoot: if not can_shoot:
return return
@ -41,6 +55,10 @@ func _process(delta):
func _on_gun_cooldown_timeout(): func _on_gun_cooldown_timeout():
can_shoot = true can_shoot = true
func _on_gun_cool_down_timeout() -> void: func _on_gun_cool_down_timeout() -> void:
can_shoot = true can_shoot = true
func _on_area_entered(area):
if area.is_in_group("enemies"):
area.explode()
shield -= max_shield / 2

View file

@ -91,4 +91,5 @@ shape = SubResource("RectangleShape2D_op7ga")
[node name="GunCooldown" type="Timer" parent="."] [node name="GunCooldown" type="Timer" parent="."]
one_shot = true 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"] [connection signal="timeout" from="GunCooldown" to="." method="_on_gun_cool_down_timeout"]

20
score_counter.gd Normal file
View file

@ -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))

1
score_counter.gd.uid Normal file
View file

@ -0,0 +1 @@
uid://val4c82n4krk

83
score_counter.tscn Normal file
View file

@ -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

12
ui.gd Normal file
View file

@ -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

1
ui.gd.uid Normal file
View file

@ -0,0 +1 @@
uid://b544c65halgk4

34
ui.tscn Normal file
View file

@ -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