369 lines
8.1 KiB
C#
369 lines
8.1 KiB
C#
|
|
using UdonSharp;
|
|
using UnityEngine;
|
|
using TMPro;
|
|
using VRC.SDKBase;
|
|
using VRC.SDK3.UdonNetworkCalling;
|
|
using VRC.Udon.Common.Interfaces;
|
|
using System.Runtime.CompilerServices;
|
|
using UnityEngine.UI;
|
|
|
|
|
|
[UdonBehaviourSyncMode(BehaviourSyncMode.Manual)]
|
|
public class PlayerPodium : UdonSharpBehaviour
|
|
{
|
|
public int PlayerNumber = 0;
|
|
|
|
[SerializeField] private GameManagerRound1 _GameManager;
|
|
[SerializeField] private GameObject _TeleportButton;
|
|
|
|
[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;
|
|
[UdonSynced, FieldChangeCallback(nameof(FlashScoreboard))] private bool _FlashScoreboard = false;
|
|
|
|
[UdonSynced, FieldChangeCallback(nameof(EnableOwnershipTransfer))] private bool _EnableOwnershipTransfer = true;
|
|
|
|
[Space]
|
|
|
|
[Header("Objects")]
|
|
[SerializeField] private ChoiceCardGroup _ChoiceCards;
|
|
[SerializeField] private RiskCardGroup _RiskCards;
|
|
[SerializeField] private Buzzer _Buzzer;
|
|
[SerializeField] private GameObject _Pedestal;
|
|
[SerializeField] private Transform _AuxiliaryVideoScreen;
|
|
[SerializeField] private Transform _AuxiliaryMapScreen;
|
|
|
|
[SerializeField] private CustomEventInteraction _OwnershipInteract;
|
|
|
|
[Space]
|
|
|
|
[Header("Effects")]
|
|
[SerializeField] private GameObject _HighlightEffect;
|
|
|
|
[SerializeField] private GameObject _ScorecardObject;
|
|
|
|
[SerializeField] private TextMeshProUGUI _NameplateUI;
|
|
[SerializeField] private TextMeshProUGUI _ScorecardUI;
|
|
[SerializeField] private Image _ScorecardBackground;
|
|
|
|
private int _FlashingScoreboardCounter = 0;
|
|
|
|
private const int MAX_FLASH_COUNTER = 8;
|
|
|
|
|
|
void Start()
|
|
{
|
|
ResetPodium();
|
|
_Buzzer.SetGameManager(_GameManager);
|
|
}
|
|
|
|
public override void OnPlayerLeft(VRCPlayerApi Player)
|
|
{
|
|
if (Player.playerId == _PlayerID)
|
|
{
|
|
ResetOwner();
|
|
}
|
|
|
|
base.OnPlayerLeft(Player);
|
|
}
|
|
|
|
public override void OnAvatarEyeHeightChanged(VRCPlayerApi Player, float PrevEyeHeightAsMeters)
|
|
{
|
|
if (Player.playerId == _PlayerID)
|
|
{
|
|
AdjustPedestalHeight(Player);
|
|
}
|
|
|
|
base.OnAvatarEyeHeightChanged(Player, PrevEyeHeightAsMeters);
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
_Pedestal.SetActive(true);
|
|
AdjustPedestalHeight(Networking.LocalPlayer);
|
|
|
|
EnableOwnershipTransfer = false;
|
|
|
|
RequestSerialization();
|
|
}
|
|
|
|
[NetworkCallable]
|
|
public void ResetOwner()
|
|
{
|
|
PlayerName = "Player " + PlayerNumber;
|
|
_PlayerID = -1;
|
|
|
|
EnableOwnershipTransfer = true;
|
|
_Pedestal.SetActive(false);
|
|
|
|
RequestSerialization();
|
|
}
|
|
|
|
public int GetPlayerID() { return _PlayerID; }
|
|
|
|
private void AdjustPedestalHeight(VRCPlayerApi Player)
|
|
{
|
|
_Pedestal.transform.localPosition = new Vector3(
|
|
_Pedestal.transform.localPosition.x,
|
|
-Player.GetAvatarEyeHeightAsMeters(),
|
|
_Pedestal.transform.localPosition.z
|
|
);
|
|
}
|
|
|
|
[NetworkCallable]
|
|
public void DisplayScore()
|
|
{
|
|
ShowScoreCard = true;
|
|
RequestSerialization();
|
|
}
|
|
private void _DisplayScorecardObject(bool Show)
|
|
{
|
|
_ScorecardObject.SetActive(Show);
|
|
}
|
|
|
|
[NetworkCallable]
|
|
public void ResetPodium()
|
|
{
|
|
ResetOwner();
|
|
EnableChoiceCards(false);
|
|
EnableBuzzer(false);
|
|
HighlightPodium(false);
|
|
ShowAuxiliaryVideoScreen(false);
|
|
ShowAuxiliaryMapScreen(false);
|
|
|
|
PlayerScore = 50;
|
|
ShowScoreCard = false;
|
|
}
|
|
|
|
|
|
public void DecreaseScoreBy5()
|
|
{
|
|
PlayerScore -= 5;
|
|
RequestSerialization();
|
|
}
|
|
|
|
public void IncreaseScoreBy5()
|
|
{
|
|
PlayerScore += 5;
|
|
RequestSerialization();
|
|
}
|
|
|
|
[NetworkCallable]
|
|
public void AdjustScore(int Adjustment)
|
|
{
|
|
PlayerScore += Adjustment;
|
|
RequestSerialization();
|
|
}
|
|
|
|
|
|
[NetworkCallable]
|
|
public void EnableChoiceCards(bool Enable)
|
|
{
|
|
_ChoiceCards.gameObject.SetActive(Enable);
|
|
_ChoiceCards.ResetCards();
|
|
_ChoiceCards.SendCustomNetworkEvent(NetworkEventTarget.Owner, "MakeChoiceTextVisible");
|
|
}
|
|
|
|
[NetworkCallable]
|
|
public void SetCardChoices(string[] Choices, int[] Indices)
|
|
{
|
|
_ChoiceCards.SetChoices(Choices, Indices);
|
|
}
|
|
|
|
[NetworkCallable]
|
|
public void LockInChoice()
|
|
{
|
|
_ChoiceCards.LockInChoice();
|
|
_ChoiceCards.SendCustomNetworkEvent(NetworkEventTarget.All, "MakeChoiceTextVisible");
|
|
}
|
|
|
|
public bool VerifyMultipleChoiceResponse(int CorrectResponse)
|
|
{
|
|
bool IsCorrectResponse = _ChoiceCards.GetSelectedChoice() == CorrectResponse;
|
|
if (IsCorrectResponse)
|
|
{
|
|
SendCustomNetworkEvent(NetworkEventTarget.Owner, nameof(AdjustScore), 10);
|
|
}
|
|
|
|
return IsCorrectResponse;
|
|
}
|
|
|
|
|
|
[NetworkCallable]
|
|
public void EnableRiskCards(bool Enable)
|
|
{
|
|
_RiskCards.gameObject.SetActive(Enable);
|
|
_RiskCards.ResetCards();
|
|
_RiskCards.SendCustomNetworkEvent(NetworkEventTarget.Owner, "MakeRiskTextVisible");
|
|
}
|
|
|
|
[NetworkCallable]
|
|
public void LockInRisk()
|
|
{
|
|
_RiskCards.LockInChoice();
|
|
_RiskCards.SendCustomNetworkEvent(NetworkEventTarget.All, "MakeRiskTextVisible");
|
|
}
|
|
|
|
[NetworkCallable]
|
|
public void VerifyFinalRoundResponse(int CorrectResponse)
|
|
{
|
|
if (_ChoiceCards.GetSelectedChoice() == CorrectResponse)
|
|
{
|
|
SendCustomNetworkEvent(NetworkEventTarget.Owner, nameof(AdjustScore), _RiskCards.GetSelectedChoice());
|
|
}
|
|
else
|
|
{
|
|
SendCustomNetworkEvent(NetworkEventTarget.Owner, nameof(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 StartBuzzedInEffect()
|
|
{
|
|
FlashScoreboard = true;
|
|
}
|
|
private void BeginBuzzedInEffect()
|
|
{
|
|
TickFlashingScoreboard();
|
|
}
|
|
public void TickFlashingScoreboard()
|
|
{
|
|
if (_FlashingScoreboardCounter < MAX_FLASH_COUNTER)
|
|
{
|
|
_FlashingScoreboardCounter++;
|
|
switch (_FlashingScoreboardCounter % 2)
|
|
{
|
|
case 1:
|
|
InvertScoreboardColours();
|
|
break;
|
|
case 0:
|
|
default:
|
|
ResetScoreboardColours();
|
|
break;
|
|
}
|
|
|
|
SendCustomEventDelayedSeconds(nameof(TickFlashingScoreboard), 0.15f);
|
|
}
|
|
else {
|
|
ResetScoreboardColours();
|
|
_FlashingScoreboardCounter = 0;
|
|
}
|
|
}
|
|
private void InvertScoreboardColours()
|
|
{
|
|
_ScorecardUI.color = Color.black;
|
|
_ScorecardBackground.color = Color.white;
|
|
}
|
|
private void ResetScoreboardColours()
|
|
{
|
|
_ScorecardUI.color = Color.white;
|
|
_ScorecardBackground.color = Color.black;
|
|
}
|
|
|
|
|
|
public void HighlightPodium(bool Highlight)
|
|
{
|
|
_HighlightEffect.SetActive(Highlight);
|
|
}
|
|
|
|
|
|
[NetworkCallable]
|
|
public void ShowAuxiliaryVideoScreen(bool Show)
|
|
{
|
|
Vector3 LocalPosition = _AuxiliaryVideoScreen.localPosition;
|
|
_AuxiliaryVideoScreen.localPosition = new Vector3(LocalPosition.x, Show ? 0.0f : -1000.0f, LocalPosition.z);
|
|
}
|
|
|
|
[NetworkCallable]
|
|
public void ShowAuxiliaryMapScreen(bool Show)
|
|
{
|
|
Vector3 LocalPosition = _AuxiliaryMapScreen.localPosition;
|
|
_AuxiliaryMapScreen.localPosition = new Vector3(LocalPosition.x, Show ? 0.0f : -1000.0f, LocalPosition.z);
|
|
}
|
|
|
|
|
|
public string PlayerName
|
|
{
|
|
set
|
|
{
|
|
_PlayerName = value;
|
|
_NameplateUI.text = value;
|
|
}
|
|
get => _PlayerName;
|
|
}
|
|
|
|
public int PlayerScore
|
|
{
|
|
set
|
|
{
|
|
_PlayerScore = value;
|
|
_ScorecardUI.text = PlayerScore.ToString();
|
|
}
|
|
get => _PlayerScore;
|
|
}
|
|
|
|
public bool ShowScoreCard
|
|
{
|
|
set
|
|
{
|
|
_ShowScoreCard = value;
|
|
_DisplayScorecardObject(value);
|
|
}
|
|
get => _ShowScoreCard;
|
|
}
|
|
|
|
public bool FlashScoreboard
|
|
{
|
|
set
|
|
{
|
|
_FlashScoreboard = value;
|
|
if (_FlashScoreboard)
|
|
{
|
|
BeginBuzzedInEffect();
|
|
}
|
|
else
|
|
{
|
|
_FlashingScoreboardCounter = 0;
|
|
}
|
|
RequestSerialization();
|
|
}
|
|
get => _FlashScoreboard;
|
|
}
|
|
|
|
private bool EnableOwnershipTransfer
|
|
{
|
|
set
|
|
{
|
|
_EnableOwnershipTransfer = value;
|
|
_OwnershipInteract.gameObject.SetActive(_EnableOwnershipTransfer);
|
|
_TeleportButton.SetActive(_EnableOwnershipTransfer);
|
|
}
|
|
get => _EnableOwnershipTransfer;
|
|
}
|
|
}
|