diff --git a/Assets/_/Features/UI/Runtime/Adventurers/AdventurersPanel.cs b/Assets/_/Features/UI/Runtime/Adventurers/AdventurersPanel.cs index c3b60089..4e87db4d 100644 --- a/Assets/_/Features/UI/Runtime/Adventurers/AdventurersPanel.cs +++ b/Assets/_/Features/UI/Runtime/Adventurers/AdventurersPanel.cs @@ -55,19 +55,70 @@ namespace GameUI.Runtime void DisplayAdventurers() { + // Build a lookup of existing cards by their Adventurer instance + Dictionary existing = new Dictionary(); + List orphaned = new List(); foreach (Transform child in _heroesPanel.transform) { - Destroy(child.gameObject); + AdventurerCardUI card = child.GetComponent(); + if (card != null && card.Adventurer != null) + { + if (!existing.ContainsKey(card.Adventurer)) + existing.Add(card.Adventurer, card); + else + orphaned.Add(card); // duplicate safety + } + else if (card != null) + { + orphaned.Add(card); + } } List filtered = FilterAdventurers(_sort); - foreach (AdventurerClass adventurer in filtered) + // Reuse or create cards to match the filtered list and order + HashSet used = new HashSet(); + for (int i = 0; i < filtered.Count; i++) { - GameObject cardAdventurerGO = Instantiate(_adventurerPrefab, _heroesPanel.transform); - cardAdventurerGO.transform.SetAsLastSibling(); - AdventurerCardUI card = cardAdventurerGO.GetComponent(); - card.setup(adventurer); + AdventurerClass adv = filtered[i]; + AdventurerCardUI card; + if (existing.TryGetValue(adv, out card)) + { + // Refresh textual data and availability indicators without touching portrait + card.setup(adv); + card.transform.SetSiblingIndex(i); + } + else + { + GameObject cardAdventurerGO = Instantiate(_adventurerPrefab, _heroesPanel.transform); + cardAdventurerGO.transform.SetSiblingIndex(i); + card = cardAdventurerGO.GetComponent(); + if (card != null) + { + card.setup(adv); + } + } + if (card != null) + used.Add(card); + } + + // Remove cards that are no longer needed (not part of filtered) or orphaned + foreach (Transform child in _heroesPanel.transform) + { + AdventurerCardUI card = child.GetComponent(); + if (card != null && !used.Contains(card)) + { + Destroy(child.gameObject); + } + } + + // Cleanup any explicit orphaned duplicates detected earlier + foreach (var card in orphaned) + { + if (card != null && !used.Contains(card)) + { + Destroy(card.gameObject); + } } }