3d-rpg/src/scripts/ui/inventory.gd
2025-06-01 16:04:30 -06:00

128 lines
3.6 KiB
GDScript

class_name Inventory extends Control
const MIN_ARMOR_RATING: float = 0.0
const MAX_ARMOR_RATING: float = 80.0
@onready var level_label: Label = %LevelLabel
@onready var strength_label: Label = %StrengthScore
@onready var agility_label: Label = %AgilityScore
@onready var speed_label: Label = %SpeedScore
@onready var endurance_label: Label = %EnduranceScore
@onready var attack_damage_value: Label = %AttackDamageValue
@onready var armor_protection_value: Label = %ArmorProtectionValue
@onready var items_grid: GridContainer = %ItemsGrid
@onready var gold_label: Label = %GoldLabel
@onready var gold: int = 0:
set(value):
gold = value
gold_label.text = "%dg" % gold
@onready var weapon_slot: CenterContainer = %WeaponSlot
@onready var shield_slot: CenterContainer = %ShieldSlot
@onready var armor_slot: CenterContainer = %ArmorSlot
@export var player: Player
func _ready() -> void:
update_stats()
load_items_from_persistant_data()
func update_stats() -> void:
level_label.text = "Level %d" % player.stats.level
strength_label.text = "%d" % player.stats.strength.score
agility_label.text = "%d" % player.stats.agility.score
speed_label.text = "%d" % player.stats.speed.score
endurance_label.text = "%d" % player.stats.endurance.score
func update_gear_stats() -> void:
attack_damage_value.text = "%d" % [player.stats.strength.get_modifier() + get_weapon_damage()]
armor_protection_value.text = "%d%%" % get_armor_protection()
func get_weapon_damage() -> float:
var damage: float = 0.0
var weapon: WeaponIcon = get_weapon()
if weapon:
damage += weapon.power
return damage
func get_armor_protection() -> float:
var protection: float = 0.0
var armor: ArmorIcon = get_armor()
if armor:
protection += armor.protection
var shield: ShieldIcon = get_shield()
if shield:
protection += shield.protection
protection = clampf(protection, MIN_ARMOR_RATING, MAX_ARMOR_RATING)
return protection
func add_item(item: ItemIcon) -> void:
disconnect_item_connections(item)
if item is CurrencyIcon:
gold += item.value
item.queue_free()
else:
items_grid.add_child(item)
item.interact.connect(equip_item)
func equip_item(item: ItemIcon) -> void:
var item_slot: CenterContainer
if item is WeaponIcon:
item_slot = weapon_slot
elif item is ArmorIcon:
item_slot = armor_slot
elif item is ShieldIcon:
item_slot = shield_slot
else:
return
for child in item_slot.get_children():
child.get_parent().remove_child(child)
add_item(child)
item.get_parent().remove_child(item)
item_slot.add_child(item)
if item is WeaponIcon:
player.rig.equip_weapon(item.item_model)
# elif item is ArmorIcon:
# player.rig.equip_weapon(item.item_model)
elif item is ShieldIcon:
player.rig.equip_offhand(item.item_model)
else:
return
disconnect_item_connections(item)
update_gear_stats()
func disconnect_item_connections(item: ItemIcon) -> void:
for connection: Dictionary in item.interact.get_connections():
item.interact.disconnect(connection.callable)
func get_weapon() -> WeaponIcon:
if weapon_slot.get_child_count() != 1:
return null
return weapon_slot.get_child(0)
func get_armor() -> ArmorIcon:
if armor_slot.get_child_count() != 1:
return null
return armor_slot.get_child(0)
func get_shield() -> ShieldIcon:
if shield_slot.get_child_count() != 1:
return null
return shield_slot.get_child(0)
func load_items_from_persistant_data() -> void:
await player.ready
for item: ItemIcon in PersistentData.get_inventory():
item.get_parent().remove_child(item)
add_item(item)
for item: ItemIcon in PersistentData.get_equipped_items():
add_item(item)
equip_item(item)
gold = PersistentData.gold