Files
DungeonCrawlerWorld/scripts/domain/entities/companion_state.gd
T

82 lines
3.3 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
class_name CompanionState
extends RefCounted
# E4 deliberately owns only the companion's earned-XP ledger. F1 remains the
# sole owner of level thresholds and character progression.
class Stance:
const STAY_BACK: StringName = &"stay_back"
const DEFEND_ME: StringName = &"defend_me"
const ATTACK_ON_SIGHT: StringName = &"attack_on_sight"
# Legacy identifiers remain readable for code and saves created before the
# player-facing behavior names were introduced.
const FOLLOW: StringName = &"follow"
const DEFEND: StringName = &"defend"
const SUPPORT: StringName = &"support"
const ActorStateType = preload("res://scripts/domain/actor_state.gd")
const ExperienceType = preload("res://scripts/domain/progression/experience.gd")
const VALID_STANCES: Array[StringName] = [Stance.STAY_BACK, Stance.DEFEND_ME, Stance.ATTACK_ON_SIGHT]
var choice_id: StringName
var actor: ActorState
var stance: StringName = Stance.STAY_BACK
var name := "Familier"
var species := "Animal"
var endurance := 4
var regeneration_skill := 1
var regeneration_perk_bonus := 0
func _init(new_choice_id: StringName, new_actor: ActorState) -> void:
choice_id = new_choice_id
actor = new_actor
static func from_choice(new_choice_id: StringName, cell: Vector2i, hit_points: int = 8, new_name: String = "Familier", new_species: String = "Animal") -> CompanionState:
var id := new_choice_id if not new_choice_id.is_empty() else &"moth"
var companion_actor := ActorStateType.new(&"companion", cell, maxi(1, hit_points), 3, id, {"accuracy": 30, "xp": 0})
var companion := CompanionState.new(id, companion_actor)
companion.name = new_name.strip_edges() if not new_name.strip_edges().is_empty() else "Familier"
companion.species = new_species.strip_edges() if not new_species.strip_edges().is_empty() else "Animal"
return companion
func is_alive() -> bool:
return actor != null and actor.is_alive()
func set_stance(new_stance: StringName) -> bool:
var normalized := normalize_stance(new_stance)
if not normalized in VALID_STANCES:
return false
stance = normalized
return true
static func normalize_stance(value: StringName) -> StringName:
match value:
Stance.FOLLOW, &"aggressif": return Stance.ATTACK_ON_SIGHT
Stance.DEFEND, Stance.SUPPORT, &"defensif", &"soutien": return Stance.DEFEND_ME
return value
static func stance_label(value: StringName) -> String:
match normalize_stance(value):
Stance.STAY_BACK: return "Reste en arrière"
Stance.DEFEND_ME: return "Défends-moi"
Stance.ATTACK_ON_SIGHT: return "Attaque à vue"
return "Inconnu"
static func stance_description(value: StringName) -> String:
match normalize_stance(value):
Stance.STAY_BACK: return "Suit le joueur sans engager les ennemis."
Stance.DEFEND_ME: return "Intercepte et attaque les ennemis qui menacent le joueur."
Stance.ATTACK_ON_SIGHT: return "Poursuit et attaque l’ennemi visible le plus proche."
return ""
# This is intentionally a neutral allocation seam: it records earned XP only
# and neither reads nor changes levels, thresholds, attributes, or skills.
func share_xp(player: ActorState, amount: int) -> Dictionary:
var reward := maxi(0, amount)
if player == null or reward == 0:
return {"player_xp": 0, "companion_xp": 0}
var allocation := ExperienceType.split_reward(reward, is_alive())
player.grant_xp(allocation.player_xp)
if allocation.companion_xp > 0:
actor.grant_xp(allocation.companion_xp)
return allocation