Refactored unit
This commit is contained in:
@@ -1,4 +1,49 @@
|
||||
class_name DeployedUnit extends Node2D
|
||||
|
||||
# The unit represented by this deployment
|
||||
enum UnitState { ALIVE, DEAD }
|
||||
|
||||
@export var unit: Unit
|
||||
|
||||
var current_stats: DeployedUnitStats
|
||||
var tactics: Array[CombatTactic] = []
|
||||
var state: UnitState = UnitState.ALIVE
|
||||
|
||||
signal unit_selected_changed(deployed: DeployedUnit, selected: bool)
|
||||
signal unit_allegiance_changed(deployed: DeployedUnit, allegiance: UnitAllegiance)
|
||||
signal unit_died(deployed: DeployedUnit)
|
||||
|
||||
func _ready() -> void:
|
||||
current_stats = DeployedUnitStats.from_unit_stats(unit.stats)
|
||||
tactics = unit.tactics.duplicate()
|
||||
_append_builtin_tactics()
|
||||
unit_allegiance_changed.emit(self, unit.allegiance)
|
||||
|
||||
func _append_builtin_tactics() -> void:
|
||||
var attack := AttackCombatTactic.new()
|
||||
attack.tactic_name = "Attack"
|
||||
attack.tactic_range = UnitMatchingCombatTacticRange.new()
|
||||
tactics.append(attack)
|
||||
|
||||
var defend := DefendCombatTactic.new()
|
||||
defend.tactic_name = "Defend"
|
||||
defend.tactic_range = AnyCombatTacticRange.new()
|
||||
tactics.append(defend)
|
||||
|
||||
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()
|
||||
|
||||
@@ -5,10 +5,10 @@ class_name DeployedUnitStats extends Resource
|
||||
@export var current_sp: int
|
||||
@export var current_fs: int
|
||||
|
||||
func _init() -> void:
|
||||
_init_stats.call_deferred()
|
||||
|
||||
func _init_stats() -> void:
|
||||
current_hp = unit_stats.max_hp
|
||||
current_sp = unit_stats.max_sp
|
||||
current_fs = unit_stats.max_fs
|
||||
static func from_unit_stats(source: UnitStats) -> DeployedUnitStats:
|
||||
var stats := DeployedUnitStats.new()
|
||||
stats.unit_stats = source
|
||||
stats.current_hp = source.max_hp
|
||||
stats.current_sp = source.max_sp
|
||||
stats.current_fs = source.max_fs
|
||||
return stats
|
||||
|
||||
Reference in New Issue
Block a user