mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-07-02 05:11:15 +00:00
Health in HUD
This commit is contained in:
parent
60f2797541
commit
4b4cbc037a
5 changed files with 45 additions and 10 deletions
|
|
@ -6,18 +6,21 @@
|
||||||
[node name="HUD" type="CanvasLayer"]
|
[node name="HUD" type="CanvasLayer"]
|
||||||
script = ExtResource("1_m0hb0")
|
script = ExtResource("1_m0hb0")
|
||||||
|
|
||||||
[node name="Health" type="Label" parent="."]
|
[node name="HealthLabel" type="Label" parent="."]
|
||||||
anchors_preset = 2
|
anchors_preset = 2
|
||||||
anchor_top = 1.0
|
anchor_top = 1.0
|
||||||
anchor_bottom = 1.0
|
anchor_bottom = 1.0
|
||||||
offset_top = -78.0
|
offset_left = 1.0
|
||||||
offset_right = 305.0
|
offset_top = -160.0
|
||||||
|
offset_right = 306.0
|
||||||
|
offset_bottom = -82.0
|
||||||
grow_vertical = 0
|
grow_vertical = 0
|
||||||
theme_override_fonts/font = ExtResource("2_0xmx2")
|
theme_override_fonts/font = ExtResource("2_0xmx2")
|
||||||
theme_override_font_sizes/font_size = 64
|
theme_override_font_sizes/font_size = 12
|
||||||
text = "100/100"
|
text = "100/100"
|
||||||
|
|
||||||
[node name="Message" type="Label" parent="."]
|
[node name="GameOver" type="Label" parent="."]
|
||||||
|
visible = false
|
||||||
anchors_preset = 8
|
anchors_preset = 8
|
||||||
anchor_left = 0.5
|
anchor_left = 0.5
|
||||||
anchor_top = 0.5
|
anchor_top = 0.5
|
||||||
|
|
|
||||||
|
|
@ -424,7 +424,6 @@ Target = NodePath("../Factory Tilemaps/HorizontalDoor")
|
||||||
RequiresKeycard = false
|
RequiresKeycard = false
|
||||||
|
|
||||||
[node name="HUD" parent="." instance=ExtResource("22_krk0o")]
|
[node name="HUD" parent="." instance=ExtResource("22_krk0o")]
|
||||||
visible = false
|
|
||||||
|
|
||||||
[node name="CameraTarget" type="Node2D" parent="."]
|
[node name="CameraTarget" type="Node2D" parent="."]
|
||||||
position = Vector2(-791, -153)
|
position = Vector2(-791, -153)
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,11 @@ public partial class GameManager : Node2D
|
||||||
{
|
{
|
||||||
SpawnPlayer();
|
SpawnPlayer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_player.HealthChanged += (newHealth, maxHealth) => _hud.UpdateHealth(newHealth, maxHealth);
|
||||||
|
|
||||||
|
//_player.Connect(nameof(_player.HealthChanged), _hud, nameof(_hud.UpdateHealth));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,15 @@ public partial class Hud : CanvasLayer
|
||||||
{
|
{
|
||||||
[Signal]
|
[Signal]
|
||||||
public delegate void StartGameEventHandler();
|
public delegate void StartGameEventHandler();
|
||||||
|
|
||||||
|
private Label _healthLabel;
|
||||||
|
|
||||||
|
public override void _Ready()
|
||||||
|
{
|
||||||
|
// Assuming the HUD has a Label node named "HealthLabel"
|
||||||
|
_healthLabel = GetNode<Label>("HealthLabel");
|
||||||
|
}
|
||||||
|
|
||||||
public void ShowMessage(string text)
|
public void ShowMessage(string text)
|
||||||
{
|
{
|
||||||
var message = GetNode<Label>("Message");
|
var message = GetNode<Label>("Message");
|
||||||
|
|
@ -29,4 +37,9 @@ public partial class Hud : CanvasLayer
|
||||||
await ToSignal(GetTree().CreateTimer(1.0), SceneTreeTimer.SignalName.Timeout);
|
await ToSignal(GetTree().CreateTimer(1.0), SceneTreeTimer.SignalName.Timeout);
|
||||||
GetNode<Button>("StartButton").Show();
|
GetNode<Button>("StartButton").Show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void UpdateHealth(float newHealth, float maxHealth)
|
||||||
|
{
|
||||||
|
_healthLabel.Text = $"{newHealth}/{maxHealth}";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
|
||||||
|
|
||||||
private Sprite2D _crosshair;
|
private Sprite2D _crosshair;
|
||||||
|
|
||||||
[Export] public float Health = 4f;
|
[Export] public float MaxHealth = 32f;
|
||||||
|
|
||||||
[Export] public Weapon EquippedWeapon;
|
[Export] public Weapon EquippedWeapon;
|
||||||
|
|
||||||
|
|
@ -46,11 +46,27 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
|
||||||
|
|
||||||
private bool _isStrafing { get; set; }
|
private bool _isStrafing { get; set; }
|
||||||
|
|
||||||
|
[Signal]
|
||||||
|
public delegate void HealthChangedEventHandler(float newHealth, float MaxHealth);
|
||||||
|
|
||||||
|
public float CurrentHealth
|
||||||
|
{
|
||||||
|
get => _currentHealth;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (_currentHealth != value)
|
||||||
|
{
|
||||||
|
_currentHealth = value;
|
||||||
|
EmitSignal(nameof(HealthChanged), _currentHealth, MaxHealth);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//private InventoryManager _inventoryManager;
|
//private InventoryManager _inventoryManager;
|
||||||
|
|
||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
_currentHealth = Health;
|
CurrentHealth = MaxHealth;
|
||||||
|
|
||||||
_animatedSprite = GetNode<AnimatedSprite2D>("./Smoothing2D/AnimatedSprite2D");
|
_animatedSprite = GetNode<AnimatedSprite2D>("./Smoothing2D/AnimatedSprite2D");
|
||||||
_crosshair = GetNode<Sprite2D>("./Smoothing2D/Crosshair");
|
_crosshair = GetNode<Sprite2D>("./Smoothing2D/Crosshair");
|
||||||
|
|
@ -245,8 +261,8 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
|
||||||
GD.Print($"Player damaged for {damage}");
|
GD.Print($"Player damaged for {damage}");
|
||||||
if (_isDestroyed) return;
|
if (_isDestroyed) return;
|
||||||
|
|
||||||
_currentHealth -= damage;
|
CurrentHealth -= damage;
|
||||||
if (!(_currentHealth <= 0)) return;
|
if (!(CurrentHealth <= 0)) return;
|
||||||
_isDestroyed = true;
|
_isDestroyed = true;
|
||||||
Explode();
|
Explode();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue