diff --git a/assets/testlevel/star-hud-empty.png b/assets/testlevel/star-hud-empty.png new file mode 100644 index 0000000..bf71c5c Binary files /dev/null and b/assets/testlevel/star-hud-empty.png differ diff --git a/assets/testlevel/star-hud-filled.png b/assets/testlevel/star-hud-filled.png new file mode 100644 index 0000000..27f009f Binary files /dev/null and b/assets/testlevel/star-hud-filled.png differ diff --git a/scenes/tools/hud.tscn b/scenes/tools/hud.tscn index 77f0986..50de63c 100644 --- a/scenes/tools/hud.tscn +++ b/scenes/tools/hud.tscn @@ -1,9 +1,10 @@ -[gd_scene load_steps=5 format=3 uid="uid://cq501sdvos586"] +[gd_scene load_steps=6 format=3 uid="uid://cq501sdvos586"] [ext_resource type="FontFile" uid="uid://bos2geotawcoj" path="res://assets/tools/fonts/PixelOperator8.ttf" id="1_2if4o"] -[ext_resource type="Script" path="res://scripts/hud.gd" id="1_20cvk"] +[ext_resource type="Script" path="res://scripts/hud/hud.gd" id="1_20cvk"] [ext_resource type="Texture2D" uid="uid://cv53vu076cw21" path="res://assets/heart/heart-filled.png" id="3_424o5"] [ext_resource type="Texture2D" uid="uid://mdwbhwkar180" path="res://assets/cat/stuff/fish_icon.png" id="3_b0tdp"] +[ext_resource type="Texture2D" uid="uid://gusxf7vpp17l" path="res://assets/testlevel/star-hud-filled.png" id="5_4i32g"] [node name="hud" type="CanvasLayer"] script = ExtResource("1_20cvk") @@ -12,15 +13,24 @@ script = ExtResource("1_20cvk") anchors_preset = 2 anchor_top = 1.0 anchor_bottom = 1.0 -offset_left = 10.0 -offset_top = -74.0 -offset_right = 434.0 -offset_bottom = -10.0 +offset_left = 8.0 +offset_top = -72.0 +offset_right = 640.0 +offset_bottom = -8.0 grow_vertical = 0 theme_override_fonts/font = ExtResource("1_2if4o") theme_override_font_sizes/font_size = 64 text = "1/999 Cheese" +[node name="timer_label" type="Label" parent="."] +offset_left = 8.0 +offset_top = 16.0 +offset_right = 640.0 +offset_bottom = 80.0 +theme_override_fonts/font = ExtResource("1_2if4o") +theme_override_font_sizes/font_size = 64 +text = "00:00.00 / 00" + [node name="anchor_control" type="Control" parent="."] layout_mode = 3 anchors_preset = 3 @@ -55,3 +65,27 @@ texture = ExtResource("3_424o5") position = Vector2(288, 288) scale = Vector2(8, 8) texture = ExtResource("3_424o5") + +[node name="anchor_star" type="Control" parent="."] +layout_mode = 3 +anchors_preset = 1 +anchor_left = 1.0 +anchor_right = 1.0 +offset_left = -40.0 +offset_bottom = 40.0 +grow_horizontal = 0 + +[node name="star_1" type="Sprite2D" parent="anchor_star"] +position = Vector2(-136, 40) +scale = Vector2(8, 8) +texture = ExtResource("5_4i32g") + +[node name="star_2" type="Sprite2D" parent="anchor_star"] +position = Vector2(-72, 40) +scale = Vector2(8, 8) +texture = ExtResource("5_4i32g") + +[node name="star_3" type="Sprite2D" parent="anchor_star"] +position = Vector2(-8, 40) +scale = Vector2(8, 8) +texture = ExtResource("5_4i32g") diff --git a/scripts/game_manager.gd b/scripts/game_manager.gd index e374756..980a984 100644 --- a/scripts/game_manager.gd +++ b/scripts/game_manager.gd @@ -6,6 +6,7 @@ var time_taken: float = 0.0 var damage_taken: bool = false var stars_earned: int = 0 var carries_fish: bool = false +var removed_time_star: bool = false @onready var player: CharacterBody2D = %player @onready var death_menu: CanvasLayer = %death_menu @@ -22,7 +23,11 @@ func _ready() -> void: func _process(delta: float) -> void: time_taken += delta - #print(time_taken) + player.hud.update_timer(time_taken, level_time.star_time) + var star_time = level_time.star_time + if time_taken >= level_time.star_time and not removed_time_star: + removed_time_star = true # Make sure we calc stars only one time + calculate_stars() func update_initial_hud() -> void: if player: @@ -101,9 +106,13 @@ func calculate_stars() -> void: stars_earned += 1 if not damage_taken: # No damage taken stars_earned += 1 + + player.hud.update_stars(stars_earned) func _on_player_take_damage() -> void: - damage_taken = true + if not damage_taken: + damage_taken = true + calculate_stars() func _on_player_trigger_death_menu() -> void: death_menu.fade_in() diff --git a/scripts/hud.gd b/scripts/hud/hud.gd similarity index 52% rename from scripts/hud.gd rename to scripts/hud/hud.gd index c33b04d..f0a109f 100644 --- a/scripts/hud.gd +++ b/scripts/hud/hud.gd @@ -1,14 +1,46 @@ extends CanvasLayer @onready var score_label: Label = $score_label +@onready var timer_label: Label = $timer_label @onready var fish_icon: Sprite2D = $anchor_control/fish_icon @onready var heart_1: Sprite2D = $anchor_control/heart_1 @onready var heart_2: Sprite2D = $anchor_control/heart_2 @onready var heart_3: Sprite2D = $anchor_control/heart_3 +@onready var star_1: Sprite2D = $anchor_star/star_1 +@onready var star_2: Sprite2D = $anchor_star/star_2 +@onready var star_3: Sprite2D = $anchor_star/star_3 var heart_filled: Texture2D = preload("res://assets/heart/heart-filled.png") var heart_empty: Texture2D = preload("res://assets/heart/heart-empty.png") +var star_filled: Texture2D = preload("res://assets/testlevel/star-hud-filled.png") +var star_empty: Texture2D = preload("res://assets/testlevel/star-hud-empty.png") + +func update_timer(time_passed: float, level_star_time: int) -> void: + var minutes = int(time_passed / 60) + var seconds = int(time_passed) % 60 + var milliseconds = int((time_passed - int(time_passed)) * 100) + + # Format the text as MM:SS.MS / SS + timer_label.text = "%02d:%02d.%02d / %02d" % [minutes, seconds, milliseconds, level_star_time] + + pass + +func update_stars(stars_earned: int) -> void: + # Save all star sprites in an array + var stars = [star_1, star_2, star_3] + + # Loop through all stars and update sprites according to earned stars + for i in range(stars.size()): + if i < stars_earned: + stars[i].texture = star_filled + else: + # If the star was filled but is now being emptied, call bounce_sprite + if stars[i].texture != star_empty: + stars[i].texture = star_empty + bounce_sprite(stars[i]) + + func update_score(score, max_score) -> void: score_label.text = str(score) + "/" + str(max_score) + " Cheese" @@ -22,15 +54,15 @@ func update_health(new_health) -> void: hearts[i].texture = heart_filled # Bounce the heart when gaining health if i == new_health - 1: - bounce_heart(hearts[i]) + bounce_sprite(hearts[i]) elif i >= new_health: hearts[i].texture = heart_empty # Bounce the heart when losing health if i == new_health: - bounce_heart(hearts[i]) + bounce_sprite(hearts[i]) # Give a visual feedback on which heart is being removed -func bounce_heart(heart: Sprite2D) -> void: +func bounce_sprite(heart: Sprite2D) -> void: var tween = get_tree().create_tween() tween.tween_property(heart, "scale", Vector2(10.0, 10.0), 0.15).set_trans(Tween.TRANS_BOUNCE) tween.tween_property(heart, "scale", Vector2(8.0, 8.0), 0.15).set_trans(Tween.TRANS_BOUNCE) diff --git a/scripts/levels/level_6.gd b/scripts/levels/level_6.gd index d4932d3..c9abf62 100644 --- a/scripts/levels/level_6.gd +++ b/scripts/levels/level_6.gd @@ -4,4 +4,4 @@ var parameters: Dictionary @onready var level_time: Node = %level_time func _ready() -> void: - level_time.star_time = 80 + level_time.star_time = 20 diff --git a/scripts/tools/level_time.gd b/scripts/tools/level_time.gd index 09cd966..645578c 100644 --- a/scripts/tools/level_time.gd +++ b/scripts/tools/level_time.gd @@ -1,4 +1,4 @@ extends Node -# Time to get a star +# Time to get a star (will be overwritten in level node) @export var star_time: int = 60