Stash
This commit is contained in:
@@ -12,14 +12,14 @@ var mob_data: Dictionary = {}
|
||||
var _sprite_config: Dictionary = {} # Animation config from server
|
||||
var _animated_sprite: AnimatedSprite2D = null
|
||||
var _using_fallback: bool = true
|
||||
var _initialized: bool = false # Track if we've already set up this mob
|
||||
var _last_animation: String = "" # Track animation to avoid spam
|
||||
|
||||
# Animation state
|
||||
var _current_state: String = "idle"
|
||||
var _facing_direction: String = "down" # down, up, left, right, front, back
|
||||
|
||||
func _ready():
|
||||
print("[MobVisual] _ready called!")
|
||||
|
||||
# Try to find child nodes (they may not exist in all mob scenes)
|
||||
fallback_sprite = get_node_or_null("Sprite2D")
|
||||
name_label = get_node_or_null("NameLabel")
|
||||
@@ -52,9 +52,6 @@ func _ready():
|
||||
# Connect to UpdateManager for async texture loading
|
||||
if UpdateManager:
|
||||
UpdateManager.file_downloaded.connect(_on_file_downloaded)
|
||||
print("[MobVisual] Connected to UpdateManager")
|
||||
else:
|
||||
print("[MobVisual] WARNING: UpdateManager is null!")
|
||||
|
||||
func update_data(data: Dictionary):
|
||||
mob_data = data
|
||||
@@ -71,36 +68,31 @@ func update_data(data: Dictionary):
|
||||
health_bar.max_value = max_hp
|
||||
health_bar.value = hp
|
||||
|
||||
# Get sprite config (sent from server with mob data)
|
||||
var sprite_config = data.get("spriteConfig", data.get("SpriteConfig", null))
|
||||
|
||||
# Debug: print what we received
|
||||
if sprite_config != null:
|
||||
print("[MobVisual] %s received spriteConfig: %s" % [mname, sprite_config])
|
||||
else:
|
||||
print("[MobVisual] %s has NO spriteConfig" % mname)
|
||||
|
||||
if sprite_config != null and sprite_config is Dictionary and not sprite_config.is_empty():
|
||||
if sprite_config != _sprite_config:
|
||||
_sprite_config = sprite_config
|
||||
_build_animated_sprite()
|
||||
elif _using_fallback:
|
||||
_setup_fallback(data)
|
||||
# Only process sprite config on first update or if it changed
|
||||
if not _initialized:
|
||||
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():
|
||||
if sprite_config != _sprite_config:
|
||||
_sprite_config = sprite_config
|
||||
_build_animated_sprite()
|
||||
else:
|
||||
_setup_fallback(data)
|
||||
|
||||
_initialized = true
|
||||
|
||||
# Update visual state
|
||||
var state = data.get("state", data.get("State", "idle")).to_lower()
|
||||
var rotation = data.get("rotation", data.get("Rotation", 0.0))
|
||||
_update_animation_state(state, rotation)
|
||||
|
||||
# Apply tint
|
||||
var tint = data.get("tintColor", data.get("TintColor", "#ffffff"))
|
||||
_apply_tint(tint)
|
||||
# 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))
|
||||
print("[MobVisual] Raw scale from data: %s (type: %s)" % [mob_scale, typeof(mob_scale)])
|
||||
|
||||
# Ensure scale is valid (at least 1.0 if 0 or invalid)
|
||||
if mob_scale == null or mob_scale <= 0:
|
||||
mob_scale = 1.0
|
||||
|
||||
@@ -111,14 +103,10 @@ func update_data(data: Dictionary):
|
||||
|
||||
func _build_animated_sprite():
|
||||
## Build AnimatedSprite2D from sprite config
|
||||
|
||||
print("[MobVisual] _build_animated_sprite called for %s" % mob_data.get("name", "unknown"))
|
||||
|
||||
var mname = mob_data.get("name", "unknown")
|
||||
var sprites = _sprite_config.get("sprites", {})
|
||||
print("[MobVisual] sprites dict type: %s, value: %s" % [typeof(sprites), sprites])
|
||||
|
||||
if sprites == null or not sprites is Dictionary or sprites.is_empty():
|
||||
print("[MobVisual] No sprites in config, using fallback")
|
||||
_setup_fallback(mob_data)
|
||||
return
|
||||
|
||||
@@ -127,25 +115,14 @@ func _build_animated_sprite():
|
||||
var missing_files = []
|
||||
for anim_name in sprites:
|
||||
var sprite_path = sprites[anim_name]
|
||||
print("[MobVisual] Checking texture: %s" % sprite_path)
|
||||
if not UpdateManager:
|
||||
print("[MobVisual] UpdateManager is null!")
|
||||
if not UpdateManager or not UpdateManager.has_file(sprite_path):
|
||||
all_cached = false
|
||||
missing_files.append(sprite_path)
|
||||
elif not UpdateManager.has_file(sprite_path):
|
||||
print("[MobVisual] File NOT in cache: %s" % sprite_path)
|
||||
all_cached = false
|
||||
missing_files.append(sprite_path)
|
||||
else:
|
||||
print("[MobVisual] File IS in cache: %s" % sprite_path)
|
||||
|
||||
if not all_cached:
|
||||
print("[MobVisual] Missing textures: %s" % missing_files)
|
||||
_setup_fallback(mob_data)
|
||||
return
|
||||
|
||||
print("[MobVisual] All textures cached, building AnimatedSprite2D")
|
||||
|
||||
# Create or reuse AnimatedSprite2D
|
||||
if not _animated_sprite:
|
||||
_animated_sprite = AnimatedSprite2D.new()
|
||||
@@ -164,21 +141,14 @@ func _build_animated_sprite():
|
||||
var texture = UpdateManager.load_texture(sprite_path)
|
||||
|
||||
if not texture:
|
||||
push_warning("[MobVisual] Failed to load texture: %s" % sprite_path)
|
||||
continue
|
||||
|
||||
print("[MobVisual] Loaded texture %s: size=%dx%d" % [anim_name, texture.get_width(), texture.get_height()])
|
||||
|
||||
# Get animation config
|
||||
var anim_config = animations.get(anim_name, {"frames": 1, "loop": true})
|
||||
var frame_count = anim_config.get("frames", 1)
|
||||
var loop = anim_config.get("loop", true)
|
||||
|
||||
# frame_count might be a float from JSON, convert to int
|
||||
frame_count = int(frame_count)
|
||||
|
||||
print("[MobVisual] Animation %s: frame_count=%d, loop=%s" % [anim_name, frame_count, loop])
|
||||
|
||||
# Add animation
|
||||
sprite_frames.add_animation(anim_name)
|
||||
sprite_frames.set_animation_speed(anim_name, fps)
|
||||
@@ -189,8 +159,6 @@ func _build_animated_sprite():
|
||||
var calculated_frames = int(tex_width / frame_width) if frame_width > 0 else 1
|
||||
frame_count = min(frame_count, calculated_frames)
|
||||
|
||||
print("[MobVisual] Texture width=%d, frame_width=%d, calculated_frames=%d, using frame_count=%d" % [tex_width, frame_width, calculated_frames, frame_count])
|
||||
|
||||
for i in range(frame_count):
|
||||
var atlas = AtlasTexture.new()
|
||||
atlas.atlas = texture
|
||||
@@ -199,26 +167,17 @@ func _build_animated_sprite():
|
||||
|
||||
_animated_sprite.sprite_frames = sprite_frames
|
||||
_animated_sprite.visible = true
|
||||
_animated_sprite.z_index = 10 # Make sure it's on top
|
||||
_animated_sprite.modulate = Color.WHITE # Ensure full visibility
|
||||
_animated_sprite.position = Vector2.ZERO # Centered on parent
|
||||
_animated_sprite.z_index = 10
|
||||
_animated_sprite.modulate = Color.WHITE
|
||||
_animated_sprite.position = Vector2.ZERO
|
||||
_using_fallback = false
|
||||
|
||||
print("[MobVisual] AnimatedSprite2D position: %s, scale: %s, modulate: %s" % [_animated_sprite.position, _animated_sprite.scale, _animated_sprite.modulate])
|
||||
|
||||
# Hide fallback
|
||||
if fallback_sprite:
|
||||
fallback_sprite.visible = false
|
||||
|
||||
# Play default animation
|
||||
_update_animation_state(_current_state, 0.0)
|
||||
|
||||
# Debug: print the animation that should be playing
|
||||
print("[MobVisual] Built animated sprite with %d animations, current animation: %s, visible: %s" % [
|
||||
sprites.size(),
|
||||
_animated_sprite.animation if _animated_sprite.sprite_frames else "none",
|
||||
_animated_sprite.visible
|
||||
])
|
||||
|
||||
func _on_file_downloaded(file_path: String):
|
||||
# Check if this is a texture we need
|
||||
@@ -261,18 +220,16 @@ func _update_animation_state(state: String, rotation: float):
|
||||
|
||||
func _rotation_to_direction(rotation: float) -> String:
|
||||
# Convert rotation (radians) to cardinal direction
|
||||
# 0 = right, PI/2 = down, PI = left, -PI/2 = up
|
||||
var angle = fmod(rotation + TAU, TAU) # Normalize to 0-2PI
|
||||
var angle = fmod(rotation + TAU, TAU)
|
||||
|
||||
# Map to front/back/left/right (common sprite sheet naming)
|
||||
if angle < PI * 0.25 or angle >= PI * 1.75:
|
||||
return "right"
|
||||
elif angle < PI * 0.75:
|
||||
return "front" # or "down"
|
||||
return "front"
|
||||
elif angle < PI * 1.25:
|
||||
return "left"
|
||||
else:
|
||||
return "back" # or "up"
|
||||
return "back"
|
||||
|
||||
func _play_animation(state: String, direction: String):
|
||||
if not _animated_sprite or not _animated_sprite.sprite_frames:
|
||||
@@ -282,28 +239,26 @@ func _play_animation(state: String, direction: String):
|
||||
|
||||
# Try different animation name formats
|
||||
var names_to_try = [
|
||||
"%s_%s" % [state, direction], # "idle_front"
|
||||
"%s_%s" % [state, _alt_direction(direction)], # "idle_down"
|
||||
state, # "idle"
|
||||
"idle_%s" % direction, # fallback to idle
|
||||
"%s_%s" % [state, direction],
|
||||
"%s_%s" % [state, _alt_direction(direction)],
|
||||
state,
|
||||
"idle_%s" % direction,
|
||||
"idle_%s" % _alt_direction(direction),
|
||||
"idle"
|
||||
]
|
||||
|
||||
for anim_name in names_to_try:
|
||||
if frames.has_animation(anim_name):
|
||||
print("[MobVisual] Playing animation: %s (current: %s, playing: %s)" % [anim_name, _animated_sprite.animation, _animated_sprite.is_playing()])
|
||||
_animated_sprite.play(anim_name)
|
||||
# Only log and play if animation changed
|
||||
if anim_name != _last_animation:
|
||||
_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))
|
||||
return
|
||||
|
||||
# No matching animation found
|
||||
push_warning("[MobVisual] No animation found for state=%s direction=%s, available: %s" % [state, direction, frames.get_animation_names()])
|
||||
|
||||
func _alt_direction(dir: String) -> String:
|
||||
# Alternative direction names
|
||||
match dir:
|
||||
"front": return "down"
|
||||
"back": return "up"
|
||||
@@ -315,7 +270,6 @@ func _update_fallback_state(state: String):
|
||||
if not fallback_sprite:
|
||||
return
|
||||
|
||||
# Simple color changes for fallback
|
||||
match state:
|
||||
"attacking":
|
||||
fallback_sprite.modulate = fallback_sprite.modulate.lightened(0.3)
|
||||
|
||||
Reference in New Issue
Block a user