Refactor health management and collectable cleanup logic for improved functionality
This commit is contained in:
@@ -30,8 +30,9 @@ func _on_area2d_body_entered(body: Node2D) -> void:
|
||||
collision_shape.call_deferred("set_disabled", true)
|
||||
if sfx:
|
||||
sfx.play()
|
||||
if not has_fade_away:
|
||||
if sfx:
|
||||
if not has_fade_away and sfx:
|
||||
await sfx.finished
|
||||
|
||||
root.queue_free()
|
||||
root.queue_free()
|
||||
elif not has_fade_away:
|
||||
root.queue_free()
|
||||
|
||||
|
@@ -9,63 +9,40 @@ signal on_health_change(delta: float, total_health: float)
|
||||
signal on_death
|
||||
|
||||
|
||||
func _get_delta(value: float) -> float:
|
||||
var old_value = health
|
||||
return value - old_value
|
||||
|
||||
func _get_delta(new_value: float) -> float:
|
||||
return new_value - health
|
||||
|
||||
|
||||
func set_health(new_value: float):
|
||||
var delta = _get_delta(new_value)
|
||||
|
||||
if sign(delta) > 0:
|
||||
if heal_fx:
|
||||
heal_fx.play()
|
||||
elif sign(delta) < 0:
|
||||
if hurt_fx:
|
||||
hurt_fx.play()
|
||||
|
||||
await hurt_fx.finished
|
||||
|
||||
if new_value >= max_health:
|
||||
health = max_health
|
||||
on_health_change.emit(delta, health)
|
||||
return
|
||||
|
||||
health = new_value
|
||||
|
||||
if health <= 0:
|
||||
on_death.emit()
|
||||
return
|
||||
|
||||
on_health_change.emit(delta, health)
|
||||
_apply_health_change(new_value)
|
||||
|
||||
|
||||
func decrease_health(value: float):
|
||||
var delta = _get_delta(value)
|
||||
|
||||
health -= value
|
||||
if hurt_fx:
|
||||
hurt_fx.play()
|
||||
await hurt_fx.finished
|
||||
|
||||
if health <= 0:
|
||||
on_death.emit()
|
||||
return
|
||||
|
||||
on_health_change.emit(delta, health)
|
||||
_apply_health_change(health - value)
|
||||
|
||||
|
||||
func increase_health(value: float):
|
||||
var delta := _get_delta(value)
|
||||
_apply_health_change(health + value)
|
||||
|
||||
health += value
|
||||
|
||||
if health >= max_health:
|
||||
health = max_health
|
||||
func _apply_health_change(new_health: float, play_fx: bool = true) -> void:
|
||||
new_health = clamp(new_health, 0.0, max_health)
|
||||
var delta := new_health - health
|
||||
|
||||
if delta == 0.0:
|
||||
return # No change
|
||||
|
||||
if play_fx:
|
||||
if delta > 0 and heal_fx:
|
||||
heal_fx.play()
|
||||
elif delta < 0 and hurt_fx:
|
||||
hurt_fx.play()
|
||||
await hurt_fx.finished
|
||||
|
||||
health = new_health
|
||||
|
||||
if health <= 0:
|
||||
on_death.emit()
|
||||
else:
|
||||
on_health_change.emit(delta, health)
|
||||
return
|
||||
|
||||
if heal_fx:
|
||||
heal_fx.play()
|
||||
on_health_change.emit(delta, health)
|
||||
|
||||
|
@@ -39,7 +39,7 @@ func on_health_change(delta: float, total_health: float) -> void:
|
||||
await get_tree().create_timer(hit_duration).timeout
|
||||
deactivate()
|
||||
|
||||
if total_health > 0:
|
||||
if total_health > 0 and delta < 0:
|
||||
handle_hit_fx()
|
||||
|
||||
|
||||
|
@@ -12,7 +12,8 @@ func _ready():
|
||||
coin.collected.connect(on_collected)
|
||||
|
||||
|
||||
func on_collected(amount: int, type: CollectableResource.CollectableType) -> void:
|
||||
|
||||
func on_collected(amount: int, type: CollectableResource.CollectableType, _body: Node2D) -> void:
|
||||
if not game_manager:
|
||||
return
|
||||
if type != CollectableResource.CollectableType.COIN:
|
||||
|
Reference in New Issue
Block a user