-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMoonCalculator.cs
More file actions
39 lines (31 loc) · 1014 Bytes
/
MoonCalculator.cs
File metadata and controls
39 lines (31 loc) · 1014 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
using UnityEngine;
// derived from https://www.subsystems.us/uploads/9/8/9/4/98948044/moonphase.pdf initially
// mostly from https://www.timeanddate.com/moon/phases/
public abstract class MoonCalculator : MonoBehaviour {
public static float MoonCycle { get { return (float) 25101 / 850; } }
public static float GetPercent() {
DateTime calibrationDate = new DateTime(2019, 3, 6, 11, 3, 0); // mar 6, 2019 @ 11:03am
double daysSince = (DateTime.Today - calibrationDate).TotalDays;
return (float) daysSince / MoonCycle;
}
public static float DaysIntoCycle() {
return GetPercent() * MoonCycle;
}
public static MoonPhase GetPhase() {
float percent = GetPercent();
int numPhases = Enum.GetValues(typeof(MoonPhase)).Length;
int closest = Mathf.RoundToInt(percent * (numPhases + 1));
return (MoonPhase) (closest % numPhases);
}
public enum MoonPhase {
New,
WaxingCrescent,
FirstQuarter,
WaxingGibbous,
Full,
WaningGibbous,
ThirdQuarter,
WaningCrescent
}
}