- Home /
Question by
math_lab · Apr 23, 2019 at 02:03 AM ·
scripting problempositionpositioningcard
Need help for a Card Game! How to place the cards at the right spot?
I want to know how or what should I do to be able that my cards goes to the right spot. Here's a screenshot of my problem, I want them to go in the rectangles.
I guess I need to add some stuff to my script. Here are my two main scripts(I didn't wrote them myself I was following a tutorial): 1-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour
{
public CardStack player;
public CardStack dealer1;
public CardStack dealer2;
public CardStack dealer3;
public CardStack deck;
void Start()
{
PlayCards();
}
private void Update()
{
}
public void PlayCards()
{
for (int i = 0; i < 8; i++)
{
player.Push(deck.Pop());
dealer1.Push(deck.Pop());
dealer2.Push(deck.Pop());
dealer3.Push(deck.Pop());
}
}
}
2-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CardStack))]
public class CardStackView : MonoBehaviour
{
CardStack deck;
Dictionary<int, CardView> fetchedCards; //Show FaceUp
int lastCount;
public Vector3 start;
public float cardOffset;
public bool faceUp = false; //Show FaceUp
public bool reverseLayerOrder = false; //Cards being remove from the top of the deck
public GameObject cardPrefab;
public CardStack player;
public CardStack dealer1;
public CardStack dealer2;
public CardStack dealer3;
public void Toggle(int card, bool isFaceUp) //Show FaceUp
{
fetchedCards[card].IsFaceUp = isFaceUp;
}
private void Start()
{
fetchedCards = new Dictionary<int, CardView>();
deck = GetComponent<CardStack>();
ShowCards();
lastCount = deck.CardCount;
deck.CardRemoved += deck_CardRemoved;
}
private void deck_CardRemoved(object sender, CardRemovedEventArgs e)
{
if (fetchedCards.ContainsKey(e.CardIndex))
{
Destroy(fetchedCards[e.CardIndex].Card);
fetchedCards.Remove(e.CardIndex);
}
}
private void Update()
{
if (lastCount != deck.CardCount)
{
lastCount = deck.CardCount;
ShowCards();
}
}
void ShowCards()
{
int cardCount = 0;
if (deck.HasCards)
{
foreach (int i in deck.GetCards())
{
float co = cardOffset * cardCount;
Vector3 temp = start + new Vector3(co, 0f);
AddCard(temp, i, cardCount);
cardCount++;
}
}
}
void AddCard(Vector3 position, int cardIndex, int positionalIndex)
{
if (fetchedCards.ContainsKey(cardIndex))
{
if (!faceUp) //Show FaceUp
{
CardModel model = fetchedCards[cardIndex].Card.GetComponent<CardModel>();
model.ToggleFace(fetchedCards[cardIndex].IsFaceUp);
}
return;
}
GameObject cardCopy = (GameObject)Instantiate(cardPrefab);
cardCopy.transform.position = position;
CardModel cardModel = cardCopy.GetComponent<CardModel>();
cardModel.cardIndex = cardIndex;
cardModel.ToggleFace(faceUp);
SpriteRenderer spriterenderer = cardCopy.GetComponent<SpriteRenderer>();
if (reverseLayerOrder) //Cards being removed from the top of the deck \/
{
spriterenderer.sortingOrder = 51 - 1 - positionalIndex;
}
else
{
spriterenderer.sortingOrder = positionalIndex;
} // /\
fetchedCards.Add(cardIndex, new CardView(cardCopy));
Debug.Log("Hand value = " + deck.HandValue());
}
}
Thanks!! Really want to finish that first project!
cardproject.png
(167.2 kB)
Comment
https://www.youtube.com/watch?v=CNu8PcAmC2c
@math_lab hope this will help. i haven't work on cards yet
Your answer
Follow this Question
Related Questions
Why Do My Obstacles Do This In My Object Pooler? 1 Answer
How to increment value of axis when something happens 2 Answers
UI image position assignment not working correctly 1 Answer
AR Object stuck in front of camera 0 Answers
Clamping local movement 0 Answers