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 # Mobile UI buttons var inventory_button: Button = null var crafting_button: Button = null var stats_button: Button = null const MINIMAP_SIZE := 120 const MINIMAP_SCALE := 3 # How many world pixels per minimap pixel func _ready(): add_to_group("hud") _setup_hotbar() _setup_minimap() _setup_mobile_buttons() call_deferred("_connect_gamestate") # Apply mobile controls visibility based on settings var mobile_enabled = _get_mobile_controls_enabled() set_mobile_controls_visible(mobile_enabled) 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 (on press or release) if event.button_index == MOUSE_BUTTON_LEFT: if DragManager and DragManager.is_dragging: # Drop on either press or release to catch the item DragManager.drop_on("hotbar", slot_index) elif not event.pressed: # Not dragging, clicked hotbar slot - use the item _on_hotbar_pressed(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 _setup_mobile_buttons(): # Create inventory button (bottom right) inventory_button = Button.new() inventory_button.text = "Inv" inventory_button.custom_minimum_size = Vector2(60, 60) var inv_style = StyleBoxFlat.new() inv_style.bg_color = Color(0.2, 0.3, 0.5, 0.9) inv_style.border_color = Color(0.4, 0.5, 0.7) inv_style.set_border_width_all(2) inv_style.set_corner_radius_all(8) inventory_button.add_theme_stylebox_override("normal", inv_style) inventory_button.pressed.connect(_on_mobile_inventory_pressed) add_child(inventory_button) # Position bottom right, above joystick inventory_button.set_anchors_and_offsets_preset(Control.PRESET_BOTTOM_RIGHT) inventory_button.offset_left = -70 inventory_button.offset_top = -140 inventory_button.offset_right = -10 inventory_button.offset_bottom = -80 # Create crafting button (bottom right, above inventory) crafting_button = Button.new() crafting_button.text = "Craft" crafting_button.custom_minimum_size = Vector2(60, 60) var craft_style = StyleBoxFlat.new() craft_style.bg_color = Color(0.5, 0.3, 0.2, 0.9) craft_style.border_color = Color(0.7, 0.5, 0.4) craft_style.set_border_width_all(2) craft_style.set_corner_radius_all(8) crafting_button.add_theme_stylebox_override("normal", craft_style) crafting_button.pressed.connect(_on_mobile_crafting_pressed) add_child(crafting_button) # Position bottom right, above inventory button crafting_button.set_anchors_and_offsets_preset(Control.PRESET_BOTTOM_RIGHT) crafting_button.offset_left = -70 crafting_button.offset_top = -210 crafting_button.offset_right = -10 crafting_button.offset_bottom = -150 # Create stats button (bottom right, above crafting) stats_button = Button.new() stats_button.text = "Stats" stats_button.custom_minimum_size = Vector2(60, 60) var stats_style = StyleBoxFlat.new() stats_style.bg_color = Color(0.3, 0.5, 0.3, 0.9) stats_style.border_color = Color(0.5, 0.7, 0.5) stats_style.set_border_width_all(2) stats_style.set_corner_radius_all(8) stats_button.add_theme_stylebox_override("normal", stats_style) stats_button.pressed.connect(_on_mobile_stats_pressed) add_child(stats_button) # Position bottom right, above crafting button stats_button.set_anchors_and_offsets_preset(Control.PRESET_BOTTOM_RIGHT) stats_button.offset_left = -70 stats_button.offset_top = -280 stats_button.offset_right = -10 stats_button.offset_bottom = -220 func _on_mobile_inventory_pressed(): # Toggle inventory visibility var dungeon_manager = get_tree().get_first_node_in_group("dungeon_manager") if dungeon_manager and dungeon_manager.has_method("_toggle_inventory"): dungeon_manager._toggle_inventory() func _on_mobile_crafting_pressed(): # Toggle crafting UI visibility var dungeon_manager = get_tree().get_first_node_in_group("dungeon_manager") if dungeon_manager and dungeon_manager.has_method("_toggle_crafting"): dungeon_manager._toggle_crafting() func _on_mobile_stats_pressed(): # Toggle char sheet visibility var dungeon_manager = get_tree().get_first_node_in_group("dungeon_manager") if dungeon_manager and dungeon_manager.has_method("_toggle_char_sheet"): dungeon_manager._toggle_char_sheet() 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 = nearby/visible, dark red = out-of-range) 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: var mob_color = Color.RED if mob.get("_visible", true) else Color(0.5, 0.15, 0.15) minimap_image.set_pixel(mob_mx, mob_my, mob_color) 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: # Show nearby (visible) mob count vs total known for spatial tuning debug var total = GameState.mobs.size() var nearby = 0 for mid in GameState.mobs: if GameState.mobs[mid].get("_visible", true): nearby += 1 if nearby != total: mobs_label.text = "Mobs: %d/%d" % [nearby, total] else: mobs_label.text = "Mobs: %d" % total 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" or item.get("type") == "Weapon" or item.get("type") == "Armor" or item.get("type") == "Accessory"): 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() func _get_mobile_controls_enabled() -> bool: # Check if we're on a touch-capable device (mobile native or web on tablets/phones) var is_touch_device = OS.has_feature("mobile") or DisplayServer.is_touchscreen_available() if is_touch_device: return ProjectSettings.get_setting("application/config/mobile_controls_enabled", true) return ProjectSettings.get_setting("application/config/mobile_controls_enabled", false) func set_mobile_controls_visible(is_visible: bool): # Show/hide mobile UI buttons if inventory_button: inventory_button.visible = is_visible if crafting_button: crafting_button.visible = is_visible if stats_button: stats_button.visible = is_visible # Show/hide touch joystick var joystick = get_node_or_null("TouchJoystick") if joystick: joystick.visible = is_visible func get_hotbar_slots() -> Array: return hotbar_slots