295 lines
8.6 KiB
C#
295 lines
8.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
public class CountrySelector : MonoBehaviour
|
|
{
|
|
[Header("UI")]
|
|
public TextMeshProUGUI countryNameText;
|
|
public TextMeshProUGUI utcText;
|
|
public TextMeshProUGUI timeText;
|
|
public TextMeshProUGUI anomalyText;
|
|
|
|
[Header("References")]
|
|
public PlaneController planeController;
|
|
public GlobeBuilder globeBuilder;
|
|
public string startCountryName = "Ukraine";
|
|
|
|
private CountryData _selected;
|
|
private CountryData _hovered;
|
|
private CountryData _startCountry;
|
|
private Camera _cam;
|
|
private float _lastClickTime;
|
|
private CountryData _lastClickedCountry;
|
|
private const float DoubleClickTime = 0.35f;
|
|
private bool _startMessageShown = false;
|
|
private Coroutine _hideCoroutine;
|
|
|
|
void Start()
|
|
{
|
|
_cam = Camera.main;
|
|
HideAll();
|
|
|
|
CountryData[] all = FindObjectsOfType<CountryData>();
|
|
foreach (CountryData c in all)
|
|
{
|
|
if (c.countryName == startCountryName)
|
|
{
|
|
_startCountry = c;
|
|
break;
|
|
}
|
|
}
|
|
if (_startCountry == null && all.Length > 0)
|
|
_startCountry = all[0];
|
|
|
|
if (!_startMessageShown)
|
|
{
|
|
_startMessageShown = true;
|
|
StartCoroutine(ShowStartMessage());
|
|
}
|
|
}
|
|
|
|
string GetDisplayName(CountryData country)
|
|
{
|
|
return !string.IsNullOrEmpty(country.countryNameUkr)
|
|
? country.countryNameUkr
|
|
: country.countryName;
|
|
}
|
|
|
|
IEnumerator ShowStartMessage()
|
|
{
|
|
if (countryNameText != null)
|
|
{
|
|
countryNameText.gameObject.SetActive(true);
|
|
utcText.gameObject.SetActive(true);
|
|
timeText.gameObject.SetActive(true);
|
|
|
|
countryNameText.text = "Ïî÷èíàºìî íàø ïîë³ò ç Óêðà¿íè!";
|
|
utcText.text = "UTC+2";
|
|
timeText.text = "Çàðàç: " + System.DateTime.UtcNow.AddHours(2).ToString("HH:mm");
|
|
|
|
yield return new WaitForSeconds(2f);
|
|
|
|
float t = 0f;
|
|
while (t < 1f)
|
|
{
|
|
t += Time.deltaTime;
|
|
float alpha = 1f - t;
|
|
countryNameText.canvasRenderer.SetAlpha(alpha);
|
|
utcText.canvasRenderer.SetAlpha(alpha);
|
|
timeText.canvasRenderer.SetAlpha(alpha);
|
|
yield return null;
|
|
}
|
|
|
|
HideAll();
|
|
ResetAlpha();
|
|
}
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (planeController == null || !planeController.IsFlying())
|
|
{
|
|
HandleHover();
|
|
HandleClick();
|
|
HandleHotkeys();
|
|
}
|
|
}
|
|
|
|
void HandleHotkeys()
|
|
{
|
|
if (Input.GetKeyDown(KeyCode.Alpha1))
|
|
FlyToByName("American Samoa");
|
|
if (Input.GetKeyDown(KeyCode.Alpha2))
|
|
FlyToByName("Kiribati");
|
|
}
|
|
|
|
void FlyToByName(string name)
|
|
{
|
|
CountryData[] all = FindObjectsOfType<CountryData>();
|
|
foreach (CountryData c in all)
|
|
{
|
|
if (c.countryName == name)
|
|
{
|
|
FlyTo(c);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
void HandleHover()
|
|
{
|
|
Ray ray = _cam.ScreenPointToRay(Input.mousePosition);
|
|
if (Physics.Raycast(ray, out RaycastHit hit))
|
|
{
|
|
CountryData c = hit.collider.GetComponent<CountryData>()
|
|
?? hit.collider.GetComponentInParent<CountryData>();
|
|
if (c != _hovered)
|
|
{
|
|
if (_hovered != null && _hovered != _selected) _hovered.OnHoverExit();
|
|
_hovered = c;
|
|
if (_hovered != null && _hovered != _selected) _hovered.OnHoverEnter();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (_hovered != null && _hovered != _selected) _hovered.OnHoverExit();
|
|
_hovered = null;
|
|
}
|
|
}
|
|
|
|
void HandleClick()
|
|
{
|
|
if (!Input.GetMouseButtonDown(0)) return;
|
|
if (UnityEngine.EventSystems.EventSystem.current != null &&
|
|
UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()) return;
|
|
|
|
Ray ray = _cam.ScreenPointToRay(Input.mousePosition);
|
|
if (!Physics.Raycast(ray, out RaycastHit hit)) { DeselectAll(); return; }
|
|
|
|
CountryData clicked = hit.collider.GetComponent<CountryData>()
|
|
?? hit.collider.GetComponentInParent<CountryData>();
|
|
if (clicked == null) { DeselectAll(); return; }
|
|
|
|
float timeSinceLastClick = Time.time - _lastClickTime;
|
|
bool isDoubleClick = timeSinceLastClick < DoubleClickTime && _lastClickedCountry == clicked;
|
|
|
|
_lastClickTime = Time.time;
|
|
_lastClickedCountry = clicked;
|
|
|
|
if (isDoubleClick)
|
|
FlyTo(clicked);
|
|
else
|
|
SingleClick(clicked);
|
|
}
|
|
|
|
void SingleClick(CountryData country)
|
|
{
|
|
if (_hideCoroutine != null) StopCoroutine(_hideCoroutine);
|
|
ResetAlpha();
|
|
|
|
if (_selected != null && _selected != country) _selected.Deselect();
|
|
_selected = country;
|
|
_selected.Select();
|
|
if (globeBuilder != null) globeBuilder.SetRotation(false);
|
|
|
|
if (country == _startCountry)
|
|
ShowFullInfo(country);
|
|
else
|
|
ShowBasicInfo(country);
|
|
}
|
|
|
|
void ShowBasicInfo(CountryData country)
|
|
{
|
|
HideAll();
|
|
if (countryNameText != null)
|
|
{
|
|
countryNameText.gameObject.SetActive(true);
|
|
countryNameText.text = GetDisplayName(country);
|
|
}
|
|
if (utcText != null)
|
|
{
|
|
utcText.gameObject.SetActive(true);
|
|
utcText.text = country.utcLabel;
|
|
}
|
|
}
|
|
|
|
void ShowFullInfo(CountryData country)
|
|
{
|
|
if (_hideCoroutine != null) StopCoroutine(_hideCoroutine);
|
|
ResetAlpha();
|
|
HideAll();
|
|
|
|
if (countryNameText != null)
|
|
{
|
|
countryNameText.gameObject.SetActive(true);
|
|
countryNameText.text = GetDisplayName(country);
|
|
}
|
|
if (utcText != null)
|
|
{
|
|
utcText.gameObject.SetActive(true);
|
|
utcText.text = country.utcLabel;
|
|
}
|
|
if (timeText != null)
|
|
{
|
|
timeText.gameObject.SetActive(true);
|
|
timeText.text = "Çàðàç: " + country.GetCurrentTime();
|
|
}
|
|
if (anomalyText != null)
|
|
{
|
|
anomalyText.gameObject.SetActive(country.hasAnomaly);
|
|
if (country.hasAnomaly)
|
|
anomalyText.text = country.anomalyFact;
|
|
}
|
|
|
|
_hideCoroutine = StartCoroutine(AutoHide());
|
|
}
|
|
|
|
IEnumerator AutoHide()
|
|
{
|
|
yield return new WaitForSeconds(5f);
|
|
float t = 0f;
|
|
while (t < 1f)
|
|
{
|
|
t += Time.deltaTime;
|
|
float alpha = 1f - t;
|
|
if (countryNameText != null) countryNameText.canvasRenderer.SetAlpha(alpha);
|
|
if (utcText != null) utcText.canvasRenderer.SetAlpha(alpha);
|
|
if (timeText != null) timeText.canvasRenderer.SetAlpha(alpha);
|
|
if (anomalyText != null) anomalyText.canvasRenderer.SetAlpha(alpha);
|
|
yield return null;
|
|
}
|
|
HideAll();
|
|
ResetAlpha();
|
|
}
|
|
|
|
void FlyTo(CountryData destination)
|
|
{
|
|
if (planeController == null || _startCountry == null) return;
|
|
if (destination == _startCountry) return;
|
|
|
|
if (_hideCoroutine != null) StopCoroutine(_hideCoroutine);
|
|
HideAll();
|
|
ResetAlpha();
|
|
|
|
if (_selected != null) { _selected.Deselect(); _selected = null; }
|
|
if (globeBuilder != null) globeBuilder.SetRotation(false);
|
|
|
|
planeController.Fly(
|
|
_startCountry,
|
|
destination,
|
|
() =>
|
|
{
|
|
_startCountry = destination;
|
|
ShowFullInfo(destination);
|
|
}
|
|
);
|
|
}
|
|
|
|
void DeselectAll()
|
|
{
|
|
if (_hideCoroutine != null) StopCoroutine(_hideCoroutine);
|
|
if (_selected != null) { _selected.Deselect(); _selected = null; }
|
|
HideAll();
|
|
ResetAlpha();
|
|
if (globeBuilder != null) globeBuilder.SetRotation(true);
|
|
}
|
|
|
|
void HideAll()
|
|
{
|
|
if (countryNameText != null) countryNameText.gameObject.SetActive(false);
|
|
if (utcText != null) utcText.gameObject.SetActive(false);
|
|
if (timeText != null) timeText.gameObject.SetActive(false);
|
|
if (anomalyText != null) anomalyText.gameObject.SetActive(false);
|
|
}
|
|
|
|
void ResetAlpha()
|
|
{
|
|
if (countryNameText != null) countryNameText.canvasRenderer.SetAlpha(1f);
|
|
if (utcText != null) utcText.canvasRenderer.SetAlpha(1f);
|
|
if (timeText != null) timeText.canvasRenderer.SetAlpha(1f);
|
|
if (anomalyText != null) anomalyText.canvasRenderer.SetAlpha(1f);
|
|
}
|
|
}
|