feat(oecs): add support for reordering relationship sources
Introduces `RelationshipReordered` change kind and `ReorderSources` method to allow reordering the source set of a relationship without removing and re-adding components. This optimizes shuffling operations like deck setup in Blackjack.
This commit is contained in:
@@ -60,27 +60,10 @@ public class DeckSetupSystem : ISystem
|
||||
for (int i = cardEntities.Length - 1; i > 0; i--)
|
||||
{
|
||||
int j = Mulberry32.NextInt(ref seed, 0, i);
|
||||
|
||||
// Swap the InDeck relationship targets (cards are always in the deck,
|
||||
// so there's nothing to swap except the cards themselves — but we
|
||||
// shuffle the card order conceptually by removing and re-adding
|
||||
// InDeck components in shuffled order). Actually, since InDeck
|
||||
// is just a tag, we just re-shuffle the order in the source collection.
|
||||
// The simplest approach: no need to swap component data; we just
|
||||
// need to ensure the cards are iterated in shuffled order.
|
||||
// We'll swap the card entities in the array.
|
||||
(cardEntities[i], cardEntities[j]) = (cardEntities[j], cardEntities[i]);
|
||||
}
|
||||
|
||||
// Now remove all InDeck and re-add in shuffled order so GetSources
|
||||
// returns them in shuffled order.
|
||||
for (int i = cardEntities.Length - 1; i >= 0; i--)
|
||||
{
|
||||
world.RemoveComponent<InDeck>(cardEntities[i]);
|
||||
}
|
||||
for (int i = 0; i < cardEntities.Length; i++)
|
||||
{
|
||||
world.AddComponent(cardEntities[i], new InDeck { Target = deckEntity });
|
||||
}
|
||||
// Reorder the source set in-place — no component add/remove needed.
|
||||
world.ReorderSources<InDeck>(deckEntity, cardEntities);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user