Files
crystall-punk-14/Content.Server/_CP14/WeatherControl/CP14WeatherControllerSystem.cs
Ed 3b0af98a2e Weather backend start (#407)
* DayCycke StartWithRandomEntry

* add weather rule

* fix

* yep, its work

* finalize weather

* fix again

* weather resprite
2024-09-01 15:22:16 +03:00

54 lines
1.9 KiB
C#

using Content.Server.GameTicking;
using Content.Server.GameTicking.Events;
using Content.Server.Weather;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Timing;
namespace Content.Server._CP14.WeatherControl;
public sealed class CP14WeatherControllerSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly GameTicker _gameTicker = default!;
[Dependency] private readonly WeatherSystem _weather = default!;
[Dependency] private readonly IPrototypeManager _proto = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<CP14WeatherControllerComponent, RoundStartingEvent>(OnRoundStart);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<CP14WeatherControllerComponent>();
while (query.MoveNext(out var uid, out var weather))
{
if (_timing.CurTime <= weather.NextWeatherTime)
continue;
var weatherData = _random.Pick(weather.Entries);
if (!_proto.TryIndex(weatherData.Visuals, out var weatherVisualsIndexed))
continue;
var weatherDuration = TimeSpan.FromSeconds(weatherData.Duration.Next(_random));
_weather.SetWeather(Transform(uid).MapID, weatherVisualsIndexed, _timing.CurTime + weatherDuration);
var clearDuration = TimeSpan.FromSeconds(weather.ClearDuration.Next(_random));
weather.NextWeatherTime = _timing.CurTime + weatherDuration + clearDuration;
}
}
private void OnRoundStart(Entity<CP14WeatherControllerComponent> ent, ref RoundStartingEvent args)
{
ent.Comp.NextWeatherTime = _timing.CurTime + TimeSpan.FromSeconds(ent.Comp.ClearDuration.Next(_random));
}
}