I'm working with unity and want to have different versions of all objects in the game, some with UI and some without. My main goal is to not duplicate any code, and keeping things light and clear.
See example below :
public class Card : ICard
{
public void DoStuff()
{
}
}
public class CardWithUI : Monobehaviour, ICard
{
public Card CardComponent;
public void DoStuff()
{
CardComponent.DoStuff();
//Update UI
}
}
Then, I'm using generics to handle card lists, to extend the different methods with UI calls :
public class CardList<TCard> : IEnumerable<TCard> where TCard : ICard
{
public List<TCard> Cards { get; } = new List<TCard>();
public virtual void Add(TCard card)
{
Cards.Add(card);
}
}
public class CardListWithUI : CardList<CardWithUI>
{
public override void Add(CardWithUI card)
{
base.Add(card);
//UI stuff
}
}
My current issue is now that I want to have card lists in players. What I want to do :
public class Player : IPlayer
{
public CardList<ICard> Cards:
}
public class PlayerWithUI:
{
public PlayerWithUI()
{
Cards = new CardListWithUI(); //Error, I can't do that
}
}
More precisely, I get the error :
"Can't convert from CardListWithUI to target type CardList<ICard>".
So I guess I'm doing something wrong, but can't figure out how to do this properly.
Thanks in advance for any help provided !