Mob fixes
This commit is contained in:
@@ -84,6 +84,13 @@ func _state_to_animation(state: String) -> String:
|
||||
state = state.to_lower()
|
||||
return STATE_TO_ANIMATION.get(state, "idle")
|
||||
|
||||
func reset_state():
|
||||
## Fully reset visual state so the node can be reused for a respawned mob.
|
||||
_current_state = "idle"
|
||||
_facing_direction = "down"
|
||||
_initialized = false
|
||||
_last_animation = ""
|
||||
|
||||
func update_data(data: Dictionary):
|
||||
mob_data = data
|
||||
|
||||
@@ -94,19 +101,19 @@ func update_data(data: Dictionary):
|
||||
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"))
|
||||
if name_label:
|
||||
name_label.text = mname
|
||||
|
||||
|
||||
# Update health bar
|
||||
var hp = data.get("health", data.get("Health", 100))
|
||||
var max_hp = data.get("maxHealth", data.get("MaxHealth", 100))
|
||||
if health_bar:
|
||||
health_bar.max_value = max_hp
|
||||
health_bar.value = hp
|
||||
|
||||
|
||||
# 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():
|
||||
@@ -145,21 +152,26 @@ func update_data(data: Dictionary):
|
||||
tint = "#ffffff"
|
||||
_apply_tint(tint)
|
||||
_initialized = true
|
||||
|
||||
# Update visual state (skip for dead mobs to preserve death animation)
|
||||
if _current_state != "dead":
|
||||
|
||||
# Resolve animation — prefer server-authoritative currentAnimation, fall back to state
|
||||
var current_anim = data.get("currentAnimation", data.get("CurrentAnimation", null))
|
||||
var rotation = data.get("rotation", data.get("Rotation", 0.0))
|
||||
|
||||
if current_anim != null and current_anim is String and not current_anim.is_empty():
|
||||
# Server says exactly what animation to play — always apply, even if currently "dead"
|
||||
var animation_state := _state_to_animation(current_anim)
|
||||
_update_animation_state(animation_state, rotation)
|
||||
elif _current_state != "dead":
|
||||
# Legacy path: use state field, but still skip if locally dead (no server override)
|
||||
var raw_state = data.get("state", data.get("State", "idle")).to_lower()
|
||||
var animation_state := _state_to_animation(raw_state)
|
||||
var rotation = data.get("rotation", data.get("Rotation", 0.0))
|
||||
|
||||
_update_animation_state(animation_state, rotation)
|
||||
|
||||
|
||||
# Apply scale
|
||||
var mob_scale = data.get("scale", data.get("Scale", 1.0))
|
||||
if mob_scale == null or mob_scale <= 0:
|
||||
mob_scale = 1.0
|
||||
|
||||
|
||||
if _animated_sprite:
|
||||
_animated_sprite.scale = Vector2.ONE * mob_scale
|
||||
elif fallback_sprite:
|
||||
@@ -422,21 +434,37 @@ func _use_cached_frames(sprite_frames: SpriteFrames) -> void:
|
||||
|
||||
func _setup_fallback(data: Dictionary):
|
||||
_using_fallback = true
|
||||
|
||||
|
||||
# Hide animated sprite if exists
|
||||
if _animated_sprite:
|
||||
_animated_sprite.visible = false
|
||||
|
||||
|
||||
if fallback_sprite:
|
||||
fallback_sprite.visible = true
|
||||
|
||||
# Use SpriteGenerator for a simple shape
|
||||
if SpriteGenerator:
|
||||
|
||||
# Try definitionId-based local sprite lookup before falling back to generic shape
|
||||
var resolved_texture: Texture2D = null
|
||||
if _definition_id != null:
|
||||
var def_path = "res://textures/mobs/%s.png" % str(_definition_id)
|
||||
if ResourceLoader.exists(def_path):
|
||||
resolved_texture = load(def_path)
|
||||
|
||||
if resolved_texture:
|
||||
fallback_sprite.texture = resolved_texture
|
||||
elif SpriteGenerator:
|
||||
fallback_sprite.texture = SpriteGenerator.get_circle()
|
||||
|
||||
# Color based on mob type or default red
|
||||
var tint = data.get("tintColor", data.get("TintColor", "#cc4444"))
|
||||
fallback_sprite.modulate = Color.from_string(tint, Color(0.8, 0.3, 0.3))
|
||||
|
||||
# Color based on tint — safe defaults when null/missing
|
||||
var tint = data.get("tintColor", data.get("TintColor", "#ffffff"))
|
||||
if tint == null or not (tint is String) or tint.is_empty():
|
||||
tint = "#ffffff"
|
||||
fallback_sprite.modulate = Color.from_string(tint, Color.WHITE)
|
||||
|
||||
# Apply safe default scale
|
||||
var mob_scale = data.get("scale", data.get("Scale", 1.0))
|
||||
if mob_scale == null or not (mob_scale is float or mob_scale is int) or mob_scale <= 0:
|
||||
mob_scale = 1.0
|
||||
fallback_sprite.scale = Vector2.ONE * mob_scale
|
||||
|
||||
func _update_animation_state(state: String, rotation: float):
|
||||
_current_state = state
|
||||
|
||||
Reference in New Issue
Block a user