CI / build-test (push) Successful in 1m7s
Add Hour, Minute and MinuteOfDay to Calendar, decomposing DayProgress into a 24x60 in-game clock so games can show a date and time, not just the day number. Covered by a test. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
83 lines
2.2 KiB
C#
83 lines
2.2 KiB
C#
using MrGameEng.Core;
|
|
using Xunit;
|
|
|
|
namespace MrGameEng.Core.Tests;
|
|
|
|
public class CalendarTests
|
|
{
|
|
[Fact]
|
|
public void Constructor_NonPositiveSecondsPerDay_Throws()
|
|
{
|
|
Assert.Throws<ArgumentOutOfRangeException>(() => new Calendar(new GameClock(), 0f));
|
|
}
|
|
|
|
[Fact]
|
|
public void TotalDays_TracksScaledClockTime()
|
|
{
|
|
var clock = new GameClock();
|
|
var calendar = new Calendar(clock, secondsPerDay: 10f);
|
|
|
|
clock.Advance(5f); // half a day
|
|
|
|
Assert.Equal(0.5, calendar.TotalDays, 5);
|
|
Assert.Equal(1, calendar.Day);
|
|
Assert.Equal(0.5f, calendar.DayProgress, 5);
|
|
}
|
|
|
|
[Fact]
|
|
public void Day_CountsFromOne_AndRollsOver()
|
|
{
|
|
var clock = new GameClock();
|
|
var calendar = new Calendar(clock, secondsPerDay: 10f);
|
|
|
|
Assert.Equal(1, calendar.Day);
|
|
|
|
clock.Advance(10f); // exactly one day
|
|
Assert.Equal(2, calendar.Day);
|
|
Assert.Equal(0f, calendar.DayProgress, 5);
|
|
|
|
clock.Advance(15f); // total 25s = 2.5 days
|
|
Assert.Equal(3, calendar.Day);
|
|
Assert.Equal(0.5f, calendar.DayProgress, 5);
|
|
}
|
|
|
|
[Fact]
|
|
public void HourAndMinute_DecomposeTimeOfDay()
|
|
{
|
|
var clock = new GameClock();
|
|
var calendar = new Calendar(clock, secondsPerDay: 1440f); // one minute per in-game minute
|
|
|
|
clock.Advance(14 * 60 + 30); // 14:30
|
|
|
|
Assert.Equal(14, calendar.Hour);
|
|
Assert.Equal(30, calendar.Minute);
|
|
Assert.Equal(14 * 60 + 30, calendar.MinuteOfDay);
|
|
}
|
|
|
|
[Fact]
|
|
public void Pause_DoesNotAdvanceTheCalendar()
|
|
{
|
|
var clock = new GameClock { TimeScale = 0f };
|
|
var calendar = new Calendar(clock, secondsPerDay: 10f);
|
|
|
|
clock.Advance(100f);
|
|
|
|
Assert.Equal(0.0, calendar.TotalDays, 5);
|
|
Assert.Equal(1, calendar.Day);
|
|
}
|
|
|
|
[Fact]
|
|
public void TimeScale_SpeedsUpTheCalendar()
|
|
{
|
|
var slow = new GameClock();
|
|
var fast = new GameClock { TimeScale = 6f };
|
|
var slowCal = new Calendar(slow, secondsPerDay: 10f);
|
|
var fastCal = new Calendar(fast, secondsPerDay: 10f);
|
|
|
|
slow.Advance(10f);
|
|
fast.Advance(10f);
|
|
|
|
Assert.Equal(6.0, fastCal.TotalDays / slowCal.TotalDays, 5);
|
|
}
|
|
}
|