refactor: remove Source property from IRelationship
Remove the redundant `Source` property from `IRelationship` and its implementations. Since the relationship component is inherently stored on the source entity, the `Source` field was unnecessary and required manual synchronization during deserialization. This change simplifies the relationship model, reduces memory usage, and removes the need for reflection-based fixups in the serializer.
This commit is contained in:
@@ -206,7 +206,7 @@ public class NunEffect : ICardEffect
|
||||
world.RemoveComponent<OnField>(target);
|
||||
if (world.HasComponent<Card>(target))
|
||||
world.RemoveComponent<Card>(target);
|
||||
world.AddComponent(target, new InDeck { Source = target, Target = discardDeck });
|
||||
world.AddComponent(target, new InDeck { Target = discardDeck });
|
||||
}
|
||||
world.Commands.Enqueue(new DrawCardCommand { Player = pending.Player, Kind = null });
|
||||
world.RemoveSingleton<PendingNun>();
|
||||
@@ -293,7 +293,7 @@ public class PegasusEffect : ICardEffect
|
||||
world.RemoveComponent<OnField>(target);
|
||||
if (world.HasComponent<Card>(target))
|
||||
world.RemoveComponent<Card>(target);
|
||||
world.AddComponent(target, new HeldBy { Source = target, Target = pending.Player });
|
||||
world.AddComponent(target, new HeldBy { Target = pending.Player });
|
||||
}
|
||||
world.RemoveSingleton<PendingPegasus>();
|
||||
return true;
|
||||
@@ -321,7 +321,7 @@ public class CurseMasterEffect : ICardEffect
|
||||
{
|
||||
var skull = world.CreateEntity();
|
||||
world.AddComponent(skull, new Skull());
|
||||
world.AddComponent(skull, new PlacedOn { Source = skull, Target = pt });
|
||||
world.AddComponent(skull, new PlacedOn { Target = pt });
|
||||
}
|
||||
}
|
||||
world.RemoveSingleton<PendingCurseMaster>();
|
||||
@@ -366,14 +366,14 @@ public static class CardEffectHelpers
|
||||
{
|
||||
var horn = world.CreateEntity();
|
||||
world.AddComponent(horn, new Horn());
|
||||
world.AddComponent(horn, new PlacedOn { Source = horn, Target = target });
|
||||
world.AddComponent(horn, new PlacedOn { Target = target });
|
||||
}
|
||||
|
||||
public static void AddSkull(World world, Entity target)
|
||||
{
|
||||
var skull = world.CreateEntity();
|
||||
world.AddComponent(skull, new Skull());
|
||||
world.AddComponent(skull, new PlacedOn { Source = skull, Target = target });
|
||||
world.AddComponent(skull, new PlacedOn { Target = target });
|
||||
}
|
||||
|
||||
public static Entity GetBanner(World world, Entity player)
|
||||
|
||||
@@ -38,7 +38,7 @@ public struct PlayCardCommand : ICommand
|
||||
|
||||
// Move from hand to field.
|
||||
world.RemoveComponent<HeldBy>(CardEntity);
|
||||
world.AddComponent(CardEntity, new OnField { Source = CardEntity, Target = fieldOwner });
|
||||
world.AddComponent(CardEntity, new OnField { Target = fieldOwner });
|
||||
world.AddComponent(CardEntity, new Card { Rank = Rank, FaceDown = FaceDown });
|
||||
|
||||
// Resolve on-play effects via registry.
|
||||
|
||||
@@ -10,6 +10,5 @@ namespace Game.CardWars;
|
||||
[MessagePackObject]
|
||||
public record struct HeldBy : IRelationship
|
||||
{
|
||||
[Key(0)] public Entity Source { get; set; }
|
||||
[Key(1)] public Entity Target { get; set; }
|
||||
[Key(0)] public Entity Target { get; set; }
|
||||
}
|
||||
@@ -9,6 +9,5 @@ namespace Game.CardWars;
|
||||
[MessagePackObject]
|
||||
public record struct InDeck : IRelationship
|
||||
{
|
||||
[Key(0)] public Entity Source { get; set; }
|
||||
[Key(1)] public Entity Target { get; set; }
|
||||
[Key(0)] public Entity Target { get; set; }
|
||||
}
|
||||
@@ -9,6 +9,5 @@ namespace Game.CardWars;
|
||||
[MessagePackObject]
|
||||
public record struct OnField : IRelationship
|
||||
{
|
||||
[Key(0)] public Entity Source { get; set; }
|
||||
[Key(1)] public Entity Target { get; set; }
|
||||
[Key(0)] public Entity Target { get; set; }
|
||||
}
|
||||
@@ -9,6 +9,5 @@ namespace Game.CardWars;
|
||||
[MessagePackObject]
|
||||
public record struct PlacedOn : IRelationship
|
||||
{
|
||||
[Key(0)] public Entity Source { get; set; }
|
||||
[Key(1)] public Entity Target { get; set; }
|
||||
[Key(0)] public Entity Target { get; set; }
|
||||
}
|
||||
@@ -38,7 +38,7 @@ public static class GameUtil
|
||||
|
||||
var cardEntity = cards.First();
|
||||
world.RemoveComponent<InDeck>(cardEntity);
|
||||
world.AddComponent(cardEntity, new HeldBy { Source = cardEntity, Target = player });
|
||||
world.AddComponent(cardEntity, new HeldBy { Target = player });
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ public static class GameUtil
|
||||
for (int i = cards.Length - 1; i >= 0; i--)
|
||||
world.RemoveComponent<InDeck>(cards[i]);
|
||||
for (int i = 0; i < cards.Length; i++)
|
||||
world.AddComponent(cards[i], new InDeck { Source = cards[i], Target = deck });
|
||||
world.AddComponent(cards[i], new InDeck { Target = deck });
|
||||
}
|
||||
|
||||
/// <summary>Get all field cards for a player.</summary>
|
||||
@@ -135,7 +135,7 @@ public static class GameUtil
|
||||
{
|
||||
world.RemoveComponent<OnField>(card);
|
||||
world.RemoveComponent<Card>(card);
|
||||
world.AddComponent(card, new InDeck { Source = card, Target = discardDeck });
|
||||
world.AddComponent(card, new InDeck { Target = discardDeck });
|
||||
}
|
||||
|
||||
// Destroy horns and skulls placed on discarded cards.
|
||||
@@ -156,7 +156,7 @@ public static class GameUtil
|
||||
foreach (var card in cards)
|
||||
{
|
||||
world.RemoveComponent<InDeck>(card);
|
||||
world.AddComponent(card, new InDeck { Source = card, Target = drawDeck });
|
||||
world.AddComponent(card, new InDeck { Target = drawDeck });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -57,7 +57,7 @@ public class GameSetupSystem : ISystem
|
||||
var defs = CardDataLoader.LoadDefinitions(world, _cardDataCsv);
|
||||
foreach (var (defEntity, _) in defs)
|
||||
{
|
||||
world.AddComponent(defEntity, new InDeck { Source = defEntity, Target = publicDeck });
|
||||
world.AddComponent(defEntity, new InDeck { Target = publicDeck });
|
||||
}
|
||||
|
||||
// Shuffle public deck.
|
||||
@@ -79,12 +79,12 @@ public class GameSetupSystem : ISystem
|
||||
world.AddComponent(leader, new Leader());
|
||||
world.AddComponent(leader, new CardDef { Name = $"领袖{i}", Ranks = new[] { 0 }, Effect = CardEffect.None, Kind = CardKind.Faction });
|
||||
world.AddComponent(leader, new Card { Rank = 0, FaceDown = false });
|
||||
world.AddComponent(leader, new OnField { Source = leader, Target = player });
|
||||
world.AddComponent(leader, new OnField { Target = player });
|
||||
|
||||
// Create banner.
|
||||
var banner = world.CreateEntity();
|
||||
world.AddComponent(banner, new Banner());
|
||||
world.AddComponent(banner, new OnField { Source = banner, Target = player });
|
||||
world.AddComponent(banner, new OnField { Target = player });
|
||||
|
||||
// Create faction deck.
|
||||
var factionDeck = world.CreateEntity();
|
||||
|
||||
@@ -42,7 +42,7 @@ public class ScoringSystem : ISystem
|
||||
if (castles.Count > 0)
|
||||
{
|
||||
var castle = castles[0];
|
||||
world.AddComponent(castle, new HeldBy { Source = castle, Target = winner });
|
||||
world.AddComponent(castle, new HeldBy { Target = winner });
|
||||
world.RemoveComponent<Castle>(castle);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user