Loading...
Loading...
Implement a complete health system with damage, healing, and health UI display.
Create a Health component script. Make it an autoload or attach to player: `var max_health = 100`, `var current_health = 100`. Add signals: `signal health_changed(new_health)`, `signal died`. Create functions: `func take_damage(amount)`, `func heal(amount)`, `func is_alive(): return current_health > 0`.
Implement take_damage function: `func take_damage(amount): current_health = max(0, current_health - amount)`, `health_changed.emit(current_health)`, `if current_health <= 0: died.emit()`. The max(0, ...) prevents negative health. Emit signals to update UI and trigger game over.
Implement heal function: `func heal(amount): current_health = min(max_health, current_health + amount)`, `health_changed.emit(current_health)`. The min(...) prevents healing above max health. Add visual feedback: flash the player sprite green when healed, red when damaged.
Create a health bar UI. In your HUD scene, add a TextureProgressBar or ColorRect. In its script: `@onready var health_bar = $HealthBar`. Connect to player health: `player.health_changed.connect(_on_health_changed)`. Update function: `func _on_health_changed(new_health): health_bar.value = new_health` (set max_value to max_health).
Apply damage from enemies. In enemy collision code: `if body.name == 'Player' and body.has_method('take_damage'): body.take_damage(damage_amount)`. Add invincibility frames: After taking damage, set `is_invincible = true`, start a timer (2 seconds), ignore damage while invincible, flash player sprite visibility on/off for visual feedback.