BorisKotlyar

Untitled

Jun 27th, 2024
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.75 KB | None | 0 0
  1. public struct CardViewCellProtocol
  2.     {
  3.         public Sprite icon;
  4.         public ReactiveCommand ClickCommand;
  5.     }
  6.    
  7.     public class CardViewCell : MonoBehaviour, IPoolable<CardViewCellProtocol, IMemoryPool>
  8.     {
  9.         [SerializeField] private SpriteRenderer sr;
  10.        
  11.         private CardViewCellProtocol _protocol;
  12.         private IMemoryPool _pool;
  13.        
  14.         public void OnSpawned(CardViewCellProtocol protocol, IMemoryPool pool)
  15.         {
  16.             sr.sprite = protocol.icon;
  17.         }
  18.        
  19.         public void OnDespawned()
  20.         {
  21.            
  22.         }
  23.  
  24.         public void DeSpawn()
  25.         {
  26.             _pool.Despawn(this);
  27.         }
  28.        
  29.         public class Factory : PlaceholderFactory<CardViewCellProtocol, CardViewCell>
  30.         {
  31.         }
  32.  
  33.         public class Pool : MonoPoolableMemoryPool<CardViewCellProtocol, IMemoryPool, CardViewCell>
  34.         {
  35.         }
  36.  
  37.         public static void InitPool(DiContainer container)
  38.         {
  39.             container
  40.                 .BindFactory<CardViewCellProtocol, CardViewCell, Factory>()
  41.                 .FromPoolableMemoryPool<CardViewCellProtocol, CardViewCell, Pool>(poolBinder =>
  42.                     poolBinder
  43.                         .WithInitialSize(5)
  44.                         .FromComponentInNewPrefabResource($"Elements/{nameof(CardViewCell)}")
  45.                         .UnderTransformGroup(nameof(CardViewCell)));
  46.         }
  47.     }
  48.    
  49.     public class HandView : MonoBehaviour
  50.     {
  51.         [SerializeField] private Transform container;
  52.  
  53.         private List<CardViewCell> _cells = new();
  54.  
  55.         public void SetCardCellView(CardViewCell cell)
  56.         {
  57.             cell.transform.SetParent(container, false);
  58.             _cells.Add(cell);
  59.         }
  60.  
  61.         public void Clear()
  62.         {
  63.             foreach (var cell in _cells)
  64.             {
  65.                 cell.DeSpawn();
  66.             }
  67.             _cells.Clear();
  68.         }
  69.     }
  70.    
  71.     public class HandInteractor : Interactor
  72.     {
  73.         private readonly IInstantiator _instantiator;
  74.         private readonly CardViewCell.Factory _cardCellFactory;
  75.         private HandView _handView;
  76.  
  77.         private CompositeDisposable _tempCompositeDisposable = new();
  78.        
  79.         public HandInteractor(IInstantiator instantiator, CardViewCell.Factory cardCellFactory)
  80.         {
  81.             _instantiator = instantiator;
  82.             _cardCellFactory = cardCellFactory;
  83.         }
  84.        
  85.         public override void Init()
  86.         {
  87.             base.Init();
  88.  
  89.             var prefab = Resources.Load<HandView>("HandView");
  90.             _handView = _instantiator.InstantiatePrefabForComponent<HandView>(prefab);
  91.  
  92.             CreateCardViews();
  93.         }
  94.  
  95.         private void CreateCardViews()
  96.         {
  97.             _tempCompositeDisposable.Clear();
  98.             _handView.Clear();
  99.            
  100.             var cardList = new List<Sprite>();
  101.            
  102.             foreach (var sprite in cardList)
  103.             {
  104.                 var cardProtocol = new CardViewCellProtocol
  105.                 {
  106.                     icon = sprite,
  107.                     ClickCommand = new ReactiveCommand()
  108.                 };
  109.  
  110.                 cardProtocol.ClickCommand.Subscribe(unit =>
  111.                 {
  112.                     // click on current card
  113.                 }).AddTo(_tempCompositeDisposable);
  114.  
  115.                 var cardCell = _cardCellFactory.Create(cardProtocol);
  116.                 _handView.SetCardCellView(cardCell);
  117.             }
  118.         }
  119.  
  120.         public override void Dispose()
  121.         {
  122.             base.Dispose();
  123.            
  124.             _tempCompositeDisposable.Clear();
  125.             _handView.Clear();
  126.             GameObject.Destroy(_handView.gameObject);
  127.         }
  128.     }
  129.    
Advertisement
Add Comment
Please, Sign In to add comment