Initial commit
This commit is contained in:
157
Assets/Materials/Scripts/SecretInfoManager.cs
Normal file
157
Assets/Materials/Scripts/SecretInfoManager.cs
Normal file
@@ -0,0 +1,157 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
using System;
|
||||
|
||||
public class SecretInfoManager : MonoBehaviour
|
||||
{
|
||||
[Header("Settings")]
|
||||
[SerializeField] private string targetTag = "Target";
|
||||
|
||||
[SerializeField] private Canvas secretCanvas;
|
||||
[SerializeField] private TextMeshProUGUI secretInfoText;
|
||||
|
||||
[SerializeField] private bool deleteZonesOnCompletion = true;
|
||||
[SerializeField] private float deleteDelay = 0.5f;
|
||||
|
||||
private int _totalTargetObjects;
|
||||
private HashSet<GameObject> _collectedObjects = new HashSet<GameObject>();
|
||||
private List<SecretInfoZone> _allZones = new List<SecretInfoZone>();
|
||||
|
||||
public event Action<int, int> OnProgressChanged;
|
||||
public int CollectedCount => _collectedObjects.Count;
|
||||
public int TotalCount => _totalTargetObjects;
|
||||
|
||||
private float _showDuration = 40f;
|
||||
|
||||
public enum LocationType
|
||||
{
|
||||
Earth,
|
||||
Moon
|
||||
}
|
||||
|
||||
[SerializeField] private LocationType locationType;
|
||||
|
||||
void Start()
|
||||
{
|
||||
//_totalTargetObjects = GameObject.FindGameObjectsWithTag(targetTag).Length;
|
||||
_totalTargetObjects = 9;
|
||||
|
||||
if (secretCanvas != null)
|
||||
{
|
||||
secretCanvas.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
_allZones.AddRange(FindObjectsOfType<SecretInfoZone>());
|
||||
UpdateProgressText();
|
||||
}
|
||||
|
||||
public void ObjectEntered(GameObject obj)
|
||||
{
|
||||
if (!obj.CompareTag(targetTag)) return;
|
||||
|
||||
if (_collectedObjects.Add(obj))
|
||||
{
|
||||
UpdateProgressText();
|
||||
CheckCompletion();
|
||||
}
|
||||
}
|
||||
|
||||
public void ObjectExited(GameObject obj)
|
||||
{
|
||||
if (!obj.CompareTag(targetTag))
|
||||
return;
|
||||
|
||||
if (_collectedObjects.Remove(obj))
|
||||
{
|
||||
UpdateProgressText();
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateProgressText()
|
||||
{
|
||||
int collected = _collectedObjects.Count;
|
||||
|
||||
|
||||
OnProgressChanged?.Invoke(collected, _totalTargetObjects);
|
||||
}
|
||||
|
||||
private void CheckCompletion()
|
||||
{
|
||||
if (_collectedObjects.Count >= _totalTargetObjects)
|
||||
{
|
||||
if (secretCanvas != null)
|
||||
{
|
||||
secretCanvas.gameObject.SetActive(true);
|
||||
StartCoroutine(HideCanvasAfterTime());
|
||||
if (secretInfoText != null)
|
||||
{
|
||||
secretInfoText.text = GetSecretText();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (deleteZonesOnCompletion)
|
||||
{
|
||||
StartCoroutine(DeleteAllZonesDelayed());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator DeleteAllZonesDelayed()
|
||||
{
|
||||
yield return new WaitForSeconds(deleteDelay);
|
||||
|
||||
foreach (SecretInfoZone zone in _allZones)
|
||||
{
|
||||
if (zone != null)
|
||||
{
|
||||
Destroy(zone.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
_allZones.Clear();
|
||||
}
|
||||
|
||||
private IEnumerator HideCanvasAfterTime()
|
||||
{
|
||||
yield return new WaitForSeconds(_showDuration);
|
||||
|
||||
if (secretCanvas != null)
|
||||
{
|
||||
secretCanvas.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private string GetSecretText()
|
||||
{
|
||||
switch (locationType)
|
||||
{
|
||||
case LocationType.Moon:
|
||||
return
|
||||
"<b>ЦІКАВИЙ ФАКТ!</b>\n\n" +
|
||||
"Гравітаційне прискорення на Місяці змінюється залежно від місця!\n\n" +
|
||||
"<b>Над темними рівнинами:</b> 1.62 м/с²\n" +
|
||||
"<b>Над гірськими масивами:</b> 1.63 м/с²\n\n" +
|
||||
"Це відбувається через <b>маскони</b> - масивні концентрації густих порід під поверхнею. " +
|
||||
"Вони як невидимі гори під землею, які посилюють гравітацію!\n\n" +
|
||||
"<b>Ще цікавіше:</b>\n" +
|
||||
"Центр мас Місяця зміщений на 2 км убік Землі - тому один бік важчий!\n\n";
|
||||
|
||||
case LocationType.Earth:
|
||||
default:
|
||||
return
|
||||
"<b>ЦІКАВИЙ ФАКТ!</b>\n\n" +
|
||||
"Галілео Галілей вимірював час за допомогою свого власного пульсу. " +
|
||||
"Точних годинників ще не існувало, тому він використовував серцебиття " +
|
||||
"для вимірювання часу падіння об'єктів.\n\n" +
|
||||
"<b>Але найдивовижніше:</b>\n" +
|
||||
"1642 рік - Галілей помирає.\n" +
|
||||
"1642 рік - Ньютон народжується.\n\n" +
|
||||
"Того самого року всесвіт передав факел знань від одного генія до іншого. " +
|
||||
"Галілей почав розуміти гравітацію, а Ньютон завершив цю роботу математикою.\n\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user