- Home /
Please help with Single Responsibility Principle
I understand the concept of the principle, but am having a hard time putting it into practice. Here is one of my scripts
public class Deck: MonoBehaviour //just want to put DeckMovement here
{
public GameObject deck;
public int numberOFCards;
public CardDatabase database;
public Vector3 deckHeight;
public float cardThickness = .025f;
public GameObject card;
public CardData cardToMove;
public GameObject hand;
public CardDatabase startingDatabase;
public GameObject message;
public GameObject objectManager;
public Text messageText;
public void Awake()
{
objectManager = GameObject.Find("Object Manager"); //ObjectManager holds references to objects that are disabled
message = GameObject.Find("Message"); //message is a gameobject use to display information to tht player. Your Turn, You loose, Card Cannot be Played ect
messageText =message.GetComponentInChildren<Text>();
}
public void Start()
{
DMovement();
transform.position = deckHeight;
}
#region Deck movement
public Vector3 DMovement()
{
numberOFCards = database.allCards.Count; //gets # of cards from the list
deckHeight = new Vector3(deck.transform.position.x, deck.transform.position.y, -numberOFCards * cardThickness+140); //gets a new transform position based off the # of cards in the list
deck.transform.position = deckHeight; //sets deck height to new transform
return deckHeight;
}
#endregion
public void DealCard()//creats cards
{
if(database.allCards.Count != 0) // if statment to see if there are any cards left in the deck
{
cardToMove = database.allCards[0]; //sets the card to be moved as 1st card is the list
database.allCards.RemoveAt(0);//removes 1st card in list
createcard();//sets data to match the card removed
GameObject cloneCard = Instantiate(card, new Vector3(0, 0, 0), Quaternion.identity) as GameObject; //instantiates the card
cloneCard.transform.SetParent(hand.transform, false); //sets the transform to be parented to the hand (IS THERE A WAY TO SEOPRATE THE SO YOU CAN SET A DIFFERENT TARGET OTHER THAT THE HAND)
cardToMove = null; //Clears out referenc to card
numberOFCards = database.allCards.Count; //sets deck height to number of cards in list
DMovement(); //Moves the deck due to list change
}
else
{
StartCoroutine(ShowMessage()); //tells the player he has no cards left in his deck
}
}
GameObject createcard() //This should be a differend script
{
CardData c = cardToMove; //Reference to CardData script and setting it to local c
Card m = card.GetComponent<Card>(); //Reference to card layout and setting it to local m
m.cardData = c; //setting the carddata c to be on card m
m.ReadData(); //sets each point of data to its place in the card
return card;
}
private void OnApplicationQuit()
{
database.allCards.Clear(); //Clears out the deck
foreach(CardData c in startingDatabase.allCards)// Repopulate deck
{
database.allCards.Add(c);
}
}
IEnumerator ShowMessage() //shows message that the player has no cards
//I believe that this could be a seporite script and more vercitile
{
message.SetActive(true);
messageText.text = "You are out of cards.";
yield return new WaitForSeconds(3.0f);
message.SetActive(false);
}
}
Any help with single responsibility would help Just cannot figure out how to fix it. Any criticism with solutions will be welcome.
Thanks guys and gals
Comment