mirror of
https://github.com/WWhiteDreamProject/wwdpublic.git
synced 2026-04-17 13:37:47 +03:00
* Power Supply and Load Stuff (#2505) # Description Changes how `ApcPowerReceiverComponent` works a bit. Separated the `Load` variable into main and side power loads. If main power demand is not met, the machine is considered unpowered. Side power demand is "optional", as can be met only partially (or not at all) and the device will continue to operate. Depending on the device, this may have different negative effects on its operaton. such as lights dimming and weapon rechargers not charging at full speed. This was first intended to fix an annoying bug with `ChargerComponent` and `ApcPowerReceiverBatteryComponent`, that made the powernet spaz out for a while if their power demand was too high. This is now fixed. --- <details><summary><h1>Media</h1></summary> <p> <details><summary>Before (heavy flashing lights)</summary> <p> https://github.com/user-attachments/assets/de7fb84f-54d0-4c8a-ba9e-7a97e8489980 </p> </details> <details><summary>After</summary> <p> https://github.com/user-attachments/assets/9cece608-24f7-4ec9-95cd-0c719c7beddb </p> </details> </p> </details> --- # Changelog 🆑 - fix: Chargers and energy turrets no longer make the lights flash rapidly if their power draw is too high - add: Lights dim if the powernet they're connected to is overloaded * больно много жрёт --------- Co-authored-by: VMSolidus <evilexecutive@gmail.com>
144 lines
5.4 KiB
C#
144 lines
5.4 KiB
C#
using Content.Shared.Light;
|
|
using Robust.Client.Animations;
|
|
using Robust.Client.GameObjects;
|
|
using Robust.Shared.Animations;
|
|
using Robust.Shared.Audio;
|
|
using Robust.Shared.Audio.Systems;
|
|
using Robust.Shared.Random;
|
|
|
|
namespace Content.Client.Light.Visualizers;
|
|
|
|
public sealed class PoweredLightVisualizerSystem : VisualizerSystem<PoweredLightVisualsComponent>
|
|
{
|
|
[Dependency] private readonly IRobustRandom _random = default!;
|
|
[Dependency] private readonly SharedAudioSystem _audio = default!;
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
SubscribeLocalEvent<PoweredLightVisualsComponent, AnimationCompletedEvent>(OnAnimationCompleted);
|
|
}
|
|
|
|
protected override void OnAppearanceChange(EntityUid uid, PoweredLightVisualsComponent comp, ref AppearanceChangeEvent args)
|
|
{
|
|
if (args.Sprite == null)
|
|
return;
|
|
|
|
if (!AppearanceSystem.TryGetData<PoweredLightState>(uid, PoweredLightVisuals.BulbState, out var state, args.Component))
|
|
return;
|
|
|
|
if (comp.SpriteStateMap.TryGetValue(state, out var spriteState))
|
|
args.Sprite.LayerSetState(PoweredLightLayers.Base, spriteState);
|
|
|
|
if (args.Sprite.LayerExists(PoweredLightLayers.Glow))
|
|
{
|
|
Color glowColor = Color.White;
|
|
if (TryComp<PointLightComponent>(uid, out var light))
|
|
{
|
|
glowColor = light.Color;
|
|
}
|
|
|
|
if (AppearanceSystem.TryGetData<float>(uid, PoweredLightVisuals.GlowAlpha, out var alpha, args.Component))
|
|
glowColor = glowColor.WithAlpha(alpha);
|
|
|
|
args.Sprite.LayerSetColor(PoweredLightLayers.Glow, glowColor);
|
|
|
|
args.Sprite.LayerSetVisible(PoweredLightLayers.Glow, state == PoweredLightState.On);
|
|
}
|
|
|
|
SetBlinkingAnimation(
|
|
uid,
|
|
state == PoweredLightState.On
|
|
&& (AppearanceSystem.TryGetData<bool>(uid, PoweredLightVisuals.Blinking, out var isBlinking, args.Component) && isBlinking),
|
|
comp
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Loops the blinking animation until the light should stop blinking.
|
|
/// </summary>
|
|
private void OnAnimationCompleted(EntityUid uid, PoweredLightVisualsComponent comp, AnimationCompletedEvent args)
|
|
{
|
|
if (args.Key != PoweredLightVisualsComponent.BlinkingAnimationKey)
|
|
return;
|
|
|
|
if(!comp.IsBlinking)
|
|
return;
|
|
|
|
AnimationSystem.Play(uid, Comp<AnimationPlayerComponent>(uid), BlinkingAnimation(comp), PoweredLightVisualsComponent.BlinkingAnimationKey);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets whether or not the given light should be blinking.
|
|
/// Triggers or clears the blinking animation of the state changes.
|
|
/// </summary>
|
|
private void SetBlinkingAnimation(EntityUid uid, bool shouldBeBlinking, PoweredLightVisualsComponent comp)
|
|
{
|
|
if (shouldBeBlinking == comp.IsBlinking)
|
|
return;
|
|
|
|
comp.IsBlinking = shouldBeBlinking;
|
|
|
|
var animationPlayer = EnsureComp<AnimationPlayerComponent>(uid);
|
|
if (shouldBeBlinking)
|
|
{
|
|
AnimationSystem.Play(uid, animationPlayer, BlinkingAnimation(comp), PoweredLightVisualsComponent.BlinkingAnimationKey);
|
|
}
|
|
else if (AnimationSystem.HasRunningAnimation(uid, animationPlayer, PoweredLightVisualsComponent.BlinkingAnimationKey))
|
|
{
|
|
AnimationSystem.Stop(uid, animationPlayer, PoweredLightVisualsComponent.BlinkingAnimationKey);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generates a blinking animation.
|
|
/// Essentially just flashes the light off and on over a random time interval.
|
|
/// The resulting animation is looped indefinitely until the comp is set to stop blinking.
|
|
/// </summary>
|
|
private Animation BlinkingAnimation(PoweredLightVisualsComponent comp)
|
|
{
|
|
var randomTime = MathHelper.Lerp(comp.MinBlinkingAnimationCycleTime, comp.MaxBlinkingAnimationCycleTime, _random.NextFloat());
|
|
var blinkingAnim = new Animation()
|
|
{
|
|
Length = TimeSpan.FromSeconds(randomTime),
|
|
AnimationTracks =
|
|
{
|
|
new AnimationTrackComponentProperty
|
|
{
|
|
ComponentType = typeof(PointLightComponent),
|
|
InterpolationMode = AnimationInterpolationMode.Nearest,
|
|
Property = nameof(PointLightComponent.AnimatedEnable),
|
|
KeyFrames =
|
|
{
|
|
new AnimationTrackProperty.KeyFrame(false, 0),
|
|
new AnimationTrackProperty.KeyFrame(true, 1)
|
|
}
|
|
},
|
|
new AnimationTrackSpriteFlick()
|
|
{
|
|
LayerKey = PoweredLightLayers.Base,
|
|
KeyFrames =
|
|
{
|
|
new AnimationTrackSpriteFlick.KeyFrame(comp.SpriteStateMap[PoweredLightState.Off], 0),
|
|
new AnimationTrackSpriteFlick.KeyFrame(comp.SpriteStateMap[PoweredLightState.On], 0.5f)
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
if (comp.BlinkingSound != null)
|
|
{
|
|
var sound = _audio.ResolveSound(comp.BlinkingSound);
|
|
blinkingAnim.AnimationTracks.Add(new AnimationTrackPlaySound()
|
|
{
|
|
KeyFrames =
|
|
{
|
|
new AnimationTrackPlaySound.KeyFrame(sound, 0.5f)
|
|
}
|
|
});
|
|
}
|
|
|
|
return blinkingAnim;
|
|
}
|
|
}
|