89 lines
3.0 KiB
C#
89 lines
3.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using TMPro;
|
|
using UnityEngine.UI;
|
|
|
|
public class SecretPlanetWeight : MonoBehaviour
|
|
{
|
|
[SerializeField] private TextMeshProUGUI _resultText;
|
|
[SerializeField] private Button _checkButton;
|
|
[SerializeField] private Rigidbody _astronautRb;
|
|
|
|
private const float _earthGravity = 9.81f;
|
|
float[] _gravities = { 274f, 3.7f, 8.87f, 1.62f, 3.71f, 24.79f, 10.44f, 8.69f, 11.15f, 9.81f };
|
|
|
|
void OnEnable()
|
|
{
|
|
RebuildDropdowns();
|
|
}
|
|
|
|
void RebuildDropdowns()
|
|
{
|
|
float mass = _astronautRb != null ? _astronautRb.mass : 75f;
|
|
SecretDropdown[] secretDropdowns = FindObjectsOfType<SecretDropdown>(true);
|
|
foreach (var sd in secretDropdowns)
|
|
{
|
|
sd.Dropdown.ClearOptions();
|
|
sd.Dropdown.options.Add(new TMP_Dropdown.OptionData("Îáðàòè"));
|
|
foreach (var g in _gravities)
|
|
{
|
|
float weightN = mass * g;
|
|
float massKg = weightN / _earthGravity;
|
|
sd.Dropdown.options.Add(new TMP_Dropdown.OptionData(Mathf.RoundToInt(massKg) + " êã"));
|
|
}
|
|
sd.Dropdown.value = 0;
|
|
sd.Dropdown.RefreshShownValue();
|
|
}
|
|
_checkButton.onClick.RemoveAllListeners();
|
|
_checkButton.onClick.AddListener(CheckAll);
|
|
_resultText.alpha = 0f;
|
|
}
|
|
|
|
void CheckAll()
|
|
{
|
|
float mass = _astronautRb != null ? _astronautRb.mass : 75f;
|
|
int correct = 0;
|
|
SecretDropdown[] secretDropdowns = FindObjectsOfType<SecretDropdown>(true);
|
|
foreach (var sd in secretDropdowns)
|
|
{
|
|
int planetIndex = sd.PlanetIndex;
|
|
float correctWeightN = mass * _gravities[planetIndex];
|
|
float correctMassKg = correctWeightN / _earthGravity;
|
|
int selectedIndex = sd.Dropdown.value - 1;
|
|
if (selectedIndex < 0) continue;
|
|
float selectedWeightN = mass * _gravities[selectedIndex];
|
|
float selectedMassKg = selectedWeightN / _earthGravity;
|
|
if (Mathf.Abs(selectedMassKg - correctMassKg) < 1f)
|
|
{
|
|
correct++;
|
|
sd.Dropdown.GetComponentInParent<Image>().color = Color.green;
|
|
}
|
|
else
|
|
{
|
|
sd.Dropdown.GetComponentInParent<Image>().color = Color.red;
|
|
}
|
|
}
|
|
if (correct == secretDropdowns.Length)
|
|
{
|
|
_resultText.text = "Áëèñêó÷å! Öå ïðàâèëüíà â³äïîâ³äü!";
|
|
_resultText.color = Color.green;
|
|
}
|
|
else
|
|
{
|
|
_resultText.text = "Ìàéæå! Ñïðîáóé ùå ðàç";
|
|
_resultText.color = Color.red;
|
|
}
|
|
StartCoroutine(ShowText());
|
|
}
|
|
|
|
IEnumerator ShowText()
|
|
{
|
|
float t = 0f;
|
|
while (t < 1f) { t += Time.deltaTime * 2f; _resultText.alpha = t; yield return null; }
|
|
yield return new WaitForSeconds(3f);
|
|
t = 1f;
|
|
while (t > 0f) { t -= Time.deltaTime * 2f; _resultText.alpha = t; yield return null; }
|
|
}
|
|
}
|