Refactored unit

This commit is contained in:
gamer147
2026-04-08 18:44:58 -04:00
parent c192d48bc4
commit b807e9897d
16 changed files with 239 additions and 154 deletions

View File

@@ -1,7 +1,7 @@
class_name CombatProposal extends Resource
class CombatantStats:
var unit: Unit
var deployed: DeployedUnit
var max_hp: int
var hp: int
var sp: int

View File

@@ -1,6 +1,6 @@
class_name CombatSystem extends Node
func create_proposal(attacker: Unit, defender: Unit, distance: int) -> CombatProposal:
func create_proposal(attacker: DeployedUnit, defender: DeployedUnit, distance: int) -> CombatProposal:
var proposal := CombatProposal.new()
var atk_tactics := _filter_tactics(attacker, distance)
@@ -19,10 +19,10 @@ func create_proposal(attacker: Unit, defender: Unit, distance: int) -> CombatPro
return proposal
func _filter_tactics(unit: Unit, distance: int) -> Array[CombatTactic]:
func _filter_tactics(deployed: DeployedUnit, distance: int) -> Array[CombatTactic]:
var valid: Array[CombatTactic] = []
for tactic in unit.tactics:
if tactic.tactic_range and tactic.tactic_range.is_valid_range(distance, unit):
for tactic in deployed.tactics:
if tactic.tactic_range and tactic.tactic_range.is_valid_range(distance, deployed.unit):
valid.append(tactic)
return valid
@@ -34,28 +34,28 @@ func _find_default_attack(tactics: Array[CombatTactic]) -> CombatTactic:
return tactics[0] if tactics.size() > 0 else null
func _snapshot(unit: Unit, opponent: Unit, available: Array[CombatTactic], selected: CombatTactic, opponent_selected: CombatTactic) -> CombatProposal.CombatantStats:
func _snapshot(deployed: DeployedUnit, opponent: DeployedUnit, available: Array[CombatTactic], selected: CombatTactic, opponent_selected: CombatTactic) -> CombatProposal.CombatantStats:
var stats := CombatProposal.CombatantStats.new()
stats.unit = unit
stats.max_hp = unit.current_stats.max_hp
stats.hp = unit.current_stats.current_hp
stats.sp = unit.current_stats.current_sp
stats.spd = unit.current_stats.spd
stats.deployed = deployed
stats.max_hp = deployed.unit.stats.max_hp
stats.hp = deployed.current_stats.current_hp
stats.sp = deployed.current_stats.current_sp
stats.spd = deployed.unit.stats.spd
stats.available_tactics = available
stats.selected_tactic = selected
if selected and selected.deals_damage():
var offensive: Dictionary = selected.get_offensive_stats(unit)
var offensive: Dictionary = selected.get_offensive_stats(deployed.unit)
stats.atk = offensive["atk"]
stats.hit = offensive["hit"] - opponent.current_stats.eva
stats.hit = offensive["hit"] - opponent.unit.stats.eva
else:
stats.atk = 0
stats.hit = 0
if opponent_selected and opponent_selected.deals_damage():
stats.def = opponent_selected.get_relevant_defense(unit)
stats.def = opponent_selected.get_relevant_defense(deployed.unit)
else:
stats.def = unit.current_stats.phys_def
stats.def = deployed.unit.stats.phys_def
return stats
@@ -74,29 +74,29 @@ func update_tactic(proposal: CombatProposal, is_attacker: bool, tactic: CombatTa
# Recalculate this side's offensive stats
if tactic and tactic.deals_damage():
var offensive: Dictionary = tactic.get_offensive_stats(self_stats.unit)
var offensive: Dictionary = tactic.get_offensive_stats(self_stats.deployed.unit)
self_stats.atk = offensive["atk"]
self_stats.hit = offensive["hit"] - opp_stats.unit.current_stats.eva
self_stats.hit = offensive["hit"] - opp_stats.deployed.unit.stats.eva
else:
self_stats.atk = 0
self_stats.hit = 0
# Recalculate opponent's def based on this side's new tactic
if tactic and tactic.deals_damage():
opp_stats.def = tactic.get_relevant_defense(opp_stats.unit)
opp_stats.def = tactic.get_relevant_defense(opp_stats.deployed.unit)
else:
opp_stats.def = opp_stats.unit.current_stats.phys_def
opp_stats.def = opp_stats.deployed.unit.stats.phys_def
func select_ai_tactic(unit: Unit, opponent: Unit, available_tactics: Array[CombatTactic]) -> CombatTactic:
func select_ai_tactic(deployed: DeployedUnit, opponent: DeployedUnit, available_tactics: Array[CombatTactic]) -> CombatTactic:
var best_tactic: CombatTactic = null
var best_damage := -1
for tactic in available_tactics:
if not tactic.deals_damage():
continue
var offensive: Dictionary = tactic.get_offensive_stats(unit)
var defense: int = tactic.get_relevant_defense(opponent)
var offensive: Dictionary = tactic.get_offensive_stats(deployed.unit)
var defense: int = tactic.get_relevant_defense(opponent.unit)
var damage := maxi(offensive["atk"] - defense, 0)
if damage > best_damage:
best_damage = damage
@@ -114,10 +114,10 @@ func select_ai_tactic(unit: Unit, opponent: Unit, available_tactics: Array[Comba
func apply_proposal(proposal: CombatProposal) -> void:
var atk_stats := proposal.attacker
var def_stats := proposal.defender
var atk_unit := atk_stats.unit
var def_unit := def_stats.unit
var atk_deployed := atk_stats.deployed
var def_deployed := def_stats.deployed
if not is_instance_valid(atk_unit) or not is_instance_valid(def_unit):
if not is_instance_valid(atk_deployed) or not is_instance_valid(def_deployed):
return
# Attacker strikes (if their tactic deals damage)
@@ -125,17 +125,17 @@ func apply_proposal(proposal: CombatProposal) -> void:
var atk_roll := randi_range(1, 100)
if atk_roll <= atk_stats.hit:
var damage := maxi(atk_stats.atk - def_stats.def, 0)
def_unit.take_damage(damage)
def_deployed.take_damage(damage)
# Counterattack if defender survives and their tactic deals damage
if is_instance_valid(def_unit) and def_unit.is_alive() \
and is_instance_valid(atk_unit) \
if is_instance_valid(def_deployed) and def_deployed.is_alive() \
and is_instance_valid(atk_deployed) \
and def_stats.selected_tactic and def_stats.selected_tactic.deals_damage():
var def_roll := randi_range(1, 100)
if def_roll <= def_stats.hit:
var damage := maxi(def_stats.atk - atk_stats.def, 0)
atk_unit.take_damage(damage)
atk_deployed.take_damage(damage)
func _is_player_controlled(unit: Unit) -> bool:
return unit.current_allegiance.type == UnitAllegiance.AllegianceType.PLAYER
func _is_player_controlled(deployed: DeployedUnit) -> bool:
return deployed.unit.allegiance.type == UnitAllegiance.AllegianceType.PLAYER