Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public struct CardViewCellProtocol
- {
- public Sprite icon;
- public ReactiveCommand ClickCommand;
- }
- public class CardViewCell : MonoBehaviour, IPoolable<CardViewCellProtocol, IMemoryPool>
- {
- [SerializeField] private SpriteRenderer sr;
- private CardViewCellProtocol _protocol;
- private IMemoryPool _pool;
- public void OnSpawned(CardViewCellProtocol protocol, IMemoryPool pool)
- {
- sr.sprite = protocol.icon;
- }
- public void OnDespawned()
- {
- }
- public void DeSpawn()
- {
- _pool.Despawn(this);
- }
- public class Factory : PlaceholderFactory<CardViewCellProtocol, CardViewCell>
- {
- }
- public class Pool : MonoPoolableMemoryPool<CardViewCellProtocol, IMemoryPool, CardViewCell>
- {
- }
- public static void InitPool(DiContainer container)
- {
- container
- .BindFactory<CardViewCellProtocol, CardViewCell, Factory>()
- .FromPoolableMemoryPool<CardViewCellProtocol, CardViewCell, Pool>(poolBinder =>
- poolBinder
- .WithInitialSize(5)
- .FromComponentInNewPrefabResource($"Elements/{nameof(CardViewCell)}")
- .UnderTransformGroup(nameof(CardViewCell)));
- }
- }
- public class HandView : MonoBehaviour
- {
- [SerializeField] private Transform container;
- private List<CardViewCell> _cells = new();
- public void SetCardCellView(CardViewCell cell)
- {
- cell.transform.SetParent(container, false);
- _cells.Add(cell);
- }
- public void Clear()
- {
- foreach (var cell in _cells)
- {
- cell.DeSpawn();
- }
- _cells.Clear();
- }
- }
- public class HandInteractor : Interactor
- {
- private readonly IInstantiator _instantiator;
- private readonly CardViewCell.Factory _cardCellFactory;
- private HandView _handView;
- private CompositeDisposable _tempCompositeDisposable = new();
- public HandInteractor(IInstantiator instantiator, CardViewCell.Factory cardCellFactory)
- {
- _instantiator = instantiator;
- _cardCellFactory = cardCellFactory;
- }
- public override void Init()
- {
- base.Init();
- var prefab = Resources.Load<HandView>("HandView");
- _handView = _instantiator.InstantiatePrefabForComponent<HandView>(prefab);
- CreateCardViews();
- }
- private void CreateCardViews()
- {
- _tempCompositeDisposable.Clear();
- _handView.Clear();
- var cardList = new List<Sprite>();
- foreach (var sprite in cardList)
- {
- var cardProtocol = new CardViewCellProtocol
- {
- icon = sprite,
- ClickCommand = new ReactiveCommand()
- };
- cardProtocol.ClickCommand.Subscribe(unit =>
- {
- // click on current card
- }).AddTo(_tempCompositeDisposable);
- var cardCell = _cardCellFactory.Create(cardProtocol);
- _handView.SetCardCellView(cardCell);
- }
- }
- public override void Dispose()
- {
- base.Dispose();
- _tempCompositeDisposable.Clear();
- _handView.Clear();
- GameObject.Destroy(_handView.gameObject);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment