-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDayNightCycleComponent.cs
69 lines (58 loc) · 2 KB
/
DayNightCycleComponent.cs
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
using Robust.Shared.Map;
using Content.Server.Light.Components;
namespace Content.Server.DayNightCycle
{
[RegisterComponent]
public partial class DayNightCycleComponent : Component
{
[DataField("dayDuration")]
public TimeSpan DayDuration { get; set; } = TimeSpan.FromMinutes(10);
[DataField("nightDuration")]
public TimeSpan NightDuration { get; set; } = TimeSpan.FromMinutes(5);
[DataField("transitionDuration")]
public TimeSpan TransitionDuration { get; set; } = TimeSpan.FromMinutes(1);
private DateTime _lastCycleStart;
private bool _isDay;
public void StartCycle()
{
_lastCycleStart = DateTime.Now;
_isDay = true;
}
public void Update()
{
var currentTime = DateTime.Now;
var cycleTime = currentTime - _lastCycleStart;
if (_isDay)
{
if (cycleTime >= DayDuration)
{
_isDay = false;
_lastCycleStart = currentTime;
}
}
else
{
if (cycleTime >= NightDuration)
{
_isDay = true;
_lastCycleStart = currentTime;
}
}
var transitionProgress = (float)cycleTime.TotalSeconds / TransitionDuration.TotalSeconds;
var lightingLevel = _isDay? 1f : 0f;
if (transitionProgress > 0f && transitionProgress < 1f)
{
lightingLevel = _isDay? 1f - (float)transitionProgress : (float)transitionProgress;
}
var mapManager = IoCManager.Resolve<IMapManager>();
var lightComponents = EntityManager.EntityQuery<Content.Server.Light.Components.DynamicLightComponent>();
foreach (var lightComponent in lightComponents)
{
lightComponent.Enabled = _isDay;
lightComponent.Range = lightingLevel;
}
}
}
}