Skip to main content

Card deck

A deck of 52 playing cards for everyone. Grab the deck to draw its top card into your hand. Shuffle mixes the cards still in the deck, and Reset puts every card back in order, dropping the ones in players' hands.

PickupDeck works for any pile of pickups: cards, tiles or tokens.

  • The deck is an object with an ST Pickup, a collider and PickupDeck. When a player grabs it, OnPickup takes the top card and calls HandOver, so the hand lets go of the deck and holds the card. The deck never moves.
  • The cards are pickups without a Rigidbody, so a dropped card stays where it was let go. They have no script. Fill Cards with them in the deck's starting order.
  • The buttons are UI Buttons whose On Click calls Shuffle and ResetDeck.
  • Only the order and the number drawn are synced. Every player hides the cards still in the deck and makes them unpickupable, so it doesn't matter where they are, and nothing is sent while they sit there.

Keep pickups, the deck and the cards, under a parent at 0,0,0, so their positions sync correctly.

For good performance all 52 cards share one material. Their faces and the back come from one 512 x 512 atlas (Kenney's CC0 Playing Cards Pack), and each card is a small mesh whose UVs pick its face. The deck's look is one box mesh, scaled in height to the cards left.

PickupDeck.cs
using StageTime.SDK;
using TMPro;
using UnityEngine;

/// <summary>
/// A deck of pickups (playing cards, tiles, tokens) shared by everyone. Grabbing the deck puts its top card in the
/// grabbing hand (STPickup.HandOver) and the deck stays where it is. Shuffle mixes the cards still in the deck and
/// ResetDeck puts every card back in order: hook them to Buttons' On Click. Only the order and the number of cards
/// drawn are synced. Every player hides the cards still in the deck and makes them unpickupable, so they can be
/// anywhere, and a card in someone's hand drops when the deck is reset.
/// Put the script on the deck, which has an STPickup and a collider, and fill Cards with the card pickups in the
/// deck's starting order. Pickups sync their position correctly only under a parent at 0,0,0.
/// </summary>
public class PickupDeck : StageTimeBehaviour
{
public STPickup[] cards;
public Transform pile; // the deck's look, scaled in height to the cards left
public float cardThickness = 0.0005f;
public TMP_Text countLabel;

[Synced] private int[] order = new int[64]; // up to 64 cards: order[position] = card index
[Synced] private int drawn; // cards taken from the top, positions 0 to drawn - 1
[Synced] private bool ordered; // false until the first Shuffle or Reset: the starting order

private STPickup deck;
private Renderer[] cardRenderers;
private Collider[] cardColliders;
private Vector3 homePosition;
private Quaternion homeRotation;

private void Start()
{
deck = GetComponent<STPickup>();
cardRenderers = new Renderer[cards.Length];
cardColliders = new Collider[cards.Length];
for (int i = 0; i < cards.Length; i++)
{
cardRenderers[i] = cards[i].GetComponent<Renderer>();
cardColliders[i] = cards[i].GetComponent<Collider>();
}
homePosition = transform.localPosition;
homeRotation = transform.localRotation;
Refresh();
}

public override void OnPickup()
{
// Grabbing made this player the deck's owner. The deck never stays in the hand: its top card does.
if (drawn >= cards.Length) return;
int card = CardAt(drawn);
drawn++;
Refresh();
deck.HandOver(cards[card]);
}

public override void OnDrop()
{
transform.localPosition = homePosition;
transform.localRotation = homeRotation;
}

public override void OnDeserialization()
{
Refresh();
}

public void Shuffle()
{
Networking.SetOwner(Networking.LocalPlayer, gameObject);
WriteOrder();
for (int p = cards.Length - 1; p > drawn; p--)
{
int q = Random.Range(drawn, p + 1);
int swap = order[p];
order[p] = order[q];
order[q] = swap;
}
Refresh();
}

public void ResetDeck()
{
Networking.SetOwner(Networking.LocalPlayer, gameObject);
for (int p = 0; p < cards.Length; p++) order[p] = p;
ordered = true;
drawn = 0;
Refresh();
}

private int CardAt(int position)
{
return ordered ? order[position] : position;
}

private void WriteOrder()
{
if (ordered) return;
for (int p = 0; p < cards.Length; p++) order[p] = p;
ordered = true;
}

private void Refresh()
{
if (cardRenderers == null) return; // before Start
for (int p = 0; p < cards.Length; p++)
{
int card = CardAt(p);
bool dealt = p < drawn;
cards[card].pickupable = dealt; // a held card that goes back in the deck is dropped
cardRenderers[card].enabled = dealt;
cardColliders[card].enabled = dealt;
}
int left = cards.Length - drawn;
deck.pickupable = left > 0;
if (pile != null)
{
pile.gameObject.SetActive(left > 0);
pile.localScale = new Vector3(1f, left * cardThickness, 1f);
}
if (countLabel != null) countLabel.text = left == 1 ? "1 card left" : left + " cards left";
}
}