Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using System;
- public class DeckDraw5Test : MonoBehaviour
- {
- //This class has MonoBehavior so it can do fancy shit in Unity. "regular" classes usually don't need it if it doesn't directly interact with shit in the scenes and using it for Objects can be arse, it'll throw errors.
- //Initializes variables for the scene elements like buttons and text fields.
- //They probably could be Private since I don't manually add the values in the editor.
- //Further info in void Start()
- public Text cardNumber;
- public Text cardHand;
- public Text cardDeck;
- public Button shuffleButton;
- public Button drawButton;
- public Button resetButton;
- Deck deck1 = new Deck(); //Initiates the Deck class to the Variable "deck1"
- /// <summary>
- /// Game does this first. void Awake() is alternative that does shit even earlier, possibly has some limitations.
- /// </summary>
- void Start()
- {
- cardNumber = GameObject.Find("cardNumber").GetComponent<Text>(); //Finds the text element in the scene without having to manually add it in Unity and assigns it as variable for further use.
- cardHand = GameObject.Find("cardHand").GetComponent<Text>(); //Does the same for the hand/output text element.
- cardDeck = GameObject.Find("cardDeck").GetComponent<Text>(); //This was supposed to be something but isn't. Go figure. Not used anywhere in code.
- shuffleButton = GameObject.Find("shuffleButton").GetComponent<Button>(); //Finds the Shuffle Button element in the scene etc.
- drawButton = GameObject.Find("drawButton").GetComponent<Button>(); //Same as above.
- resetButton = GameObject.Find("resetButton").GetComponent<Button>(); //You know the drill.
- shuffleButton.onClick.AddListener(Shuffle); //Adds a listener that activates Shuffle method when Shuffle button is pressed.
- resetButton.onClick.AddListener(ResetDeck); //Does the same for Reset button.
- drawButton.onClick.AddListener(Draw); //Does the same for Draw button.
- Number(); //Shows the number of cards left in the deck.
- }
- /// <summary>
- /// Activated by the "Draw" button (Line 32)
- /// Draws 5 Cards (if available)
- /// </summary>
- public void Draw()
- {
- if (deck1.Count() > 5) //Checks number of cards in deck, if there's more than 5 then allows method to continue. Used to avoid null errors.
- {
- for (int i = 0; i < 5; i++) //For loop, vomits 5 cards.
- {
- cardHand.text = cardHand.text + deck1.DealCard().ToString() + ("\r\n"); //Uses DealCard method in Deck.cs for the deck1 object and prints it to the cardHand textbox with few snazzy cosmetics added to the end to make it more readable.
- Number(); //Refreshes the amount of cards still left in cardDeck, vomits the info to cardNumber.
- }
- }
- }
- /// <summary>
- /// Activated by the "Shuffle" button (Line 30)
- /// </summary>
- public void Shuffle()
- {
- deck1.Shuffle(); //Calls Shuffle from Decks Class for the deck1 object, shuffles the cards IN the deck, regardless of draws, doesn't reset the deck.
- Number(); //Refesh deck number counter
- }
- /// <summary>
- /// Activated by the "Shuffle" button (Line 31)
- /// </summary>
- public void ResetDeck()
- {
- cardHand.text = (""); //Clears the text output box
- deck1 = new Deck(); //Generates a new Deck
- Number(); //Refresh deck number counter
- }
- /// <summary>
- /// Refreshes deck number counter
- /// </summary>
- public void Number()
- {
- cardNumber.text = deck1.Count().ToString(); //Counts the deck1 object number of cards in list and converts it to string, then puts it to the number counter
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment