254 lines
11 KiB
GDScript
254 lines
11 KiB
GDScript
extends PanelContainer
|
||
## Visual card widget.
|
||
##
|
||
## Call setup(dto, def) after instantiation, where:
|
||
## dto — PlayerCardDto dictionary from collection.get_cards
|
||
## def — CardDefinitionDto from ContentCache.get_definition() (pass {} if absent)
|
||
##
|
||
## Emits card_pressed(definition_id) on tap/click.
|
||
## Call set_selected(true/false) to highlight the card (used by the arch-spirit picker).
|
||
|
||
# ── Colour tables ─────────────────────────────────────────────────────────────
|
||
# Affinities match SOR.Database.Enums.Affinity.
|
||
# Basic: Void, Aether, Lake, Hill, Forest, Field.
|
||
# Non-basics are two-basic combos; tile-only affinities never appear on cards.
|
||
|
||
const AFFINITY_BORDER := {
|
||
# ── Basic ──────────────────────────────────────────────────────────────
|
||
"Void": Color(0.45, 0.18, 0.65),
|
||
"Aether": Color(0.35, 0.72, 1.00),
|
||
"Lake": Color(0.22, 0.52, 0.92),
|
||
"Hill": Color(0.70, 0.55, 0.30),
|
||
"Forest": Color(0.28, 0.68, 0.25),
|
||
"Field": Color(0.82, 0.72, 0.18),
|
||
# ── Non-basic ──────────────────────────────────────────────────────────
|
||
"Ocean": Color(0.12, 0.38, 0.82),
|
||
"Island": Color(0.22, 0.65, 0.72),
|
||
"Marsh": Color(0.35, 0.60, 0.38),
|
||
"Swamp": Color(0.50, 0.58, 0.22),
|
||
"Mountain": Color(0.55, 0.55, 0.62),
|
||
"Steppe": Color(0.55, 0.58, 0.25),
|
||
"Fort": Color(0.78, 0.65, 0.35),
|
||
"Plains": Color(0.55, 0.78, 0.35),
|
||
"Grove": Color(0.18, 0.52, 0.20),
|
||
"Jungle": Color(0.32, 0.72, 0.18),
|
||
"PerfectAether": Color(0.70, 0.88, 1.00),
|
||
"PureVoid": Color(0.30, 0.08, 0.48),
|
||
}
|
||
|
||
const AFFINITY_BG := {
|
||
# ── Basic ──────────────────────────────────────────────────────────────
|
||
"Void": Color(0.15, 0.04, 0.22),
|
||
"Aether": Color(0.06, 0.18, 0.38),
|
||
"Lake": Color(0.05, 0.15, 0.40),
|
||
"Hill": Color(0.28, 0.20, 0.08),
|
||
"Forest": Color(0.08, 0.24, 0.08),
|
||
"Field": Color(0.25, 0.20, 0.03),
|
||
# ── Non-basic ──────────────────────────────────────────────────────────
|
||
"Ocean": Color(0.03, 0.10, 0.32),
|
||
"Island": Color(0.05, 0.22, 0.28),
|
||
"Marsh": Color(0.08, 0.18, 0.10),
|
||
"Swamp": Color(0.14, 0.18, 0.05),
|
||
"Mountain": Color(0.15, 0.15, 0.18),
|
||
"Steppe": Color(0.18, 0.20, 0.06),
|
||
"Fort": Color(0.25, 0.20, 0.08),
|
||
"Plains": Color(0.15, 0.26, 0.08),
|
||
"Grove": Color(0.04, 0.16, 0.05),
|
||
"Jungle": Color(0.08, 0.24, 0.04),
|
||
"PerfectAether": Color(0.12, 0.22, 0.42),
|
||
"PureVoid": Color(0.08, 0.02, 0.14),
|
||
}
|
||
|
||
# One icon per basic affinity. Non-basics share their primary component's icon
|
||
# via _affinity_icon() below.
|
||
const AFFINITY_ICONS := {
|
||
"Void": preload("res://icons/affinity_void.svg"),
|
||
"Aether": preload("res://icons/affinity_aether.svg"),
|
||
"Lake": preload("res://icons/affinity_lake.svg"),
|
||
"Hill": preload("res://icons/affinity_hill.svg"),
|
||
"Forest": preload("res://icons/affinity_forest.svg"),
|
||
"Field": preload("res://icons/affinity_field.svg"),
|
||
}
|
||
|
||
## Returns the icon texture for any affinity, mapping non-basics to their
|
||
## primary basic component.
|
||
static func affinity_icon(affinity: String) -> Texture2D:
|
||
match affinity:
|
||
"Lake", "Ocean", "Island", "Marsh", "Swamp":
|
||
return AFFINITY_ICONS["Lake"]
|
||
"Hill", "Mountain", "Steppe", "Fort":
|
||
return AFFINITY_ICONS["Hill"]
|
||
"Forest", "Plains", "Grove":
|
||
return AFFINITY_ICONS["Forest"]
|
||
"Field", "Jungle":
|
||
return AFFINITY_ICONS["Field"]
|
||
"Aether", "PerfectAether":
|
||
return AFFINITY_ICONS["Aether"]
|
||
"Void", "PureVoid":
|
||
return AFFINITY_ICONS["Void"]
|
||
_:
|
||
return null
|
||
|
||
const RARITY_COLOR := {
|
||
"Common": Color(0.67, 0.67, 0.67),
|
||
"Uncommon": Color(0.27, 0.85, 0.27),
|
||
"Rare": Color(0.27, 0.53, 1.00),
|
||
"Epic": Color(0.67, 0.27, 1.00),
|
||
"Legendary": Color(1.00, 0.67, 0.13),
|
||
}
|
||
|
||
# ── Signals ───────────────────────────────────────────────────────────────────
|
||
|
||
## Emitted on mouse-click or screen-touch.
|
||
signal card_pressed(definition_id: String)
|
||
|
||
# ── Node refs ─────────────────────────────────────────────────────────────────
|
||
|
||
@onready var sp_label : Label = %SpLabel
|
||
@onready var st_label : Label = %StLabel
|
||
@onready var foil_label : Label = %FoilLabel
|
||
@onready var art_rect : ColorRect = %ArtRect
|
||
@onready var art_symbol : TextureRect = %ArtSymbol
|
||
@onready var name_label : Label = %NameLabel
|
||
@onready var type_label : Label = %TypeLabel
|
||
@onready var rarity_label : Label = %RarityLabel
|
||
@onready var dmg_label : Label = %DmgLabel
|
||
@onready var hp_label : Label = %HpLabel
|
||
@onready var def_label : Label = %DefLabel
|
||
|
||
# ── State ─────────────────────────────────────────────────────────────────────
|
||
|
||
var _affinity : String = ""
|
||
var _definition_id : String = ""
|
||
var _is_selected : bool = false
|
||
var _is_ready : bool = false
|
||
var _pending_dto : Dictionary = {}
|
||
var _pending_def : Dictionary = {}
|
||
|
||
|
||
func _ready() -> void:
|
||
_is_ready = true
|
||
if not _pending_dto.is_empty():
|
||
_apply_setup(_pending_dto, _pending_def)
|
||
_pending_dto.clear()
|
||
_pending_def.clear()
|
||
|
||
# ── Public API ────────────────────────────────────────────────────────────────
|
||
|
||
## Populate the widget.
|
||
func setup(dto: Dictionary, def: Dictionary = {}) -> void:
|
||
if not _is_ready:
|
||
_pending_dto = dto.duplicate(true)
|
||
_pending_def = def.duplicate(true)
|
||
return
|
||
|
||
_apply_setup(dto, def)
|
||
|
||
|
||
func _apply_setup(dto: Dictionary, def: Dictionary = {}) -> void:
|
||
_definition_id = str(dto.get("cardDefinitionId", def.get("id", "")))
|
||
_affinity = str(def.get("affinity", dto.get("affinity", "")))
|
||
var rarity := str(def.get("rarity", dto.get("rarity", "")))
|
||
var name := str(def.get("name", dto.get("name", "")))
|
||
var card_type := str(def.get("cardType", dto.get("cardType", "")))
|
||
var is_foil := bool(dto.get("isFoil", false))
|
||
|
||
_apply_style()
|
||
|
||
art_rect.color = AFFINITY_BG.get(_affinity, Color(0.18, 0.18, 0.24))
|
||
art_symbol.texture = affinity_icon(_affinity)
|
||
|
||
foil_label.visible = is_foil
|
||
name_label.text = str(dto.get("name", "—"))
|
||
type_label.text = str(dto.get("cardType", ""))
|
||
name_label.text = name if not name.is_empty() else name_label.text
|
||
type_label.text = card_type if not card_type.is_empty() else type_label.text
|
||
rarity_label.text = rarity
|
||
rarity_label.modulate = RARITY_COLOR.get(rarity, Color(0.67, 0.67, 0.67))
|
||
|
||
_load_stats(def)
|
||
_load_abilities(def)
|
||
|
||
|
||
## Toggle the selection highlight (arch-spirit picker).
|
||
func set_selected(selected: bool) -> void:
|
||
_is_selected = selected
|
||
_apply_style()
|
||
|
||
|
||
## The cached definition ID this widget represents.
|
||
func get_definition_id() -> String:
|
||
return _definition_id
|
||
|
||
# ── Input ─────────────────────────────────────────────────────────────────────
|
||
|
||
func _gui_input(event: InputEvent) -> void:
|
||
if event is InputEventMouseButton \
|
||
and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
|
||
card_pressed.emit(_definition_id)
|
||
accept_event()
|
||
elif event is InputEventScreenTouch and event.pressed:
|
||
card_pressed.emit(_definition_id)
|
||
accept_event()
|
||
|
||
# ── Visuals ───────────────────────────────────────────────────────────────────
|
||
|
||
func _apply_style() -> void:
|
||
var border: Color
|
||
var width: int
|
||
if _is_selected:
|
||
border = Color(1.0, 1.0, 1.0)
|
||
width = 3
|
||
else:
|
||
border = AFFINITY_BORDER.get(_affinity, Color(0.31, 0.38, 0.63))
|
||
width = 2
|
||
var s := StyleBoxFlat.new()
|
||
s.bg_color = Color(0.09, 0.09, 0.14)
|
||
s.border_color = border
|
||
s.set_border_width_all(width)
|
||
s.set_corner_radius_all(6)
|
||
add_theme_stylebox_override("panel", s)
|
||
|
||
# ── Stats ─────────────────────────────────────────────────────────────────────
|
||
|
||
func _load_stats(def: Dictionary) -> void:
|
||
var sp : Variant = def.get("spCost", null)
|
||
var st : Variant = def.get("stCost", null)
|
||
var dmg : Variant = def.get("damage", null)
|
||
var hp : Variant = def.get("maxHealth", null)
|
||
var dfs : Variant = def.get("maxDefense", null)
|
||
var mov : Variant = def.get("movementPoints", null)
|
||
|
||
sp_label.text = ("◆%s" % str(sp)) if sp != null else "◆?"
|
||
st_label.text = ("⚡%s" % str(st)) if (st != null and int(st) > 0) else ""
|
||
st_label.visible = st != null and int(st) > 0
|
||
|
||
dmg_label.text = "⚔ %s" % (str(dmg) if dmg != null else "–")
|
||
hp_label.text = "♥ %s" % (str(hp) if hp != null else "–")
|
||
def_label.text = "▲ %s" % (str(dfs) if dfs != null else "–")
|
||
|
||
sp_label.tooltip_text = "Spirit Cost"
|
||
st_label.tooltip_text = "Stamina Cost"
|
||
dmg_label.tooltip_text = "Attack Damage" \
|
||
+ (" | Move: %s" % str(mov) if mov != null else "")
|
||
hp_label.tooltip_text = "Max Health"
|
||
def_label.tooltip_text = "Max Defense"
|
||
|
||
|
||
func _load_abilities(def: Dictionary) -> void:
|
||
var json_str := str(def.get("abilities", ""))
|
||
if json_str.is_empty() or json_str == "[]":
|
||
return
|
||
var parsed: Variant = JSON.parse_string(json_str)
|
||
if typeof(parsed) != TYPE_ARRAY:
|
||
return
|
||
var lines: PackedStringArray = []
|
||
for ab in parsed as Array:
|
||
if typeof(ab) == TYPE_DICTIONARY:
|
||
var d := ab as Dictionary
|
||
var n := str(d.get("name", d.get("Name", "")))
|
||
var t := str(d.get("description", d.get("Description", "")))
|
||
lines.append(("[%s] %s" % [n, t]) if not t.is_empty() else ("[%s]" % n))
|
||
if not lines.is_empty():
|
||
art_rect.tooltip_text = "\n".join(lines)
|