Jamie Greunbaum 05cf67b1e2 - Location board orientation corrected to properly match Unity axes.
- Markers are no longer thrown when dropped while walking.
- Markers stay much more stable and are far less prone to falling over.
- Rounds 2 and 3 properly sync to other clients.
2025-06-26 17:09:42 -04:00

142 lines
3.5 KiB
C#

using System.Linq;
using UdonSharp;
using VRC.SDKBase;
[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()
{
_CurrentCountry = 0;
UnityEngine.Random.InitState(Networking.GetServerTimeInMilliseconds());
int[] NewRandomCountries = new int[MAX_SELECTED_COUNTRIES];
int[] NewRandomCities = new int[MAX_SELECTED_COUNTRIES];
// Fill the countries array with values too big to be considered 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))
{
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[] NewRandomCountriesAndCities = new int[MAX_SELECTED_COUNTRIES * 2];
NewRandomCountries.CopyTo(NewRandomCountriesAndCities, 0);
NewRandomCities.CopyTo(NewRandomCountriesAndCities, MAX_SELECTED_COUNTRIES);
RandomisedCountriesAndCities = NewRandomCountriesAndCities;
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;
}
private void AssignRandomCountriesAndCities()
{
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;
}
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();
}
get => _RandomisedCountriesAndCities;
}
}