mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-11 03:15:53 +00:00
Health in HUD
This commit is contained in:
parent
60f2797541
commit
4b4cbc037a
5 changed files with 45 additions and 10 deletions
|
|
@ -37,7 +37,11 @@ public partial class GameManager : Node2D
|
|||
{
|
||||
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.
|
||||
|
|
|
|||
|
|
@ -5,7 +5,15 @@ public partial class Hud : CanvasLayer
|
|||
{
|
||||
[Signal]
|
||||
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)
|
||||
{
|
||||
var message = GetNode<Label>("Message");
|
||||
|
|
@ -29,4 +37,9 @@ public partial class Hud : CanvasLayer
|
|||
await ToSignal(GetTree().CreateTimer(1.0), SceneTreeTimer.SignalName.Timeout);
|
||||
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;
|
||||
|
||||
[Export] public float Health = 4f;
|
||||
[Export] public float MaxHealth = 32f;
|
||||
|
||||
[Export] public Weapon EquippedWeapon;
|
||||
|
||||
|
|
@ -46,11 +46,27 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
|
|||
|
||||
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;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_currentHealth = Health;
|
||||
CurrentHealth = MaxHealth;
|
||||
|
||||
_animatedSprite = GetNode<AnimatedSprite2D>("./Smoothing2D/AnimatedSprite2D");
|
||||
_crosshair = GetNode<Sprite2D>("./Smoothing2D/Crosshair");
|
||||
|
|
@ -245,8 +261,8 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
|
|||
GD.Print($"Player damaged for {damage}");
|
||||
if (_isDestroyed) return;
|
||||
|
||||
_currentHealth -= damage;
|
||||
if (!(_currentHealth <= 0)) return;
|
||||
CurrentHealth -= damage;
|
||||
if (!(CurrentHealth <= 0)) return;
|
||||
_isDestroyed = true;
|
||||
Explode();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue