Example-Code / CardArchitecture / CollectionCard.cs
CollectionCard.cs
Raw
//#nullable enable this is commented out because it slows down development, adding so much busy work of adding question marks and null checks and logging if somethings null all over the place.
using UnityEngine;
using CCG.Bigfoot.InputSystem;
using CCG.Bigfoot.Utility;
namespace CCG.Bigfoot.CardScripts
{
    /// <summary>
    /// This is the card object used outside of battle on the card prefab that is used outside of battle.
    /// </summary>
    /// <remarks>
    /// Authors: CS
    /// Created: 2024-01-11
    /// </remarks>
    public sealed partial class CollectionCard : LandingCard, IPoolable
    {
        #region Variables
            #region IPoolable Implementation
            public ObjectPooler.PooledPrefabName Identifier { get; set; }
            public bool IsInUseEvenIfInactive { get; set; }
            public MonoBehaviour Mono { get; set; }
        #endregion

        #endregion

        #region Callbacks
        //--Shared callbacks are only present in the partial class inheriting from the base class. Start is a unified initialization.
        /*
        protected override void Start()
        {
            //--Since these objects are pooled and are many, we want to avoid several mono-callbacks executing at the same time.
        }*/

        private void OnDestroy()
        {
            Deactivate(false);
        }
        #endregion

        #region Methods
        public override void Set(ScriptableObject data)
        {
            base.Set(data);
            _inputManager = InputManager.Instance;
            CardData.RectTransform = _rectTransform;
        }
        public override void Wash()
        {
            base.Wash();
            Mono = null;
        }

        #region IPoolable Implementation
        void IPoolable.Setup(ScriptableObject data) 
        {
            IsInUseEvenIfInactive = true;
            Mono = this;
            
            Set(data);

            transform.localPosition = Vector3.zero;
        }

        public void Activate()
        {
            SetActive(true);
        }

        public void Deactivate(bool isReturnToPoolAfterDeactivate)
        {
            Wash();
            IsInUseEvenIfInactive = false;
            SetActive(false);
            if(isReturnToPoolAfterDeactivate) ObjectPooler.Instance.ReturnToPool(transform);
        }

        public void SetActive(bool isActive)
        {
            gameObject.SetActive(isActive);
        }
        #endregion
        #endregion
    }
}