Initial
This commit is contained in:
@@ -0,0 +1,214 @@
|
||||
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 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)
|
||||
|
||||
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)
|
||||
|
||||
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))
|
||||
hotbar_container.add_child(slot)
|
||||
hotbar_slots.append(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]
|
||||
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 = _get_item_emoji(item)
|
||||
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, 5 - elapsed)
|
||||
if escape_countdown:
|
||||
escape_countdown.text = "%.1f" % remaining
|
||||
Reference in New Issue
Block a user