ECS expendable lights (#6370)

This commit is contained in:
metalgearsloth
2022-02-10 13:01:43 +11:00
committed by GitHub
parent 8ef1b1a87c
commit 1182ac19fb
3 changed files with 182 additions and 181 deletions

View File

@@ -1,12 +1,5 @@
using Content.Server.Clothing.Components;
using Content.Shared.Interaction;
using Content.Shared.Item;
using Content.Shared.Light.Component;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Player;
using Robust.Shared.ViewVariables;
namespace Content.Server.Light.Components
@@ -15,162 +8,14 @@ namespace Content.Server.Light.Components
/// Component that represents a handheld expendable light which can be activated and eventually dies over time.
/// </summary>
[RegisterComponent]
public class ExpendableLightComponent : SharedExpendableLightComponent, IUse
public sealed class ExpendableLightComponent : SharedExpendableLightComponent
{
[Dependency] private readonly IEntityManager _entMan = default!;
/// <summary>
/// Status of light, whether or not it is emitting light.
/// </summary>
[ViewVariables]
public bool Activated => CurrentState == ExpendableLightState.Lit || CurrentState == ExpendableLightState.Fading;
public bool Activated => CurrentState is ExpendableLightState.Lit or ExpendableLightState.Fading;
[ViewVariables]
private float _stateExpiryTime = default;
private AppearanceComponent? _appearance = default;
bool IUse.UseEntity(UseEntityEventArgs eventArgs)
{
return TryActivate();
}
protected override void Initialize()
{
base.Initialize();
if (_entMan.TryGetComponent<SharedItemComponent?>(Owner, out var item))
{
item.EquippedPrefix = "unlit";
}
CurrentState = ExpendableLightState.BrandNew;
Owner.EnsureComponent<PointLightComponent>();
_entMan.TryGetComponent(Owner, out _appearance);
}
/// <summary>
/// Enables the light if it is not active. Once active it cannot be turned off.
/// </summary>
public bool TryActivate()
{
if (!Activated && CurrentState == ExpendableLightState.BrandNew)
{
if (_entMan.TryGetComponent<SharedItemComponent?>(Owner, out var item))
{
item.EquippedPrefix = "lit";
}
CurrentState = ExpendableLightState.Lit;
_stateExpiryTime = GlowDuration;
UpdateSpriteAndSounds(Activated);
UpdateVisualizer();
return true;
}
return false;
}
private void UpdateVisualizer()
{
_appearance?.SetData(ExpendableLightVisuals.State, CurrentState);
switch (CurrentState)
{
case ExpendableLightState.Lit:
_appearance?.SetData(ExpendableLightVisuals.Behavior, TurnOnBehaviourID);
break;
case ExpendableLightState.Fading:
_appearance?.SetData(ExpendableLightVisuals.Behavior, FadeOutBehaviourID);
break;
case ExpendableLightState.Dead:
_appearance?.SetData(ExpendableLightVisuals.Behavior, string.Empty);
break;
}
}
private void UpdateSpriteAndSounds(bool on)
{
if (_entMan.TryGetComponent(Owner, out SpriteComponent? sprite))
{
switch (CurrentState)
{
case ExpendableLightState.Lit:
{
SoundSystem.Play(Filter.Pvs(Owner), LitSound.GetSound(), Owner);
if (IconStateLit != string.Empty)
{
sprite.LayerSetState(2, IconStateLit);
sprite.LayerSetShader(2, "shaded");
}
sprite.LayerSetVisible(1, true);
break;
}
case ExpendableLightState.Fading:
{
break;
}
default:
case ExpendableLightState.Dead:
{
if (DieSound != null) SoundSystem.Play(Filter.Pvs(Owner), DieSound.GetSound(), Owner);
sprite.LayerSetState(0, IconStateSpent);
sprite.LayerSetShader(0, "shaded");
sprite.LayerSetVisible(1, false);
break;
}
}
}
if (_entMan.TryGetComponent(Owner, out ClothingComponent? clothing))
{
clothing.EquippedPrefix = on ? "Activated" : string.Empty;
}
}
public void Update(float frameTime)
{
if (!Activated) return;
_stateExpiryTime -= frameTime;
if (_stateExpiryTime <= 0f)
{
switch (CurrentState)
{
case ExpendableLightState.Lit:
CurrentState = ExpendableLightState.Fading;
_stateExpiryTime = FadeOutDuration;
UpdateVisualizer();
break;
default:
case ExpendableLightState.Fading:
CurrentState = ExpendableLightState.Dead;
_entMan.GetComponent<MetaDataComponent>(Owner).EntityName = SpentName;
_entMan.GetComponent<MetaDataComponent>(Owner).EntityDescription = SpentDesc;
UpdateSpriteAndSounds(Activated);
UpdateVisualizer();
if (_entMan.TryGetComponent<SharedItemComponent?>(Owner, out var item))
{
item.EquippedPrefix = "unlit";
}
break;
}
}
}
[ViewVariables] public float StateExpiryTime = default;
}
}

View File

@@ -1,30 +1,184 @@
using Content.Server.Clothing.Components;
using Content.Server.Light.Components;
using Content.Shared.Interaction;
using Content.Shared.Item;
using Content.Shared.Light.Component;
using Content.Shared.Verbs;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Player;
namespace Content.Server.Light.EntitySystems
{
[UsedImplicitly]
public class ExpendableLightSystem : EntitySystem
public sealed class ExpendableLightSystem : EntitySystem
{
public override void Update(float frameTime)
{
foreach (var light in EntityManager.EntityQuery<ExpendableLightComponent>())
{
light.Update(frameTime);
}
}
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ExpendableLightComponent, ComponentInit>(OnExpLightInit);
SubscribeLocalEvent<ExpendableLightComponent, UseInHandEvent>(OnExpLightUse);
SubscribeLocalEvent<ExpendableLightComponent, GetActivationVerbsEvent>(AddIgniteVerb);
}
public override void Update(float frameTime)
{
foreach (var light in EntityManager.EntityQuery<ExpendableLightComponent>())
{
UpdateLight(light, frameTime);
}
}
private void UpdateLight(ExpendableLightComponent component, float frameTime)
{
if (!component.Activated) return;
component.StateExpiryTime -= frameTime;
if (component.StateExpiryTime <= 0f)
{
switch (component.CurrentState)
{
case ExpendableLightState.Lit:
component.CurrentState = ExpendableLightState.Fading;
component.StateExpiryTime = component.FadeOutDuration;
UpdateVisualizer(component);
break;
default:
case ExpendableLightState.Fading:
component.CurrentState = ExpendableLightState.Dead;
var meta = MetaData(component.Owner);
meta.EntityName = component.SpentName;
meta.EntityDescription = component.SpentDesc;
UpdateSpriteAndSounds(component);
UpdateVisualizer(component);
if (TryComp<SharedItemComponent>(component.Owner, out var item))
{
item.EquippedPrefix = "unlit";
}
break;
}
}
}
/// <summary>
/// Enables the light if it is not active. Once active it cannot be turned off.
/// </summary>
public bool TryActivate(ExpendableLightComponent component)
{
if (!component.Activated && component.CurrentState == ExpendableLightState.BrandNew)
{
if (TryComp<SharedItemComponent>(component.Owner, out var item))
{
item.EquippedPrefix = "lit";
}
component.CurrentState = ExpendableLightState.Lit;
component.StateExpiryTime = component.GlowDuration;
UpdateSpriteAndSounds(component);
UpdateVisualizer(component);
return true;
}
return false;
}
private void UpdateVisualizer(ExpendableLightComponent component, AppearanceComponent? appearance = null)
{
if (!Resolve(component.Owner, ref appearance, false)) return;
appearance.SetData(ExpendableLightVisuals.State, component.CurrentState);
switch (component.CurrentState)
{
case ExpendableLightState.Lit:
appearance.SetData(ExpendableLightVisuals.Behavior, component.TurnOnBehaviourID);
break;
case ExpendableLightState.Fading:
appearance.SetData(ExpendableLightVisuals.Behavior, component.FadeOutBehaviourID);
break;
case ExpendableLightState.Dead:
appearance.SetData(ExpendableLightVisuals.Behavior, string.Empty);
break;
}
}
private void UpdateSpriteAndSounds(ExpendableLightComponent component)
{
if (TryComp<SpriteComponent>(component.Owner, out var sprite))
{
switch (component.CurrentState)
{
case ExpendableLightState.Lit:
{
SoundSystem.Play(Filter.Pvs(component.Owner), component.LitSound.GetSound(), component.Owner);
if (component.IconStateLit != string.Empty)
{
sprite.LayerSetState(2, component.IconStateLit);
sprite.LayerSetShader(2, "shaded");
}
sprite.LayerSetVisible(1, true);
break;
}
case ExpendableLightState.Fading:
{
break;
}
default:
case ExpendableLightState.Dead:
{
if (component.DieSound != null)
SoundSystem.Play(Filter.Pvs(component.Owner), component.DieSound.GetSound(), component.Owner);
sprite.LayerSetState(0, component.IconStateSpent);
sprite.LayerSetShader(0, "shaded");
sprite.LayerSetVisible(1, false);
break;
}
}
}
if (TryComp<ClothingComponent>(component.Owner, out var clothing))
{
clothing.EquippedPrefix = component.Activated ? "Activated" : string.Empty;
}
}
private void OnExpLightInit(EntityUid uid, ExpendableLightComponent component, ComponentInit args)
{
if (TryComp<SharedItemComponent?>(uid, out var item))
{
item.EquippedPrefix = "unlit";
}
component.CurrentState = ExpendableLightState.BrandNew;
EntityManager.EnsureComponent<PointLightComponent>(uid);
}
private void OnExpLightUse(EntityUid uid, ExpendableLightComponent component, UseInHandEvent args)
{
if (args.Handled) return;
if (TryActivate(component))
args.Handled = true;
}
private void AddIgniteVerb(EntityUid uid, ExpendableLightComponent component, GetActivationVerbsEvent args)
{
if (!args.CanAccess || !args.CanInteract)
@@ -35,10 +189,12 @@ namespace Content.Server.Light.EntitySystems
// Ignite the flare or make the glowstick glow.
// Also hot damn, those are some shitty glowsticks, we need to get a refund.
Verb verb = new();
verb.Text = Loc.GetString("expendable-light-start-verb");
verb.IconTexture = "/Textures/Interface/VerbIcons/light.svg.192dpi.png";
verb.Act = () => component.TryActivate();
Verb verb = new()
{
Text = Loc.GetString("expendable-light-start-verb"),
IconTexture = "/Textures/Interface/VerbIcons/light.svg.192dpi.png",
Act = () => TryActivate(component)
};
args.Verbs.Add(verb);
}
}

View File

@@ -36,39 +36,39 @@ namespace Content.Shared.Light.Component
[ViewVariables]
[DataField("turnOnBehaviourID")]
protected string TurnOnBehaviourID { get; set; } = string.Empty;
public string TurnOnBehaviourID { get; set; } = string.Empty;
[ViewVariables]
[DataField("fadeOutBehaviourID")]
protected string FadeOutBehaviourID { get; set; } = string.Empty;
public string FadeOutBehaviourID { get; set; } = string.Empty;
[ViewVariables]
[DataField("glowDuration")]
protected float GlowDuration { get; set; } = 60 * 15f;
public float GlowDuration { get; set; } = 60 * 15f;
[ViewVariables]
[DataField("fadeOutDuration")]
protected float FadeOutDuration { get; set; } = 60 * 5f;
public float FadeOutDuration { get; set; } = 60 * 5f;
[ViewVariables]
[DataField("spentDesc")]
protected string SpentDesc { get; set; } = string.Empty;
public string SpentDesc { get; set; } = string.Empty;
[ViewVariables]
[DataField("spentName")]
protected string SpentName { get; set; } = string.Empty;
public string SpentName { get; set; } = string.Empty;
[ViewVariables]
[DataField("iconStateSpent")]
protected string IconStateSpent { get; set; } = string.Empty;
public string IconStateSpent { get; set; } = string.Empty;
[ViewVariables]
[DataField("iconStateOn")]
protected string IconStateLit { get; set; } = string.Empty;
public string IconStateLit { get; set; } = string.Empty;
[ViewVariables]
[DataField("litSound", required: true)]
protected SoundSpecifier LitSound { get; set; } = default!;
public SoundSpecifier LitSound { get; set; } = default!;
[ViewVariables]
[DataField("loopedSound")]
@@ -76,6 +76,6 @@ namespace Content.Shared.Light.Component
[ViewVariables]
[DataField("dieSound")]
protected SoundSpecifier? DieSound { get; set; } = null;
public SoundSpecifier? DieSound { get; set; } = null;
}
}