Unit death

This commit is contained in:
gamer147
2026-04-02 09:01:10 -04:00
parent ce92c6e435
commit 528cb50e58
5 changed files with 67 additions and 9 deletions

View File

@@ -1,5 +1,7 @@
class_name Unit extends Node2D
enum UnitState { ALIVE, DEAD }
#region Templates
@export var stat_template: UnitStats
@export var info_template: UnitInfo
@@ -9,15 +11,33 @@ class_name Unit extends Node2D
var current_stats: UnitStats
var current_info: UnitInfo
var current_allegiance: UnitAllegiance
var state: UnitState = UnitState.ALIVE
signal unit_selected_changed(unit: Unit, selected: bool)
signal unit_allegiance_changed(unit: Unit, allegiance: UnitAllegiance)
signal unit_died(unit: Unit)
func _ready() -> void:
current_stats = stat_template.duplicate(true)
current_info = info_template.duplicate(true)
current_allegiance = allegiance_template.duplicate(true)
unit_allegiance_changed.emit(self, current_allegiance)
func set_selected(selected: bool) -> void:
unit_selected_changed.emit(self, selected)
func is_alive() -> bool:
return state == UnitState.ALIVE
func take_damage(amount: int) -> void:
if state != UnitState.ALIVE:
return
current_stats.current_hp -= amount
if current_stats.current_hp <= 0:
current_stats.current_hp = 0
_die()
func _die() -> void:
state = UnitState.DEAD
unit_died.emit(self)
queue_free()