Refactor rhythm action architecture
This commit is contained in:
49
scenes/components/health_component.gd
Normal file
49
scenes/components/health_component.gd
Normal file
@@ -0,0 +1,49 @@
|
||||
class_name HealthComponent
|
||||
extends Node
|
||||
|
||||
signal health_changed(current: int, maximum: int)
|
||||
signal depleted
|
||||
|
||||
@export var maximum := 100
|
||||
@export var current := 100
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
_emit_changed()
|
||||
|
||||
|
||||
func set_values(next_current: int, next_maximum: int) -> void:
|
||||
maximum = max(1, next_maximum)
|
||||
current = clampi(next_current, 0, maximum)
|
||||
_emit_changed()
|
||||
|
||||
|
||||
func apply_damage(amount: int) -> void:
|
||||
if amount <= 0:
|
||||
return
|
||||
current = clampi(current - amount, 0, maximum)
|
||||
_emit_changed()
|
||||
if current == 0:
|
||||
depleted.emit()
|
||||
|
||||
|
||||
func heal(amount: int) -> void:
|
||||
if amount <= 0:
|
||||
return
|
||||
current = clampi(current + amount, 0, maximum)
|
||||
_emit_changed()
|
||||
|
||||
|
||||
func _emit_changed() -> void:
|
||||
health_changed.emit(current, maximum)
|
||||
_event_bus().emit_signal("player_health_changed", current, maximum)
|
||||
|
||||
|
||||
func _event_bus() -> Node:
|
||||
var root := get_tree().root
|
||||
var bus := root.get_node_or_null("EventBus")
|
||||
if bus == null:
|
||||
bus = load("res://autoload/event_bus.gd").new()
|
||||
bus.name = "EventBus"
|
||||
root.add_child(bus)
|
||||
return bus
|
||||
Reference in New Issue
Block a user