235 lines
4.8 KiB
C#

using UdonSharp;
using UnityEngine;
using TMPro;
using VRC.SDKBase;
using VRC.SDK3.UdonNetworkCalling;
using VRC.Udon.Common.Interfaces;
using VRC.Udon.Common;
[UdonBehaviourSyncMode(BehaviourSyncMode.Manual)]
public class PlayerPodium : UdonSharpBehaviour
{
public int PlayerNumber = 0;
[SerializeField] public GameManagerRound1 GameManager;
[UdonSynced, FieldChangeCallback(nameof(PlayerName))] private string _PlayerName = "Player";
[UdonSynced] private int _PlayerID = -1;
[UdonSynced, FieldChangeCallback(nameof(PlayerScore))] private int _PlayerScore = 50;
[UdonSynced, FieldChangeCallback(nameof(ShowScoreCard))] private bool _ShowScoreCard = false;
[SerializeField] private ChoiceCardGroup _ChoiceCards;
[SerializeField] private RiskCardGroup _RiskCards;
[SerializeField] private Buzzer _Buzzer;
[Header("Effects")]
[SerializeField] private GameObject _BuzzedInEffect;
[SerializeField] private GameObject _ScorecardObject;
[SerializeField] private TextMeshProUGUI _NameplateUI;
[SerializeField] private TextMeshProUGUI _ScorecardUI;
void Start()
{
ResetPodium();
}
public void SetPlayerName()
{
if (_PlayerID != -1)
{
return;
}
Networking.SetOwner(Networking.LocalPlayer, gameObject);
Networking.SetOwner(Networking.LocalPlayer, _ChoiceCards.gameObject);
Networking.SetOwner(Networking.LocalPlayer, _RiskCards.gameObject);
Networking.SetOwner(Networking.LocalPlayer, _Buzzer.gameObject);
PlayerName = Networking.LocalPlayer.displayName;
_PlayerID = Networking.LocalPlayer.playerId;
RequestSerialization();
}
[NetworkCallable]
public void ResetOwner()
{
Networking.SetOwner(Networking.InstanceOwner, gameObject);
Networking.SetOwner(Networking.InstanceOwner, _ChoiceCards.gameObject);
Networking.SetOwner(Networking.InstanceOwner, _RiskCards.gameObject);
Networking.SetOwner(Networking.InstanceOwner, _Buzzer.gameObject);
PlayerName = "Player " + PlayerNumber;
_PlayerID = -1;
}
public int GetPlayerID() { return _PlayerID; }
[NetworkCallable]
public void DisplayScore()
{
ShowScoreCard = true;
RequestSerialization();
}
private void _DisplayScorecardObject(bool Show)
{
_ScorecardObject.SetActive(Show);
}
[NetworkCallable]
public void ResetPodium()
{
ResetOwner();
EnableChoiceCards(false);
EnableBuzzer(false);
EnableBuzzInEffect(false);
PlayerScore = 50;
ShowScoreCard = false;
}
[NetworkCallable]
public void DecreaseScoreBy5()
{
PlayerScore -= 5;
}
[NetworkCallable]
public void IncreaseScoreBy5()
{
PlayerScore += 5;
}
[NetworkCallable]
public void AdjustScore(int Adjustment)
{
PlayerScore += Adjustment;
}
[NetworkCallable]
public void EnableChoiceCards(bool Enable)
{
_ChoiceCards.gameObject.SetActive(Enable);
_ChoiceCards.ResetCards();
NetworkCalling.SendCustomNetworkEvent(
(IUdonEventReceiver)_ChoiceCards,
NetworkEventTarget.Owner,
"MakeChoiceTextVisible");
}
[NetworkCallable]
public void SetCardChoices(string[] Choices, int[] Indices)
{
_ChoiceCards.SetChoices(Choices, Indices);
}
[NetworkCallable]
public void LockInChoice()
{
_ChoiceCards.LockInChoice();
NetworkCalling.SendCustomNetworkEvent(
(IUdonEventReceiver)_ChoiceCards,
NetworkEventTarget.All,
"MakeChoiceTextVisible");
}
[NetworkCallable]
public void VerifyMultipleChoiceResponse(int CorrectResponse)
{
if (_ChoiceCards.GetSelectedChoice() == CorrectResponse)
{
AdjustScore(10);
}
}
[NetworkCallable]
public void EnableRiskCards(bool Enable)
{
_RiskCards.gameObject.SetActive(Enable);
_RiskCards.ResetCards();
NetworkCalling.SendCustomNetworkEvent(
(IUdonEventReceiver)_RiskCards,
NetworkEventTarget.Owner,
"MakeRiskTextVisible");
}
[NetworkCallable]
public void LockInRisk()
{
_RiskCards.LockInChoice();
NetworkCalling.SendCustomNetworkEvent(
(IUdonEventReceiver)_RiskCards,
NetworkEventTarget.All,
"MakeRiskTextVisible");
}
[NetworkCallable]
public void VerifyFinalRoundResponse(int CorrectResponse)
{
if (_ChoiceCards.GetSelectedChoice() == CorrectResponse)
{
AdjustScore(_RiskCards.GetSelectedChoice());
}
else
{
AdjustScore(-_RiskCards.GetSelectedChoice());
}
}
[NetworkCallable]
public void EnableBuzzer(bool Enable)
{
_Buzzer.gameObject.SetActive(Enable);
_Buzzer.transform.localPosition = Vector3.zero;
_Buzzer.transform.localEulerAngles = Vector3.zero;
}
[NetworkCallable]
public void EnableBuzzInEffect(bool Enable)
{
_BuzzedInEffect.SetActive(Enable);
}
public string PlayerName
{
set
{
_PlayerName = value;
_NameplateUI.text = value;
}
get => _PlayerName;
}
public int PlayerScore
{
set
{
_PlayerScore = value;
_ScorecardUI.text = PlayerScore.ToString();
RequestSerialization();
}
get => _PlayerScore;
}
public bool ShowScoreCard
{
set
{
_ShowScoreCard = value;
_DisplayScorecardObject(value);
}
get => _ShowScoreCard;
}
}