2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Clothing.Components;
|
|
|
|
|
using Content.Shared.Interaction;
|
2021-12-30 22:56:10 +01:00
|
|
|
using Content.Shared.Item;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Light.Component;
|
2020-09-03 16:02:40 -04:00
|
|
|
using Robust.Server.GameObjects;
|
2020-09-13 14:23:52 +02:00
|
|
|
using Robust.Shared.Audio;
|
2020-09-03 16:02:40 -04:00
|
|
|
using Robust.Shared.GameObjects;
|
2021-12-03 11:15:12 +01:00
|
|
|
using Robust.Shared.IoC;
|
2021-03-21 09:12:03 -07:00
|
|
|
using Robust.Shared.Player;
|
2020-09-03 16:02:40 -04:00
|
|
|
using Robust.Shared.ViewVariables;
|
|
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.Light.Components
|
2020-09-03 16:02:40 -04:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Component that represents a handheld expendable light which can be activated and eventually dies over time.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[RegisterComponent]
|
|
|
|
|
public class ExpendableLightComponent : SharedExpendableLightComponent, IUse
|
|
|
|
|
{
|
2021-12-08 17:17:12 +01:00
|
|
|
[Dependency] private readonly IEntityManager _entMan = default!;
|
|
|
|
|
|
2020-09-03 16:02:40 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Status of light, whether or not it is emitting light.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ViewVariables]
|
|
|
|
|
public bool Activated => CurrentState == ExpendableLightState.Lit || CurrentState == ExpendableLightState.Fading;
|
|
|
|
|
|
|
|
|
|
[ViewVariables]
|
|
|
|
|
private float _stateExpiryTime = default;
|
2021-03-16 15:50:20 +01:00
|
|
|
private AppearanceComponent? _appearance = default;
|
2020-09-03 16:02:40 -04:00
|
|
|
|
|
|
|
|
bool IUse.UseEntity(UseEntityEventArgs eventArgs)
|
|
|
|
|
{
|
|
|
|
|
return TryActivate();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-19 19:41:26 -07:00
|
|
|
protected override void Initialize()
|
2020-09-03 16:02:40 -04:00
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
2021-12-30 22:56:10 +01:00
|
|
|
if (_entMan.TryGetComponent<SharedItemComponent?>(Owner, out var item))
|
2020-09-03 16:02:40 -04:00
|
|
|
{
|
2021-02-27 09:23:14 +00:00
|
|
|
item.EquippedPrefix = "unlit";
|
2020-09-03 16:02:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CurrentState = ExpendableLightState.BrandNew;
|
|
|
|
|
Owner.EnsureComponent<PointLightComponent>();
|
2021-12-08 17:17:12 +01:00
|
|
|
_entMan.TryGetComponent(Owner, out _appearance);
|
2020-09-03 16:02:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Enables the light if it is not active. Once active it cannot be turned off.
|
|
|
|
|
/// </summary>
|
2021-10-05 14:29:03 +11:00
|
|
|
public bool TryActivate()
|
2020-09-03 16:02:40 -04:00
|
|
|
{
|
2021-07-26 18:17:42 +02:00
|
|
|
if (!Activated && CurrentState == ExpendableLightState.BrandNew)
|
2020-09-03 16:02:40 -04:00
|
|
|
{
|
2021-12-30 22:56:10 +01:00
|
|
|
if (_entMan.TryGetComponent<SharedItemComponent?>(Owner, out var item))
|
2020-09-03 16:02:40 -04:00
|
|
|
{
|
2021-02-27 09:23:14 +00:00
|
|
|
item.EquippedPrefix = "lit";
|
2020-09-03 16:02:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CurrentState = ExpendableLightState.Lit;
|
|
|
|
|
_stateExpiryTime = GlowDuration;
|
2021-03-16 15:50:20 +01:00
|
|
|
|
2020-09-03 16:02:40 -04:00
|
|
|
UpdateSpriteAndSounds(Activated);
|
|
|
|
|
UpdateVisualizer();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateVisualizer()
|
|
|
|
|
{
|
2021-07-31 12:41:59 +02:00
|
|
|
_appearance?.SetData(ExpendableLightVisuals.State, CurrentState);
|
|
|
|
|
|
2020-09-03 16:02:40 -04:00
|
|
|
switch (CurrentState)
|
|
|
|
|
{
|
|
|
|
|
case ExpendableLightState.Lit:
|
2021-07-31 12:41:59 +02:00
|
|
|
_appearance?.SetData(ExpendableLightVisuals.Behavior, TurnOnBehaviourID);
|
2020-09-03 16:02:40 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ExpendableLightState.Fading:
|
2021-07-31 12:41:59 +02:00
|
|
|
_appearance?.SetData(ExpendableLightVisuals.Behavior, FadeOutBehaviourID);
|
2020-09-03 16:02:40 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ExpendableLightState.Dead:
|
2021-07-31 12:41:59 +02:00
|
|
|
_appearance?.SetData(ExpendableLightVisuals.Behavior, string.Empty);
|
2020-09-03 16:02:40 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateSpriteAndSounds(bool on)
|
|
|
|
|
{
|
2021-12-08 17:17:12 +01:00
|
|
|
if (_entMan.TryGetComponent(Owner, out SpriteComponent? sprite))
|
2020-09-03 16:02:40 -04:00
|
|
|
{
|
|
|
|
|
switch (CurrentState)
|
|
|
|
|
{
|
|
|
|
|
case ExpendableLightState.Lit:
|
2021-08-13 21:31:37 -07:00
|
|
|
{
|
2021-07-31 19:52:33 +02:00
|
|
|
SoundSystem.Play(Filter.Pvs(Owner), LitSound.GetSound(), Owner);
|
2021-08-13 21:31:37 -07:00
|
|
|
|
2021-02-27 09:23:14 +00:00
|
|
|
if (IconStateLit != string.Empty)
|
|
|
|
|
{
|
|
|
|
|
sprite.LayerSetState(2, IconStateLit);
|
|
|
|
|
sprite.LayerSetShader(2, "shaded");
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-03 16:02:40 -04:00
|
|
|
sprite.LayerSetVisible(1, true);
|
|
|
|
|
break;
|
2021-08-13 21:31:37 -07:00
|
|
|
}
|
2020-09-03 16:02:40 -04:00
|
|
|
case ExpendableLightState.Fading:
|
2021-08-13 21:31:37 -07:00
|
|
|
{
|
2020-09-03 16:02:40 -04:00
|
|
|
break;
|
2021-08-13 21:31:37 -07:00
|
|
|
}
|
2020-09-03 16:02:40 -04:00
|
|
|
default:
|
|
|
|
|
case ExpendableLightState.Dead:
|
2021-08-13 21:31:37 -07:00
|
|
|
{
|
|
|
|
|
if (DieSound != null) SoundSystem.Play(Filter.Pvs(Owner), DieSound.GetSound(), Owner);
|
2020-09-03 16:02:40 -04:00
|
|
|
|
2021-02-27 09:23:14 +00:00
|
|
|
sprite.LayerSetState(0, IconStateSpent);
|
|
|
|
|
sprite.LayerSetShader(0, "shaded");
|
2020-09-03 16:02:40 -04:00
|
|
|
sprite.LayerSetVisible(1, false);
|
|
|
|
|
break;
|
2021-08-13 21:31:37 -07:00
|
|
|
}
|
2020-09-03 16:02:40 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-08 17:17:12 +01:00
|
|
|
if (_entMan.TryGetComponent(Owner, out ClothingComponent? clothing))
|
2020-09-03 16:02:40 -04:00
|
|
|
{
|
2021-12-30 22:56:10 +01:00
|
|
|
clothing.EquippedPrefix = on ? "Activated" : string.Empty;
|
2020-09-03 16:02:40 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2021-12-08 17:17:12 +01:00
|
|
|
_entMan.GetComponent<MetaDataComponent>(Owner).EntityName = SpentName;
|
|
|
|
|
_entMan.GetComponent<MetaDataComponent>(Owner).EntityDescription = SpentDesc;
|
2020-09-03 16:02:40 -04:00
|
|
|
|
|
|
|
|
UpdateSpriteAndSounds(Activated);
|
|
|
|
|
UpdateVisualizer();
|
|
|
|
|
|
2021-12-30 22:56:10 +01:00
|
|
|
if (_entMan.TryGetComponent<SharedItemComponent?>(Owner, out var item))
|
2020-09-03 16:02:40 -04:00
|
|
|
{
|
2021-02-27 09:23:14 +00:00
|
|
|
item.EquippedPrefix = "unlit";
|
2020-09-03 16:02:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|