359 lines
12 KiB
GDScript
359 lines
12 KiB
GDScript
extends CanvasLayer
|
|
|
|
@onready var hp_bar = $TopLeft/HPBar
|
|
@onready var hp_text = $TopLeft/HPBar/Label
|
|
@onready var mp_bar = $TopLeft/MPBar
|
|
@onready var mp_text = $TopLeft/MPBar/Label
|
|
@onready var sta_bar = $TopLeft/STABar
|
|
@onready var sta_text = $TopLeft/STABar/Label
|
|
|
|
@onready var tier_label = $TopCenter/DungeonInfo/TierLabel
|
|
@onready var mobs_label = $TopCenter/DungeonInfo/MobsLabel
|
|
|
|
@onready var minimap_container = $TopRight/Minimap
|
|
@onready var minimap_texture = $TopRight/Minimap/MinimapTexture
|
|
@onready var event_log = $TopRight/EventLog
|
|
|
|
@onready var hotbar_container = $BottomCenter/Hotbar
|
|
|
|
@onready var escape_indicator = $EscapeIndicator
|
|
@onready var escape_countdown = $EscapeIndicator/Countdown
|
|
|
|
var hotbar_slots: Array = []
|
|
var minimap_image: Image = null
|
|
var minimap_tex: ImageTexture = null
|
|
|
|
const MINIMAP_SIZE := 120
|
|
const MINIMAP_SCALE := 3 # How many world pixels per minimap pixel
|
|
|
|
func _ready():
|
|
_setup_hotbar()
|
|
_setup_minimap()
|
|
call_deferred("_connect_gamestate")
|
|
|
|
func _connect_gamestate():
|
|
if GameState:
|
|
GameState.dungeon_state_updated.connect(_update_resources)
|
|
GameState.inventory_updated.connect(_update_hotbar)
|
|
GameState.dungeon_entered.connect(_on_dungeon_entered)
|
|
GameState.loot_received.connect(_on_loot_received)
|
|
if UpdateManager:
|
|
UpdateManager.file_downloaded.connect(_on_file_downloaded)
|
|
UpdateManager.update_completed.connect(_on_updates_completed)
|
|
|
|
func _on_file_downloaded(file_path: String):
|
|
# If an item texture was downloaded, refresh hotbar to show it
|
|
if file_path.begins_with("textures/items/"):
|
|
_update_hotbar()
|
|
|
|
func _on_updates_completed(_count: int):
|
|
# Refresh hotbar after all updates complete
|
|
_update_hotbar()
|
|
|
|
func _on_dungeon_entered():
|
|
_generate_minimap()
|
|
_update_resources()
|
|
|
|
func _setup_hotbar():
|
|
if not hotbar_container:
|
|
return
|
|
for i in range(8):
|
|
var slot = Button.new()
|
|
slot.custom_minimum_size = Vector2(48, 48)
|
|
slot.text = str(i + 1)
|
|
slot.set_meta("hotbar_slot", i)
|
|
|
|
var style = StyleBoxFlat.new()
|
|
style.bg_color = Color(0.15, 0.15, 0.2, 0.9)
|
|
style.border_color = Color(0.3, 0.3, 0.4)
|
|
style.set_border_width_all(2)
|
|
style.set_corner_radius_all(4)
|
|
slot.add_theme_stylebox_override("normal", style)
|
|
|
|
slot.pressed.connect(_on_hotbar_pressed.bind(i))
|
|
slot.gui_input.connect(_on_hotbar_slot_input.bind(i))
|
|
hotbar_container.add_child(slot)
|
|
hotbar_slots.append(slot)
|
|
|
|
# Connect to DragManager for drops
|
|
if DragManager:
|
|
DragManager.item_dropped.connect(_on_item_dropped)
|
|
|
|
func _on_hotbar_slot_input(event: InputEvent, slot_index: int):
|
|
if not event is InputEventMouseButton:
|
|
return
|
|
|
|
# Check if dropping from DragManager
|
|
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
|
|
if DragManager and DragManager.is_dragging:
|
|
DragManager.drop_on("hotbar", slot_index)
|
|
|
|
# Right click to clear hotbar slot
|
|
elif event.button_index == MOUSE_BUTTON_RIGHT and event.pressed:
|
|
GameState.assign_hotbar(slot_index, -1) # -1 to clear
|
|
_update_hotbar()
|
|
|
|
func _on_item_dropped(data: Dictionary, target_type: String, target_slot: int):
|
|
if target_type != "hotbar":
|
|
return
|
|
|
|
var item = data.get("item", {})
|
|
var source_slot = data.get("source_slot", -1)
|
|
|
|
# Only consumables can go in hotbar
|
|
var item_type = item.get("type", item.get("Type", ""))
|
|
if item_type != "Consumable":
|
|
print("Only consumables can be assigned to hotbar")
|
|
return
|
|
|
|
if source_slot >= 0:
|
|
GameState.assign_hotbar(target_slot, source_slot)
|
|
_update_hotbar()
|
|
print("Assigned inventory slot %d to hotbar slot %d" % [source_slot, target_slot])
|
|
|
|
func _setup_minimap():
|
|
minimap_image = Image.create(MINIMAP_SIZE, MINIMAP_SIZE, false, Image.FORMAT_RGBA8)
|
|
minimap_tex = ImageTexture.create_from_image(minimap_image)
|
|
if minimap_texture:
|
|
minimap_texture.texture = minimap_tex
|
|
|
|
func _generate_minimap():
|
|
if not GameState or GameState.dungeon.tiles.is_empty():
|
|
return
|
|
|
|
minimap_image.fill(Color(0.1, 0.1, 0.15, 0.8))
|
|
|
|
var tiles = GameState.dungeon.tiles
|
|
var width = GameState.dungeon.width
|
|
var height = GameState.dungeon.height
|
|
|
|
# Scale factor to fit dungeon in minimap
|
|
var scale_x = float(MINIMAP_SIZE) / float(width)
|
|
var scale_y = float(MINIMAP_SIZE) / float(height)
|
|
var scale = min(scale_x, scale_y)
|
|
|
|
# Offset to center the minimap
|
|
var offset_x = int((MINIMAP_SIZE - width * scale) / 2)
|
|
var offset_y = int((MINIMAP_SIZE - height * scale) / 2)
|
|
|
|
for x in range(width):
|
|
if x >= tiles.size():
|
|
continue
|
|
for y in range(height):
|
|
if y >= tiles[x].size():
|
|
continue
|
|
|
|
var mx = int(x * scale) + offset_x
|
|
var my = int(y * scale) + offset_y
|
|
|
|
if mx >= 0 and mx < MINIMAP_SIZE and my >= 0 and my < MINIMAP_SIZE:
|
|
var color = Color(0.3, 0.3, 0.35) if tiles[x][y] == 1 else Color(0.5, 0.45, 0.4)
|
|
minimap_image.set_pixel(mx, my, color)
|
|
|
|
minimap_tex.update(minimap_image)
|
|
|
|
func _process(_delta):
|
|
_update_escape_indicator()
|
|
_update_minimap_entities()
|
|
|
|
func _update_minimap_entities():
|
|
if not GameState or not minimap_tex or GameState.dungeon.tiles.is_empty():
|
|
return
|
|
|
|
# Start fresh each frame (regenerate base map periodically would be better for perf)
|
|
var width = GameState.dungeon.width
|
|
var height = GameState.dungeon.height
|
|
var scale = min(float(MINIMAP_SIZE) / float(width), float(MINIMAP_SIZE) / float(height))
|
|
var offset_x = int((MINIMAP_SIZE - width * scale) / 2)
|
|
var offset_y = int((MINIMAP_SIZE - height * scale) / 2)
|
|
|
|
# Draw player position
|
|
var px = GameState.self_player.get("x", 0) / 32.0 # Convert from pixels to tiles
|
|
var py = GameState.self_player.get("y", 0) / 32.0
|
|
var player_mx = int(px * scale) + offset_x
|
|
var player_my = int(py * scale) + offset_y
|
|
|
|
# Draw a small dot for player (green)
|
|
for dx in range(-1, 2):
|
|
for dy in range(-1, 2):
|
|
var draw_x = player_mx + dx
|
|
var draw_y = player_my + dy
|
|
if draw_x >= 0 and draw_x < MINIMAP_SIZE and draw_y >= 0 and draw_y < MINIMAP_SIZE:
|
|
minimap_image.set_pixel(draw_x, draw_y, Color.GREEN)
|
|
|
|
# Draw mobs (red dots)
|
|
for mob_id in GameState.mobs:
|
|
var mob = GameState.mobs[mob_id]
|
|
var mx = mob.get("x", mob.get("X", 0)) / 32.0
|
|
var my = mob.get("y", mob.get("Y", 0)) / 32.0
|
|
var mob_mx = int(mx * scale) + offset_x
|
|
var mob_my = int(my * scale) + offset_y
|
|
if mob_mx >= 0 and mob_mx < MINIMAP_SIZE and mob_my >= 0 and mob_my < MINIMAP_SIZE:
|
|
minimap_image.set_pixel(mob_mx, mob_my, Color.RED)
|
|
|
|
minimap_tex.update(minimap_image)
|
|
|
|
func _update_resources():
|
|
if not GameState:
|
|
return
|
|
|
|
var p = GameState.self_player
|
|
|
|
if hp_bar:
|
|
hp_bar.max_value = p.get("max_health", 100)
|
|
hp_bar.value = p.get("health", 100)
|
|
if hp_text:
|
|
hp_text.text = "%d/%d" % [int(p.get("health", 0)), int(p.get("max_health", 100))]
|
|
|
|
if mp_bar:
|
|
mp_bar.max_value = p.get("max_mana", 50)
|
|
mp_bar.value = p.get("mana", 50)
|
|
if mp_text:
|
|
mp_text.text = "%d/%d" % [int(p.get("mana", 0)), int(p.get("max_mana", 50))]
|
|
|
|
if sta_bar:
|
|
sta_bar.max_value = p.get("max_stamina", 55)
|
|
sta_bar.value = p.get("stamina", 55)
|
|
if sta_text:
|
|
sta_text.text = "%d/%d" % [int(p.get("stamina", 0)), int(p.get("max_stamina", 55))]
|
|
|
|
if tier_label:
|
|
tier_label.text = "Tier %d" % GameState.dungeon.tier
|
|
if mobs_label:
|
|
mobs_label.text = "Mobs: %d" % GameState.mobs.size()
|
|
|
|
func _update_hotbar():
|
|
if not GameState:
|
|
return
|
|
for i in range(8):
|
|
if i >= hotbar_slots.size():
|
|
break
|
|
var slot = hotbar_slots[i]
|
|
|
|
# Clear existing children (sprites, labels)
|
|
for child in slot.get_children():
|
|
child.queue_free()
|
|
|
|
var hotbar_entry = GameState.hotbar[i]
|
|
|
|
if hotbar_entry and hotbar_entry is Dictionary and hotbar_entry.has("inventory_slot"):
|
|
var inv_slot = hotbar_entry.inventory_slot
|
|
if inv_slot < GameState.inventory.size():
|
|
var item = GameState.inventory[inv_slot]
|
|
if item and item.get("type") == "Consumable":
|
|
slot.text = ""
|
|
|
|
# Try to load sprite from CDN
|
|
var sprite_path = item.get("spritePath", item.get("SpritePath", ""))
|
|
var has_sprite = false
|
|
|
|
if sprite_path and not sprite_path.is_empty() and UpdateManager:
|
|
var texture = UpdateManager.load_texture(sprite_path)
|
|
if texture:
|
|
var sprite = TextureRect.new()
|
|
sprite.texture = texture
|
|
sprite.expand_mode = TextureRect.EXPAND_FIT_WIDTH_PROPORTIONAL
|
|
sprite.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
|
|
sprite.custom_minimum_size = Vector2(28, 28)
|
|
sprite.position = Vector2(6, 6) # Center in slot
|
|
|
|
# Apply tint if specified
|
|
var tint = item.get("tintColor", item.get("TintColor", ""))
|
|
if tint and not tint.is_empty():
|
|
sprite.modulate = Color.from_string(tint, Color.WHITE)
|
|
|
|
slot.add_child(sprite)
|
|
has_sprite = true
|
|
|
|
# Fall back to emoji if no sprite
|
|
if not has_sprite:
|
|
slot.text = _get_item_emoji(item)
|
|
|
|
# Add stack count label
|
|
var quantity = item.get("quantity", item.get("Quantity", 1))
|
|
if quantity > 1:
|
|
var count_label = Label.new()
|
|
count_label.text = str(quantity)
|
|
count_label.add_theme_font_size_override("font_size", 9)
|
|
count_label.add_theme_color_override("font_color", Color.WHITE)
|
|
count_label.add_theme_color_override("font_shadow_color", Color.BLACK)
|
|
count_label.add_theme_constant_override("shadow_offset_x", 1)
|
|
count_label.add_theme_constant_override("shadow_offset_y", 1)
|
|
count_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
|
|
count_label.position = Vector2(slot.custom_minimum_size.x - 18, slot.custom_minimum_size.y - 14)
|
|
slot.add_child(count_label)
|
|
continue
|
|
|
|
slot.text = str(i + 1)
|
|
|
|
func _get_item_emoji(item: Dictionary) -> String:
|
|
var item_name = item.get("name", "").to_lower()
|
|
if "health" in item_name: return "❤️"
|
|
if "mana" in item_name: return "💙"
|
|
if "escape" in item_name: return "🏃"
|
|
return "🧪"
|
|
|
|
func _on_hotbar_pressed(index: int):
|
|
if not GameState:
|
|
return
|
|
var hotbar_entry = GameState.hotbar[index]
|
|
if hotbar_entry and hotbar_entry is Dictionary and hotbar_entry.has("inventory_slot"):
|
|
GameState.send_use_item(hotbar_entry.inventory_slot)
|
|
|
|
func _update_escape_indicator():
|
|
if not escape_indicator or not GameState:
|
|
return
|
|
escape_indicator.visible = GameState.is_escaping
|
|
if GameState.is_escaping:
|
|
var elapsed = (Time.get_ticks_msec() - GameState.escape_start_time) / 1000.0
|
|
var remaining = max(0, 20 - elapsed)
|
|
if escape_countdown:
|
|
escape_countdown.text = "%.1f" % remaining
|
|
|
|
func _on_loot_received(items: Array):
|
|
print("[HUD] Loot received! %d items" % items.size())
|
|
for item in items:
|
|
var item_name = item.get("name", item.get("Name", "Unknown"))
|
|
var rarity = item.get("rarity", item.get("Rarity", "Common"))
|
|
var quantity = item.get("quantity", item.get("Quantity", 1))
|
|
|
|
var color = _get_rarity_color(rarity)
|
|
var text = "+%d %s" % [quantity, item_name] if quantity > 1 else "+ %s" % item_name
|
|
add_log_entry(text, color)
|
|
|
|
func _get_rarity_color(rarity: String) -> Color:
|
|
match rarity.to_lower():
|
|
"common": return Color(0.7, 0.7, 0.7)
|
|
"uncommon": return Color(0.18, 0.8, 0.44)
|
|
"rare": return Color(0.2, 0.6, 0.86)
|
|
"epic": return Color(0.61, 0.35, 0.71)
|
|
"legendary": return Color(0.95, 0.61, 0.07)
|
|
_: return Color.WHITE
|
|
|
|
func add_log_entry(text: String, color: Color):
|
|
if not event_log:
|
|
print("[HUD] No event_log found for loot display!")
|
|
return
|
|
|
|
var label = RichTextLabel.new()
|
|
label.bbcode_enabled = true
|
|
label.fit_content = true
|
|
label.scroll_active = false
|
|
label.text = "[color=#%s]%s[/color]" % [color.to_html(false), text]
|
|
event_log.add_child(label)
|
|
|
|
# Auto-remove after 5 seconds
|
|
var timer = get_tree().create_timer(5.0)
|
|
timer.timeout.connect(func():
|
|
if is_instance_valid(label):
|
|
label.queue_free()
|
|
)
|
|
|
|
# Keep log from getting too long (use call_deferred to avoid modifying during iteration)
|
|
if event_log.get_child_count() > 10:
|
|
var to_remove = event_log.get_child_count() - 10
|
|
for i in range(to_remove):
|
|
var child = event_log.get_child(0)
|
|
if is_instance_valid(child):
|
|
child.queue_free()
|