Death Chests
This commit is contained in:
@@ -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})
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
@@ -0,0 +1 @@
|
||||
uid://dw06v6bwbcuph
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user