Mob fixes
This commit is contained in:
@@ -143,7 +143,23 @@ func _on_game_state_changed(state: String):
|
||||
if not is_playing and visual_renderer:
|
||||
visual_renderer.clear_tiles()
|
||||
|
||||
func _clear_entity_containers():
|
||||
## Remove all entity nodes from previous dungeon session.
|
||||
for container in [mobs_container, players_container, chests_container, items_container, portals_container, loot_chests_container]:
|
||||
if container:
|
||||
for child in container.get_children():
|
||||
child.queue_free()
|
||||
# Also clear projectile tracking
|
||||
for proj_id in projectiles:
|
||||
var proj = projectiles[proj_id]
|
||||
if is_instance_valid(proj):
|
||||
proj.queue_free()
|
||||
projectiles.clear()
|
||||
|
||||
func _on_dungeon_entered():
|
||||
# Clear stale entity nodes from any previous dungeon session
|
||||
_clear_entity_containers()
|
||||
|
||||
# Scale the tilemap to match world coordinates
|
||||
if tilemap:
|
||||
tilemap.scale = Vector2(TILE_SCALE, TILE_SCALE)
|
||||
@@ -615,14 +631,30 @@ func _update_mob_entities():
|
||||
var y = mob.get("y", mob.get("Y", 0))
|
||||
entity_node.position = Vector2(x, y)
|
||||
|
||||
# Check if this mob just died
|
||||
# Determine alive/dead from server payload
|
||||
var is_alive = mob.get("isAlive", mob.get("IsAlive", null))
|
||||
var hp = mob.get("health", mob.get("Health", 100))
|
||||
var was_alive = not entity_node.get_meta("is_dead", false)
|
||||
var mob_dead: bool
|
||||
if is_alive != null:
|
||||
mob_dead = not is_alive
|
||||
else:
|
||||
mob_dead = hp <= 0
|
||||
var was_dead = entity_node.get_meta("is_dead", false)
|
||||
|
||||
if hp <= 0 and was_alive:
|
||||
if mob_dead and not was_dead:
|
||||
# Alive → dead transition
|
||||
entity_node.set_meta("is_dead", true)
|
||||
if entity_node.has_method("play_death_animation"):
|
||||
entity_node.play_death_animation()
|
||||
elif not mob_dead and was_dead:
|
||||
# Dead → alive transition (respawn / reused ID) — reset stale state
|
||||
entity_node.set_meta("is_dead", false)
|
||||
if entity_node.has_method("reset_state"):
|
||||
entity_node.reset_state()
|
||||
|
||||
# Track hasLoot for loot prompt gating
|
||||
var has_loot = mob.get("hasLoot", mob.get("HasLoot", false))
|
||||
entity_node.set_meta("has_loot", has_loot)
|
||||
|
||||
if entity_node.has_method("update_data"):
|
||||
entity_node.update_data(mob)
|
||||
@@ -708,12 +740,12 @@ func _try_loot_nearby_mob():
|
||||
var player_pos = $Player.position
|
||||
var loot_range = 50.0 # Pixels
|
||||
|
||||
# Find nearest dead mob within range
|
||||
# Find nearest dead mob within range that has loot
|
||||
var nearest_mob = null
|
||||
var nearest_distance = loot_range
|
||||
|
||||
for child in mobs_container.get_children():
|
||||
if child.get_meta("is_dead", false):
|
||||
if child.get_meta("is_dead", false) and child.get_meta("has_loot", false):
|
||||
var distance = player_pos.distance_to(child.position)
|
||||
if distance < nearest_distance:
|
||||
nearest_distance = distance
|
||||
@@ -916,19 +948,19 @@ func _create_loot_popup():
|
||||
loot_popup = button
|
||||
|
||||
func _update_loot_popup():
|
||||
"""Update loot popup position and visibility based on nearby corpses"""
|
||||
"""Update loot popup position and visibility based on nearby lootable corpses"""
|
||||
if not loot_popup or not player_node or not mobs_container:
|
||||
return
|
||||
|
||||
var player_pos = player_node.position
|
||||
var loot_range = 50.0 # Pixels
|
||||
|
||||
# Find nearest dead mob within range
|
||||
# Find nearest dead mob within range that has loot
|
||||
var nearest_mob = null
|
||||
var nearest_distance = loot_range
|
||||
|
||||
for child in mobs_container.get_children():
|
||||
if child.get_meta("is_dead", false):
|
||||
if child.get_meta("is_dead", false) and child.get_meta("has_loot", false):
|
||||
var distance = player_pos.distance_to(child.position)
|
||||
if distance < nearest_distance:
|
||||
nearest_distance = distance
|
||||
|
||||
@@ -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