Spatial packets

This commit is contained in:
dingo
2026-02-09 08:58:44 -05:00
parent a0ee3deeca
commit 1c8dafabd1
9 changed files with 358 additions and 73 deletions
+63 -12
View File
@@ -16,6 +16,11 @@ 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
var _sprite_config_key: String = ""
var _definition_id = null # Server definitionId for fallback art resolution
# Shared cache to reuse sprite frames across mobs with identical configs
static var _sprite_frames_cache: Dictionary = {} # key -> SpriteFrames
# Animation state
var _current_state: String = "idle"
@@ -81,8 +86,14 @@ func _state_to_animation(state: String) -> String:
func update_data(data: Dictionary):
mob_data = data
var mob_id = data.get("id", data.get("Id", "unknown"))
var sprite_config = data.get("spriteConfig", data.get("SpriteConfig", null))
# Track definitionId for fallback art resolution
var def_id = data.get("definitionId", data.get("DefinitionId", null))
if def_id != null:
_definition_id = def_id
# Update name
var mname = data.get("name", data.get("Name", "Mob"))
@@ -96,25 +107,43 @@ func update_data(data: Dictionary):
health_bar.max_value = max_hp
health_bar.value = hp
# 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():
# Process sprite config on first update or if it changed.
# Server may send null for invalid configs — treat as missing.
if sprite_config != null and sprite_config is Dictionary and not sprite_config.is_empty():
# Validate that config has at minimum sheets or sprites
var has_sheets = sprite_config.has("sheets") and sprite_config["sheets"] is Dictionary and not sprite_config["sheets"].is_empty()
var has_sprites = sprite_config.has("sprites") and sprite_config["sprites"] is Dictionary and not sprite_config["sprites"].is_empty()
if has_sheets or has_sprites:
_sprite_config_key = _get_sprite_config_key(sprite_config)
if sprite_config != _sprite_config:
_sprite_config = sprite_config
_using_fallback = true
if _animated_sprite:
_animated_sprite.visible = false
if UpdateManager and UpdateManager.has_method("queue_build"):
UpdateManager.queue_build(Callable(self, "_build_animated_sprite"))
else:
_build_animated_sprite()
else:
# Config present but missing actual sprite data — use fallback
if not _sprite_config.is_empty():
_sprite_config = {}
_sprite_config_key = ""
_setup_fallback(data)
# Apply tint on first setup
else:
# Null or missing sprite config — use fallback with safe defaults
if not _sprite_config.is_empty():
_sprite_config = {}
_sprite_config_key = ""
_setup_fallback(data)
if not _initialized:
print("[MobVisual] First init for mob %s (%s)" % [mname, mob_id])
# Apply tint on first setup (null-safe: server may send null for invalid tint)
var tint = data.get("tintColor", data.get("TintColor", "#ffffff"))
if tint == null or not (tint is String) or tint.is_empty():
tint = "#ffffff"
_apply_tint(tint)
_initialized = true
# Update visual state (skip for dead mobs to preserve death animation)
@@ -139,6 +168,12 @@ func update_data(data: Dictionary):
func _build_animated_sprite():
## Build AnimatedSprite2D from sprite config
## Detects format and uses appropriate builder
if _sprite_config_key != "" and _sprite_frames_cache.has(_sprite_config_key):
_use_cached_frames(_sprite_frames_cache[_sprite_config_key])
_using_fallback = false
if fallback_sprite:
fallback_sprite.visible = false
return
# Check for new format (has "sheets" key)
if _sprite_config.has("sheets"):
@@ -244,7 +279,7 @@ func _build_from_new_format():
sprite_frames.add_frame(anim_name, atlas)
_animated_sprite.sprite_frames = sprite_frames
_cache_and_apply_frames(sprite_frames)
_animated_sprite.visible = true
_animated_sprite.z_index = 10
_animated_sprite.modulate = Color.WHITE
@@ -328,7 +363,7 @@ func _build_from_legacy_format():
atlas.region = Rect2(i * frame_width, 0, frame_width, frame_height)
sprite_frames.add_frame(anim_name, atlas)
_animated_sprite.sprite_frames = sprite_frames
_cache_and_apply_frames(sprite_frames)
_animated_sprite.visible = true
_animated_sprite.z_index = 10
_animated_sprite.modulate = Color.WHITE
@@ -369,6 +404,22 @@ func _on_file_downloaded(file_path: String):
_build_animated_sprite()
return
func _get_sprite_config_key(config: Dictionary) -> String:
# Use JSON as a stable-ish cache key (server sends consistent key order)
return JSON.stringify(config)
func _cache_and_apply_frames(sprite_frames: SpriteFrames) -> void:
if _sprite_config_key != "":
_sprite_frames_cache[_sprite_config_key] = sprite_frames
_use_cached_frames(sprite_frames)
func _use_cached_frames(sprite_frames: SpriteFrames) -> void:
if not _animated_sprite:
_animated_sprite = AnimatedSprite2D.new()
_animated_sprite.name = "MobAnimatedSprite"
add_child(_animated_sprite)
_animated_sprite.sprite_frames = sprite_frames
func _setup_fallback(data: Dictionary):
_using_fallback = true