Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- // okay i have another question regarding lists.
- // basically im trying to make a card game system
- // where you get a random card from a list of unsed cards.
- // and then it adds that card to a slot in an ability systm.
- // Once you use the card it adds the ability to an unused card list.
- // Once all the cards are used it recycles the used list
- // and makes all the used cards unused again.
- // The porblem is once the cards have been used the second time
- // thgey are still added to the abil9yt system but i can use it
- // due to a null refrence.
- // basically what i wanna do is you have a deck of cards.
- // each card is an ability. You have 3 abilyt slots.
- // every 30 seconds a card is drawn randomly from your deck
- // and added to an ability slot.
- // Once that ability is used it is added to a used ability list or pool.
- // Once ALL abilities are used they must be recycled
- internal sealed class CardClient : MonoBehaviour
- {
- [SerializeField]
- private CardManager cardManager;
- private void Start()
- {
- this.cardManager.Reset();
- }
- private void Update()
- {
- if (Input.GetMouseButtonDown(0))
- {
- Card drawn;
- if (this.cardManager.GetUnused(out drawn))
- {
- Debug.LogFormat("Drew card with data: {0}", drawn.SomeData);
- }
- else
- {
- Debug.LogWarning("No more cards left.");
- }
- }
- if (Input.GetKeyDown(KeyCode.R))
- {
- this.cardManager.Reset();
- }
- }
- }
- [Serializable]
- internal sealed class CardManager
- {
- private Stack<Card> unusedCards = new Stack<Card>();
- [SerializeField]
- private Card[] cardPrefabs;
- public void Reset()
- {
- this.unusedCards.Clear();
- foreach (var card in this.GetAllCardsShuffled())
- {
- this.unusedCards.Push(card);
- }
- }
- public bool GetUnused(out Card card)
- {
- if (this.unusedCards.Count > 0)
- {
- card = this.unusedCards.Pop();
- return true;
- }
- card = null;
- return false;
- }
- private IEnumerable<Card> GetAllCardsShuffled()
- {
- var cards = new List<Card>(this.cardPrefabs);
- Card[] shuffled = new Card[cards.Count];
- for (int range = cards.Count - 1; range >= 0; range--)
- {
- int i = UnityEngine.Random.Range(0, range);
- shuffled[range] = cards[i];
- cards.RemoveAt(i);
- }
- return shuffled;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement