Spatial packets
This commit is contained in:
@@ -1,47 +1,17 @@
|
|||||||
[gd_scene load_steps=3 format=3]
|
[gd_scene load_steps=3 format=3]
|
||||||
|
|
||||||
[sub_resource type="GDScript" id="GDScript_other_player"]
|
[ext_resource type="Script" path="res://scripts/dungeon/other_player.gd" id="1"]
|
||||||
script/source = "extends Node2D
|
|
||||||
|
|
||||||
@onready var sprite: Sprite2D = $Sprite
|
|
||||||
@onready var name_label: Label = $NameLabel
|
|
||||||
@onready var health_bar: ProgressBar = $HealthBar
|
|
||||||
|
|
||||||
var player_data: Dictionary = {}
|
|
||||||
|
|
||||||
func _ready():
|
|
||||||
# Get texture from generator if available
|
|
||||||
if SpriteGenerator:
|
|
||||||
sprite.texture = SpriteGenerator.get_circle()
|
|
||||||
|
|
||||||
# Other players are blue
|
|
||||||
sprite.modulate = Color(0.27, 0.67, 1.0) # #44aaff
|
|
||||||
|
|
||||||
func update_data(data: Dictionary):
|
|
||||||
player_data = data
|
|
||||||
|
|
||||||
var pname = data.get(\"name\", data.get(\"Name\", \"Player\"))
|
|
||||||
name_label.text = pname
|
|
||||||
|
|
||||||
var hp = data.get(\"health\", data.get(\"Health\", 100))
|
|
||||||
var max_hp = data.get(\"maxHealth\", data.get(\"MaxHealth\", 100))
|
|
||||||
health_bar.max_value = max_hp
|
|
||||||
health_bar.value = hp
|
|
||||||
|
|
||||||
var is_alive = data.get(\"isAlive\", data.get(\"IsAlive\", true))
|
|
||||||
if is_alive:
|
|
||||||
sprite.modulate = Color(0.27, 0.67, 1.0) # Blue
|
|
||||||
else:
|
|
||||||
sprite.modulate = Color(0.3, 0.3, 0.3) # Gray when dead
|
|
||||||
"
|
|
||||||
|
|
||||||
[sub_resource type="CircleShape2D" id="CircleShape2D_1"]
|
[sub_resource type="CircleShape2D" id="CircleShape2D_1"]
|
||||||
radius = 14.0
|
radius = 14.0
|
||||||
|
|
||||||
[node name="OtherPlayer" type="Node2D"]
|
[node name="OtherPlayer" type="Node2D"]
|
||||||
script = SubResource("GDScript_other_player")
|
script = ExtResource("1")
|
||||||
|
|
||||||
[node name="Sprite" type="Sprite2D" parent="."]
|
[node name="AnimatedSprite" type="AnimatedSprite2D" parent="."]
|
||||||
|
scale = Vector2(2, 2)
|
||||||
|
|
||||||
|
[node name="FallbackSprite" type="Sprite2D" parent="."]
|
||||||
modulate = Color(0.267, 0.667, 1, 1)
|
modulate = Color(0.267, 0.667, 1, 1)
|
||||||
scale = Vector2(0.875, 0.875)
|
scale = Vector2(0.875, 0.875)
|
||||||
|
|
||||||
|
|||||||
@@ -75,6 +75,7 @@ var dungeon: Dictionary = {
|
|||||||
var self_player: Dictionary = {}
|
var self_player: Dictionary = {}
|
||||||
var players: Dictionary = {}
|
var players: Dictionary = {}
|
||||||
var mobs: Dictionary = {}
|
var mobs: Dictionary = {}
|
||||||
|
var mob_catalog: Dictionary = {} # Cached static mob render metadata (id -> {definitionId, name, spriteConfig, tintColor, scale})
|
||||||
var chests: Dictionary = {}
|
var chests: Dictionary = {}
|
||||||
var dropped_items: Dictionary = {}
|
var dropped_items: Dictionary = {}
|
||||||
var portals: Dictionary = {}
|
var portals: Dictionary = {}
|
||||||
@@ -288,10 +289,11 @@ func _handle_dungeon_enter(data: Dictionary):
|
|||||||
|
|
||||||
players.clear()
|
players.clear()
|
||||||
mobs.clear()
|
mobs.clear()
|
||||||
|
mob_catalog.clear()
|
||||||
chests.clear()
|
chests.clear()
|
||||||
dropped_items.clear()
|
dropped_items.clear()
|
||||||
portals.clear()
|
portals.clear()
|
||||||
|
|
||||||
# Parse initial portals
|
# Parse initial portals
|
||||||
var portal_list = data.get("portals", data.get("Portals", []))
|
var portal_list = data.get("portals", data.get("Portals", []))
|
||||||
for p in portal_list:
|
for p in portal_list:
|
||||||
@@ -429,11 +431,54 @@ func _handle_dungeon_state(data: Dictionary):
|
|||||||
else:
|
else:
|
||||||
players[pid] = p
|
players[pid] = p
|
||||||
|
|
||||||
|
# Parse mob catalog (sent once per dungeon join, contains static render metadata)
|
||||||
|
var catalog = data.get("mobCatalog", data.get("MobCatalog", []))
|
||||||
|
if catalog is Array and catalog.size() > 0:
|
||||||
|
for entry in catalog:
|
||||||
|
var cid = entry.get("id", entry.get("Id"))
|
||||||
|
if cid != null:
|
||||||
|
mob_catalog[cid] = entry
|
||||||
|
print("[GameState] Mob catalog received: %d entries" % catalog.size())
|
||||||
|
# Prefetch textures from catalog entries
|
||||||
|
_prefetch_mob_textures_from_list(catalog)
|
||||||
|
|
||||||
|
# With spatial filtering, mobs array only contains nearby/relevant mobs.
|
||||||
|
# Don't clear the full dict — update present mobs and track visibility.
|
||||||
var ms = data.get("mobs", data.get("Mobs", []))
|
var ms = data.get("mobs", data.get("Mobs", []))
|
||||||
mobs.clear()
|
var mobs_in_packet: Array = []
|
||||||
for m in ms:
|
for m in ms:
|
||||||
var mid = m.get("id", m.get("Id"))
|
var mid = m.get("id", m.get("Id"))
|
||||||
|
mobs_in_packet.append(mid)
|
||||||
|
# Merge catalog metadata into mob data when per-tick data lacks render fields
|
||||||
|
if mob_catalog.has(mid):
|
||||||
|
var cat = mob_catalog[mid]
|
||||||
|
if not m.has("spriteConfig") and not m.has("SpriteConfig"):
|
||||||
|
var sc = cat.get("spriteConfig", cat.get("SpriteConfig", null))
|
||||||
|
if sc != null:
|
||||||
|
m["spriteConfig"] = sc
|
||||||
|
if not m.has("tintColor") and not m.has("TintColor"):
|
||||||
|
var tc = cat.get("tintColor", cat.get("TintColor", null))
|
||||||
|
if tc != null:
|
||||||
|
m["tintColor"] = tc
|
||||||
|
if not m.has("scale") and not m.has("Scale"):
|
||||||
|
var s = cat.get("scale", cat.get("Scale", null))
|
||||||
|
if s != null:
|
||||||
|
m["scale"] = s
|
||||||
|
if not m.has("name") and not m.has("Name"):
|
||||||
|
var n = cat.get("name", cat.get("Name", null))
|
||||||
|
if n != null:
|
||||||
|
m["name"] = n
|
||||||
|
if not m.has("definitionId") and not m.has("DefinitionId"):
|
||||||
|
var did = cat.get("definitionId", cat.get("DefinitionId", null))
|
||||||
|
if did != null:
|
||||||
|
m["definitionId"] = did
|
||||||
|
m["_visible"] = true
|
||||||
|
m["_last_seen"] = Time.get_ticks_msec()
|
||||||
mobs[mid] = m
|
mobs[mid] = m
|
||||||
|
# Mark mobs not in this packet as out-of-range (but keep them)
|
||||||
|
for mid in mobs:
|
||||||
|
if mid not in mobs_in_packet:
|
||||||
|
mobs[mid]["_visible"] = false
|
||||||
|
|
||||||
var cs = data.get("chests", data.get("Chests", []))
|
var cs = data.get("chests", data.get("Chests", []))
|
||||||
for c_item in cs:
|
for c_item in cs:
|
||||||
@@ -446,11 +491,13 @@ func _handle_dungeon_state(data: Dictionary):
|
|||||||
var iid = i.get("id", i.get("Id"))
|
var iid = i.get("id", i.get("Id"))
|
||||||
dropped_items[iid] = i
|
dropped_items[iid] = i
|
||||||
|
|
||||||
# Parse portals
|
# Parse portals — server sends the authoritative active set each packet
|
||||||
var ports = data.get("portals", data.get("Portals", []))
|
var ports = data.get("portals", data.get("Portals", []))
|
||||||
for p in ports:
|
if ports is Array and ports.size() > 0:
|
||||||
var pid = p.get("id", p.get("Id"))
|
portals.clear()
|
||||||
portals[pid] = p
|
for p in ports:
|
||||||
|
var pid = p.get("id", p.get("Id"))
|
||||||
|
portals[pid] = p
|
||||||
|
|
||||||
# Parse loot chests (personal to this player)
|
# Parse loot chests (personal to this player)
|
||||||
var lchests = data.get("lootChests", data.get("LootChests", []))
|
var lchests = data.get("lootChests", data.get("LootChests", []))
|
||||||
@@ -547,6 +594,7 @@ func _handle_escape_success(_data: Dictionary):
|
|||||||
# Clear dungeon data
|
# Clear dungeon data
|
||||||
players.clear()
|
players.clear()
|
||||||
mobs.clear()
|
mobs.clear()
|
||||||
|
mob_catalog.clear()
|
||||||
chests.clear()
|
chests.clear()
|
||||||
dropped_items.clear()
|
dropped_items.clear()
|
||||||
portals.clear()
|
portals.clear()
|
||||||
@@ -558,7 +606,7 @@ func _handle_escape_success(_data: Dictionary):
|
|||||||
"tiles": [],
|
"tiles": [],
|
||||||
"phase": "active"
|
"phase": "active"
|
||||||
}
|
}
|
||||||
|
|
||||||
escaped.emit()
|
escaped.emit()
|
||||||
# Server will send HUB_STATE next which will call set_state(STATE_HUB)
|
# Server will send HUB_STATE next which will call set_state(STATE_HUB)
|
||||||
|
|
||||||
@@ -633,6 +681,7 @@ func _handle_portal_used(_data: Dictionary):
|
|||||||
# Clear dungeon data
|
# Clear dungeon data
|
||||||
players.clear()
|
players.clear()
|
||||||
mobs.clear()
|
mobs.clear()
|
||||||
|
mob_catalog.clear()
|
||||||
chests.clear()
|
chests.clear()
|
||||||
dropped_items.clear()
|
dropped_items.clear()
|
||||||
portals.clear()
|
portals.clear()
|
||||||
|
|||||||
@@ -56,6 +56,9 @@ var _last_aim_sent: float = 0.0
|
|||||||
var _prefetched_mob_paths: Dictionary = {}
|
var _prefetched_mob_paths: Dictionary = {}
|
||||||
var _mob_prefetch_in_progress: bool = false
|
var _mob_prefetch_in_progress: bool = false
|
||||||
|
|
||||||
|
# Spatial visibility: how long (ms) to keep an out-of-range mob entity before removing it
|
||||||
|
const MOB_OUT_OF_RANGE_TIMEOUT_MS: int = 5000
|
||||||
|
|
||||||
func _ready():
|
func _ready():
|
||||||
# Add to group for HUD to access
|
# Add to group for HUD to access
|
||||||
add_to_group("dungeon_manager")
|
add_to_group("dungeon_manager")
|
||||||
@@ -477,7 +480,7 @@ func _on_state_updated():
|
|||||||
_update_entities(players_container, GameState.players, player_scene)
|
_update_entities(players_container, GameState.players, player_scene)
|
||||||
if mob_scene and mobs_container:
|
if mob_scene and mobs_container:
|
||||||
await _prefetch_mob_textures_if_needed()
|
await _prefetch_mob_textures_if_needed()
|
||||||
_update_entities(mobs_container, GameState.mobs, mob_scene)
|
_update_mob_entities()
|
||||||
if chest_scene and chests_container:
|
if chest_scene and chests_container:
|
||||||
_update_entities(chests_container, GameState.chests, chest_scene)
|
_update_entities(chests_container, GameState.chests, chest_scene)
|
||||||
if item_scene and items_container:
|
if item_scene and items_container:
|
||||||
@@ -543,6 +546,8 @@ func _prefetch_mob_textures_if_needed() -> void:
|
|||||||
_mob_prefetch_in_progress = false
|
_mob_prefetch_in_progress = false
|
||||||
|
|
||||||
func _update_entities(container: Node2D, data: Dictionary, scene: PackedScene):
|
func _update_entities(container: Node2D, data: Dictionary, scene: PackedScene):
|
||||||
|
## Generic entity updater for players, chests, items, portals.
|
||||||
|
## Mobs use _update_mob_entities() instead for visibility-scoped lifecycle.
|
||||||
if scene == null or container == null:
|
if scene == null or container == null:
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -562,17 +567,6 @@ func _update_entities(container: Node2D, data: Dictionary, scene: PackedScene):
|
|||||||
var y = entity_data.get("y", entity_data.get("Y", 0))
|
var y = entity_data.get("y", entity_data.get("Y", 0))
|
||||||
entity_node.position = Vector2(x, y)
|
entity_node.position = Vector2(x, y)
|
||||||
|
|
||||||
# Check if this is a mob that just died
|
|
||||||
if container == mobs_container:
|
|
||||||
var hp = entity_data.get("health", entity_data.get("Health", 100))
|
|
||||||
var was_alive = not entity_node.get_meta("is_dead", false)
|
|
||||||
|
|
||||||
if hp <= 0 and was_alive:
|
|
||||||
# Mark as dead and play death animation
|
|
||||||
entity_node.set_meta("is_dead", true)
|
|
||||||
if entity_node.has_method("play_death_animation"):
|
|
||||||
entity_node.play_death_animation()
|
|
||||||
|
|
||||||
if entity_node.has_method("update_data"):
|
if entity_node.has_method("update_data"):
|
||||||
entity_node.update_data(entity_data)
|
entity_node.update_data(entity_data)
|
||||||
|
|
||||||
@@ -580,12 +574,80 @@ func _update_entities(container: Node2D, data: Dictionary, scene: PackedScene):
|
|||||||
if child.has_meta("entity_id"):
|
if child.has_meta("entity_id"):
|
||||||
var id = child.get_meta("entity_id")
|
var id = child.get_meta("entity_id")
|
||||||
if id not in seen_ids:
|
if id not in seen_ids:
|
||||||
# For mobs, keep dead bodies for looting
|
child.queue_free()
|
||||||
if container == mobs_container and child.get_meta("is_dead", false):
|
|
||||||
# Don't remove dead mob bodies
|
func _update_mob_entities():
|
||||||
|
## Visibility-scoped mob entity lifecycle.
|
||||||
|
## Mobs present in the packet are created/updated normally.
|
||||||
|
## Mobs absent from the packet (out of range) are kept for a timeout before removal.
|
||||||
|
if mob_scene == null or mobs_container == null:
|
||||||
|
return
|
||||||
|
|
||||||
|
var now_ms = Time.get_ticks_msec()
|
||||||
|
var seen_ids = []
|
||||||
|
|
||||||
|
for id in GameState.mobs:
|
||||||
|
var mob = GameState.mobs[id]
|
||||||
|
var is_visible = mob.get("_visible", true)
|
||||||
|
|
||||||
|
if not is_visible:
|
||||||
|
# Out-of-range mob — check timeout
|
||||||
|
var last_seen = mob.get("_last_seen", now_ms)
|
||||||
|
if now_ms - last_seen > MOB_OUT_OF_RANGE_TIMEOUT_MS:
|
||||||
|
# Timed out — remove entity node and data
|
||||||
|
var entity_node = _find_entity_by_id(mobs_container, id)
|
||||||
|
if entity_node and not entity_node.get_meta("is_dead", false):
|
||||||
|
entity_node.queue_free()
|
||||||
|
else:
|
||||||
|
# Still within grace period — keep the node but don't update position
|
||||||
|
seen_ids.append(id)
|
||||||
|
continue
|
||||||
|
|
||||||
|
seen_ids.append(id)
|
||||||
|
var entity_node = _find_entity_by_id(mobs_container, id)
|
||||||
|
|
||||||
|
if entity_node == null:
|
||||||
|
entity_node = mob_scene.instantiate()
|
||||||
|
entity_node.set_meta("entity_id", id)
|
||||||
|
mobs_container.add_child(entity_node)
|
||||||
|
|
||||||
|
var x = mob.get("x", mob.get("X", 0))
|
||||||
|
var y = mob.get("y", mob.get("Y", 0))
|
||||||
|
entity_node.position = Vector2(x, y)
|
||||||
|
|
||||||
|
# Check if this mob just died
|
||||||
|
var hp = mob.get("health", mob.get("Health", 100))
|
||||||
|
var was_alive = not entity_node.get_meta("is_dead", false)
|
||||||
|
|
||||||
|
if hp <= 0 and was_alive:
|
||||||
|
entity_node.set_meta("is_dead", true)
|
||||||
|
if entity_node.has_method("play_death_animation"):
|
||||||
|
entity_node.play_death_animation()
|
||||||
|
|
||||||
|
if entity_node.has_method("update_data"):
|
||||||
|
entity_node.update_data(mob)
|
||||||
|
|
||||||
|
# Remove entity nodes that are no longer tracked at all
|
||||||
|
for child in mobs_container.get_children():
|
||||||
|
if child.has_meta("entity_id"):
|
||||||
|
var id = child.get_meta("entity_id")
|
||||||
|
if id not in seen_ids:
|
||||||
|
# Keep dead mob bodies for looting
|
||||||
|
if child.get_meta("is_dead", false):
|
||||||
continue
|
continue
|
||||||
child.queue_free()
|
child.queue_free()
|
||||||
|
|
||||||
|
# Clean up timed-out mobs from GameState dict
|
||||||
|
var to_remove: Array = []
|
||||||
|
for id in GameState.mobs:
|
||||||
|
var mob = GameState.mobs[id]
|
||||||
|
if not mob.get("_visible", true):
|
||||||
|
var last_seen = mob.get("_last_seen", now_ms)
|
||||||
|
if now_ms - last_seen > MOB_OUT_OF_RANGE_TIMEOUT_MS:
|
||||||
|
to_remove.append(id)
|
||||||
|
for id in to_remove:
|
||||||
|
GameState.mobs.erase(id)
|
||||||
|
|
||||||
func _find_entity_by_id(container: Node2D, id) -> Node2D:
|
func _find_entity_by_id(container: Node2D, id) -> Node2D:
|
||||||
for child in container.get_children():
|
for child in container.get_children():
|
||||||
if child.has_meta("entity_id") and child.get_meta("entity_id") == id:
|
if child.has_meta("entity_id") and child.get_meta("entity_id") == id:
|
||||||
|
|||||||
@@ -16,6 +16,11 @@ var _animated_sprite: AnimatedSprite2D = null
|
|||||||
var _using_fallback: bool = true
|
var _using_fallback: bool = true
|
||||||
var _initialized: bool = false # Track if we've already set up this mob
|
var _initialized: bool = false # Track if we've already set up this mob
|
||||||
var _last_animation: String = "" # Track animation to avoid spam
|
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
|
# Animation state
|
||||||
var _current_state: String = "idle"
|
var _current_state: String = "idle"
|
||||||
@@ -81,8 +86,14 @@ func _state_to_animation(state: String) -> String:
|
|||||||
|
|
||||||
func update_data(data: Dictionary):
|
func update_data(data: Dictionary):
|
||||||
mob_data = data
|
mob_data = data
|
||||||
|
|
||||||
var mob_id = data.get("id", data.get("Id", "unknown"))
|
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
|
# Update name
|
||||||
var mname = data.get("name", data.get("Name", "Mob"))
|
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.max_value = max_hp
|
||||||
health_bar.value = hp
|
health_bar.value = hp
|
||||||
|
|
||||||
# Only process sprite config on first update or if it changed
|
# Process sprite config on first update or if it changed.
|
||||||
if not _initialized:
|
# Server may send null for invalid configs — treat as missing.
|
||||||
print("[MobVisual] First init for mob %s (%s)" % [mname, mob_id])
|
if sprite_config != null and sprite_config is Dictionary and not sprite_config.is_empty():
|
||||||
var sprite_config = data.get("spriteConfig", data.get("SpriteConfig", null))
|
# 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()
|
||||||
if sprite_config != null and sprite_config is Dictionary and not sprite_config.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:
|
if sprite_config != _sprite_config:
|
||||||
_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"):
|
if UpdateManager and UpdateManager.has_method("queue_build"):
|
||||||
UpdateManager.queue_build(Callable(self, "_build_animated_sprite"))
|
UpdateManager.queue_build(Callable(self, "_build_animated_sprite"))
|
||||||
else:
|
else:
|
||||||
_build_animated_sprite()
|
_build_animated_sprite()
|
||||||
else:
|
else:
|
||||||
|
# Config present but missing actual sprite data — use fallback
|
||||||
|
if not _sprite_config.is_empty():
|
||||||
|
_sprite_config = {}
|
||||||
|
_sprite_config_key = ""
|
||||||
_setup_fallback(data)
|
_setup_fallback(data)
|
||||||
|
else:
|
||||||
# Apply tint on first setup
|
# 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"))
|
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)
|
_apply_tint(tint)
|
||||||
|
|
||||||
_initialized = true
|
_initialized = true
|
||||||
|
|
||||||
# Update visual state (skip for dead mobs to preserve death animation)
|
# Update visual state (skip for dead mobs to preserve death animation)
|
||||||
@@ -139,6 +168,12 @@ func update_data(data: Dictionary):
|
|||||||
func _build_animated_sprite():
|
func _build_animated_sprite():
|
||||||
## Build AnimatedSprite2D from sprite config
|
## Build AnimatedSprite2D from sprite config
|
||||||
## Detects format and uses appropriate builder
|
## 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)
|
# Check for new format (has "sheets" key)
|
||||||
if _sprite_config.has("sheets"):
|
if _sprite_config.has("sheets"):
|
||||||
@@ -244,7 +279,7 @@ func _build_from_new_format():
|
|||||||
|
|
||||||
sprite_frames.add_frame(anim_name, atlas)
|
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.visible = true
|
||||||
_animated_sprite.z_index = 10
|
_animated_sprite.z_index = 10
|
||||||
_animated_sprite.modulate = Color.WHITE
|
_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)
|
atlas.region = Rect2(i * frame_width, 0, frame_width, frame_height)
|
||||||
sprite_frames.add_frame(anim_name, atlas)
|
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.visible = true
|
||||||
_animated_sprite.z_index = 10
|
_animated_sprite.z_index = 10
|
||||||
_animated_sprite.modulate = Color.WHITE
|
_animated_sprite.modulate = Color.WHITE
|
||||||
@@ -369,6 +404,22 @@ func _on_file_downloaded(file_path: String):
|
|||||||
_build_animated_sprite()
|
_build_animated_sprite()
|
||||||
return
|
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):
|
func _setup_fallback(data: Dictionary):
|
||||||
_using_fallback = true
|
_using_fallback = true
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,142 @@
|
|||||||
|
extends Node2D
|
||||||
|
|
||||||
|
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite
|
||||||
|
@onready var fallback_sprite: Sprite2D = $FallbackSprite
|
||||||
|
@onready var name_label: Label = $NameLabel
|
||||||
|
@onready var health_bar: ProgressBar = $HealthBar
|
||||||
|
|
||||||
|
var player_data: Dictionary = {}
|
||||||
|
var player_id: int = 0
|
||||||
|
var _tint_color: Color = Color(0.27, 0.67, 1.0)
|
||||||
|
|
||||||
|
func _ready():
|
||||||
|
# Start with fallback circle until the sprite sheet is ready
|
||||||
|
if fallback_sprite and SpriteGenerator:
|
||||||
|
fallback_sprite.texture = SpriteGenerator.get_circle()
|
||||||
|
fallback_sprite.visible = true
|
||||||
|
|
||||||
|
if animated_sprite:
|
||||||
|
animated_sprite.visible = false
|
||||||
|
|
||||||
|
# Try to load sprite sheet immediately
|
||||||
|
if UpdateManager:
|
||||||
|
var texture = UpdateManager.load_texture("textures/player/Warrior-Blue.png")
|
||||||
|
if texture:
|
||||||
|
_setup_sprite_frames(texture)
|
||||||
|
else:
|
||||||
|
if not UpdateManager.file_downloaded.is_connected(_on_file_downloaded):
|
||||||
|
UpdateManager.file_downloaded.connect(_on_file_downloaded)
|
||||||
|
|
||||||
|
func update_data(data: Dictionary):
|
||||||
|
player_data = data
|
||||||
|
player_id = int(data.get("id", data.get("Id", 0)))
|
||||||
|
|
||||||
|
var pname = data.get("name", data.get("Name", "Player"))
|
||||||
|
if name_label:
|
||||||
|
name_label.text = pname
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
var is_alive = data.get("isAlive", data.get("IsAlive", true))
|
||||||
|
if is_alive:
|
||||||
|
_tint_color = _color_from_id(player_id)
|
||||||
|
else:
|
||||||
|
_tint_color = Color(0.3, 0.3, 0.3)
|
||||||
|
|
||||||
|
_apply_tint(_tint_color)
|
||||||
|
|
||||||
|
# Ensure idle animation is playing when available
|
||||||
|
if animated_sprite and animated_sprite.sprite_frames:
|
||||||
|
if animated_sprite.animation != "walk_down":
|
||||||
|
animated_sprite.play("walk_down")
|
||||||
|
|
||||||
|
func _on_file_downloaded(file_path: String):
|
||||||
|
if file_path != "textures/player/Warrior-Blue.png":
|
||||||
|
return
|
||||||
|
if UpdateManager:
|
||||||
|
var texture = UpdateManager.load_texture("textures/player/Warrior-Blue.png")
|
||||||
|
if texture:
|
||||||
|
_setup_sprite_frames(texture)
|
||||||
|
|
||||||
|
func _apply_tint(tint: Color):
|
||||||
|
if animated_sprite:
|
||||||
|
animated_sprite.modulate = tint
|
||||||
|
if fallback_sprite:
|
||||||
|
fallback_sprite.modulate = tint
|
||||||
|
|
||||||
|
func _setup_sprite_frames(texture: Texture2D):
|
||||||
|
if not animated_sprite:
|
||||||
|
return
|
||||||
|
|
||||||
|
var frames = SpriteFrames.new()
|
||||||
|
var frame_width = 32
|
||||||
|
var frame_height = 32
|
||||||
|
|
||||||
|
var create_anim = func(anim_name: String, row_y: int, start_col: int, frame_count: int, loop: bool = true):
|
||||||
|
frames.add_animation(anim_name)
|
||||||
|
frames.set_animation_loop(anim_name, loop)
|
||||||
|
frames.set_animation_speed(anim_name, 8.0)
|
||||||
|
|
||||||
|
for i in range(frame_count):
|
||||||
|
var atlas = AtlasTexture.new()
|
||||||
|
atlas.atlas = texture
|
||||||
|
atlas.region = Rect2(
|
||||||
|
(start_col + i) * frame_width,
|
||||||
|
row_y,
|
||||||
|
frame_width,
|
||||||
|
frame_height
|
||||||
|
)
|
||||||
|
frames.add_frame(anim_name, atlas)
|
||||||
|
|
||||||
|
create_anim.call("walk_down", 0, 0, 4)
|
||||||
|
create_anim.call("walk_right", 64, 0, 4)
|
||||||
|
create_anim.call("walk_up", 128, 0, 4)
|
||||||
|
create_anim.call("walk_left", 192, 0, 4)
|
||||||
|
|
||||||
|
create_anim.call("attack_sword_down", 0, 4, 4, false)
|
||||||
|
create_anim.call("attack_sword_right", 64, 4, 4, false)
|
||||||
|
create_anim.call("attack_sword_up", 128, 4, 4, false)
|
||||||
|
create_anim.call("attack_sword_left", 192, 4, 4, false)
|
||||||
|
|
||||||
|
create_anim.call("attack_bow_down", 0, 8, 4, false)
|
||||||
|
create_anim.call("attack_bow_right", 64, 8, 4, false)
|
||||||
|
create_anim.call("attack_bow_up", 128, 8, 4, false)
|
||||||
|
create_anim.call("attack_bow_left", 192, 8, 4, false)
|
||||||
|
|
||||||
|
create_anim.call("attack_spell_down", 0, 12, 4, false)
|
||||||
|
create_anim.call("attack_spell_right", 64, 12, 4, false)
|
||||||
|
create_anim.call("attack_spell_up", 128, 12, 4, false)
|
||||||
|
create_anim.call("attack_spell_left", 192, 12, 4, false)
|
||||||
|
|
||||||
|
create_anim.call("attack_fist_down", 0, 16, 4, false)
|
||||||
|
create_anim.call("attack_fist_right", 64, 16, 4, false)
|
||||||
|
create_anim.call("attack_fist_up", 128, 16, 4, false)
|
||||||
|
create_anim.call("attack_fist_left", 192, 16, 4, false)
|
||||||
|
|
||||||
|
create_anim.call("hurt_down", 0, 20, 1, false)
|
||||||
|
create_anim.call("hurt_right", 64, 20, 1, false)
|
||||||
|
create_anim.call("hurt_up", 128, 20, 1, false)
|
||||||
|
create_anim.call("hurt_left", 192, 20, 1, false)
|
||||||
|
|
||||||
|
create_anim.call("dead_down", 0, 21, 4, false)
|
||||||
|
create_anim.call("dead_right", 64, 21, 4, false)
|
||||||
|
create_anim.call("dead_up", 128, 21, 4, false)
|
||||||
|
create_anim.call("dead_left", 192, 21, 4, false)
|
||||||
|
|
||||||
|
animated_sprite.sprite_frames = frames
|
||||||
|
animated_sprite.visible = true
|
||||||
|
animated_sprite.play("walk_down")
|
||||||
|
|
||||||
|
if fallback_sprite:
|
||||||
|
fallback_sprite.visible = false
|
||||||
|
|
||||||
|
_apply_tint(_tint_color)
|
||||||
|
|
||||||
|
func _color_from_id(id_value: int) -> Color:
|
||||||
|
# Stable, distinct tint per player ID
|
||||||
|
var hue = float(abs(id_value) % 360) / 360.0
|
||||||
|
return Color.from_hsv(hue, 0.6, 1.0)
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
uid://tmg1icw1h5ac
|
||||||
+13
-3
@@ -281,7 +281,7 @@ func _update_minimap_entities():
|
|||||||
if draw_x >= 0 and draw_x < MINIMAP_SIZE and draw_y >= 0 and draw_y < MINIMAP_SIZE:
|
if draw_x >= 0 and draw_x < MINIMAP_SIZE and draw_y >= 0 and draw_y < MINIMAP_SIZE:
|
||||||
minimap_image.set_pixel(draw_x, draw_y, Color.GREEN)
|
minimap_image.set_pixel(draw_x, draw_y, Color.GREEN)
|
||||||
|
|
||||||
# Draw mobs (red dots)
|
# Draw mobs (red = nearby/visible, dark red = out-of-range)
|
||||||
for mob_id in GameState.mobs:
|
for mob_id in GameState.mobs:
|
||||||
var mob = GameState.mobs[mob_id]
|
var mob = GameState.mobs[mob_id]
|
||||||
var mx = mob.get("x", mob.get("X", 0)) / 32.0
|
var mx = mob.get("x", mob.get("X", 0)) / 32.0
|
||||||
@@ -289,7 +289,8 @@ func _update_minimap_entities():
|
|||||||
var mob_mx = int(mx * scale) + offset_x
|
var mob_mx = int(mx * scale) + offset_x
|
||||||
var mob_my = int(my * scale) + offset_y
|
var mob_my = int(my * scale) + offset_y
|
||||||
if mob_mx >= 0 and mob_mx < MINIMAP_SIZE and mob_my >= 0 and mob_my < MINIMAP_SIZE:
|
if mob_mx >= 0 and mob_mx < MINIMAP_SIZE and mob_my >= 0 and mob_my < MINIMAP_SIZE:
|
||||||
minimap_image.set_pixel(mob_mx, mob_my, Color.RED)
|
var mob_color = Color.RED if mob.get("_visible", true) else Color(0.5, 0.15, 0.15)
|
||||||
|
minimap_image.set_pixel(mob_mx, mob_my, mob_color)
|
||||||
|
|
||||||
minimap_tex.update(minimap_image)
|
minimap_tex.update(minimap_image)
|
||||||
|
|
||||||
@@ -320,7 +321,16 @@ func _update_resources():
|
|||||||
if tier_label:
|
if tier_label:
|
||||||
tier_label.text = "Tier %d" % GameState.dungeon.tier
|
tier_label.text = "Tier %d" % GameState.dungeon.tier
|
||||||
if mobs_label:
|
if mobs_label:
|
||||||
mobs_label.text = "Mobs: %d" % GameState.mobs.size()
|
# Show nearby (visible) mob count vs total known for spatial tuning debug
|
||||||
|
var total = GameState.mobs.size()
|
||||||
|
var nearby = 0
|
||||||
|
for mid in GameState.mobs:
|
||||||
|
if GameState.mobs[mid].get("_visible", true):
|
||||||
|
nearby += 1
|
||||||
|
if nearby != total:
|
||||||
|
mobs_label.text = "Mobs: %d/%d" % [nearby, total]
|
||||||
|
else:
|
||||||
|
mobs_label.text = "Mobs: %d" % total
|
||||||
|
|
||||||
func _update_hotbar():
|
func _update_hotbar():
|
||||||
if not GameState:
|
if not GameState:
|
||||||
|
|||||||
Reference in New Issue
Block a user