2020-01-12 02:02:58 +01:00
|
|
|
|
using Content.Server.GameObjects.Components.Power;
|
2019-07-27 11:56:36 +02:00
|
|
|
|
using Content.Server.GameObjects.Components.Sound;
|
2018-11-21 20:58:11 +01:00
|
|
|
|
using Content.Server.GameObjects.EntitySystems;
|
|
|
|
|
|
using Content.Server.Interfaces.GameObjects;
|
2020-05-23 02:27:31 -07:00
|
|
|
|
using Content.Server.Utility;
|
2018-11-21 20:58:11 +01:00
|
|
|
|
using Content.Shared.GameObjects;
|
2020-01-09 00:27:52 +01:00
|
|
|
|
using Content.Shared.GameObjects.Components;
|
2019-10-10 11:50:53 +02:00
|
|
|
|
using Content.Shared.Interfaces;
|
2019-04-15 21:11:38 -06:00
|
|
|
|
using Robust.Server.GameObjects;
|
|
|
|
|
|
using Robust.Server.GameObjects.Components.Container;
|
2020-05-31 12:40:36 -05:00
|
|
|
|
using Robust.Server.GameObjects.EntitySystems;
|
2019-05-05 18:52:06 +02:00
|
|
|
|
using Robust.Server.Interfaces.GameObjects;
|
2019-04-15 21:11:38 -06:00
|
|
|
|
using Robust.Shared.GameObjects;
|
2020-05-31 12:40:36 -05:00
|
|
|
|
using Robust.Shared.GameObjects.Systems;
|
2019-04-15 21:11:38 -06:00
|
|
|
|
using Robust.Shared.Interfaces.GameObjects;
|
2019-10-10 11:50:53 +02:00
|
|
|
|
using Robust.Shared.IoC;
|
|
|
|
|
|
using Robust.Shared.Localization;
|
2019-04-15 21:11:38 -06:00
|
|
|
|
using Robust.Shared.Utility;
|
|
|
|
|
|
using Robust.Shared.ViewVariables;
|
2018-08-28 08:39:20 -07:00
|
|
|
|
|
|
|
|
|
|
namespace Content.Server.GameObjects.Components.Interactable
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2018-11-21 20:58:11 +01:00
|
|
|
|
/// Component that represents a handheld lightsource which can be toggled on and off.
|
2018-08-28 08:39:20 -07:00
|
|
|
|
/// </summary>
|
2019-07-31 15:02:36 +02:00
|
|
|
|
[RegisterComponent]
|
2020-05-23 17:23:25 +02:00
|
|
|
|
internal sealed class HandheldLightComponent : SharedHandheldLightComponent, IUse, IExamine, IInteractUsing, IMapInit
|
2018-08-28 08:39:20 -07:00
|
|
|
|
{
|
2019-10-10 11:50:53 +02:00
|
|
|
|
#pragma warning disable 649
|
|
|
|
|
|
[Dependency] private readonly ISharedNotifyManager _notifyManager;
|
|
|
|
|
|
[Dependency] private readonly ILocalizationManager _localizationManager;
|
|
|
|
|
|
#pragma warning restore 649
|
|
|
|
|
|
|
2020-01-12 02:01:00 +01:00
|
|
|
|
[ViewVariables(VVAccess.ReadWrite)] public float Wattage { get; set; } = 10;
|
2018-11-21 20:58:11 +01:00
|
|
|
|
[ViewVariables] private ContainerSlot _cellContainer;
|
2018-11-11 20:05:09 +01:00
|
|
|
|
private PointLightComponent _pointLight;
|
|
|
|
|
|
private SpriteComponent _spriteComponent;
|
2019-04-08 12:18:27 +02:00
|
|
|
|
private ClothingComponent _clothingComponent;
|
2018-11-11 20:05:09 +01:00
|
|
|
|
|
2018-11-21 20:58:11 +01:00
|
|
|
|
[ViewVariables]
|
2018-11-11 20:05:09 +01:00
|
|
|
|
private PowerCellComponent Cell
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
2018-11-21 20:58:11 +01:00
|
|
|
|
if (_cellContainer.ContainedEntity == null) return null;
|
2018-11-11 20:05:09 +01:00
|
|
|
|
|
|
|
|
|
|
_cellContainer.ContainedEntity.TryGetComponent(out PowerCellComponent cell);
|
|
|
|
|
|
return cell;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-08-28 08:39:20 -07:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-11-21 20:58:11 +01:00
|
|
|
|
/// Status of light, whether or not it is emitting light.
|
2018-08-28 08:39:20 -07:00
|
|
|
|
/// </summary>
|
2018-09-09 15:34:43 +02:00
|
|
|
|
[ViewVariables]
|
2018-11-21 20:58:11 +01:00
|
|
|
|
public bool Activated { get; private set; }
|
2018-08-28 08:39:20 -07:00
|
|
|
|
|
2020-05-23 17:23:25 +02:00
|
|
|
|
bool IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
|
2018-11-21 20:58:11 +01:00
|
|
|
|
{
|
2020-05-23 17:23:25 +02:00
|
|
|
|
if (!eventArgs.Using.HasComponent<PowerCellComponent>()) return false;
|
2018-11-21 20:58:11 +01:00
|
|
|
|
|
|
|
|
|
|
if (Cell != null) return false;
|
|
|
|
|
|
|
2019-07-27 11:56:36 +02:00
|
|
|
|
var handsComponent = eventArgs.User.GetComponent<IHandsComponent>();
|
|
|
|
|
|
|
2020-05-23 17:23:25 +02:00
|
|
|
|
if (!handsComponent.Drop(eventArgs.Using, _cellContainer))
|
2019-07-27 11:56:36 +02:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-07 08:55:15 -05:00
|
|
|
|
EntitySystem.Get<AudioSystem>().PlayFromEntity("/Audio/items/weapons/pistol_magin.ogg", Owner);
|
2020-05-31 12:40:36 -05:00
|
|
|
|
|
2019-07-27 11:56:36 +02:00
|
|
|
|
|
2020-01-12 02:02:58 +01:00
|
|
|
|
Dirty();
|
|
|
|
|
|
|
2019-07-27 11:56:36 +02:00
|
|
|
|
return true;
|
2018-11-21 20:58:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-05-31 19:29:06 +01:00
|
|
|
|
void IExamine.Examine(FormattedMessage message, bool inDetailsRange)
|
2018-11-21 20:58:11 +01:00
|
|
|
|
{
|
2019-10-13 22:49:07 +02:00
|
|
|
|
var loc = IoCManager.Resolve<ILocalizationManager>();
|
|
|
|
|
|
|
2019-04-09 17:33:53 +02:00
|
|
|
|
if (Activated)
|
|
|
|
|
|
{
|
2019-10-13 22:49:07 +02:00
|
|
|
|
message.AddMarkup(loc.GetString("The light is currently [color=darkgreen]on[/color]."));
|
2019-04-09 17:33:53 +02:00
|
|
|
|
}
|
2018-11-21 20:58:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-05 19:42:49 +02:00
|
|
|
|
bool IUse.UseEntity(UseEntityEventArgs eventArgs)
|
2018-11-21 20:58:11 +01:00
|
|
|
|
{
|
2019-10-10 11:50:53 +02:00
|
|
|
|
return ToggleStatus(eventArgs.User);
|
2018-11-21 20:58:11 +01:00
|
|
|
|
}
|
2018-11-11 20:05:09 +01:00
|
|
|
|
|
2018-08-28 08:39:20 -07:00
|
|
|
|
public override void Initialize()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
|
2018-11-11 20:05:09 +01:00
|
|
|
|
_pointLight = Owner.GetComponent<PointLightComponent>();
|
|
|
|
|
|
_spriteComponent = Owner.GetComponent<SpriteComponent>();
|
2019-04-08 12:18:27 +02:00
|
|
|
|
Owner.TryGetComponent(out _clothingComponent);
|
2018-11-11 20:05:09 +01:00
|
|
|
|
_cellContainer =
|
|
|
|
|
|
ContainerManagerComponent.Ensure<ContainerSlot>("flashlight_cell_container", Owner, out var existed);
|
2018-08-28 08:39:20 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-11-21 20:58:11 +01:00
|
|
|
|
/// Illuminates the light if it is not active, extinguishes it if it is active.
|
2018-08-28 08:39:20 -07:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>True if the light's status was toggled, false otherwise.</returns>
|
2019-10-10 11:50:53 +02:00
|
|
|
|
private bool ToggleStatus(IEntity user)
|
2018-08-28 08:39:20 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Update sprite and light states to match the activation.
|
|
|
|
|
|
if (Activated)
|
|
|
|
|
|
{
|
2019-07-27 11:56:36 +02:00
|
|
|
|
TurnOff();
|
2018-08-28 08:39:20 -07:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2019-10-10 11:50:53 +02:00
|
|
|
|
TurnOn(user);
|
2018-08-28 08:39:20 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Toggle always succeeds.
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-10 11:50:53 +02:00
|
|
|
|
private void TurnOff()
|
2018-11-11 20:05:09 +01:00
|
|
|
|
{
|
2019-07-27 11:56:36 +02:00
|
|
|
|
if (!Activated)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2018-11-11 20:05:09 +01:00
|
|
|
|
|
2019-07-30 13:31:46 +02:00
|
|
|
|
SetState(false);
|
2018-11-11 20:05:09 +01:00
|
|
|
|
Activated = false;
|
2019-07-27 11:56:36 +02:00
|
|
|
|
|
2020-06-07 08:55:15 -05:00
|
|
|
|
EntitySystem.Get<AudioSystem>().PlayFromEntity("/Audio/items/flashlight_toggle.ogg", Owner);
|
2020-05-31 12:40:36 -05:00
|
|
|
|
|
2018-11-11 20:05:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-10 11:50:53 +02:00
|
|
|
|
private void TurnOn(IEntity user)
|
2018-11-11 20:05:09 +01:00
|
|
|
|
{
|
2019-07-27 11:56:36 +02:00
|
|
|
|
if (Activated)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2018-11-11 20:05:09 +01:00
|
|
|
|
|
|
|
|
|
|
var cell = Cell;
|
2019-07-27 11:56:36 +02:00
|
|
|
|
if (cell == null)
|
|
|
|
|
|
{
|
2020-05-31 12:40:36 -05:00
|
|
|
|
|
2020-06-07 08:55:15 -05:00
|
|
|
|
EntitySystem.Get<AudioSystem>().PlayFromEntity("/Audio/machines/button.ogg", Owner);
|
2020-01-09 00:27:52 +01:00
|
|
|
|
|
2019-10-10 11:50:53 +02:00
|
|
|
|
_notifyManager.PopupMessage(Owner, user, _localizationManager.GetString("Cell missing..."));
|
2019-07-27 11:56:36 +02:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2018-11-11 20:05:09 +01:00
|
|
|
|
|
|
|
|
|
|
// To prevent having to worry about frame time in here.
|
|
|
|
|
|
// Let's just say you need a whole second of charge before you can turn it on.
|
|
|
|
|
|
// Simple enough.
|
2019-07-27 11:56:36 +02:00
|
|
|
|
if (cell.AvailableCharge(1) < Wattage)
|
|
|
|
|
|
{
|
2020-06-07 08:55:15 -05:00
|
|
|
|
EntitySystem.Get<AudioSystem>().PlayFromEntity("/Audio/machines/button.ogg", Owner);
|
2019-10-10 11:50:53 +02:00
|
|
|
|
_notifyManager.PopupMessage(Owner, user, _localizationManager.GetString("Dead cell..."));
|
2019-07-27 11:56:36 +02:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2018-11-11 20:05:09 +01:00
|
|
|
|
|
2019-07-27 11:56:36 +02:00
|
|
|
|
Activated = true;
|
2019-07-30 13:31:46 +02:00
|
|
|
|
SetState(true);
|
2019-07-27 11:56:36 +02:00
|
|
|
|
|
2020-06-07 08:55:15 -05:00
|
|
|
|
EntitySystem.Get<AudioSystem>().PlayFromEntity("/Audio/items/flashlight_toggle.ogg", Owner);
|
2020-05-31 12:40:36 -05:00
|
|
|
|
|
2019-04-08 12:18:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-30 13:31:46 +02:00
|
|
|
|
private void SetState(bool on)
|
2019-04-08 12:18:27 +02:00
|
|
|
|
{
|
2019-07-30 13:31:46 +02:00
|
|
|
|
_spriteComponent.LayerSetVisible(1, on);
|
|
|
|
|
|
_pointLight.Enabled = on;
|
2019-04-08 12:18:27 +02:00
|
|
|
|
if (_clothingComponent != null)
|
|
|
|
|
|
{
|
2019-07-30 13:31:46 +02:00
|
|
|
|
_clothingComponent.ClothingEquippedPrefix = on ? "On" : "Off";
|
2019-04-08 12:18:27 +02:00
|
|
|
|
}
|
2018-11-11 20:05:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-21 20:58:11 +01:00
|
|
|
|
public void OnUpdate(float frameTime)
|
2018-08-28 08:39:20 -07:00
|
|
|
|
{
|
2018-11-21 20:58:11 +01:00
|
|
|
|
if (!Activated) return;
|
2018-08-28 08:39:20 -07:00
|
|
|
|
|
2018-11-21 20:58:11 +01:00
|
|
|
|
var cell = Cell;
|
|
|
|
|
|
if (cell == null || !cell.TryDeductWattage(Wattage, frameTime)) TurnOff();
|
2020-01-09 00:27:52 +01:00
|
|
|
|
|
|
|
|
|
|
Dirty();
|
2018-08-28 08:39:20 -07:00
|
|
|
|
}
|
2018-11-11 20:05:09 +01:00
|
|
|
|
|
2018-11-21 20:58:11 +01:00
|
|
|
|
private void EjectCell(IEntity user)
|
|
|
|
|
|
{
|
2019-07-27 11:56:36 +02:00
|
|
|
|
if (Cell == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2018-11-21 20:58:11 +01:00
|
|
|
|
|
|
|
|
|
|
var cell = Cell;
|
|
|
|
|
|
|
2019-07-27 11:56:36 +02:00
|
|
|
|
if (!_cellContainer.Remove(cell.Owner))
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!user.TryGetComponent(out HandsComponent hands))
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2018-11-21 20:58:11 +01:00
|
|
|
|
|
2019-07-27 11:56:36 +02:00
|
|
|
|
if (!hands.PutInHand(cell.Owner.GetComponent<ItemComponent>()))
|
|
|
|
|
|
{
|
2019-01-18 11:40:30 +01:00
|
|
|
|
cell.Owner.Transform.GridPosition = user.Transform.GridPosition;
|
2019-07-27 11:56:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-07 08:55:15 -05:00
|
|
|
|
EntitySystem.Get<AudioSystem>().PlayFromEntity("/Audio/items/weapons/pistol_magout.ogg", Owner);
|
2020-05-31 12:40:36 -05:00
|
|
|
|
|
2018-11-21 20:58:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-01-09 00:27:52 +01:00
|
|
|
|
public override ComponentState GetComponentState()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Cell == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new HandheldLightComponentState(null);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (Cell.AvailableCharge(1) < Wattage)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Practically zero.
|
|
|
|
|
|
// This is so the item status works correctly.
|
|
|
|
|
|
return new HandheldLightComponentState(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new HandheldLightComponentState(Cell.Charge / Cell.Capacity);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-21 20:58:11 +01:00
|
|
|
|
[Verb]
|
|
|
|
|
|
public sealed class EjectCellVerb : Verb<HandheldLightComponent>
|
2018-11-11 20:05:09 +01:00
|
|
|
|
{
|
2020-05-23 03:09:44 +02:00
|
|
|
|
protected override void GetData(IEntity user, HandheldLightComponent component, VerbData data)
|
2018-11-11 20:05:09 +01:00
|
|
|
|
{
|
2020-05-23 03:09:44 +02:00
|
|
|
|
if (component.Cell == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
data.Text = "Eject cell (cell missing)";
|
|
|
|
|
|
data.Visibility = VerbVisibility.Disabled;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
data.Text = "Eject cell";
|
|
|
|
|
|
}
|
2018-11-21 20:58:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void Activate(IEntity user, HandheldLightComponent component)
|
2018-11-11 20:05:09 +01:00
|
|
|
|
{
|
2018-11-21 20:58:11 +01:00
|
|
|
|
component.EjectCell(user);
|
2018-11-11 20:05:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-05-05 18:52:06 +02:00
|
|
|
|
|
|
|
|
|
|
void IMapInit.MapInit()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_cellContainer.ContainedEntity != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2020-01-09 00:27:52 +01:00
|
|
|
|
|
2019-12-16 21:44:24 -08:00
|
|
|
|
var cell = Owner.EntityManager.SpawnEntity("PowerCellSmallHyper", Owner.Transform.GridPosition);
|
2019-05-05 18:52:06 +02:00
|
|
|
|
_cellContainer.Insert(cell);
|
|
|
|
|
|
}
|
2018-08-28 08:39:20 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|