Initial commit
This commit is contained in:
156
Assets/Scripts/CalendarManager.cs
Normal file
156
Assets/Scripts/CalendarManager.cs
Normal file
@@ -0,0 +1,156 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
public enum PlanetType
|
||||
{
|
||||
Mercury,
|
||||
Venus,
|
||||
Earth,
|
||||
Mars,
|
||||
Jupiter,
|
||||
Saturn,
|
||||
Uranus,
|
||||
Neptune,
|
||||
Pluto
|
||||
}
|
||||
|
||||
public class CalendarManager : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Calendar calendar;
|
||||
[SerializeField] private PlanetSpin planetSpin;
|
||||
|
||||
[SerializeField] private PlanetType planetType = PlanetType.Earth;
|
||||
|
||||
private DateTime _currentDate;
|
||||
|
||||
private float _timer = 0f;
|
||||
private float _planetDayProgress = 0f;
|
||||
|
||||
private float _baseRotationSpeed;
|
||||
|
||||
private Dictionary<PlanetType, float> _planetDayLength =
|
||||
new Dictionary<PlanetType, float>()
|
||||
{
|
||||
{ PlanetType.Mercury, 58.6f },
|
||||
{ PlanetType.Venus, 243f },
|
||||
{ PlanetType.Earth, 1f },
|
||||
{ PlanetType.Mars, 1.03f },
|
||||
{ PlanetType.Jupiter, 0.41f },
|
||||
{ PlanetType.Saturn, 0.44f },
|
||||
{ PlanetType.Uranus, 0.72f },
|
||||
{ PlanetType.Neptune, 0.67f },
|
||||
{ PlanetType.Pluto, 6.39f }
|
||||
};
|
||||
|
||||
private Dictionary<PlanetType, float> _baseRotationSpeeds =
|
||||
new Dictionary<PlanetType, float>()
|
||||
{
|
||||
{ PlanetType.Mercury, 0.26f },
|
||||
{ PlanetType.Venus, -0.025f },
|
||||
{ PlanetType.Earth, 15f },
|
||||
{ PlanetType.Mars, 14.6f },
|
||||
{ PlanetType.Jupiter, 36.4f },
|
||||
{ PlanetType.Saturn, 33.6f },
|
||||
{ PlanetType.Uranus, -20.9f },
|
||||
{ PlanetType.Neptune, 22.4f },
|
||||
{ PlanetType.Pluto, -2.35f }
|
||||
};
|
||||
|
||||
void Start()
|
||||
{
|
||||
_currentDate = DateTime.Now;
|
||||
|
||||
ApplyPlanetSettings();
|
||||
|
||||
if (calendar != null)
|
||||
calendar.SetDate(_currentDate);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
CheckResetInput();
|
||||
|
||||
if (planetSpin == null) return;
|
||||
|
||||
if (Mathf.Abs(_baseRotationSpeed) < 0.0001f) return;
|
||||
|
||||
float currentSpeed = planetSpin.RotationSpeed;
|
||||
|
||||
float speedRatio = currentSpeed / _baseRotationSpeed;
|
||||
|
||||
if (Mathf.Abs(speedRatio) < 0.0001f) return;
|
||||
|
||||
float secondsPerEarthDay = 1f / Mathf.Abs(speedRatio);
|
||||
|
||||
_timer += Time.deltaTime;
|
||||
|
||||
if (_timer >= secondsPerEarthDay)
|
||||
{
|
||||
_timer = 0f;
|
||||
|
||||
float planetMultiplier =
|
||||
1f / _planetDayLength[planetType];
|
||||
|
||||
_planetDayProgress += planetMultiplier;
|
||||
|
||||
if (_planetDayProgress >= 1f)
|
||||
{
|
||||
int daysToAdd =
|
||||
Mathf.FloorToInt(_planetDayProgress);
|
||||
|
||||
_currentDate =
|
||||
_currentDate.AddDays(daysToAdd);
|
||||
|
||||
_planetDayProgress -= daysToAdd;
|
||||
|
||||
if (calendar != null)
|
||||
calendar.SetDate(_currentDate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ChangePlanet(PlanetType newPlanet)
|
||||
{
|
||||
planetType = newPlanet;
|
||||
ApplyPlanetSettings();
|
||||
}
|
||||
|
||||
private void ApplyPlanetSettings()
|
||||
{
|
||||
_baseRotationSpeed =
|
||||
_baseRotationSpeeds[planetType];
|
||||
|
||||
if (planetSpin != null)
|
||||
planetSpin.SetRotationSpeed(
|
||||
_baseRotationSpeed);
|
||||
|
||||
_planetDayProgress = 0f;
|
||||
_timer = 0f;
|
||||
}
|
||||
|
||||
private void CheckResetInput()
|
||||
{
|
||||
for (int i = 0; i <= 9; i++)
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.Alpha0 + i) ||
|
||||
Input.GetKeyDown(KeyCode.Keypad0 + i))
|
||||
{
|
||||
ResetToBaseSpeed();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ResetToBaseSpeed()
|
||||
{
|
||||
if (planetSpin == null) return;
|
||||
|
||||
planetSpin.SetRotationSpeed(
|
||||
_baseRotationSpeed);
|
||||
|
||||
_timer = 0f;
|
||||
_planetDayProgress = 0f;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user