Advertisement
Rengaw

CardSystem

Apr 21st, 2018
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. // okay i have another question regarding lists.
  6. // basically im trying to make a card game system
  7. // where you get a random card from a list of unsed cards.
  8. // and then it adds that card to a slot in an ability systm.
  9. // Once you use the card it adds the ability to an unused card list.
  10. // Once all the cards are used it recycles the used list
  11. // and makes all the used cards unused again.
  12. // The porblem is once the cards have been used the second time
  13. // thgey are still added to the abil9yt system but i can use it
  14. // due to a null refrence.
  15.  
  16. // basically what i wanna do is you have a deck of cards.
  17. // each card is an ability. You have 3 abilyt slots.
  18. // every 30 seconds a card is drawn randomly from your deck
  19. // and added to an ability slot.
  20. // Once that ability is used it is added to a used ability list or pool.
  21. // Once ALL abilities are used they must be recycled
  22.  
  23. internal sealed class CardClient : MonoBehaviour
  24. {
  25.     [SerializeField]
  26.     private CardManager cardManager;
  27.  
  28.     private void Start()
  29.     {
  30.         this.cardManager.Reset();
  31.     }
  32.  
  33.     private void Update()
  34.     {
  35.         if (Input.GetMouseButtonDown(0))
  36.         {
  37.             Card drawn;
  38.             if (this.cardManager.GetUnused(out drawn))
  39.             {
  40.                 Debug.LogFormat("Drew card with data: {0}", drawn.SomeData);
  41.             }
  42.             else
  43.             {
  44.                 Debug.LogWarning("No more cards left.");
  45.             }
  46.         }
  47.  
  48.         if (Input.GetKeyDown(KeyCode.R))
  49.         {
  50.             this.cardManager.Reset();
  51.         }
  52.     }
  53. }
  54.  
  55. [Serializable]
  56. internal sealed class CardManager
  57. {
  58.     private Stack<Card> unusedCards = new Stack<Card>();
  59.  
  60.     [SerializeField]
  61.     private Card[] cardPrefabs;
  62.  
  63.     public void Reset()
  64.     {
  65.         this.unusedCards.Clear();
  66.         foreach (var card in this.GetAllCardsShuffled())
  67.         {
  68.             this.unusedCards.Push(card);
  69.         }
  70.     }
  71.  
  72.     public bool GetUnused(out Card card)
  73.     {
  74.         if (this.unusedCards.Count > 0)
  75.         {
  76.             card = this.unusedCards.Pop();
  77.             return true;
  78.         }
  79.  
  80.         card = null;
  81.         return false;
  82.     }
  83.  
  84.     private IEnumerable<Card> GetAllCardsShuffled()
  85.     {
  86.         var cards = new List<Card>(this.cardPrefabs);
  87.  
  88.         Card[] shuffled = new Card[cards.Count];
  89.         for (int range = cards.Count - 1; range >= 0; range--)
  90.         {
  91.             int i = UnityEngine.Random.Range(0, range);
  92.             shuffled[range] = cards[i];
  93.             cards.RemoveAt(i);
  94.         }
  95.  
  96.         return shuffled;
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement