Initial commit
This commit is contained in:
141
Assets/Scripts/CelestialCalendarController.cs
Normal file
141
Assets/Scripts/CelestialCalendarController.cs
Normal file
@@ -0,0 +1,141 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class CelestialCalendarController : MonoBehaviour
|
||||
{
|
||||
[System.Serializable]
|
||||
public class PlanetCalendar
|
||||
{
|
||||
public GameObject planet;
|
||||
public GameObject calendar;
|
||||
public GameObject monthText;
|
||||
public GameObject daysGrid;
|
||||
public GameObject infoText;
|
||||
}
|
||||
|
||||
[SerializeField] private List<PlanetCalendar> planetCalendars;
|
||||
private GameObject _activeCalendar;
|
||||
private PlanetCalendar _activeItem;
|
||||
private Camera _activeCamera;
|
||||
|
||||
void Start()
|
||||
{
|
||||
foreach (var item in planetCalendars)
|
||||
{
|
||||
if (item.calendar != null)
|
||||
item.calendar.SetActive(false);
|
||||
if (item.infoText != null)
|
||||
item.infoText.SetActive(false);
|
||||
}
|
||||
UpdateActiveCamera();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
UpdateActiveCamera();
|
||||
if (_activeCamera == null) return;
|
||||
|
||||
for (int i = 0; i <= 9; i++)
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.Alpha0 + i) ||
|
||||
Input.GetKeyDown(KeyCode.Keypad0 + i))
|
||||
{
|
||||
HideActive();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.DownArrow))
|
||||
{
|
||||
ToggleInfoMode();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Input.GetMouseButtonDown(0)) return;
|
||||
|
||||
Ray ray = _activeCamera.ScreenPointToRay(Input.mousePosition);
|
||||
RaycastHit hit;
|
||||
|
||||
if (!Physics.Raycast(ray, out hit))
|
||||
{
|
||||
HideActive();
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var item in planetCalendars)
|
||||
{
|
||||
if (item.planet == null) continue;
|
||||
if (hit.collider.gameObject == item.planet ||
|
||||
hit.collider.transform.IsChildOf(item.planet.transform))
|
||||
{
|
||||
ToggleCalendar(item);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
HideActive();
|
||||
}
|
||||
|
||||
void UpdateActiveCamera()
|
||||
{
|
||||
Camera[] cameras = Camera.allCameras;
|
||||
foreach (var cam in cameras)
|
||||
{
|
||||
if (cam.enabled && cam.gameObject.activeInHierarchy)
|
||||
{
|
||||
_activeCamera = cam;
|
||||
return;
|
||||
}
|
||||
}
|
||||
_activeCamera = null;
|
||||
}
|
||||
|
||||
void ToggleCalendar(PlanetCalendar item)
|
||||
{
|
||||
if (item.calendar == null) return;
|
||||
|
||||
if (_activeCalendar == item.calendar)
|
||||
{
|
||||
HideActive();
|
||||
return;
|
||||
}
|
||||
|
||||
HideActive();
|
||||
item.calendar.SetActive(true);
|
||||
|
||||
if (item.monthText != null) item.monthText.SetActive(true);
|
||||
if (item.daysGrid != null) item.daysGrid.SetActive(true);
|
||||
if (item.infoText != null) item.infoText.SetActive(false);
|
||||
|
||||
_activeCalendar = item.calendar;
|
||||
_activeItem = item;
|
||||
}
|
||||
|
||||
void ToggleInfoMode()
|
||||
{
|
||||
if (_activeItem == null) return;
|
||||
|
||||
bool infoActive = _activeItem.infoText != null && _activeItem.infoText.activeSelf;
|
||||
|
||||
if (_activeItem.monthText != null) _activeItem.monthText.SetActive(infoActive);
|
||||
if (_activeItem.daysGrid != null) _activeItem.daysGrid.SetActive(infoActive);
|
||||
if (_activeItem.infoText != null) _activeItem.infoText.SetActive(!infoActive);
|
||||
}
|
||||
|
||||
void HideActive()
|
||||
{
|
||||
if (_activeCalendar != null)
|
||||
{
|
||||
if (_activeItem != null)
|
||||
{
|
||||
if (_activeItem.monthText != null) _activeItem.monthText.SetActive(true);
|
||||
if (_activeItem.daysGrid != null) _activeItem.daysGrid.SetActive(true);
|
||||
if (_activeItem.infoText != null) _activeItem.infoText.SetActive(false);
|
||||
}
|
||||
_activeCalendar.SetActive(false);
|
||||
_activeCalendar = null;
|
||||
_activeItem = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user