taloria/game/scenes/ui/inventar.gd

405 lines
11 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.

extends Control
## Inventar als Modal über der Karte (docs/ui.md §1).
##
## 5×3-Raster mit Item-Icons, ausgerüstete Slots links, Details und Aktionen
## rechts. Ausrüsten prüft die Klassenbeschränkung aus `classes.json`;
## Verbrauchsgüter laufen über `Game.benutzen()`.
signal geschlossen
const THEME := preload("res://scripts/ui/ui_theme.gd")
const SLOTS := [
{"key": "weapon", "label": "Waffe"},
{"key": "armor", "label": "Rüstung"},
{"key": "shield", "label": "Schild"},
]
const SPALTEN := 5
const ZEILEN := 3
const KACHEL := 88
const ABSTAND := 8
const RASTER := Vector2(640, 200)
var _index := 0
var _wurzel: Control
var _sel_id := ""
var _sel_quelle := "" ## "inventar" oder "slot"
var _meldung := ""
func _ready() -> void:
theme = THEME.baue()
set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
_wurzel = Control.new()
_wurzel.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
add_child(_wurzel)
_bauen()
func _bauen() -> void:
_leeren(_wurzel)
var dimm := UiTheme.dimmung()
dimm.color = Color(0.04, 0.03, 0.02, 0.72)
_wurzel.add_child(dimm)
var p := Panel.new()
p.position = Vector2(260, 160)
p.size = Vector2(1400, 760)
p.add_theme_stylebox_override("panel", THEME.flaeche(Color(1, 1, 1)))
_wurzel.add_child(p)
_titel(p)
_reiter(p)
_gold(p)
if Game.party.is_empty():
_label(p, "Keine Figur in der Party.", Vector2(40, 200))
_schliessen_knopf(p)
return
var figur: Dictionary = Game.party[_index]
_ausgeruestet(p, figur)
_rucksack(p, figur)
_details(p, figur)
_label(p, "RK %d Angriff +%d TP %d/%d" % [
Game.rk(figur), Game.angriffsbonus(figur),
int(figur["hp"]["current"]), int(figur["hp"]["max"])],
Vector2(40, 680), Vector2(700, 40))
_schliessen_knopf(p)
func _titel(p: Panel) -> void:
var l := Label.new()
l.text = "INVENTAR"
l.position = Vector2(40, 24)
l.add_theme_color_override("font_color", UiTheme.GOLD)
p.add_child(l)
func _gold(p: Panel) -> void:
var l := Label.new()
l.text = "Gold: %d" % Game.gold
l.position = Vector2(1080, 24)
l.size = Vector2(280, 40)
l.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
l.clip_text = true
l.add_theme_color_override("font_color", UiTheme.GOLD)
p.add_child(l)
func _reiter(p: Panel) -> void:
var x := 40
for i in Game.party.size():
var b := Button.new()
b.text = str(Game.party[i].get("name", "?"))
b.position = Vector2(x, 80)
b.size = Vector2(170, 52)
b.focus_mode = Control.FOCUS_NONE
b.clip_text = true
b.modulate = Color(1.45, 1.25, 0.95) if i == _index else Color(1, 1, 1)
b.pressed.connect(_waehlen_figur.bind(i))
p.add_child(b)
x += 180
# ------------------------------------------------------------- ausgerüstet --
func _ausgeruestet(p: Panel, figur: Dictionary) -> void:
_label(p, "AUSGERÜSTET", Vector2(40, 160),
Vector2(300, 40), UiTheme.GOLD)
var equipment: Dictionary = figur.get("equipment", {})
for i in SLOTS.size():
var slot: Dictionary = SLOTS[i]
var y := 210 + i * 100
var id := ""
if str(slot["key"]) == "shield":
if bool(equipment.get("shield", false)):
id = "schild"
else:
id = str(equipment.get(slot["key"], ""))
if not id.is_empty():
_icon(p, id, Vector2(40, y))
_label(p, str(slot["label"]), Vector2(120, y + 12),
Vector2(260, 40), UiTheme.CREAM_D)
_label(p, _item_name(id) if not id.is_empty() else "—",
Vector2(120, y + 44), Vector2(260, 40))
if not id.is_empty():
var b := Button.new()
b.text = "Ablegen"
b.position = Vector2(400, y + 6)
b.size = Vector2(200, 52)
b.focus_mode = Control.FOCUS_NONE
b.clip_text = true
b.pressed.connect(_ablegen.bind(str(slot["key"])))
p.add_child(b)
# ---------------------------------------------------------------- Rucksack --
func _rucksack(p: Panel, figur: Dictionary) -> void:
_label(p, "RUCKSACK", RASTER + Vector2(0, -40),
Vector2(300, 40), UiTheme.GOLD)
var inventar: Array = figur.get("inventory", [])
for i in SPALTEN * ZEILEN:
var x := RASTER.x + (i % SPALTEN) * (KACHEL + ABSTAND)
var y := RASTER.y + (i / SPALTEN) * (KACHEL + ABSTAND)
if i < inventar.size():
var eintrag = inventar[i]
var id := ""
var anzahl := 1
if typeof(eintrag) == TYPE_DICTIONARY:
id = str(eintrag.get("id", ""))
anzahl = int(eintrag.get("count", 1))
else:
id = str(eintrag)
_slot_knopf(p, id, anzahl, Vector2(x, y))
else:
_leerer_slot(p, Vector2(x, y))
func _slot_knopf(p: Panel, id: String, anzahl: int, pos: Vector2) -> void:
var b := Button.new()
b.position = pos
b.size = Vector2(KACHEL, KACHEL)
b.focus_mode = Control.FOCUS_NONE
b.tooltip_text = _item_name(id)
_slot_style(b, _sel_id == id and _sel_quelle == "inventar")
b.pressed.connect(_waehlen_item.bind(id))
p.add_child(b)
var t := _item_textur(id)
if t != null:
var icon := TextureRect.new()
icon.texture = t
icon.position = Vector2(12, 6)
icon.size = Vector2(64, 64)
icon.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
icon.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
icon.texture_filter = CanvasItem.TEXTURE_FILTER_NEAREST
icon.mouse_filter = Control.MOUSE_FILTER_IGNORE
b.add_child(icon)
if anzahl > 1:
var zahl := Label.new()
zahl.text = str(anzahl)
zahl.position = Vector2(44, 54)
zahl.size = Vector2(40, 30)
zahl.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
zahl.add_theme_color_override("font_outline_color", UiTheme.INK)
zahl.add_theme_constant_override("outline_size", 6)
zahl.mouse_filter = Control.MOUSE_FILTER_IGNORE
b.add_child(zahl)
func _leerer_slot(p: Panel, pos: Vector2) -> void:
var panel := Panel.new()
panel.position = pos
panel.size = Vector2(KACHEL, KACHEL)
var sb := StyleBoxFlat.new()
sb.bg_color = Color(UiTheme.PANEL_LO, 0.7)
sb.border_color = Color(UiTheme.LINE, 0.45)
sb.set_border_width_all(2)
panel.add_theme_stylebox_override("panel", sb)
p.add_child(panel)
func _slot_style(b: Button, gewaehlt: bool) -> void:
var normal := StyleBoxFlat.new()
normal.bg_color = UiTheme.PANEL_LO
normal.border_color = UiTheme.GOLD if gewaehlt else UiTheme.LINE
normal.set_border_width_all(2)
var heiss := normal.duplicate() as StyleBoxFlat
heiss.bg_color = UiTheme.PANEL_HI
heiss.border_color = UiTheme.GOLD
b.add_theme_stylebox_override("normal", normal)
b.add_theme_stylebox_override("hover", heiss)
b.add_theme_stylebox_override("pressed", heiss)
b.add_theme_stylebox_override("focus", normal)
# ----------------------------------------------------------------- Details --
func _details(p: Panel, figur: Dictionary) -> void:
var box := Panel.new()
box.position = Vector2(640, 510)
box.size = Vector2(720, 150)
box.add_theme_stylebox_override("panel", THEME.flaeche(Color(1, 1, 1)))
p.add_child(box)
if _sel_id.is_empty():
_label(box, "Wähle einen Gegenstand.", Vector2(20, 20),
Vector2(680, 40), UiTheme.CREAM_D)
if not _meldung.is_empty():
_label(box, _meldung, Vector2(20, 80), Vector2(680, 36),
Color(0.88, 0.78, 0.55))
return
_label(box, _item_name(_sel_id), Vector2(20, 16),
Vector2(420, 40), UiTheme.GOLD)
var typ := _item_typ(_sel_id)
var item: Dictionary = Content.entry("items", _sel_id)
var beschreibung := "Sonstiges"
match typ:
"weapon":
beschreibung = "Waffe - %s" % str(item.get("damage", "?"))
"armor":
beschreibung = "Rüstung - RK +%d" % int(item.get("ac_bonus", 0))
"shield":
beschreibung = "Schild - RK +%d" % int(item.get("ac_bonus", 0))
_label(box, beschreibung, Vector2(20, 58), Vector2(420, 36), UiTheme.CREAM_D)
if _sel_quelle == "slot":
_aktions_knopf(box, "Ablegen", _ablegen.bind(_sel_id))
elif typ in ["weapon", "armor", "shield"]:
var erlaubt: bool = Game.ausruesten_erlaubt(figur, _sel_id)
if erlaubt:
_aktions_knopf(box, "Ausrüsten", _ausruesten.bind(_sel_id))
else:
_label(box, "Diese Klasse darf das nicht tragen.",
Vector2(20, 104), Vector2(560, 36), Color(0.85, 0.55, 0.42))
else:
_aktions_knopf(box, "Benutzen", _benutzen.bind(_sel_id))
if not _meldung.is_empty():
_label(box, _meldung, Vector2(20, 104), Vector2(660, 36),
Color(0.88, 0.78, 0.55))
func _aktions_knopf(box: Panel, text: String, cb: Callable) -> void:
var b := Button.new()
b.text = text
b.position = Vector2(450, 30)
b.size = Vector2(250, 56)
b.focus_mode = Control.FOCUS_NONE
b.clip_text = true
b.pressed.connect(cb)
box.add_child(b)
# -------------------------------------------------------------- Aktionen ----
func _waehlen_figur(index: int) -> void:
_index = index
_sel_id = ""
_sel_quelle = ""
_meldung = ""
_bauen()
func _waehlen_item(id: String) -> void:
_sel_id = id
_sel_quelle = "inventar"
_meldung = ""
_bauen()
func _ausruesten(id: String) -> void:
if Game.party.is_empty():
return
if Game.ausruesten(Game.party[_index], id):
_meldung = "%s ausgerüstet." % _item_name(id)
_sel_id = ""
_sel_quelle = ""
else:
_meldung = "Nicht erlaubt oder nicht im Rucksack."
_bauen()
func _ablegen(slot: String) -> void:
if Game.party.is_empty():
return
var figur: Dictionary = Game.party[_index]
var id := ""
if slot == "shield":
id = "schild"
else:
id = str(figur.get("equipment", {}).get(slot, ""))
if Game.ablegen(figur, slot):
_meldung = "%s abgelegt." % _item_name(id)
_sel_id = ""
_sel_quelle = ""
else:
_meldung = "Slot ist leer."
_bauen()
func _benutzen(id: String) -> void:
if Game.party.is_empty():
return
_meldung = Game.benutzen(Game.party[_index], id)
if Game.inventar_hat(Game.party[_index], id) <= 0:
_sel_id = ""
_sel_quelle = ""
_bauen()
func _schliessen_knopf(p: Panel) -> void:
var b := Button.new()
b.text = "Schließen"
b.position = Vector2(1120, 680)
b.size = Vector2(240, 56)
b.focus_mode = Control.FOCUS_NONE
b.clip_text = true
b.pressed.connect(func() -> void: geschlossen.emit())
p.add_child(b)
# --------------------------------------------------------------- Bausteine --
func _label(eltern: Node, text: String, pos: Vector2,
groesse: Vector2 = Vector2(300, 40), farbe: Color = UiTheme.CREAM) -> Label:
var l := Label.new()
l.text = text
l.position = pos
l.size = groesse
l.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
l.clip_text = true
l.add_theme_color_override("font_color", farbe)
eltern.add_child(l)
return l
func _icon(eltern: Node, id: String, pos: Vector2) -> void:
var t := _item_textur(id)
if t == null:
return
var icon := TextureRect.new()
icon.texture = t
icon.position = pos
icon.size = Vector2(64, 64)
icon.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
icon.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
icon.texture_filter = CanvasItem.TEXTURE_FILTER_NEAREST
icon.mouse_filter = Control.MOUSE_FILTER_IGNORE
eltern.add_child(icon)
func _item_textur(id: String) -> Texture2D:
var pfad := "res://assets/objects/item_%s_64.png" % id
if not ResourceLoader.exists(pfad):
return null
return load(pfad)
func _item_name(id: String) -> String:
if id.is_empty():
return "—"
var item: Dictionary = Content.entry("items", id)
return str(item.get("name", id))
func _item_typ(id: String) -> String:
return str(Content.entry("items", id).get("type", ""))
func _leeren(node: Node) -> void:
for kind in node.get_children():
node.remove_child(kind)
kind.queue_free()