145 lines
5.0 KiB
C#
145 lines
5.0 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using TMPro;
|
||
using System.Collections;
|
||
|
||
public enum FrictionType { Статичне, Ковзання, Кочення }
|
||
public class ObjectInfoDisplay : MonoBehaviour
|
||
{
|
||
|
||
[SerializeField] private TextMeshProUGUI _nameText;
|
||
[SerializeField] private TextMeshProUGUI _infoText;
|
||
public float displayTime = 2f;
|
||
public float fadeTime = 1f;
|
||
|
||
private Coroutine _currentCoroutine;
|
||
|
||
private void Start()
|
||
{
|
||
SetAlpha(0f);
|
||
}
|
||
|
||
public void ShowInfo(string name, float mass)
|
||
{
|
||
if (_currentCoroutine != null)
|
||
StopCoroutine(_currentCoroutine);
|
||
_currentCoroutine = StartCoroutine(DisplayAndFade(name, mass));
|
||
}
|
||
|
||
public void ShowSlide(string objName, string surfaceName, float mu, FrictionType type)
|
||
{
|
||
if (_currentCoroutine != null)
|
||
StopCoroutine(_currentCoroutine);
|
||
_currentCoroutine = StartCoroutine(DisplayAndFadeSlide(objName, surfaceName, mu, type));
|
||
}
|
||
|
||
public void ShowFall(string objName, float speed)
|
||
{
|
||
if (_currentCoroutine != null)
|
||
StopCoroutine(_currentCoroutine);
|
||
_currentCoroutine = StartCoroutine(DisplayAndFadeFall(objName, speed));
|
||
}
|
||
|
||
public void ShowSlideAndFall(string objName, string surfaceName, float mu, FrictionType type, float speed)
|
||
{
|
||
if (_currentCoroutine != null)
|
||
StopCoroutine(_currentCoroutine);
|
||
_currentCoroutine = StartCoroutine(DisplaySlideAndFallSequence(objName, surfaceName, mu, type, speed));
|
||
}
|
||
|
||
private IEnumerator DisplayAndFade(string name, float mass)
|
||
{
|
||
_nameText.text = name;
|
||
if (mass >= 1f)
|
||
_infoText.text = "Маса: " + mass + " кг";
|
||
else
|
||
_infoText.text = "Маса: " + mass * 1000 + " г";
|
||
SetAlpha(1f);
|
||
yield return new WaitForSeconds(displayTime);
|
||
yield return Fade();
|
||
}
|
||
|
||
private IEnumerator DisplayAndFadeSlide(string objName, string surfaceName, float mu, FrictionType type)
|
||
{
|
||
string surfaceGenitive = surfaceName switch
|
||
{
|
||
"Дерев'яна поверхня" => "дерев'яній поверхні",
|
||
"Металева поверхня" => "металевій поверхні",
|
||
"Крижана поверхня" => "крижаній поверхні",
|
||
"Гумова поверхня" => "гумовій поверхні",
|
||
_ => surfaceName
|
||
};
|
||
string typeStr = type switch
|
||
{
|
||
FrictionType.Статичне => "статичне",
|
||
FrictionType.Ковзання => "ковзання",
|
||
FrictionType.Кочення => "кочення",
|
||
_ => ""
|
||
};
|
||
_nameText.text = objName + " по " + surfaceGenitive;
|
||
_infoText.text = "μ " + typeStr + " = " + mu.ToString("F2");
|
||
SetAlpha(1f);
|
||
yield return new WaitForSeconds(displayTime);
|
||
yield return Fade();
|
||
}
|
||
|
||
private IEnumerator DisplayAndFadeFall(string objName, float speed)
|
||
{
|
||
_nameText.text = objName;
|
||
_infoText.text = "Швидкість падіння: " + speed.ToString("F1") + " м/с";
|
||
SetAlpha(1f);
|
||
yield return new WaitForSeconds(displayTime);
|
||
yield return Fade();
|
||
}
|
||
|
||
private IEnumerator DisplaySlideAndFallSequence(string objName, string surfaceName, float mu, FrictionType type, float speed)
|
||
{
|
||
string surfaceGenitive = surfaceName switch
|
||
{
|
||
"Дерев'яна поверхня" => "дерев'яній поверхні",
|
||
"Металева поверхня" => "металевій поверхні",
|
||
"Крижана поверхня" => "крижаній поверхні",
|
||
"Гумова поверхня" => "гумовій поверхні",
|
||
_ => surfaceName
|
||
};
|
||
string typeStr = type switch
|
||
{
|
||
FrictionType.Статичне => "статичне",
|
||
FrictionType.Ковзання => "ковзання",
|
||
FrictionType.Кочення => "кочення",
|
||
_ => ""
|
||
};
|
||
_nameText.text = objName + " по " + surfaceGenitive;
|
||
_infoText.text = "μ " + typeStr + " = " + mu.ToString("F2");
|
||
SetAlpha(1f);
|
||
yield return new WaitForSeconds(2f);
|
||
|
||
_nameText.text = objName;
|
||
_infoText.text = "Швидкість падіння: " + speed.ToString("F1") + " м/с";
|
||
yield return new WaitForSeconds(2f);
|
||
yield return Fade();
|
||
}
|
||
|
||
private IEnumerator Fade()
|
||
{
|
||
float timer = 0f;
|
||
while (timer < fadeTime)
|
||
{
|
||
timer += Time.deltaTime;
|
||
SetAlpha(1f - timer / fadeTime);
|
||
yield return null;
|
||
}
|
||
SetAlpha(0f);
|
||
}
|
||
|
||
private void SetAlpha(float alpha)
|
||
{
|
||
Color nc = _nameText.color;
|
||
Color ic = _infoText.color;
|
||
nc.a = alpha;
|
||
ic.a = alpha;
|
||
_nameText.color = nc;
|
||
_infoText.color = ic;
|
||
}
|
||
}
|