Files
Crash-Course/Assets/Scripts/Game/CreatePlayerJoinCards.cs

65 lines
1.8 KiB
C#
Raw Permalink Normal View History

2025-04-18 15:54:50 -04:00
using UnityEngine;
using Game;
using Music;
using Player;
2025-02-16 16:47:45 -05:00
using UnityEngine.InputSystem;
2025-04-16 19:57:54 -04:00
namespace Game
2025-02-16 16:47:45 -05:00
{
2025-04-18 15:54:50 -04:00
/// <summary>
/// This class is used to create cards for players when they join the game.
/// </summary>
2025-04-16 19:57:54 -04:00
public class PlayerCardCreator : MonoBehaviour
2025-02-16 16:47:45 -05:00
{
2025-04-18 15:54:50 -04:00
/// <summary>
/// A single instance of this class that can be accessed from anywhere.
/// </summary>
2025-04-16 19:57:54 -04:00
public static PlayerCardCreator Instance;
2025-04-18 15:54:50 -04:00
/// <summary>
/// The template used to create new player cards.
/// </summary>
2025-04-16 19:57:54 -04:00
public GameObject playerJoinCardPrefab;
2025-04-18 15:54:50 -04:00
/// <summary>
/// Makes sure there is only one PlayerCardCreator in the game.
/// </summary>
private void Awake()
2025-02-16 16:47:45 -05:00
{
2025-04-18 15:54:50 -04:00
// If this is the first instance, set it as the main one.
if (Instance == null)
{
Instance = this;
}
2025-04-16 19:57:54 -04:00
else
{
2025-04-18 15:54:50 -04:00
// If another instance already exists, remove this one.
2025-04-16 19:57:54 -04:00
Destroy(gameObject);
}
2025-02-16 16:47:45 -05:00
}
2025-04-18 15:54:50 -04:00
/// <summary>
/// Creates a new player card and returns it.
/// </summary>
/// <returns>The new player card, or nothing if it couldn't be created.</returns>
public PlayerJoinCard CreateCard()
2025-04-16 19:57:54 -04:00
{
try
{
2025-04-18 15:54:50 -04:00
// Make a new player card using the template.
GameObject card = Instantiate(playerJoinCardPrefab, transform);
2025-04-18 15:54:50 -04:00
// Return the player card so it can be used.
return card.GetComponent<PlayerJoinCard>();
}
catch
{
2025-04-18 15:54:50 -04:00
// If something goes wrong, return nothing.
return null;
}
2025-04-16 19:57:54 -04:00
}
2025-02-16 16:47:45 -05:00
}
}
2025-04-18 15:54:50 -04:00