diff --git a/scenes/dungeon/dungeon.tscn b/scenes/dungeon/dungeon.tscn index 1727842..042b195 100644 --- a/scenes/dungeon/dungeon.tscn +++ b/scenes/dungeon/dungeon.tscn @@ -25,6 +25,8 @@ tile_set = ExtResource("2_frwky") [node name="Portals" type="Node2D" parent="Entities"] +[node name="LootChests" type="Node2D" parent="Entities"] + [node name="Player" parent="." instance=ExtResource("2_player")] [node name="Hud" parent="." instance=ExtResource("2_4f38h")] diff --git a/scenes/dungeon/loot_chest.tscn b/scenes/dungeon/loot_chest.tscn new file mode 100644 index 0000000..c7b3d94 --- /dev/null +++ b/scenes/dungeon/loot_chest.tscn @@ -0,0 +1,25 @@ +[gd_scene load_steps=3 format=3 uid="uid://dlootchest123"] + +[ext_resource type="Script" path="res://scripts/dungeon/loot_chest.gd" id="1"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_chest"] +radius = 20.0 + +[node name="LootChest" type="Node2D"] +script = ExtResource("1") + +[node name="Sprite2D" type="Sprite2D" parent="."] +scale = Vector2(2, 2) + +[node name="Area2D" type="Area2D" parent="."] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"] +shape = SubResource("CircleShape2D_chest") + +[node name="Label" type="Label" parent="."] +offset_left = -30 +offset_top = -40 +offset_right = 30 +offset_bottom = -20 +horizontal_alignment = 1 +text = "Loot" diff --git a/scenes/hub/hub_screen.tscn b/scenes/hub/hub_screen.tscn index 368543e..1da3187 100644 --- a/scenes/hub/hub_screen.tscn +++ b/scenes/hub/hub_screen.tscn @@ -43,6 +43,10 @@ layout_mode = 2 [node name="XPBar" type="ProgressBar" parent="Control/HBoxContainer/LeftPanel/CharacterInfo"] layout_mode = 2 +[node name="GoldLabel" type="Label" parent="Control/HBoxContainer/LeftPanel/CharacterInfo"] +layout_mode = 2 +text = "Gold: 0" + [node name="StatsPanel" type="GridContainer" parent="Control/HBoxContainer/LeftPanel"] layout_mode = 2 columns = 3 diff --git a/scenes/ui/death_screen.tscn b/scenes/ui/death_screen.tscn index 4c78473..15e17c0 100644 --- a/scenes/ui/death_screen.tscn +++ b/scenes/ui/death_screen.tscn @@ -1,10 +1,9 @@ -[gd_scene load_steps=2 format=3 uid="uid://death_screen"] +[gd_scene load_steps=2 format=3 uid="uid://cbpaygd2cw45c"] -[ext_resource type="Script" path="res://scripts/ui/death_screen.gd" id="1_death"] +[ext_resource type="Script" uid="uid://cmsghh3o0ta18" path="res://scripts/ui/death_screen.gd" id="1_death"] [node name="DeathScreen" type="CanvasLayer"] layer = 20 -visible = false script = ExtResource("1_death") [node name="ColorRect" type="ColorRect" parent="."] diff --git a/scenes/ui/hud.tscn b/scenes/ui/hud.tscn index 2c78d86..e326d0f 100644 --- a/scenes/ui/hud.tscn +++ b/scenes/ui/hud.tscn @@ -13,6 +13,7 @@ bg_color = Color(1, 0.77254903, 0.11764706, 1) [node name="Hud" type="CanvasLayer"] layer = 5 +visible = false script = ExtResource("1_hud") [node name="TopLeft" type="Control" parent="."] @@ -144,9 +145,11 @@ grow_vertical = 2 stretch_mode = 5 [node name="EventLog" type="VBoxContainer" parent="TopRight"] +clip_contents = true layout_mode = 0 +offset_left = -42.0 offset_top = 134.0 -offset_right = 125.0 +offset_right = 133.0 offset_bottom = 434.0 [node name="BottomCenter" type="Control" parent="."] diff --git a/scripts/autoload/game_state.gd b/scripts/autoload/game_state.gd index 5ee7ec6..1605927 100644 --- a/scripts/autoload/game_state.gd +++ b/scripts/autoload/game_state.gd @@ -10,6 +10,9 @@ signal inventory_updated signal chat_message_received(sender: String, message: String, type: String) signal loot_received(items: Array) signal player_died +signal loot_chest_spawned(chest_data: Dictionary) +signal loot_chest_state_received(chest_data: Dictionary) +signal death_loot_lost(data: Dictionary) # Use constants instead of enum to avoid type issues const STATE_AUTH = 0 @@ -37,7 +40,8 @@ var character: Dictionary = { "dexterity": 1, "max_health": 60, "max_mana": 28, - "max_stamina": 55 + "max_stamina": 55, + "gold": 0 } # Dungeon data @@ -57,6 +61,7 @@ var mobs: Dictionary = {} var chests: Dictionary = {} var dropped_items: Dictionary = {} var portals: Dictionary = {} +var loot_chests: Dictionary = {} # Inventory var inventory: Array = [] @@ -122,6 +127,12 @@ func _on_message_received(opcode: int, data: Dictionary): _handle_death(data) OpCodes.EVENT_LEVELUP: _handle_levelup(data) + OpCodes.LOOT_CHEST_SPAWNED: + _handle_loot_chest_spawned(data) + OpCodes.LOOT_CHEST_STATE: + _handle_loot_chest_state(data) + OpCodes.PLAYER_DEATH_LOOT_LOST: + _handle_death_loot_lost(data) _: print("[GameState] Unhandled opcode: ", opcode) @@ -148,7 +159,8 @@ func _handle_hub_state(data: Dictionary): "dexterity": int(c.get("dexterity", c.get("Dexterity", 1))), "max_health": int(c.get("maxHealth", c.get("MaxHealth", 60))), "max_mana": int(c.get("maxMana", c.get("MaxMana", 28))), - "max_stamina": int(c.get("maxStamina", c.get("MaxStamina", 55))) + "max_stamina": int(c.get("maxStamina", c.get("MaxStamina", 55))), + "gold": int(c.get("gold", c.get("Gold", 0))) } set_state(STATE_HUB) character_updated.emit() @@ -268,6 +280,13 @@ func _handle_dungeon_state(data: Dictionary): var pid = p.get("id", p.get("Id")) portals[pid] = p + # Parse loot chests (personal to this player) + var lchests = data.get("lootChests", data.get("LootChests", [])) + loot_chests.clear() + for lc in lchests: + var lcid = lc.get("id", lc.get("Id")) + loot_chests[lcid] = lc + dungeon_state_updated.emit() func _handle_inventory_state(data: Dictionary): @@ -406,6 +425,7 @@ func _handle_portal_used(_data: Dictionary): chests.clear() dropped_items.clear() portals.clear() + loot_chests.clear() dungeon = { "id": "", "tier": 1, @@ -417,3 +437,33 @@ func _handle_portal_used(_data: Dictionary): escaped.emit() # Server will send HUB_STATE next which will call set_state(STATE_HUB) + +# Loot chest handlers +func _handle_loot_chest_spawned(data: Dictionary): + print("[GameState] Loot chest spawned: ", data) + loot_chest_spawned.emit(data) + +func _handle_loot_chest_state(data: Dictionary): + print("[GameState] Loot chest state received") + loot_chest_state_received.emit(data) + +func _handle_death_loot_lost(data: Dictionary): + print("[GameState] Death loot lost: ", data) + death_loot_lost.emit(data) + +# Loot chest actions +func open_loot_chest(chest_id: int): + Network.send_message(OpCodes.LOOT_CHEST_OPEN, {"ChestId": chest_id}) + +func take_loot_chest_item(chest_id: int, slot_index: int, target_slot: int = -1): + Network.send_message(OpCodes.LOOT_CHEST_TAKE_ITEM, { + "ChestId": chest_id, + "SlotIndex": slot_index, + "TargetSlotIndex": target_slot + }) + +func take_all_loot_chest(chest_id: int): + Network.send_message(OpCodes.LOOT_CHEST_TAKE_ALL, {"ChestId": chest_id}) + +func close_loot_chest(chest_id: int): + Network.send_message(OpCodes.LOOT_CHEST_CLOSE, {"ChestId": chest_id}) diff --git a/scripts/autoload/opcodes.gd b/scripts/autoload/opcodes.gd index 9c79987..bb27d45 100644 --- a/scripts/autoload/opcodes.gd +++ b/scripts/autoload/opcodes.gd @@ -23,6 +23,17 @@ const HOTBAR_ASSIGN = 256 const CHAT_SEND = 260 const CHAT_MESSAGE = 261 +# Loot Chests +const LOOT_CHEST_OPEN = 270 +const LOOT_CHEST_STATE = 271 +const LOOT_CHEST_TAKE_ITEM = 272 +const LOOT_CHEST_TAKE_ALL = 273 +const LOOT_CHEST_CLOSE = 274 +const LOOT_CHEST_SPAWNED = 275 + +# Player Death +const PLAYER_DEATH_LOOT_LOST = 280 + # Dungeon const DUNGEON_ENTER = 300 const DUNGEON_ENTER_RESULT = 301 diff --git a/scripts/dungeon/dungeon_manager.gd b/scripts/dungeon/dungeon_manager.gd index 130fab1..e5fa0c9 100644 --- a/scripts/dungeon/dungeon_manager.gd +++ b/scripts/dungeon/dungeon_manager.gd @@ -15,6 +15,7 @@ const WORLD_TILE_SIZE := TILE_SIZE * TILE_SCALE # 32 pixels per tile in world @onready var chests_container = $Entities/Chests @onready var items_container = $Entities/DroppedItems @onready var portals_container = $Entities/Portals +@onready var loot_chests_container = $Entities/LootChests @onready var tilemap: TileMapLayer = $TileMapLayer @onready var hud = $Hud @onready var inventory_panel = $Inventory @@ -26,6 +27,7 @@ var mob_scene: PackedScene = null var chest_scene: PackedScene = null var item_scene: PackedScene = null var portal_scene: PackedScene = null +var loot_chest_scene: PackedScene = null var aim_angle: float = 0.0 @@ -41,6 +43,8 @@ func _ready(): item_scene = load("res://scenes/dungeon/dropped_item.tscn") if ResourceLoader.exists("res://scenes/dungeon/portal.tscn"): portal_scene = load("res://scenes/dungeon/portal.tscn") + if ResourceLoader.exists("res://scenes/dungeon/loot_chest.tscn"): + loot_chest_scene = load("res://scenes/dungeon/loot_chest.tscn") # Defer GameState connections call_deferred("_connect_gamestate") @@ -246,6 +250,8 @@ func _on_state_updated(): _update_entities(items_container, GameState.dropped_items, item_scene) if portal_scene and portals_container: _update_entities(portals_container, GameState.portals, portal_scene) + if loot_chest_scene and loot_chests_container: + _update_loot_chests() func _update_entities(container: Node2D, data: Dictionary, scene: PackedScene): if scene == null or container == null: @@ -300,3 +306,29 @@ func _use_hotbar_slot(index: int): var hotbar_entry = GameState.hotbar[index] if hotbar_entry and hotbar_entry.has("inventory_slot"): GameState.send_use_item(hotbar_entry.inventory_slot) + +func _update_loot_chests(): + if loot_chest_scene == null or loot_chests_container == null: + return + + var seen_ids = [] + + for id in GameState.loot_chests: + seen_ids.append(id) + var chest_data = GameState.loot_chests[id] + var chest_node = _find_entity_by_id(loot_chests_container, id) + + if chest_node == null: + chest_node = loot_chest_scene.instantiate() + chest_node.set_meta("entity_id", id) + loot_chests_container.add_child(chest_node) + + if chest_node.has_method("setup"): + chest_node.setup(chest_data) + + # Remove chests that no longer exist + for child in loot_chests_container.get_children(): + if child.has_meta("entity_id"): + var id = child.get_meta("entity_id") + if id not in seen_ids: + child.queue_free() diff --git a/scripts/dungeon/loot_chest.gd b/scripts/dungeon/loot_chest.gd new file mode 100644 index 0000000..cabbd63 --- /dev/null +++ b/scripts/dungeon/loot_chest.gd @@ -0,0 +1,74 @@ +extends Node2D + +var chest_id: int = 0 +var item_count: int = 0 +var gold: int = 0 +var is_open: bool = false + +@onready var sprite = $Sprite2D +@onready var area = $Area2D +@onready var label = $Label + +var _time: float = 0.0 + +func _ready(): + # Gold glow effect + modulate = Color(1.0, 0.85, 0.3) + + # Connect signals + area.body_entered.connect(_on_body_entered) + area.input_event.connect(_on_input_event) + + _update_label() + +func _process(delta: float): + _time += delta + + # Pulsing glow effect + var pulse = 0.8 + sin(_time * 3.0) * 0.2 + modulate = Color(1.0 * pulse, 0.85 * pulse, 0.3) + + # Slight hover animation + sprite.position.y = sin(_time * 2.0) * 2.0 + +func setup(data: Dictionary): + chest_id = data.get("id", data.get("Id", 0)) + item_count = data.get("itemCount", data.get("ItemCount", 0)) + gold = data.get("gold", data.get("Gold", 0)) + is_open = data.get("isOpen", data.get("IsOpen", false)) + + position.x = data.get("x", data.get("X", 0)) + position.y = data.get("y", data.get("Y", 0)) + + _update_label() + +func _update_label(): + var text_parts = [] + if item_count > 0: + text_parts.append("%d items" % item_count) + if gold > 0: + text_parts.append("%d gold" % gold) + + if text_parts.size() > 0: + label.text = ", ".join(text_parts) + else: + label.text = "Empty" + +func _on_body_entered(body: Node2D): + # When player walks into chest, open it + if body.name == "Player": + _open_chest() + +func _on_input_event(_viewport: Node, event: InputEvent, _shape_idx: int): + if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT: + # Check distance to player + var player = get_parent().get_node_or_null("../Player") + if player: + var dist = position.distance_to(player.position) + if dist <= 96: + _open_chest() + else: + print("Too far from loot chest") + +func _open_chest(): + GameState.open_loot_chest(chest_id) diff --git a/scripts/dungeon/loot_chest.gd.uid b/scripts/dungeon/loot_chest.gd.uid new file mode 100644 index 0000000..7f3debb --- /dev/null +++ b/scripts/dungeon/loot_chest.gd.uid @@ -0,0 +1 @@ +uid://dw06v6bwbcuph diff --git a/scripts/ui/hub_ui.gd b/scripts/ui/hub_ui.gd index c11a16b..c69e103 100644 --- a/scripts/ui/hub_ui.gd +++ b/scripts/ui/hub_ui.gd @@ -3,6 +3,7 @@ extends CanvasLayer @onready var name_label = $Control/HBoxContainer/LeftPanel/CharacterInfo/NameLabel @onready var level_label = $Control/HBoxContainer/LeftPanel/CharacterInfo/LevelLabel @onready var xp_bar = $Control/HBoxContainer/LeftPanel/CharacterInfo/XPBar +@onready var gold_label = $Control/HBoxContainer/LeftPanel/CharacterInfo/GoldLabel @onready var str_value = $Control/HBoxContainer/LeftPanel/StatsPanel/StrValue @onready var int_value = $Control/HBoxContainer/LeftPanel/StatsPanel/IntValue @@ -46,6 +47,10 @@ func _update_ui(): xp_bar.max_value = c.xp_next xp_bar.value = c.xp + # Gold display + if gold_label: + gold_label.text = "Gold: %d" % c.get("gold", 0) + str_value.text = str(c.strength) int_value.text = str(c.intelligence) wis_value.text = str(c.wisdom)