Mob visuals
This commit is contained in:
+179
-16
@@ -2,7 +2,9 @@ extends Node2D
|
||||
|
||||
## Mob Visual
|
||||
## Renders mobs using sprite sheets from CDN with JSON animation config
|
||||
## Falls back to simple shapes if assets aren't available
|
||||
## Supports two formats:
|
||||
## - Legacy: separate sprite strip per animation
|
||||
## - New: single sprite sheet with frame sequences
|
||||
|
||||
var fallback_sprite: Sprite2D = null
|
||||
var name_label: Label = null
|
||||
@@ -56,6 +58,8 @@ func _ready():
|
||||
func update_data(data: Dictionary):
|
||||
mob_data = data
|
||||
|
||||
var mob_id = data.get("id", data.get("Id", "unknown"))
|
||||
|
||||
# Update name
|
||||
var mname = data.get("name", data.get("Name", "Mob"))
|
||||
if name_label:
|
||||
@@ -70,6 +74,7 @@ func update_data(data: Dictionary):
|
||||
|
||||
# Only process sprite config on first update or if it changed
|
||||
if not _initialized:
|
||||
print("[MobVisual] First init for mob %s (%s)" % [mname, mob_id])
|
||||
var sprite_config = data.get("spriteConfig", data.get("SpriteConfig", null))
|
||||
|
||||
if sprite_config != null and sprite_config is Dictionary and not sprite_config.is_empty():
|
||||
@@ -79,6 +84,10 @@ func update_data(data: Dictionary):
|
||||
else:
|
||||
_setup_fallback(data)
|
||||
|
||||
# Apply tint on first setup
|
||||
var tint = data.get("tintColor", data.get("TintColor", "#ffffff"))
|
||||
_apply_tint(tint)
|
||||
|
||||
_initialized = true
|
||||
|
||||
# Update visual state
|
||||
@@ -86,11 +95,6 @@ func update_data(data: Dictionary):
|
||||
var rotation = data.get("rotation", data.get("Rotation", 0.0))
|
||||
_update_animation_state(state, rotation)
|
||||
|
||||
# Apply tint (only on first setup)
|
||||
if not _initialized:
|
||||
var tint = data.get("tintColor", data.get("TintColor", "#ffffff"))
|
||||
_apply_tint(tint)
|
||||
|
||||
# Apply scale
|
||||
var mob_scale = data.get("scale", data.get("Scale", 1.0))
|
||||
if mob_scale == null or mob_scale <= 0:
|
||||
@@ -103,7 +107,136 @@ func update_data(data: Dictionary):
|
||||
|
||||
func _build_animated_sprite():
|
||||
## Build AnimatedSprite2D from sprite config
|
||||
var mname = mob_data.get("name", "unknown")
|
||||
## Detects format and uses appropriate builder
|
||||
|
||||
# Check for new format (has "sheets" key)
|
||||
if _sprite_config.has("sheets"):
|
||||
_build_from_new_format()
|
||||
else:
|
||||
# Legacy format (has "sprites" key)
|
||||
_build_from_legacy_format()
|
||||
|
||||
func _build_from_new_format():
|
||||
## New format: sprite sheets with frame sequences
|
||||
## {
|
||||
## "sheets": { "main": { "path": "...", "width": 512, ... } },
|
||||
## "animations": { "idle_down": { "sheet": "main", "frames": [0,1,2,3], ... } }
|
||||
## }
|
||||
|
||||
var sheets = _sprite_config.get("sheets", {})
|
||||
var animations = _sprite_config.get("animations", {})
|
||||
|
||||
if sheets.is_empty() or animations.is_empty():
|
||||
print("[MobVisual] No sheets or animations in config")
|
||||
_setup_fallback(mob_data)
|
||||
return
|
||||
|
||||
# Check if all sheet textures are cached, request download if not
|
||||
var all_cached = true
|
||||
var missing_paths = []
|
||||
for sheet_key in sheets:
|
||||
var sheet_info = sheets[sheet_key]
|
||||
var path = sheet_info.get("path", "")
|
||||
if path != "":
|
||||
if UpdateManager and not UpdateManager.has_file(path):
|
||||
all_cached = false
|
||||
missing_paths.append(path)
|
||||
# Request download if UpdateManager supports it
|
||||
if UpdateManager.has_method("request_file"):
|
||||
UpdateManager.request_file(path)
|
||||
|
||||
if not all_cached:
|
||||
print("[MobVisual] Missing textures, using fallback: ", missing_paths)
|
||||
_setup_fallback(mob_data)
|
||||
return
|
||||
|
||||
# Create or reuse AnimatedSprite2D
|
||||
if not _animated_sprite:
|
||||
_animated_sprite = AnimatedSprite2D.new()
|
||||
_animated_sprite.name = "MobAnimatedSprite"
|
||||
add_child(_animated_sprite)
|
||||
|
||||
# Build SpriteFrames
|
||||
var sprite_frames = SpriteFrames.new()
|
||||
|
||||
# Load all sheet textures
|
||||
var sheet_textures: Dictionary = {}
|
||||
for sheet_key in sheets:
|
||||
var sheet_info = sheets[sheet_key]
|
||||
var path = sheet_info.get("path", "")
|
||||
if path != "":
|
||||
var texture = UpdateManager.load_texture(path)
|
||||
if texture:
|
||||
sheet_textures[sheet_key] = {
|
||||
"texture": texture,
|
||||
"width": sheet_info.get("width", texture.get_width()),
|
||||
"height": sheet_info.get("height", texture.get_height()),
|
||||
"frameWidth": sheet_info.get("frameWidth", 64),
|
||||
"frameHeight": sheet_info.get("frameHeight", 64)
|
||||
}
|
||||
|
||||
# Build each animation
|
||||
for anim_name in animations:
|
||||
var anim_config = animations[anim_name]
|
||||
var sheet_key = anim_config.get("sheet", "")
|
||||
|
||||
if not sheet_textures.has(sheet_key):
|
||||
continue
|
||||
|
||||
var sheet_data = sheet_textures[sheet_key]
|
||||
var texture = sheet_data["texture"]
|
||||
var frame_width: int = int(sheet_data["frameWidth"])
|
||||
var frame_height: int = int(sheet_data["frameHeight"])
|
||||
var sheet_width: int = int(sheet_data["width"])
|
||||
var columns: int = int(sheet_width / frame_width) if frame_width > 0 else 1
|
||||
|
||||
var frames = anim_config.get("frames", [0])
|
||||
var fps = anim_config.get("fps", 8.0)
|
||||
var loop = anim_config.get("loop", true)
|
||||
var flip_x = anim_config.get("flipX", false)
|
||||
var flip_y = anim_config.get("flipY", false)
|
||||
|
||||
# Add animation
|
||||
sprite_frames.add_animation(anim_name)
|
||||
sprite_frames.set_animation_speed(anim_name, fps)
|
||||
sprite_frames.set_animation_loop(anim_name, loop)
|
||||
|
||||
# Add frames from sprite sheet using frame indices
|
||||
for frame_idx in frames:
|
||||
var idx: int = int(frame_idx) # Convert float to int
|
||||
var col: int = idx % columns
|
||||
var row: int = idx / columns
|
||||
|
||||
var atlas = AtlasTexture.new()
|
||||
atlas.atlas = texture
|
||||
atlas.region = Rect2(col * frame_width, row * frame_height, frame_width, frame_height)
|
||||
|
||||
sprite_frames.add_frame(anim_name, atlas)
|
||||
|
||||
_animated_sprite.sprite_frames = sprite_frames
|
||||
_animated_sprite.visible = true
|
||||
_animated_sprite.z_index = 10
|
||||
_animated_sprite.modulate = Color.WHITE
|
||||
_animated_sprite.position = Vector2.ZERO
|
||||
_using_fallback = false
|
||||
|
||||
print("[MobVisual] NEW FORMAT - Built sprite with animations: %s" % [sprite_frames.get_animation_names()])
|
||||
|
||||
# Hide fallback
|
||||
if fallback_sprite:
|
||||
fallback_sprite.visible = false
|
||||
|
||||
# Play default animation
|
||||
_update_animation_state(_current_state, 0.0)
|
||||
|
||||
func _build_from_legacy_format():
|
||||
## Legacy format: separate sprite strip per animation
|
||||
## {
|
||||
## "sprites": { "idle_down": "path/to/idle_down.png", ... },
|
||||
## "animations": { "idle_down": { "frames": 4, "loop": true } },
|
||||
## "frameWidth": 64, "frameHeight": 64, "fps": 8
|
||||
## }
|
||||
|
||||
var sprites = _sprite_config.get("sprites", {})
|
||||
|
||||
if sprites == null or not sprites is Dictionary or sprites.is_empty():
|
||||
@@ -112,12 +245,11 @@ func _build_animated_sprite():
|
||||
|
||||
# Check if all required textures are cached
|
||||
var all_cached = true
|
||||
var missing_files = []
|
||||
for anim_name in sprites:
|
||||
var sprite_path = sprites[anim_name]
|
||||
if not UpdateManager or not UpdateManager.has_file(sprite_path):
|
||||
all_cached = false
|
||||
missing_files.append(sprite_path)
|
||||
break
|
||||
|
||||
if not all_cached:
|
||||
_setup_fallback(mob_data)
|
||||
@@ -181,13 +313,24 @@ func _build_animated_sprite():
|
||||
|
||||
func _on_file_downloaded(file_path: String):
|
||||
# Check if this is a texture we need
|
||||
if not _sprite_config.is_empty():
|
||||
var sprites = _sprite_config.get("sprites", {})
|
||||
for anim_name in sprites:
|
||||
if sprites[anim_name] == file_path:
|
||||
# A texture we need was downloaded, try building again
|
||||
if _sprite_config.is_empty():
|
||||
return
|
||||
|
||||
# Check new format sheets
|
||||
if _sprite_config.has("sheets"):
|
||||
var sheets = _sprite_config.get("sheets", {})
|
||||
for sheet_key in sheets:
|
||||
var sheet_info = sheets[sheet_key]
|
||||
if sheet_info.get("path", "") == file_path:
|
||||
_build_animated_sprite()
|
||||
return
|
||||
|
||||
# Check legacy format sprites
|
||||
var sprites = _sprite_config.get("sprites", {})
|
||||
for anim_name in sprites:
|
||||
if sprites[anim_name] == file_path:
|
||||
_build_animated_sprite()
|
||||
return
|
||||
|
||||
func _setup_fallback(data: Dictionary):
|
||||
_using_fallback = true
|
||||
@@ -247,16 +390,36 @@ func _play_animation(state: String, direction: String):
|
||||
"idle"
|
||||
]
|
||||
|
||||
# Get flip info from sprite config (new format)
|
||||
var should_flip_x = false
|
||||
var should_flip_y = false
|
||||
|
||||
for anim_name in names_to_try:
|
||||
if frames.has_animation(anim_name):
|
||||
# Check for flip settings in new format
|
||||
if _sprite_config.has("animations"):
|
||||
var anim_config = _sprite_config["animations"].get(anim_name, {})
|
||||
should_flip_x = anim_config.get("flipX", false)
|
||||
should_flip_y = anim_config.get("flipY", false)
|
||||
|
||||
# Only log and play if animation changed
|
||||
if anim_name != _last_animation:
|
||||
print("[MobVisual] Playing animation: %s (state=%s, dir=%s)" % [anim_name, state, direction])
|
||||
_last_animation = anim_name
|
||||
_animated_sprite.play(anim_name)
|
||||
|
||||
# Handle left direction with flip
|
||||
_animated_sprite.flip_h = (direction == "left" and not frames.has_animation("%s_left" % state))
|
||||
# Handle flipping
|
||||
# For left direction without a dedicated left animation, flip the right one
|
||||
if direction == "left" and not frames.has_animation("%s_left" % state):
|
||||
should_flip_x = not should_flip_x # Toggle flip
|
||||
|
||||
_animated_sprite.flip_h = should_flip_x
|
||||
_animated_sprite.flip_v = should_flip_y
|
||||
return
|
||||
|
||||
# No animation found
|
||||
print("[MobVisual] No animation found for state=%s, dir=%s. Tried: %s" % [state, direction, names_to_try])
|
||||
print("[MobVisual] Available animations: %s" % [frames.get_animation_names()])
|
||||
|
||||
func _alt_direction(dir: String) -> String:
|
||||
match dir:
|
||||
|
||||
@@ -32,7 +32,9 @@ func _connect_gamestate():
|
||||
|
||||
func _on_chat_message(sender: String, message: String, type: String):
|
||||
print("[ChatUI] _on_chat_message received - Sender: '%s', Message: '%s', Type: '%s'" % [sender, message, type])
|
||||
|
||||
_add_message(sender, message, type)
|
||||
chat_messages_container.visible = true;
|
||||
|
||||
func _add_message(sender: String, message: String, type: String = "normal"):
|
||||
print("[ChatUI] Adding message to chat log")
|
||||
|
||||
@@ -72,6 +72,7 @@ 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)
|
||||
|
||||
@@ -270,7 +270,7 @@ func _on_inventory_gui_input(event: InputEvent, slot_index: int):
|
||||
# Check if we should equip or use
|
||||
var drag_item = DragManager.get_drag_item()
|
||||
var item_type = drag_item.get("type", drag_item.get("Type", ""))
|
||||
|
||||
GameState.send_use_item(slot_index)
|
||||
# If released over same slot or no target, treat as click
|
||||
# For now, just end the drag
|
||||
DragManager.end_drag()
|
||||
@@ -282,7 +282,7 @@ func _on_inventory_gui_input(event: InputEvent, slot_index: int):
|
||||
var item_type = item.get("type", item.get("Type", ""))
|
||||
if item_type == "Consumable":
|
||||
# Use consumable on right click
|
||||
GameState.send_use_item(slot_index)
|
||||
_show_hotbar_menu(slot_index)
|
||||
elif item_type in ["Weapon", "Armor", "Accessory"]:
|
||||
# Equip on right click
|
||||
GameState.send_equip_item(slot_index)
|
||||
|
||||
+147
-12
@@ -24,8 +24,14 @@ const RARITY_COLORS = {
|
||||
var inventory_slots: Array = []
|
||||
var stash_slots: Array = []
|
||||
var tooltip: PanelContainer = null
|
||||
var quantity_dialog: Window = null
|
||||
var is_vip: bool = false
|
||||
|
||||
# Pending transfer info
|
||||
var _pending_action: String = "" # "deposit" or "withdraw"
|
||||
var _pending_slot: int = -1
|
||||
var _pending_max_quantity: int = 1
|
||||
|
||||
func _ready():
|
||||
visible = false
|
||||
|
||||
@@ -35,6 +41,7 @@ func _ready():
|
||||
_setup_inventory_slots()
|
||||
_setup_stash_slots()
|
||||
_create_tooltip()
|
||||
_create_quantity_dialog()
|
||||
|
||||
call_deferred("_connect_gamestate")
|
||||
|
||||
@@ -122,10 +129,78 @@ func _create_tooltip():
|
||||
tooltip.visible = false
|
||||
add_child(tooltip)
|
||||
|
||||
func _create_quantity_dialog():
|
||||
quantity_dialog = Window.new()
|
||||
quantity_dialog.title = "Select Quantity"
|
||||
quantity_dialog.size = Vector2i(250, 150)
|
||||
quantity_dialog.unresizable = true
|
||||
quantity_dialog.transient = true
|
||||
quantity_dialog.exclusive = true
|
||||
quantity_dialog.visible = false
|
||||
|
||||
var panel = PanelContainer.new()
|
||||
panel.anchors_preset = Control.PRESET_FULL_RECT
|
||||
|
||||
var vbox = VBoxContainer.new()
|
||||
vbox.add_theme_constant_override("separation", 10)
|
||||
|
||||
var label = Label.new()
|
||||
label.name = "QuantityLabel"
|
||||
label.text = "How many?"
|
||||
label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
||||
vbox.add_child(label)
|
||||
|
||||
var hbox = HBoxContainer.new()
|
||||
hbox.alignment = BoxContainer.ALIGNMENT_CENTER
|
||||
|
||||
var spin = SpinBox.new()
|
||||
spin.name = "QuantitySpinner"
|
||||
spin.min_value = 1
|
||||
spin.max_value = 99
|
||||
spin.value = 1
|
||||
spin.custom_minimum_size = Vector2(100, 30)
|
||||
hbox.add_child(spin)
|
||||
|
||||
var max_btn = Button.new()
|
||||
max_btn.name = "MaxButton"
|
||||
max_btn.text = "Max"
|
||||
max_btn.pressed.connect(_on_quantity_max)
|
||||
hbox.add_child(max_btn)
|
||||
|
||||
vbox.add_child(hbox)
|
||||
|
||||
var btn_row = HBoxContainer.new()
|
||||
btn_row.alignment = BoxContainer.ALIGNMENT_CENTER
|
||||
btn_row.add_theme_constant_override("separation", 20)
|
||||
|
||||
var ok_btn = Button.new()
|
||||
ok_btn.name = "OkButton"
|
||||
ok_btn.text = "Transfer"
|
||||
ok_btn.custom_minimum_size = Vector2(80, 30)
|
||||
ok_btn.pressed.connect(_on_quantity_confirm)
|
||||
btn_row.add_child(ok_btn)
|
||||
|
||||
var cancel_btn = Button.new()
|
||||
cancel_btn.name = "CancelButton"
|
||||
cancel_btn.text = "Cancel"
|
||||
cancel_btn.custom_minimum_size = Vector2(80, 30)
|
||||
cancel_btn.pressed.connect(_on_quantity_cancel)
|
||||
btn_row.add_child(cancel_btn)
|
||||
|
||||
vbox.add_child(btn_row)
|
||||
|
||||
panel.add_child(vbox)
|
||||
quantity_dialog.add_child(panel)
|
||||
quantity_dialog.close_requested.connect(_on_quantity_cancel)
|
||||
add_child(quantity_dialog)
|
||||
|
||||
func show_stash():
|
||||
visible = true
|
||||
# Request stash from server
|
||||
# Request inventory first, then stash
|
||||
if GameState:
|
||||
GameState.request_inventory()
|
||||
# Small delay before requesting stash
|
||||
await get_tree().create_timer(0.1).timeout
|
||||
GameState.request_stash()
|
||||
|
||||
func _on_stash_updated(stash_data: Dictionary):
|
||||
@@ -141,10 +216,6 @@ func _on_stash_result(result: Dictionary):
|
||||
if error == "vip_required":
|
||||
is_vip = false
|
||||
_update_vip_state(false)
|
||||
|
||||
# Could show a toast/notification here
|
||||
if not success and message:
|
||||
print("[Stash] Error: ", message)
|
||||
|
||||
func _update_vip_state(is_vip_status: bool):
|
||||
if vip_message:
|
||||
@@ -232,13 +303,22 @@ func _update_slot(slot: Button, item):
|
||||
|
||||
func _get_item_emoji(item: Dictionary) -> String:
|
||||
var item_type = item.get("type", item.get("Type", "")).to_lower()
|
||||
var item_name = item.get("name", item.get("Name", "")).to_lower()
|
||||
|
||||
match item_type:
|
||||
"consumable": return "🧪"
|
||||
"material": return "📦"
|
||||
"consumable":
|
||||
if "health" in item_name or "potion" in item_name: return "🧪"
|
||||
if "mana" in item_name: return "💙"
|
||||
return "🧪"
|
||||
"material":
|
||||
if "bone" in item_name: return "🦴"
|
||||
if "hide" in item_name or "leather" in item_name: return "🥩"
|
||||
if "essence" in item_name or "soul" in item_name: return "👻"
|
||||
return "📦"
|
||||
"weapon": return "⚔️"
|
||||
"armor": return "🛡️"
|
||||
"accessory": return "💍"
|
||||
_: return "❓"
|
||||
_: return "📦"
|
||||
|
||||
func _on_inventory_slot_input(event: InputEvent, slot_index: int):
|
||||
if not event is InputEventMouseButton:
|
||||
@@ -251,9 +331,12 @@ func _on_inventory_slot_input(event: InputEvent, slot_index: int):
|
||||
return
|
||||
|
||||
if event.button_index == MOUSE_BUTTON_RIGHT:
|
||||
# Right-click to deposit to stash
|
||||
# Right-click to deposit to stash - show quantity dialog
|
||||
var quantity = item.get("quantity", item.get("Quantity", 1))
|
||||
GameState.stash_deposit(slot_index, quantity)
|
||||
if quantity > 1:
|
||||
_show_quantity_dialog("deposit", slot_index, quantity)
|
||||
else:
|
||||
GameState.stash_deposit(slot_index, 1)
|
||||
|
||||
func _on_stash_slot_input(event: InputEvent, slot_index: int):
|
||||
if not event is InputEventMouseButton:
|
||||
@@ -270,9 +353,61 @@ func _on_stash_slot_input(event: InputEvent, slot_index: int):
|
||||
return
|
||||
|
||||
if event.button_index == MOUSE_BUTTON_RIGHT:
|
||||
# Right-click to withdraw from stash
|
||||
# Right-click to withdraw from stash - show quantity dialog
|
||||
var quantity = item.get("quantity", item.get("Quantity", 1))
|
||||
GameState.stash_withdraw(slot_index, quantity)
|
||||
if quantity > 1:
|
||||
_show_quantity_dialog("withdraw", slot_index, quantity)
|
||||
else:
|
||||
GameState.stash_withdraw(slot_index, 1)
|
||||
|
||||
func _show_quantity_dialog(action: String, slot: int, max_qty: int):
|
||||
_pending_action = action
|
||||
_pending_slot = slot
|
||||
_pending_max_quantity = max_qty
|
||||
|
||||
var spinner = quantity_dialog.get_node("PanelContainer/VBoxContainer/HBoxContainer/QuantitySpinner")
|
||||
if spinner:
|
||||
spinner.max_value = max_qty
|
||||
spinner.value = max_qty
|
||||
|
||||
var label = quantity_dialog.get_node("PanelContainer/VBoxContainer/QuantityLabel")
|
||||
if label:
|
||||
if action == "deposit":
|
||||
label.text = "Deposit how many?"
|
||||
else:
|
||||
label.text = "Withdraw how many?"
|
||||
|
||||
# Center dialog
|
||||
var screen_size = get_viewport().get_visible_rect().size
|
||||
quantity_dialog.position = Vector2i(
|
||||
int((screen_size.x - quantity_dialog.size.x) / 2),
|
||||
int((screen_size.y - quantity_dialog.size.y) / 2)
|
||||
)
|
||||
quantity_dialog.visible = true
|
||||
quantity_dialog.grab_focus()
|
||||
|
||||
func _on_quantity_max():
|
||||
var spinner = quantity_dialog.get_node("PanelContainer/VBoxContainer/HBoxContainer/QuantitySpinner")
|
||||
if spinner:
|
||||
spinner.value = _pending_max_quantity
|
||||
|
||||
func _on_quantity_confirm():
|
||||
var spinner = quantity_dialog.get_node("PanelContainer/VBoxContainer/HBoxContainer/QuantitySpinner")
|
||||
var qty = int(spinner.value) if spinner else 1
|
||||
|
||||
if _pending_action == "deposit":
|
||||
GameState.stash_deposit(_pending_slot, qty)
|
||||
elif _pending_action == "withdraw":
|
||||
GameState.stash_withdraw(_pending_slot, qty)
|
||||
|
||||
quantity_dialog.visible = false
|
||||
_pending_action = ""
|
||||
_pending_slot = -1
|
||||
|
||||
func _on_quantity_cancel():
|
||||
quantity_dialog.visible = false
|
||||
_pending_action = ""
|
||||
_pending_slot = -1
|
||||
|
||||
func _on_slot_hover(slot: Button):
|
||||
if not slot.has_meta("item"):
|
||||
|
||||
Reference in New Issue
Block a user