Re-organize all projects (#4166)
This commit is contained in:
@@ -1,302 +0,0 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.GameObjects.Components.GUI;
|
||||
using Content.Server.GameObjects.Components.Items.Storage;
|
||||
using Content.Server.GameObjects.Components.Weapon.Ranged.Barrels;
|
||||
using Content.Shared.GameObjects.Components.Power;
|
||||
using Content.Shared.GameObjects.EntitySystems.ActionBlocker;
|
||||
using Content.Shared.GameObjects.Verbs;
|
||||
using Content.Shared.Interfaces;
|
||||
using Content.Shared.Interfaces.GameObjects.Components;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerReceiverUsers
|
||||
{
|
||||
[ComponentReference(typeof(IActivate))]
|
||||
[ComponentReference(typeof(IInteractUsing))]
|
||||
public abstract class BaseCharger : Component, IActivate, IInteractUsing
|
||||
{
|
||||
[ViewVariables]
|
||||
private BatteryComponent? _heldBattery;
|
||||
|
||||
[ViewVariables]
|
||||
private ContainerSlot _container = default!;
|
||||
|
||||
[ViewVariables]
|
||||
private CellChargerStatus _status;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("chargeRate")]
|
||||
private int _chargeRate = 100;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("transferEfficiency")]
|
||||
private float _transferEfficiency = 0.85f;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
Owner.EnsureComponent<PowerReceiverComponent>();
|
||||
_container = ContainerHelpers.EnsureContainer<ContainerSlot>(Owner, $"{Name}-powerCellContainer");
|
||||
// Default state in the visualizer is OFF, so when this gets powered on during initialization it will generally show empty
|
||||
}
|
||||
|
||||
public override void HandleMessage(ComponentMessage message, IComponent? component)
|
||||
{
|
||||
base.HandleMessage(message, component);
|
||||
switch (message)
|
||||
{
|
||||
case PowerChangedMessage powerChanged:
|
||||
PowerUpdate(powerChanged);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnRemove()
|
||||
{
|
||||
_heldBattery = null;
|
||||
|
||||
base.OnRemove();
|
||||
}
|
||||
|
||||
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
|
||||
{
|
||||
var result = TryInsertItem(eventArgs.Using);
|
||||
if (!result)
|
||||
{
|
||||
eventArgs.User.PopupMessage(Owner, Loc.GetString("Unable to insert capacitor"));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void IActivate.Activate(ActivateEventArgs eventArgs)
|
||||
{
|
||||
RemoveItem(eventArgs.User);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This will remove the item directly into the user's hand / floor
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
private void RemoveItem(IEntity user)
|
||||
{
|
||||
var heldItem = _container.ContainedEntity;
|
||||
if (heldItem == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_container.Remove(heldItem);
|
||||
_heldBattery = null;
|
||||
if (user.TryGetComponent(out HandsComponent? handsComponent))
|
||||
{
|
||||
handsComponent.PutInHandOrDrop(heldItem.GetComponent<ItemComponent>());
|
||||
}
|
||||
|
||||
if (heldItem.TryGetComponent(out ServerBatteryBarrelComponent? batteryBarrelComponent))
|
||||
{
|
||||
batteryBarrelComponent.UpdateAppearance();
|
||||
}
|
||||
|
||||
UpdateStatus();
|
||||
}
|
||||
|
||||
private void PowerUpdate(PowerChangedMessage eventArgs)
|
||||
{
|
||||
UpdateStatus();
|
||||
}
|
||||
|
||||
[Verb]
|
||||
private sealed class InsertVerb : Verb<BaseCharger>
|
||||
{
|
||||
protected override void GetData(IEntity user, BaseCharger component, VerbData data)
|
||||
{
|
||||
if (!ActionBlockerSystem.CanInteract(user))
|
||||
{
|
||||
data.Visibility = VerbVisibility.Invisible;
|
||||
return;
|
||||
}
|
||||
if (!user.TryGetComponent(out HandsComponent? handsComponent))
|
||||
{
|
||||
data.Visibility = VerbVisibility.Invisible;
|
||||
return;
|
||||
}
|
||||
|
||||
if (component._container.ContainedEntity != null || handsComponent.GetActiveHand == null)
|
||||
{
|
||||
data.Visibility = VerbVisibility.Invisible;
|
||||
return;
|
||||
}
|
||||
|
||||
var heldItemName = Loc.GetString(handsComponent.GetActiveHand.Owner.Name);
|
||||
|
||||
data.Text = Loc.GetString("Insert {0}", heldItemName);
|
||||
data.IconTexture = "/Textures/Interface/VerbIcons/insert.svg.192dpi.png";
|
||||
}
|
||||
|
||||
protected override void Activate(IEntity user, BaseCharger component)
|
||||
{
|
||||
if (!user.TryGetComponent(out HandsComponent? handsComponent))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (handsComponent.GetActiveHand == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var userItem = handsComponent.GetActiveHand.Owner;
|
||||
handsComponent.Drop(userItem);
|
||||
component.TryInsertItem(userItem);
|
||||
}
|
||||
}
|
||||
|
||||
[Verb]
|
||||
private sealed class EjectVerb : Verb<BaseCharger>
|
||||
{
|
||||
protected override void GetData(IEntity user, BaseCharger component, VerbData data)
|
||||
{
|
||||
if (!ActionBlockerSystem.CanInteract(user))
|
||||
{
|
||||
data.Visibility = VerbVisibility.Invisible;
|
||||
return;
|
||||
}
|
||||
if (component._container.ContainedEntity == null)
|
||||
{
|
||||
data.Visibility = VerbVisibility.Invisible;
|
||||
return;
|
||||
}
|
||||
|
||||
var containerItemName = Loc.GetString(component._container.ContainedEntity.Name);
|
||||
|
||||
data.Text = Loc.GetString("Eject {0}", containerItemName);
|
||||
data.IconTexture = "/Textures/Interface/VerbIcons/eject.svg.192dpi.png";
|
||||
}
|
||||
|
||||
protected override void Activate(IEntity user, BaseCharger component)
|
||||
{
|
||||
component.RemoveItem(user);
|
||||
}
|
||||
}
|
||||
|
||||
private CellChargerStatus GetStatus()
|
||||
{
|
||||
if (Owner.TryGetComponent(out PowerReceiverComponent? receiver) &&
|
||||
!receiver.Powered)
|
||||
{
|
||||
return CellChargerStatus.Off;
|
||||
}
|
||||
if (_container.ContainedEntity == null)
|
||||
{
|
||||
return CellChargerStatus.Empty;
|
||||
}
|
||||
if (_heldBattery != null && Math.Abs(_heldBattery.MaxCharge - _heldBattery.CurrentCharge) < 0.01)
|
||||
{
|
||||
return CellChargerStatus.Charged;
|
||||
}
|
||||
return CellChargerStatus.Charging;
|
||||
}
|
||||
|
||||
private bool TryInsertItem(IEntity entity)
|
||||
{
|
||||
if (!IsEntityCompatible(entity) || _container.ContainedEntity != null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!_container.Insert(entity))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
_heldBattery = GetBatteryFrom(entity);
|
||||
UpdateStatus();
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If the supplied entity should fit into the charger.
|
||||
/// </summary>
|
||||
protected abstract bool IsEntityCompatible(IEntity entity);
|
||||
|
||||
protected abstract BatteryComponent? GetBatteryFrom(IEntity entity);
|
||||
|
||||
private void UpdateStatus()
|
||||
{
|
||||
// Not called UpdateAppearance just because it messes with the load
|
||||
var status = GetStatus();
|
||||
if (_status == status ||
|
||||
!Owner.TryGetComponent(out PowerReceiverComponent? receiver))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_status = status;
|
||||
Owner.TryGetComponent(out AppearanceComponent? appearance);
|
||||
|
||||
switch (_status)
|
||||
{
|
||||
// Update load just in case
|
||||
case CellChargerStatus.Off:
|
||||
receiver.Load = 0;
|
||||
appearance?.SetData(CellVisual.Light, CellChargerStatus.Off);
|
||||
break;
|
||||
case CellChargerStatus.Empty:
|
||||
receiver.Load = 0;
|
||||
appearance?.SetData(CellVisual.Light, CellChargerStatus.Empty);
|
||||
break;
|
||||
case CellChargerStatus.Charging:
|
||||
receiver.Load = (int) (_chargeRate / _transferEfficiency);
|
||||
appearance?.SetData(CellVisual.Light, CellChargerStatus.Charging);
|
||||
break;
|
||||
case CellChargerStatus.Charged:
|
||||
receiver.Load = 0;
|
||||
appearance?.SetData(CellVisual.Light, CellChargerStatus.Charged);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
appearance?.SetData(CellVisual.Occupied, _container.ContainedEntity != null);
|
||||
}
|
||||
|
||||
public void OnUpdate(float frameTime) //todo: make single system for this
|
||||
{
|
||||
if (_status == CellChargerStatus.Empty || _status == CellChargerStatus.Charged || _container.ContainedEntity == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
TransferPower(frameTime);
|
||||
}
|
||||
|
||||
private void TransferPower(float frameTime)
|
||||
{
|
||||
if (Owner.TryGetComponent(out PowerReceiverComponent? receiver) &&
|
||||
!receiver.Powered)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_heldBattery == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_heldBattery.CurrentCharge += _chargeRate * frameTime;
|
||||
// Just so the sprite won't be set to 99.99999% visibility
|
||||
if (_heldBattery.MaxCharge - _heldBattery.CurrentCharge < 0.01)
|
||||
{
|
||||
_heldBattery.CurrentCharge = _heldBattery.MaxCharge;
|
||||
}
|
||||
UpdateStatus();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,182 +0,0 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Content.Shared.GameObjects.EntitySystems;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Utility;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerReceiverUsers
|
||||
{
|
||||
/// <summary>
|
||||
/// Component that represents an emergency light, it has an internal battery that charges when the power is on.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public class EmergencyLightComponent : Component, IExamine
|
||||
{
|
||||
public override string Name => "EmergencyLight";
|
||||
|
||||
[ViewVariables]
|
||||
private EmergencyLightState State
|
||||
{
|
||||
get => _state;
|
||||
set
|
||||
{
|
||||
if (_state == value)
|
||||
return;
|
||||
|
||||
_state = value;
|
||||
Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, new EmergencyLightMessage(this, _state));
|
||||
}
|
||||
}
|
||||
|
||||
private EmergencyLightState _state = EmergencyLightState.Empty;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("wattage")]
|
||||
private float _wattage = 5;
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("chargingWattage")]
|
||||
private float _chargingWattage = 60;
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("chargingEfficiency")]
|
||||
private float _chargingEfficiency = 0.85f;
|
||||
|
||||
/// <summary>
|
||||
/// For attaching UpdateState() to events.
|
||||
/// </summary>
|
||||
public void UpdateState(PowerChangedMessage e)
|
||||
{
|
||||
UpdateState();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the light's power drain, battery drain, sprite and actual light state.
|
||||
/// </summary>
|
||||
public void UpdateState()
|
||||
{
|
||||
if (!Owner.TryGetComponent(out PowerReceiverComponent? receiver))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (receiver.Powered)
|
||||
{
|
||||
receiver.Load = (int) Math.Abs(_wattage);
|
||||
TurnOff();
|
||||
State = EmergencyLightState.Charging;
|
||||
}
|
||||
else
|
||||
{
|
||||
TurnOn();
|
||||
State = EmergencyLightState.On;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnUpdate(float frameTime)
|
||||
{
|
||||
if (Owner.Deleted || !Owner.TryGetComponent(out BatteryComponent? battery))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if(State == EmergencyLightState.On)
|
||||
{
|
||||
if (!battery.TryUseCharge(_wattage * frameTime))
|
||||
{
|
||||
State = EmergencyLightState.Empty;
|
||||
TurnOff();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
battery.CurrentCharge += _chargingWattage * frameTime * _chargingEfficiency;
|
||||
if (battery.BatteryState == BatteryState.Full)
|
||||
{
|
||||
if (Owner.TryGetComponent(out PowerReceiverComponent? receiver))
|
||||
{
|
||||
receiver.Load = 1;
|
||||
}
|
||||
|
||||
State = EmergencyLightState.Full;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void TurnOff()
|
||||
{
|
||||
if (Owner.TryGetComponent(out SpriteComponent? sprite))
|
||||
{
|
||||
sprite.LayerSetState(0, "emergency_light_off");
|
||||
}
|
||||
|
||||
if (Owner.TryGetComponent(out PointLightComponent? light))
|
||||
{
|
||||
light.Enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void TurnOn()
|
||||
{
|
||||
if (Owner.TryGetComponent(out SpriteComponent? sprite))
|
||||
{
|
||||
sprite.LayerSetState(0, "emergency_light_on");
|
||||
}
|
||||
|
||||
if (Owner.TryGetComponent(out PointLightComponent? light))
|
||||
{
|
||||
light.Enabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
public override void HandleMessage(ComponentMessage message, IComponent? component)
|
||||
{
|
||||
base.HandleMessage(message, component);
|
||||
switch (message)
|
||||
{
|
||||
case PowerChangedMessage powerChanged:
|
||||
UpdateState(powerChanged);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void IExamine.Examine(FormattedMessage message, bool inDetailsRange)
|
||||
{
|
||||
message.AddMarkup(Loc.GetString($"The battery indicator displays: {BatteryStateText[State]}."));
|
||||
}
|
||||
|
||||
public enum EmergencyLightState
|
||||
{
|
||||
Charging,
|
||||
Full,
|
||||
Empty,
|
||||
On
|
||||
}
|
||||
|
||||
public Dictionary<EmergencyLightState, string> BatteryStateText = new()
|
||||
{
|
||||
{ EmergencyLightState.Full, "[color=darkgreen]Full[/color]"},
|
||||
{ EmergencyLightState.Empty, "[color=darkred]Empty[/color]"},
|
||||
{ EmergencyLightState.Charging, "[color=darkorange]Charging[/color]"},
|
||||
{ EmergencyLightState.On, "[color=darkorange]Discharging[/color]"}
|
||||
};
|
||||
}
|
||||
|
||||
public sealed class EmergencyLightMessage : EntityEventArgs
|
||||
{
|
||||
public EmergencyLightComponent Component { get; }
|
||||
|
||||
public EmergencyLightComponent.EmergencyLightState State { get; }
|
||||
|
||||
public EmergencyLightMessage(EmergencyLightComponent component, EmergencyLightComponent.EmergencyLightState state)
|
||||
{
|
||||
Component = component;
|
||||
State = state;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,139 +0,0 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.GameObjects.EntitySystems;
|
||||
using Content.Shared.Interfaces.GameObjects.Components;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerReceiverUsers
|
||||
{
|
||||
public enum LightBulbState
|
||||
{
|
||||
Normal,
|
||||
Broken,
|
||||
Burned,
|
||||
}
|
||||
|
||||
public enum LightBulbType
|
||||
{
|
||||
Bulb,
|
||||
Tube,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Component that represents a light bulb. Can be broken, or burned, which turns them mostly useless.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public class LightBulbComponent : Component, ILand, IBreakAct
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
|
||||
/// <summary>
|
||||
/// Invoked whenever the state of the light bulb changes.
|
||||
/// </summary>
|
||||
public event EventHandler<EventArgs>? OnLightBulbStateChange;
|
||||
public event EventHandler<EventArgs?>? OnLightColorChange;
|
||||
|
||||
[DataField("color")]
|
||||
private Color _color = Color.White;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)] public Color Color
|
||||
{
|
||||
get { return _color; }
|
||||
set
|
||||
{
|
||||
_color = value;
|
||||
OnLightColorChange?.Invoke(this, null);
|
||||
UpdateColor();
|
||||
}
|
||||
}
|
||||
|
||||
public override string Name => "LightBulb";
|
||||
|
||||
[DataField("bulb")]
|
||||
public LightBulbType Type = LightBulbType.Tube;
|
||||
|
||||
[DataField("BurningTemperature")]
|
||||
private int _burningTemperature = 1400;
|
||||
public int BurningTemperature => _burningTemperature;
|
||||
|
||||
[DataField("PowerUse")]
|
||||
private int _powerUse = 40;
|
||||
public int PowerUse => _powerUse;
|
||||
|
||||
/// <summary>
|
||||
/// The current state of the light bulb. Invokes the OnLightBulbStateChange event when set.
|
||||
/// It also updates the bulb's sprite accordingly.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)] public LightBulbState State
|
||||
{
|
||||
get { return _state; }
|
||||
set
|
||||
{
|
||||
var sprite = Owner.GetComponent<SpriteComponent>();
|
||||
OnLightBulbStateChange?.Invoke(this, EventArgs.Empty);
|
||||
_state = value;
|
||||
switch (value)
|
||||
{
|
||||
case LightBulbState.Normal:
|
||||
sprite.LayerSetState(0, "normal");
|
||||
break;
|
||||
case LightBulbState.Broken:
|
||||
sprite.LayerSetState(0, "broken");
|
||||
break;
|
||||
case LightBulbState.Burned:
|
||||
sprite.LayerSetState(0, "burned");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private LightBulbState _state = LightBulbState.Normal;
|
||||
|
||||
public void UpdateColor()
|
||||
{
|
||||
if (!Owner.TryGetComponent(out SpriteComponent? sprite))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
sprite.Color = Color;
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
UpdateColor();
|
||||
}
|
||||
|
||||
void ILand.Land(LandEventArgs eventArgs)
|
||||
{
|
||||
PlayBreakSound();
|
||||
State = LightBulbState.Broken;
|
||||
}
|
||||
|
||||
public void OnBreak(BreakageEventArgs eventArgs)
|
||||
{
|
||||
State = LightBulbState.Broken;
|
||||
}
|
||||
|
||||
public void PlayBreakSound()
|
||||
{
|
||||
var soundCollection = _prototypeManager.Index<SoundCollectionPrototype>("GlassBreak");
|
||||
var file = _random.Pick(soundCollection.PickFiles);
|
||||
|
||||
SoundSystem.Play(Filter.Pvs(Owner), file, Owner);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
#nullable enable
|
||||
using Content.Shared.Interfaces.GameObjects.Components;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerReceiverUsers
|
||||
{
|
||||
/// <summary>
|
||||
/// Recharges an entity with a <see cref="BatteryComponent"/>.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(IActivate))]
|
||||
[ComponentReference(typeof(BaseCharger))]
|
||||
public sealed class PowerCellChargerComponent : BaseCharger
|
||||
{
|
||||
public override string Name => "PowerCellCharger";
|
||||
|
||||
protected override bool IsEntityCompatible(IEntity entity)
|
||||
{
|
||||
return entity.HasComponent<BatteryComponent>();
|
||||
}
|
||||
|
||||
protected override BatteryComponent GetBatteryFrom(IEntity entity)
|
||||
{
|
||||
return entity.GetComponent<BatteryComponent>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,338 +0,0 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.GameObjects.Components.GUI;
|
||||
using Content.Server.GameObjects.Components.Items.Storage;
|
||||
using Content.Server.GameObjects.Components.MachineLinking;
|
||||
using Content.Server.GameObjects.Components.MachineLinking.Signals;
|
||||
using Content.Server.GameObjects.Components.Mobs;
|
||||
using Content.Server.GameObjects.Components.Observer;
|
||||
using Content.Shared.Actions;
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.GameObjects.Components.Damage;
|
||||
using Content.Shared.GameObjects.Components.Power.ApcNetComponents.PowerReceiverUsers;
|
||||
using Content.Shared.Interfaces;
|
||||
using Content.Shared.Interfaces.GameObjects.Components;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerReceiverUsers
|
||||
{
|
||||
/// <summary>
|
||||
/// Component that represents a wall light. It has a light bulb that can be replaced when broken.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public class PoweredLightComponent : Component, IInteractHand, IInteractUsing, IMapInit, ISignalReceiver<bool>, ISignalReceiver<ToggleSignal>, IGhostBooAffected
|
||||
{
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
|
||||
public override string Name => "PoweredLight";
|
||||
|
||||
private static readonly TimeSpan _thunkDelay = TimeSpan.FromSeconds(2);
|
||||
// time to blink light when ghost made boo nearby
|
||||
private static readonly TimeSpan ghostBlinkingTime = TimeSpan.FromSeconds(10);
|
||||
private static readonly TimeSpan ghostBlinkingCooldown = TimeSpan.FromSeconds(60);
|
||||
|
||||
[ComponentDependency]
|
||||
private readonly AppearanceComponent? _appearance;
|
||||
|
||||
private TimeSpan _lastThunk;
|
||||
private TimeSpan? _lastGhostBlink;
|
||||
|
||||
[DataField("hasLampOnSpawn")]
|
||||
private bool _hasLampOnSpawn = true;
|
||||
|
||||
[ViewVariables] [DataField("on")]
|
||||
private bool _on = true;
|
||||
|
||||
[ViewVariables]
|
||||
private bool _currentLit;
|
||||
|
||||
[ViewVariables]
|
||||
private bool _isBlinking;
|
||||
|
||||
[ViewVariables] [DataField("ignoreGhostsBoo")]
|
||||
private bool _ignoreGhostsBoo;
|
||||
|
||||
[DataField("bulb")] private LightBulbType _bulbType = LightBulbType.Tube;
|
||||
public LightBulbType BulbType => _bulbType;
|
||||
|
||||
[ViewVariables] private ContainerSlot _lightBulbContainer = default!;
|
||||
|
||||
[ViewVariables]
|
||||
public LightBulbComponent? LightBulb
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_lightBulbContainer.ContainedEntity == null) return null;
|
||||
|
||||
_lightBulbContainer.ContainedEntity.TryGetComponent(out LightBulbComponent? bulb);
|
||||
|
||||
return bulb;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO CONSTRUCTION make this use a construction graph
|
||||
|
||||
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
|
||||
{
|
||||
return InsertBulb(eventArgs.Using);
|
||||
}
|
||||
|
||||
bool IInteractHand.InteractHand(InteractHandEventArgs eventArgs)
|
||||
{
|
||||
if (!eventArgs.User.TryGetComponent(out IDamageableComponent? damageableComponent))
|
||||
{
|
||||
Eject();
|
||||
return false;
|
||||
}
|
||||
if(eventArgs.User.TryGetComponent(out HeatResistanceComponent? heatResistanceComponent))
|
||||
{
|
||||
if(CanBurn(heatResistanceComponent.GetHeatResistance()))
|
||||
{
|
||||
Burn();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Eject();
|
||||
return true;
|
||||
|
||||
bool CanBurn(int heatResistance)
|
||||
{
|
||||
if (LightBulb == null)
|
||||
return false;
|
||||
|
||||
return _currentLit && heatResistance < LightBulb.BurningTemperature;
|
||||
}
|
||||
|
||||
void Burn()
|
||||
{
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("You burn your hand!"));
|
||||
damageableComponent.ChangeDamage(DamageType.Heat, 20, false, Owner);
|
||||
SoundSystem.Play(Filter.Pvs(Owner), "/Audio/Effects/lightburn.ogg", Owner);
|
||||
}
|
||||
|
||||
void Eject()
|
||||
{
|
||||
EjectBulb(eventArgs.User);
|
||||
UpdateLight();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Try to replace current bulb with a new one
|
||||
/// </summary>
|
||||
public bool ReplaceBulb(IEntity bulb)
|
||||
{
|
||||
EjectBulb();
|
||||
return InsertBulb(bulb);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts the bulb if possible.
|
||||
/// </summary>
|
||||
/// <returns>True if it could insert it, false if it couldn't.</returns>
|
||||
private bool InsertBulb(IEntity bulb)
|
||||
{
|
||||
if (LightBulb != null) return false;
|
||||
if (!bulb.TryGetComponent(out LightBulbComponent? lightBulb)) return false;
|
||||
if (lightBulb.Type != _bulbType) return false;
|
||||
|
||||
var inserted = _lightBulbContainer.Insert(bulb);
|
||||
|
||||
lightBulb.OnLightBulbStateChange += UpdateLight;
|
||||
lightBulb.OnLightColorChange += UpdateLight;
|
||||
|
||||
UpdateLight();
|
||||
|
||||
return inserted;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ejects the bulb to a mob's hand if possible.
|
||||
/// </summary>
|
||||
private void EjectBulb(IEntity? user = null)
|
||||
{
|
||||
if (LightBulb == null) return;
|
||||
|
||||
var bulb = LightBulb;
|
||||
|
||||
bulb.OnLightBulbStateChange -= UpdateLight;
|
||||
bulb.OnLightColorChange -= UpdateLight;
|
||||
|
||||
if (!_lightBulbContainer.Remove(bulb.Owner)) return;
|
||||
|
||||
if (user != null)
|
||||
{
|
||||
if (!user.TryGetComponent(out HandsComponent? hands)
|
||||
|| !hands.PutInHand(bulb.Owner.GetComponent<ItemComponent>()))
|
||||
bulb.Owner.Transform.Coordinates = user.Transform.Coordinates;
|
||||
}
|
||||
else
|
||||
{
|
||||
bulb.Owner.Transform.Coordinates = Owner.Transform.Coordinates;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// For attaching UpdateLight() to events.
|
||||
/// </summary>
|
||||
public void UpdateLight(object? sender, EventArgs? e)
|
||||
{
|
||||
UpdateLight();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the light's power drain, sprite and actual light state.
|
||||
/// </summary>
|
||||
public void UpdateLight()
|
||||
{
|
||||
var powerReceiver = Owner.GetComponent<PowerReceiverComponent>();
|
||||
|
||||
if (LightBulb == null) // No light bulb.
|
||||
{
|
||||
_currentLit = false;
|
||||
powerReceiver.Load = 0;
|
||||
_appearance?.SetData(PoweredLightVisuals.BulbState, PoweredLightState.Empty);
|
||||
return;
|
||||
}
|
||||
|
||||
switch (LightBulb.State)
|
||||
{
|
||||
case LightBulbState.Normal:
|
||||
if (powerReceiver.Powered && _on)
|
||||
{
|
||||
_currentLit = true;
|
||||
powerReceiver.Load = LightBulb.PowerUse;
|
||||
_appearance?.SetData(PoweredLightVisuals.BulbState, PoweredLightState.On);
|
||||
_appearance?.SetData(PoweredLightVisuals.BulbColor, LightBulb.Color);
|
||||
var time = _gameTiming.CurTime;
|
||||
if (time > _lastThunk + _thunkDelay)
|
||||
{
|
||||
_lastThunk = time;
|
||||
SoundSystem.Play(Filter.Pvs(Owner), "/Audio/Machines/light_tube_on.ogg", Owner, AudioParams.Default.WithVolume(-10f));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_currentLit = false;
|
||||
_appearance?.SetData(PoweredLightVisuals.BulbState, PoweredLightState.Off);
|
||||
}
|
||||
break;
|
||||
case LightBulbState.Broken:
|
||||
_currentLit = false;
|
||||
_appearance?.SetData(PoweredLightVisuals.BulbState, PoweredLightState.Broken);
|
||||
break;
|
||||
case LightBulbState.Burned:
|
||||
_currentLit = false;
|
||||
_appearance?.SetData(PoweredLightVisuals.BulbState, PoweredLightState.Burned);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
_lightBulbContainer = ContainerHelpers.EnsureContainer<ContainerSlot>(Owner, "light_bulb");
|
||||
}
|
||||
|
||||
public override void HandleMessage(ComponentMessage message, IComponent? component)
|
||||
{
|
||||
base.HandleMessage(message, component);
|
||||
switch (message)
|
||||
{
|
||||
case PowerChangedMessage:
|
||||
UpdateLight();
|
||||
break;
|
||||
case DamageChangedMessage msg:
|
||||
TryDestroyBulb(msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void TryDestroyBulb(DamageChangedMessage msg)
|
||||
{
|
||||
if (!msg.TookDamage)
|
||||
return;
|
||||
|
||||
if (LightBulb == null || LightBulb.State == LightBulbState.Broken)
|
||||
return;
|
||||
|
||||
LightBulb.State = LightBulbState.Broken;
|
||||
LightBulb.PlayBreakSound();
|
||||
UpdateLight();
|
||||
}
|
||||
|
||||
void IMapInit.MapInit()
|
||||
{
|
||||
if (_hasLampOnSpawn)
|
||||
{
|
||||
var prototype = _bulbType switch
|
||||
{
|
||||
LightBulbType.Bulb => "LightBulb",
|
||||
LightBulbType.Tube => "LightTube",
|
||||
_ => throw new ArgumentOutOfRangeException()
|
||||
};
|
||||
|
||||
var entity = Owner.EntityManager.SpawnEntity(prototype, Owner.Transform.Coordinates);
|
||||
_lightBulbContainer.Insert(entity);
|
||||
}
|
||||
|
||||
// need this to update visualizers
|
||||
UpdateLight();
|
||||
}
|
||||
|
||||
public void TriggerSignal(bool signal)
|
||||
{
|
||||
_on = signal;
|
||||
UpdateLight();
|
||||
}
|
||||
|
||||
public void TriggerSignal(ToggleSignal signal)
|
||||
{
|
||||
_on = !_on;
|
||||
UpdateLight();
|
||||
}
|
||||
|
||||
public void ToggleBlinkingLight(bool isNowBlinking)
|
||||
{
|
||||
if (_isBlinking == isNowBlinking)
|
||||
return;
|
||||
|
||||
_isBlinking = isNowBlinking;
|
||||
_appearance?.SetData(PoweredLightVisuals.Blinking, _isBlinking);
|
||||
}
|
||||
|
||||
public bool AffectedByGhostBoo(InstantActionEventArgs args)
|
||||
{
|
||||
if (_ignoreGhostsBoo)
|
||||
return false;
|
||||
|
||||
// check cooldown first to prevent abuse
|
||||
var time = _gameTiming.CurTime;
|
||||
if (_lastGhostBlink != null)
|
||||
{
|
||||
if (time <= _lastGhostBlink + ghostBlinkingCooldown)
|
||||
return false;
|
||||
}
|
||||
_lastGhostBlink = time;
|
||||
|
||||
ToggleBlinkingLight(true);
|
||||
Owner.SpawnTimer(ghostBlinkingTime, () => {
|
||||
ToggleBlinkingLight(false);
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
#nullable enable
|
||||
using Content.Server.GameObjects.Components.Weapon.Ranged.Barrels;
|
||||
using Content.Shared.Interfaces.GameObjects.Components;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerReceiverUsers
|
||||
{
|
||||
/// <summary>
|
||||
/// Recharges the battery in a <see cref="ServerBatteryBarrelComponent"/>.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(IActivate))]
|
||||
[ComponentReference(typeof(BaseCharger))]
|
||||
public sealed class WeaponCapacitorChargerComponent : BaseCharger
|
||||
{
|
||||
public override string Name => "WeaponCapacitorCharger";
|
||||
|
||||
protected override bool IsEntityCompatible(IEntity entity)
|
||||
{
|
||||
return entity.TryGetComponent(out ServerBatteryBarrelComponent? battery) && battery.PowerCell != null ||
|
||||
entity.TryGetComponent(out PowerCellSlotComponent? slot) && slot.HasCell;
|
||||
}
|
||||
|
||||
protected override BatteryComponent? GetBatteryFrom(IEntity entity)
|
||||
{
|
||||
if (entity.TryGetComponent(out PowerCellSlotComponent? slot))
|
||||
{
|
||||
if (slot.Cell != null)
|
||||
{
|
||||
return slot.Cell;
|
||||
}
|
||||
}
|
||||
|
||||
if (entity.TryGetComponent(out ServerBatteryBarrelComponent? battery))
|
||||
{
|
||||
if (battery.PowerCell != null)
|
||||
{
|
||||
return battery.PowerCell;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user