121 lines
3.3 KiB
C#
121 lines
3.3 KiB
C#
|
|
using UdonSharp;
|
|
using UnityEngine;
|
|
using VRC.SDK3.UdonNetworkCalling;
|
|
using VRC.SDKBase;
|
|
using VRC.Udon;
|
|
using VRC.Udon.Common.Interfaces;
|
|
|
|
public class NoPointsMeter : UdonSharpBehaviour
|
|
{
|
|
private float currentDisplayedScore = 100f;
|
|
[SerializeField] private int targetScore = 100;
|
|
[SerializeField] private bool isCounting = false;
|
|
[UdonSynced] public int networkTargetScore;
|
|
public string propertyName = "_MyFloat";
|
|
|
|
public Material pointBarMaterial;
|
|
public Material pointMeterMaterial;
|
|
public Material screenMaterial;
|
|
public Material platformMaterial;
|
|
public Material circlesMaterial;
|
|
|
|
public float baseSpeed = 10f;
|
|
public TMPro.TMP_Text scoreText;
|
|
public AudioSource audioSource;
|
|
|
|
public AudioClip pointsMeterBar;
|
|
public AudioClip pointsMeterStops;
|
|
public AudioClip pointlessAnswer;
|
|
public AudioClip incorrectAnswer;
|
|
|
|
void Start()
|
|
{
|
|
ChangeSetColour(new Color(0.1686f, 0.6706f, 0.8471f));
|
|
}
|
|
|
|
public override void OnDeserialization()
|
|
{
|
|
StartCountdown();
|
|
}
|
|
|
|
public void StartCountdown()
|
|
{
|
|
|
|
targetScore = networkTargetScore;
|
|
isCounting = true;
|
|
}
|
|
|
|
[NetworkCallable]
|
|
public void ResetMeter()
|
|
{
|
|
currentDisplayedScore = 100f;
|
|
targetScore = 100;
|
|
isCounting = false;
|
|
networkTargetScore = 100;
|
|
ChangeSetColour(new Color(0.1686f, 0.6706f, 0.8471f));
|
|
pointBarMaterial.SetFloat(propertyName, 0f);
|
|
scoreText.text = "100";
|
|
audioSource.Stop();
|
|
}
|
|
|
|
public void ResetMeterNetworked()
|
|
{
|
|
SendCustomNetworkEvent(NetworkEventTarget.All, "ResetMeter");
|
|
}
|
|
|
|
public void ChangeSetColour(Color color)
|
|
{
|
|
platformMaterial.SetColor("_EmissionColor", color);
|
|
screenMaterial.SetColor("_EmissionColor", color);
|
|
pointMeterMaterial.SetColor("_EmissionColor", color);
|
|
circlesMaterial.SetColor("_EmissionColor", color);
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (!isCounting) return;
|
|
|
|
float distance = currentDisplayedScore - targetScore;
|
|
|
|
if (!audioSource.isPlaying)
|
|
{
|
|
audioSource.PlayOneShot(pointsMeterBar);
|
|
}
|
|
|
|
if (distance <= 0.5f)
|
|
{
|
|
currentDisplayedScore = targetScore;
|
|
isCounting = false;
|
|
audioSource.Stop();
|
|
if (targetScore == 0)
|
|
{
|
|
audioSource.PlayOneShot(pointlessAnswer);
|
|
ChangeSetColour(Color.green);
|
|
}
|
|
else if (targetScore == 100)
|
|
{
|
|
ChangeSetColour(Color.red);
|
|
scoreText.text = "X";
|
|
audioSource.PlayOneShot(incorrectAnswer);
|
|
}
|
|
else
|
|
{
|
|
audioSource.PlayOneShot(pointsMeterStops);
|
|
}
|
|
return;
|
|
}
|
|
float normalizedScore = Mathf.Clamp01(currentDisplayedScore / 100f);
|
|
float speedMultiplier = Mathf.Sqrt(normalizedScore);
|
|
|
|
currentDisplayedScore = Mathf.MoveTowards(
|
|
currentDisplayedScore,
|
|
targetScore,
|
|
(baseSpeed * speedMultiplier) * Time.deltaTime
|
|
);
|
|
|
|
pointBarMaterial.SetFloat(propertyName, 1 - currentDisplayedScore / 100.0f);
|
|
scoreText.text = Mathf.RoundToInt(currentDisplayedScore).ToString();
|
|
}
|
|
}
|