CarmenSandiego/Assets/UdonSharp/Podiums/ChoiceCardGroup.cs

240 lines
5.1 KiB
C#

using TMPro;
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon.Common;
[UdonBehaviourSyncMode(BehaviourSyncMode.Manual)]
public class ChoiceCardGroup : UdonSharpBehaviour
{
[SerializeField] private PlayerPodium _Podium;
[SerializeField] private GameObject _ChoiceCardEnabler;
[SerializeField] private ChoiceCard[] _ChoiceCards;
[SerializeField] private TextMeshProUGUI[] _ChoiceCardText;
[SerializeField] private Material[] _ChoiceCardColourOptions;
[SerializeField] private GameObject _PCCardHeldPosition;
[SerializeField] private Animator _PCCardAnimator;
[UdonSynced] private bool _Enabled = false;
[UdonSynced] private int _Colour = -1;
[UdonSynced] private bool _ChoiceTextVisibleForAllPlayers = false;
[UdonSynced] private bool _DisableInteractive = false;
[UdonSynced] private bool _SetPickupable = true;
[UdonSynced] private bool _ChoiceLocked = false;
[UdonSynced] private bool _TurnForward = false;
[UdonSynced] private int _SelectedChoice = -1;
public override void OnDeserialization(DeserializationResult Result)
{
_SetEnabled_Synced();
_InteractiveAndPickupable_Synced();
_SetColourChoice_Synced();
_MakeChoiceTextVisible_Synced();
_TurnForward_Synced();
base.OnDeserialization(Result);
}
public override void OnOwnershipTransferred(VRCPlayerApi Player)
{
foreach (ChoiceCard Card in _ChoiceCards)
{
Networking.SetOwner(Player, Card.gameObject);
}
base.OnOwnershipTransferred(Player);
}
public void SetEnabled(bool Enable, int ColourChoice)
{
_Enabled = Enable;
_SetEnabled_Synced();
ResetCards(ColourChoice, false);
MakeChoiceTextVisible(false, false);
RequestSerialization();
}
private void _SetEnabled_Synced()
{
_ChoiceCardEnabler.gameObject.SetActive(_Enabled);
}
public void ChoiceCardPickedUp(int ChoiceNumber)
{
_SelectedChoice = ChoiceNumber;
RequestSerialization();
}
public void ChoiceCardInteract(int ChoiceNumber)
{
if (_SelectedChoice == ChoiceNumber)
{
RevealChoice();
return;
}
_SelectedChoice = ChoiceNumber;
foreach (ChoiceCard Card in _ChoiceCards)
{
Card.DisableInteractive(false);
if (Card.ChoiceNumber == _SelectedChoice)
{
if (!Networking.LocalPlayer.IsUserInVR())
{
Card.SetToHeldPosition();
Card.DisableInteractive(true);
}
}
else
{
Card.ResetPosition();
}
}
RequestSerialization();
}
private void _TurnForward_Synced()
{
_PCCardAnimator.SetBool("Turn Forward", _TurnForward);
}
private void RevealChoice()
{
if (!Networking.LocalPlayer.IsUserInVR())
{
foreach (ChoiceCard Card in _ChoiceCards)
{
if (Card.ChoiceNumber == _SelectedChoice)
{
Card.SetToHeldPosition();
_TurnForward = true;
_TurnForward_Synced();
Card.DisableInteractive(true);
RequestSerialization();
}
else
{
Card.ResetPosition();
}
}
}
}
public int GetSelectedChoice()
{
return _SelectedChoice;
}
public void SetChoices(string[] Choices, int[] Indices)
{
for (int i = 0; i < _ChoiceCards.Length && i < _ChoiceCardText.Length; i++)
{
_ChoiceCards[Indices[i]].ChoiceNumber = (i + 1);
_ChoiceCardText[Indices[i]].text = Choices[i];
}
}
public void MakeChoiceTextVisible(bool VisibleForAllPlayers = false, bool RequestSerialisation = true)
{
_ChoiceTextVisibleForAllPlayers = VisibleForAllPlayers;
_MakeChoiceTextVisible_Synced(true);
if (RequestSerialisation)
{
RequestSerialization();
}
}
private void _MakeChoiceTextVisible_Synced(bool VisibleLocally = false)
{
for (int i = 0; i < _ChoiceCardText.Length; i++)
{
_ChoiceCardText[i].gameObject.SetActive(VisibleLocally || _ChoiceTextVisibleForAllPlayers);
}
}
public void LockInChoice()
{
_ChoiceLocked = true;
_InteractiveAndPickupable_Synced();
RequestSerialization();
}
public void ResetCards(int Colour = 0, bool RequestSerialisation = true)
{
_SelectedChoice = -1;
for (int i = 0; i < _ChoiceCards.Length && i < _ChoiceCardText.Length; i++)
{
ChoiceCard Card = _ChoiceCards[i];
Card.ResetPosition();
_ChoiceCardText[i].text = "";
_ChoiceCardText[i].gameObject.SetActive(false);
_Colour = Colour;
_SetColourChoice_Synced();
_DisableInteractive = _SetPickupable = Networking.LocalPlayer.IsUserInVR();
}
_TurnForward = false;
_TurnForward_Synced();
_ChoiceLocked = false;
_InteractiveAndPickupable_Synced();
if (RequestSerialisation)
{
RequestSerialization();
}
}
private void _SetColourChoice_Synced()
{
foreach (ChoiceCard Card in _ChoiceCards)
{
Card.SetColourMaterial(_ChoiceCardColourOptions[(_Colour >= 0 && _Colour < _ChoiceCardColourOptions.Length) ? _Colour : 0]);
}
}
private void _InteractiveAndPickupable_Synced()
{
if (_ChoiceLocked)
{
foreach (ChoiceCard Card in _ChoiceCards)
{
Card.DisableInteractive(Card.ChoiceNumber != _SelectedChoice && _TurnForward);
Card.SetPickupable(Card.ChoiceNumber == _SelectedChoice);
}
}
else
{
foreach (ChoiceCard Card in _ChoiceCards)
{
Card.DisableInteractive(_DisableInteractive);
Card.SetPickupable(_SetPickupable);
}
}
}
public int GetColourOptionsCount()
{
return _ChoiceCardColourOptions.Length;
}
}