Skip to content

Commit

Permalink
Implement InGameTime
Browse files Browse the repository at this point in the history
  • Loading branch information
mertwole committed Aug 24, 2024
1 parent 0a8b436 commit 2f650e2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions JamGame/Assets/Scripts/Level/GlobalTime/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class Model : MonoBehaviour
[SerializeField]
private RealTimeSeconds dayLength;

public const float HOURS_IN_DAY = 24;

private float scale = 1.0f;

private object setTimeScaleLockHolder = null;
Expand Down
67 changes: 67 additions & 0 deletions JamGame/Assets/Scripts/Level/GlobalTime/TimeMeasures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ public override bool Equals(object obj)
}
}

public override int GetHashCode()
{
return seconds.GetHashCode();
}

public static bool operator >(RealTimeSeconds a, RealTimeSeconds b)
{
return a.seconds > b.seconds;
Expand Down Expand Up @@ -164,5 +169,67 @@ public enum Unit

[SerializeField]
private float value;

public static InGameTime Zero = new() { value = 0, timeUnit = Unit.Days };

public readonly RealTimeSeconds RealTimeSeconds =>
timeUnit switch
{
Unit.Hours
=> new RealTimeSeconds(value * Model.DayLength.Value / Model.HOURS_IN_DAY),
Unit.Days => new RealTimeSeconds(value * Model.DayLength.Value),
_ => throw new NotImplementedException()
};

private float ToDays()
{
return timeUnit switch
{
Unit.Hours => value / Model.HOURS_IN_DAY,
Unit.Days => value,
_ => throw new NotImplementedException()
};
}

public static InGameTime operator +(InGameTime a, InGameTime b)
{
return new InGameTime { value = a.ToDays() + b.ToDays(), timeUnit = Unit.Days };
}

public static InGameTime operator -(InGameTime a, InGameTime b)
{
return new InGameTime { value = a.ToDays() - b.ToDays(), timeUnit = Unit.Days };
}

public static InGameTime operator *(InGameTime a, float b)
{
a.value *= b;
return a;
}

public static InGameTime operator *(float a, InGameTime b)
{
return b * a;
}

public static bool operator >(InGameTime a, InGameTime b)
{
return a.ToDays() > b.ToDays();
}

public static bool operator <(InGameTime a, InGameTime b)
{
return a.ToDays() < b.ToDays();
}

public static bool operator <=(InGameTime a, InGameTime b)
{
return a.ToDays() <= b.ToDays();
}

public static bool operator >=(InGameTime a, InGameTime b)
{
return a.ToDays() >= b.ToDays();
}
}
}

0 comments on commit 2f650e2

Please sign in to comment.