class_name Creature extends Node signal damaged(amount) signal healed(amount) signal death() @export_category("Creature") @export var max_hp: int = 1 @export var death_sound: AudioStream = null @export var free_on_death: bool = true var hp: int : get: return hp set(value): var old_hp = hp hp = clampi(value, 0, max_hp) if hp < old_hp: self.damaged.emit(old_hp - hp) elif hp > old_hp: self.healed.emit(hp - old_hp) if hp == 0: self.death.emit() func _ready() -> void: self.hp = self.max_hp self.death.connect(self._creature_on_death) if self.has_method("_on_ready"): self.call("_on_ready") func take_damage(damage: int) -> void: self.hp -= damage func heal(amount: int) -> void: self.hp += amount func _creature_on_death() -> void: # Play a sound on creature death if self.death_sound: var audio_player = AudioStreamPlayer2D.new() self.get_parent().add_child(audio_player) audio_player.stream = self.death_sound audio_player.bus = "Sound Effects" audio_player.finished.connect(audio_player.queue_free) audio_player.play() # Destroy on death if self.free_on_death: self.queue_free()