149 lines
3.7 KiB
C#

using System.Linq;
using UdonSharp;
using VRC.SDK3.UdonNetworkCalling;
using VRC.SDKBase;
using VRC.Udon.Common.Interfaces;
[UdonBehaviourSyncMode(BehaviourSyncMode.Manual)]
public class FloorMap : UdonSharpBehaviour
{
public FloorMapCountry[] Countries;
[UdonSynced] private int _CurrentCountry = 0;
[UdonSynced, FieldChangeCallback(nameof(RandomisedCountriesAndCities))] private int[] _RandomisedCountriesAndCities;
private string[] _ChosenCountries = new string[MAX_SELECTED_COUNTRIES];
private string[] _ChosenCities = new string[MAX_SELECTED_COUNTRIES];
private const int MAX_SELECTED_COUNTRIES = 13;
public void RandomiseCountries()
{
for (int i = 0; i < Countries.Length; i++)
{
Countries[i].gameObject.SetActive(false);
}
UnityEngine.Random.InitState(Networking.GetServerTimeInMilliseconds());
int[] NewRandomCountries = new int[MAX_SELECTED_COUNTRIES];
int[] NewRandomCities = new int[MAX_SELECTED_COUNTRIES];
// Fill the countries array with a value too big to be valid.
int NumberOfCountries = Countries.Length;
for (int i = 0; i < MAX_SELECTED_COUNTRIES; i++)
{
NewRandomCountries[i] = NumberOfCountries;
}
for (int i = 0; i < MAX_SELECTED_COUNTRIES; i++)
{
int NewPulledIndex = NumberOfCountries;
while (CountryIsAlreadyInArray(NewRandomCountries, NewPulledIndex) || Countries[NewPulledIndex].HardModeSelection)
{
NewPulledIndex = UnityEngine.Random.Range(0, NumberOfCountries);
}
NewRandomCountries[i] = NewPulledIndex;
NewRandomCities[i] = UnityEngine.Random.Range(0, Countries[NewPulledIndex].Locations.Length);
}
// We're syncing the countries and cities as one array to keep
// things simple to sync all at once.
// Array format is all countries first, then all cities.
int[] RandomCountriesAndCities = new int[MAX_SELECTED_COUNTRIES * 2];
NewRandomCountries.CopyTo(RandomCountriesAndCities, 0);
NewRandomCities.CopyTo(RandomCountriesAndCities, MAX_SELECTED_COUNTRIES);
RandomisedCountriesAndCities = RandomCountriesAndCities;
RequestSerialization();
}
private bool CountryIsAlreadyInArray(int[] Countries, int IndexToCheck)
{
for (int i = 0; i < MAX_SELECTED_COUNTRIES; i++)
{
if (Countries[i] == IndexToCheck)
{
return true;
}
}
return false;
}
public void AssignRandomCountriesAndCities(int[] RandomisedCountriesAndCities)
{
DisableAllLocations();
_CurrentCountry = 0;
for (int i = 0; i < MAX_SELECTED_COUNTRIES; i++)
{
FloorMapCountry Country = Countries[RandomisedCountriesAndCities[i]];
Country.gameObject.SetActive(true);
FloorMapLocation Location = Country.Locations[RandomisedCountriesAndCities[MAX_SELECTED_COUNTRIES + i]];
Location.gameObject.SetActive(true);
_ChosenCountries[i] = Location.Country;
_ChosenCities[i] = Location.City;
}
}
public int NextCountry()
{
_CurrentCountry++;
if (_CurrentCountry >= MAX_SELECTED_COUNTRIES)
{
// We ran out of countries. Just end the game here.
return -1;
}
RequestSerialization();
return _CurrentCountry;
}
public int GetCurrentCountryIndex()
{
return _CurrentCountry;
}
public string GetCurrentCountry()
{
return _ChosenCountries[_CurrentCountry];
}
public string GetCurrentCity()
{
return _ChosenCities[_CurrentCountry];
}
private void DisableAllLocations()
{
for (int i = 0; i < Countries.Length; i++)
{
FloorMapCountry Country = Countries[i];
for (int j = 0; j < Country.Locations.Length; j++)
{
Country.Locations[j].gameObject.SetActive(false);
}
Country.gameObject.SetActive(false);
}
}
public int[] RandomisedCountriesAndCities
{
set
{
_RandomisedCountriesAndCities = value;
AssignRandomCountriesAndCities(_RandomisedCountriesAndCities);
}
get => _RandomisedCountriesAndCities;
}
}