我为我的游戏编写了日夜系统,但我遇到了其中一种方法的问题。到目前为止,除了一种方法之外我编码得很好。这是完整的代码:
using Plus.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Plus.Roleplay.GameEnvironment
{
class GameEnvironmentHandler
{
private List<TimeSpan> _darkTimes;
private List<TimeSpan> _lightTimes;
public GameEnvironmentHandler()
{
_darkTimes = new List<TimeSpan>
{
new TimeSpan(19, 0, 0),
new TimeSpan(19, 30, 0),
new TimeSpan(20, 0, 0),
new TimeSpan(20, 30, 0),
new TimeSpan (21, 0, 0)
};
_lightTimes = new List<TimeSpan>
{
new TimeSpan(06, 0, 0),
new TimeSpan(06, 30, 0),
new TimeSpan(07, 0, 0),
new TimeSpan(07, 30, 0),
new TimeSpan (08, 0, 0)
};
}
public bool IsGettingDark()
{
return RoleplayHelper.IsTimeOfDayBetween(DateTime.Now,
_darkTimes.First(), _darkTimes.Last());
}
public bool IsGettingLight()
{
return RoleplayHelper.IsTimeOfDayBetween(DateTime.Now,
_lightTimes.First(), _lightTimes.Last())
|| RoleplayHelper.IsTimeOfDayBetween(DateTime.Now,
_lightTimes.First(), _darkTimes.Last());
}
}
}
我遇到问题的方法是IsGettingLight(),我编写了它来检查时间是否在第一次和最后一次光照时间之间,但是如果它在最后一次开始变亮之后,在第一次之前时间变暗,我怎样才能检查它是否变暗,变暗或变暗?我知道它与在第一个黑暗时间消除一点时间有关,但我究竟要删除什么,以及如何删除?
答案 0 :(得分:0)
这是一种方法:
// firstLight starts at 5:30
var firstLight = new TimeSpan(5, 30, 0);
// fullLight start at 8:00
var fullLightStart = new TimeSpan(8, 0, 0);
// first Darkness starts at 18:30
var firstDark = new TimeSpan(18, 30, 0);
// fullDarkness starts at 21:00
var fullDarkStart = new TimeSpan(21, 0, 0);
然后,要查看当天您所处的位置,您可以将当前TimeOfDay
与上述值进行比较:
var currentTime = DateTime.Now.TimeOfDay;
var IsGettingLight = currentTime >= firstLight && currentTime < fullLightStart;
var IsFullLight = currentTime >= fullLightStart && currentTime < firstDark;
var IsGettingDark = currentTime >= firstDark && currentTime < fullDarkStart;
var IsFullDark = currentTime >= fullDarkStart || currentTime < firstLight;
var IsLight = IsGettingLight || IsFullLight;
var IsDark = IsGettingDark || IsFullDark;