using UdonSharp; using UnityEngine; using VRC.SDK3.Data; using VRC.SDK3.UdonNetworkCalling; using VRC.Udon.Common.Interfaces; public enum ContinentMap { Africa, Asia, Europe, NorthAmerica, Oceania, SouthAmerica } public enum GameStatus { Pregame, Begin, RanOutOfTime, RanOutOfMarkers, Win } [UdonBehaviourSyncMode(BehaviourSyncMode.Manual)] public class GameManagerRound3 : GameManagerBase { [SerializeField] private CaseManager _CaseManager; [SerializeField] private FloorMap[] _Maps; [SerializeField] private FloorMapMarker[] _Markers; [SerializeField] private AudioManager _AudioManager; [UdonSynced] private ContinentMap _ActiveMap = ContinentMap.Africa; private DataDictionary _ContinentData; [UdonSynced] private int _StageIndex = 0; [UdonSynced] private GameStatus _GameStatus = GameStatus.Pregame; [UdonSynced] private int _Timer = TIMER_LENGTH; private const float LENGTH_OF_ONE_SECOND = 1.04444444444444444f; private const int TIMER_LENGTH = 45; [UdonSynced] private int _SuccessCounter = 0; [UdonSynced] private int _FailureCounter = 0; private const int MAX_FAILURE_COUNT = 2; private const int MAX_SUCCESS_COUNT = 8; public override void InitialiseGameMode() { base.InitialiseGameMode(); } public override void LoadQuestionData(DataToken Data) { _ContinentData = Data.DataDictionary; if (_ContinentData.ContainsKey("Continent")) { _ActiveMap = (ContinentMap)(int)_ContinentData["Continent"].Number; for (int i = 0; i < _Maps.Length; i++) { FloorMap Map = _Maps[i]; if (Map != null) { Map.gameObject.SetActive(i == (int)_ActiveMap); } } FloorMap CurrentMap = GetCurrentMap(); NetworkCalling.SendCustomNetworkEvent((IUdonEventReceiver)CurrentMap, NetworkEventTarget.Owner, "RandomiseCountries"); } else { Debug.LogError("Malformed round data. Ensure Round 3 contains a continent to use for the map."); } EnableInteraction("Display Briefing"); } private void DisplayBriefing() { HostCardCaptureCarmenExplainerInterface CaptureCarmenExplainerInterface = (HostCardCaptureCarmenExplainerInterface)GetHostCardInterface(RoundSegmentType.CaptureCarmenExplainer); CaptureCarmenExplainerInterface.HeaderUI.text = RoundSegmentTypeToString(RoundSegmentType.CaptureCarmenExplainer); EnableInteraction("Begin"); } public void BeginRound() { UpdateInterface(); FloorMapMarker Marker = GetCurrentMarker(); Marker.EnablePickup(true); Marker.LocationFindingEnabled = true; StartTimer(); } private void StartTimer() { NetworkCalling.SendCustomNetworkEvent((IUdonEventReceiver)_AudioManager, NetworkEventTarget.All, "PlayMusicLoop", MusicEventType.CarmenChaseMusic); _GameStatus = GameStatus.Begin; _Timer = TIMER_LENGTH; SendCustomEventDelayedSeconds(nameof(TickTimer), LENGTH_OF_ONE_SECOND); RequestSerialization(); } public void TickTimer() { _Timer--; if (_Timer < 0) { NetworkCalling.SendCustomNetworkEvent((IUdonEventReceiver)this, NetworkEventTarget.Owner, nameof(GameHasBeenLost), false); return; } SendCustomEventDelayedSeconds(nameof(TickTimer), LENGTH_OF_ONE_SECOND); RequestSerialization(); } public void CorrectResponse() { PlayCorrectSound(); FloorMapMarker Marker = GetCurrentMarker(); Marker.EnablePickup(false); Marker.LocationFindingEnabled = false; _FailureCounter = 0; _SuccessCounter++; if (_SuccessCounter >= MAX_SUCCESS_COUNT) { NetworkCalling.SendCustomNetworkEvent((IUdonEventReceiver)this, NetworkEventTarget.Owner, nameof(GameHasBeenWon)); } else { SetUpNextCountry(); } RequestSerialization(); } public void IncorrectResponse() { _FailureCounter++; if (_FailureCounter >= MAX_FAILURE_COUNT) { _FailureCounter = 0; FloorMapMarker Marker = GetCurrentMarker(); Marker.EnablePickup(false); Marker.LocationFindingEnabled = false; SetUpNextCountry(); } PlayIncorrectSound(); } public FloorMap GetCurrentMap() { return _Maps[(int)_ActiveMap]; } public FloorMapMarker GetCurrentMarker() { return _Markers[GetCurrentMap().GetCurrentCountryIndex()]; } public string GetCurrentCountry() { return GetCurrentMap().GetCurrentCountry(); } public string GetCurrentCity() { return GetCurrentMap().GetCurrentCity(); } public void SetUpNextCountry() { int NextCountry = GetCurrentMap().NextCountry(); if (NextCountry < 0) { // If we ran out of countries, lose the game here. NetworkCalling.SendCustomNetworkEvent((IUdonEventReceiver)this, NetworkEventTarget.Owner, nameof(GameHasBeenLost), true); return; } _Markers[NextCountry].EnablePickup(true); _Markers[NextCountry].LocationFindingEnabled = true; UpdateInterface(); } [NetworkCallable] public void GameHasBeenWon() { if ((int)_GameStatus <= (int)GameStatus.Begin) { GameStatusUpdate(GameStatus.Win); HostCardBetweenRoundsInterface GameWinInterface = (HostCardBetweenRoundsInterface)GetHostCardInterface(RoundSegmentType.BetweenSegments); GameWinInterface.HeaderUI.text = "The player has won the game. " + _Timer + " seconds to spare."; NetworkCalling.SendCustomNetworkEvent((IUdonEventReceiver)_AudioManager, NetworkEventTarget.All, "PlayMusicLoop", MusicEventType.CarmenSandiegoWindDown); } } [NetworkCallable] public void GameHasBeenLost(bool RanOutOfMarkers) { if ((int)_GameStatus != (int)GameStatus.Win) { for (int i = 0; i < _Markers.Length; i++) { _Markers[i].EnablePickup(false); } GameStatusUpdate(RanOutOfMarkers ? GameStatus.RanOutOfMarkers : GameStatus.RanOutOfTime); HostCardBetweenRoundsInterface GameLossInterface = (HostCardBetweenRoundsInterface)GetHostCardInterface(RoundSegmentType.BetweenSegments); GameLossInterface.HeaderUI.text = "The player has lost. " + _SuccessCounter + " countries in " + TIMER_LENGTH + " seconds."; NetworkCalling.SendCustomNetworkEvent((IUdonEventReceiver)_AudioManager, NetworkEventTarget.All, "StopMusic"); } } private void GameStatusUpdate(GameStatus NewStatus) { if ((int)_GameStatus <= (int)GameStatus.Begin) { _GameStatus = NewStatus; } RequestSerialization(); } public void PlayCorrectSound() { _AudioManager.PlaySFX(SFXEventType.MapCorrect); } public void PlayIncorrectSound() { _AudioManager.PlaySFX(SFXEventType.MapIncorrect); } private void UpdateInterface() { HostCardCaptureCarmenInterface CaptureCarmenInterface = (HostCardCaptureCarmenInterface)GetHostCardInterface(RoundSegmentType.CaptureCarmen); CaptureCarmenInterface.HeaderUI.text = RoundSegmentTypeToString(RoundSegmentType.CaptureCarmen); CaptureCarmenInterface.CommentUI.text = GetCurrentCity() + ", " + GetCurrentCountry(); } protected override HostCardInterfaceBase GetHostCardInterface(RoundSegmentType Question) { return _HostCard.EnableHostCardDisplay(RoundType.CaptureCarmen, Question); } protected override void _HostCardUseButtonDown_Internal() { DisableInteraction(); _StageIndex++; switch (_StageIndex) { case 1: DisplayBriefing(); break; case 2: BeginRound(); break; } } }