emitter ecs & bugfixes (#4348)
* a * finished emitter ecs Co-authored-by: Paul <ritter.paul1+git@googlemail.com>
This commit is contained in:
@@ -1,257 +1,45 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.Access.Components;
|
||||
using Content.Server.Power.Components;
|
||||
using Content.Server.Projectiles.Components;
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Notification;
|
||||
using Content.Shared.Notification.Managers;
|
||||
using Content.Shared.Singularity.Components;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Utility;
|
||||
using Robust.Shared.ViewVariables;
|
||||
using Timer = Robust.Shared.Timing.Timer;
|
||||
|
||||
|
||||
namespace Content.Server.Singularity.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(IActivate))]
|
||||
public class EmitterComponent : Component, IActivate, IInteractUsing
|
||||
public class EmitterComponent : Component
|
||||
{
|
||||
[Dependency] private readonly IRobustRandom _robustRandom = default!;
|
||||
|
||||
[ComponentDependency] private readonly AppearanceComponent? _appearance = default;
|
||||
[ComponentDependency] private readonly AccessReader? _accessReader = default;
|
||||
[ComponentDependency] public readonly AppearanceComponent? Appearance = default;
|
||||
[ComponentDependency] public readonly AccessReader? AccessReader = default;
|
||||
[ComponentDependency] public readonly PowerConsumerComponent? PowerConsumer = default;
|
||||
|
||||
public override string Name => "Emitter";
|
||||
|
||||
private CancellationTokenSource? _timerCancel;
|
||||
|
||||
private PowerConsumerComponent _powerConsumer = default!;
|
||||
public CancellationTokenSource? TimerCancel;
|
||||
|
||||
// whether the power switch is in "on"
|
||||
[ViewVariables] public bool IsOn { get; private set; }
|
||||
[ViewVariables] public bool IsOn;
|
||||
// Whether the power switch is on AND the machine has enough power (so is actively firing)
|
||||
[ViewVariables] private bool _isPowered;
|
||||
[ViewVariables] private bool _isLocked;
|
||||
[ViewVariables] public bool IsPowered;
|
||||
[ViewVariables] public bool IsLocked;
|
||||
|
||||
// For the "emitter fired" sound
|
||||
private const float Variation = 0.25f;
|
||||
private const float Volume = 0.5f;
|
||||
private const float Distance = 3f;
|
||||
public const float Variation = 0.25f;
|
||||
public const float Volume = 0.5f;
|
||||
public const float Distance = 3f;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)] private int _fireShotCounter;
|
||||
[ViewVariables] public int FireShotCounter;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)] [DataField("fireSound")] private string _fireSound = "/Audio/Weapons/emitter.ogg";
|
||||
[ViewVariables(VVAccess.ReadWrite)] [DataField("boltType")] private string _boltType = "EmitterBolt";
|
||||
[ViewVariables(VVAccess.ReadWrite)] [DataField("powerUseActive")] private int _powerUseActive = 500;
|
||||
[ViewVariables(VVAccess.ReadWrite)] [DataField("fireBurstSize")] private int _fireBurstSize = 3;
|
||||
[ViewVariables(VVAccess.ReadWrite)] [DataField("fireInterval")] private TimeSpan _fireInterval = TimeSpan.FromSeconds(2);
|
||||
[ViewVariables(VVAccess.ReadWrite)] [DataField("fireBurstDelayMin")] private TimeSpan _fireBurstDelayMin = TimeSpan.FromSeconds(2);
|
||||
[ViewVariables(VVAccess.ReadWrite)] [DataField("fireBurstDelayMax")] private TimeSpan _fireBurstDelayMax = TimeSpan.FromSeconds(10);
|
||||
|
||||
void IActivate.Activate(ActivateEventArgs eventArgs)
|
||||
{
|
||||
if (_isLocked)
|
||||
{
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("comp-emitter-access-locked", ("target", Owner)));
|
||||
return;
|
||||
}
|
||||
|
||||
if (Owner.TryGetComponent(out PhysicsComponent? phys) && phys.BodyType == BodyType.Static)
|
||||
{
|
||||
if (!IsOn)
|
||||
{
|
||||
SwitchOn();
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("comp-emitter-turned-on", ("target", Owner)));
|
||||
}
|
||||
else
|
||||
{
|
||||
SwitchOff();
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("comp-emitter-turned-off", ("target", Owner)));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("comp-emitter-not-anchored", ("target", Owner)));
|
||||
}
|
||||
}
|
||||
|
||||
Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
|
||||
{
|
||||
if (_accessReader == null || !eventArgs.Using.TryGetComponent(out IAccess? access))
|
||||
{
|
||||
return Task.FromResult(false);
|
||||
}
|
||||
|
||||
if (_accessReader.IsAllowed(access))
|
||||
{
|
||||
_isLocked ^= true;
|
||||
|
||||
if (_isLocked)
|
||||
{
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("comp-emitter-lock", ("target", Owner)));
|
||||
}
|
||||
else
|
||||
{
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("comp-emitter-unlock", ("target", Owner)));
|
||||
}
|
||||
|
||||
UpdateAppearance();
|
||||
}
|
||||
else
|
||||
{
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("comp-emitter-access-denied"));
|
||||
}
|
||||
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
|
||||
public void SwitchOff()
|
||||
{
|
||||
IsOn = false;
|
||||
_powerConsumer.DrawRate = 0;
|
||||
PowerOff();
|
||||
UpdateAppearance();
|
||||
}
|
||||
|
||||
public void SwitchOn()
|
||||
{
|
||||
IsOn = true;
|
||||
_powerConsumer.DrawRate = _powerUseActive;
|
||||
// Do not directly PowerOn().
|
||||
// OnReceivedPowerChanged will get fired due to DrawRate change which will turn it on.
|
||||
UpdateAppearance();
|
||||
}
|
||||
|
||||
public void PowerOff()
|
||||
{
|
||||
if (!_isPowered)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_isPowered = false;
|
||||
|
||||
// Must be set while emitter powered.
|
||||
DebugTools.AssertNotNull(_timerCancel);
|
||||
_timerCancel!.Cancel();
|
||||
|
||||
UpdateAppearance();
|
||||
}
|
||||
|
||||
public void PowerOn()
|
||||
{
|
||||
if (_isPowered)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_isPowered = true;
|
||||
|
||||
_fireShotCounter = 0;
|
||||
_timerCancel = new CancellationTokenSource();
|
||||
|
||||
Timer.Spawn(_fireBurstDelayMax, ShotTimerCallback, _timerCancel.Token);
|
||||
|
||||
UpdateAppearance();
|
||||
}
|
||||
|
||||
private void ShotTimerCallback()
|
||||
{
|
||||
// Any power-off condition should result in the timer for this method being cancelled
|
||||
// and thus not firing
|
||||
DebugTools.Assert(_isPowered);
|
||||
DebugTools.Assert(IsOn);
|
||||
DebugTools.Assert(_powerConsumer.DrawRate <= _powerConsumer.ReceivedPower);
|
||||
|
||||
Fire();
|
||||
|
||||
TimeSpan delay;
|
||||
if (_fireShotCounter < _fireBurstSize)
|
||||
{
|
||||
_fireShotCounter += 1;
|
||||
delay = _fireInterval;
|
||||
}
|
||||
else
|
||||
{
|
||||
_fireShotCounter = 0;
|
||||
var diff = _fireBurstDelayMax - _fireBurstDelayMin;
|
||||
// TIL you can do TimeSpan * double.
|
||||
delay = _fireBurstDelayMin + _robustRandom.NextFloat() * diff;
|
||||
}
|
||||
|
||||
// Must be set while emitter powered.
|
||||
DebugTools.AssertNotNull(_timerCancel);
|
||||
Timer.Spawn(delay, ShotTimerCallback, _timerCancel!.Token);
|
||||
}
|
||||
|
||||
private void Fire()
|
||||
{
|
||||
var projectile = Owner.EntityManager.SpawnEntity(_boltType, Owner.Transform.Coordinates);
|
||||
|
||||
if (!projectile.TryGetComponent<PhysicsComponent>(out var physicsComponent))
|
||||
{
|
||||
Logger.Error("Emitter tried firing a bolt, but it was spawned without a PhysicsComponent");
|
||||
return;
|
||||
}
|
||||
|
||||
physicsComponent.BodyStatus = BodyStatus.InAir;
|
||||
|
||||
if (!projectile.TryGetComponent<ProjectileComponent>(out var projectileComponent))
|
||||
{
|
||||
Logger.Error("Emitter tried firing a bolt, but it was spawned without a ProjectileComponent");
|
||||
return;
|
||||
}
|
||||
|
||||
projectileComponent.IgnoreEntity(Owner);
|
||||
|
||||
physicsComponent
|
||||
.LinearVelocity = Owner.Transform.WorldRotation.ToWorldVec() * 20f;
|
||||
projectile.Transform.WorldRotation = Owner.Transform.WorldRotation;
|
||||
|
||||
// TODO: Move to projectile's code.
|
||||
Timer.Spawn(3000, () => projectile.Delete());
|
||||
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _fireSound, Owner,
|
||||
AudioHelpers.WithVariation(Variation).WithVolume(Volume).WithMaxDistance(Distance));
|
||||
}
|
||||
|
||||
private void UpdateAppearance()
|
||||
{
|
||||
if (_appearance == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
EmitterVisualState state;
|
||||
if (_isPowered)
|
||||
{
|
||||
state = EmitterVisualState.On;
|
||||
}
|
||||
else if (IsOn)
|
||||
{
|
||||
state = EmitterVisualState.Underpowered;
|
||||
}
|
||||
else
|
||||
{
|
||||
state = EmitterVisualState.Off;
|
||||
}
|
||||
|
||||
_appearance.SetData(EmitterVisuals.VisualState, state);
|
||||
_appearance.SetData(EmitterVisuals.Locked, _isLocked);
|
||||
}
|
||||
[ViewVariables] [DataField("fireSound")] public string FireSound = "/Audio/Weapons/emitter.ogg";
|
||||
[ViewVariables] [DataField("boltType")] public string BoltType = "EmitterBolt";
|
||||
[ViewVariables] [DataField("powerUseActive")] public int PowerUseActive = 500;
|
||||
[ViewVariables] [DataField("fireBurstSize")] public int FireBurstSize = 3;
|
||||
[ViewVariables] [DataField("fireInterval")] public TimeSpan FireInterval = TimeSpan.FromSeconds(2);
|
||||
[ViewVariables] [DataField("fireBurstDelayMin")] public TimeSpan FireBurstDelayMin = TimeSpan.FromSeconds(2);
|
||||
[ViewVariables] [DataField("fireBurstDelayMax")] public TimeSpan FireBurstDelayMax = TimeSpan.FromSeconds(10);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,102 @@
|
||||
using Content.Server.Power.EntitySystems;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using Content.Server.Access.Components;
|
||||
using Content.Server.Power.EntitySystems;
|
||||
using Content.Server.Projectiles.Components;
|
||||
using Content.Server.Singularity.Components;
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Notification.Managers;
|
||||
using Content.Shared.Singularity.Components;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Utility;
|
||||
using Timer = Robust.Shared.Timing.Timer;
|
||||
|
||||
namespace Content.Server.Singularity.EntitySystems
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public class EmitterSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<EmitterComponent, PowerConsumerReceivedChanged>(ReceivedChanged);
|
||||
SubscribeLocalEvent<EmitterComponent, InteractHandEvent>(OnInteractHand);
|
||||
SubscribeLocalEvent<EmitterComponent, InteractUsingEvent>(OnInteractUsing);
|
||||
}
|
||||
|
||||
private static void ReceivedChanged(
|
||||
private void OnInteractUsing(EntityUid uid, EmitterComponent component, InteractUsingEvent args)
|
||||
{
|
||||
if(args.Handled) return;
|
||||
|
||||
if (component.AccessReader == null || !args.Used.TryGetComponent(out IAccess? access))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (component.AccessReader.IsAllowed(access))
|
||||
{
|
||||
component.IsLocked ^= true;
|
||||
|
||||
if (component.IsLocked)
|
||||
{
|
||||
component.Owner.PopupMessage(args.User, Loc.GetString("comp-emitter-lock", ("target", component.Owner)));
|
||||
}
|
||||
else
|
||||
{
|
||||
component.Owner.PopupMessage(args.User, Loc.GetString("comp-emitter-unlock", ("target", component.Owner)));
|
||||
}
|
||||
|
||||
UpdateAppearance(component);
|
||||
}
|
||||
else
|
||||
{
|
||||
component.Owner.PopupMessage(args.User, Loc.GetString("comp-emitter-access-denied"));
|
||||
}
|
||||
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void OnInteractHand(EntityUid uid, EmitterComponent component, InteractHandEvent args)
|
||||
{
|
||||
args.Handled = true;
|
||||
if (component.IsLocked)
|
||||
{
|
||||
component.Owner.PopupMessage(args.User, Loc.GetString("comp-emitter-access-locked", ("target", component.Owner)));
|
||||
return;
|
||||
}
|
||||
|
||||
if (component.Owner.TryGetComponent(out PhysicsComponent? phys) && phys.BodyType == BodyType.Static)
|
||||
{
|
||||
if (!component.IsOn)
|
||||
{
|
||||
SwitchOn(component);
|
||||
component.Owner.PopupMessage(args.User, Loc.GetString("comp-emitter-turned-on", ("target", component.Owner)));
|
||||
}
|
||||
else
|
||||
{
|
||||
SwitchOff(component);
|
||||
component.Owner.PopupMessage(args.User, Loc.GetString("comp-emitter-turned-off", ("target", component.Owner)));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
component.Owner.PopupMessage(args.User, Loc.GetString("comp-emitter-not-anchored", ("target", component.Owner)));
|
||||
}
|
||||
}
|
||||
|
||||
private void ReceivedChanged(
|
||||
EntityUid uid,
|
||||
EmitterComponent component,
|
||||
PowerConsumerReceivedChanged args)
|
||||
@@ -27,12 +108,147 @@ namespace Content.Server.Singularity.EntitySystems
|
||||
|
||||
if (args.ReceivedPower < args.DrawRate)
|
||||
{
|
||||
component.PowerOff();
|
||||
PowerOff(component);
|
||||
}
|
||||
else
|
||||
{
|
||||
component.PowerOn();
|
||||
}
|
||||
PowerOn(component);
|
||||
}
|
||||
}
|
||||
|
||||
public void SwitchOff(EmitterComponent component)
|
||||
{
|
||||
component.IsOn = false;
|
||||
if (component.PowerConsumer != null) component.PowerConsumer.DrawRate = 0;
|
||||
PowerOff(component);
|
||||
UpdateAppearance(component);
|
||||
}
|
||||
|
||||
public void SwitchOn(EmitterComponent component)
|
||||
{
|
||||
component.IsOn = true;
|
||||
if (component.PowerConsumer != null) component.PowerConsumer.DrawRate = component.PowerUseActive;
|
||||
// Do not directly PowerOn().
|
||||
// OnReceivedPowerChanged will get fired due to DrawRate change which will turn it on.
|
||||
UpdateAppearance(component);
|
||||
}
|
||||
|
||||
public void PowerOff(EmitterComponent component)
|
||||
{
|
||||
if (!component.IsPowered)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
component.IsPowered = false;
|
||||
|
||||
// Must be set while emitter powered.
|
||||
DebugTools.AssertNotNull(component.TimerCancel);
|
||||
component.TimerCancel?.Cancel();
|
||||
|
||||
UpdateAppearance(component);
|
||||
}
|
||||
|
||||
public void PowerOn(EmitterComponent component)
|
||||
{
|
||||
if (component.IsPowered)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
component.IsPowered = true;
|
||||
|
||||
component.FireShotCounter = 0;
|
||||
component.TimerCancel = new CancellationTokenSource();
|
||||
|
||||
Timer.Spawn(component.FireBurstDelayMax, () => ShotTimerCallback(component), component.TimerCancel.Token);
|
||||
|
||||
UpdateAppearance(component);
|
||||
}
|
||||
|
||||
private void ShotTimerCallback(EmitterComponent component)
|
||||
{
|
||||
// Any power-off condition should result in the timer for this method being cancelled
|
||||
// and thus not firing
|
||||
DebugTools.Assert(component.IsPowered);
|
||||
DebugTools.Assert(component.IsOn);
|
||||
DebugTools.Assert(component.PowerConsumer != null && (component.PowerConsumer.DrawRate <= component.PowerConsumer.ReceivedPower));
|
||||
|
||||
Fire(component);
|
||||
|
||||
TimeSpan delay;
|
||||
if (component.FireShotCounter < component.FireBurstSize)
|
||||
{
|
||||
component.FireShotCounter += 1;
|
||||
delay = component.FireInterval;
|
||||
}
|
||||
else
|
||||
{
|
||||
component.FireShotCounter = 0;
|
||||
var diff = component.FireBurstDelayMax - component.FireBurstDelayMin;
|
||||
// TIL you can do TimeSpan * double.
|
||||
delay = component.FireBurstDelayMin + _random.NextFloat() * diff;
|
||||
}
|
||||
|
||||
// Must be set while emitter powered.
|
||||
DebugTools.AssertNotNull(component.TimerCancel);
|
||||
Timer.Spawn(delay, () => ShotTimerCallback(component), component.TimerCancel!.Token);
|
||||
}
|
||||
|
||||
private void Fire(EmitterComponent component)
|
||||
{
|
||||
var projectile = component.Owner.EntityManager.SpawnEntity(component.BoltType, component.Owner.Transform.Coordinates);
|
||||
|
||||
if (!projectile.TryGetComponent<PhysicsComponent>(out var physicsComponent))
|
||||
{
|
||||
Logger.Error("Emitter tried firing a bolt, but it was spawned without a PhysicsComponent");
|
||||
return;
|
||||
}
|
||||
|
||||
physicsComponent.BodyStatus = BodyStatus.InAir;
|
||||
|
||||
if (!projectile.TryGetComponent<ProjectileComponent>(out var projectileComponent))
|
||||
{
|
||||
Logger.Error("Emitter tried firing a bolt, but it was spawned without a ProjectileComponent");
|
||||
return;
|
||||
}
|
||||
|
||||
projectileComponent.IgnoreEntity(component.Owner);
|
||||
|
||||
physicsComponent
|
||||
.LinearVelocity = component.Owner.Transform.WorldRotation.ToWorldVec() * 20f;
|
||||
projectile.Transform.WorldRotation = component.Owner.Transform.WorldRotation;
|
||||
|
||||
// TODO: Move to projectile's code.
|
||||
Timer.Spawn(3000, () => projectile.Delete());
|
||||
|
||||
SoundSystem.Play(Filter.Pvs(component.Owner), component.FireSound, component.Owner,
|
||||
AudioHelpers.WithVariation(EmitterComponent.Variation).WithVolume(EmitterComponent.Volume).WithMaxDistance(EmitterComponent.Distance));
|
||||
}
|
||||
|
||||
private void UpdateAppearance(EmitterComponent component)
|
||||
{
|
||||
if (component.Appearance == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
EmitterVisualState state;
|
||||
if (component.IsPowered)
|
||||
{
|
||||
state = EmitterVisualState.On;
|
||||
}
|
||||
else if (component.IsOn)
|
||||
{
|
||||
state = EmitterVisualState.Underpowered;
|
||||
}
|
||||
else
|
||||
{
|
||||
state = EmitterVisualState.Off;
|
||||
}
|
||||
|
||||
component.Appearance.SetData(EmitterVisuals.VisualState, state);
|
||||
component.Appearance.SetData(EmitterVisuals.Locked, component.IsLocked);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using Content.Server.Administration;
|
||||
using Content.Server.ParticleAccelerator.Components;
|
||||
using Content.Server.Singularity.Components;
|
||||
using Content.Server.Singularity.EntitySystems;
|
||||
using Content.Shared.Administration;
|
||||
using Content.Shared.Singularity.Components;
|
||||
using Robust.Shared.Console;
|
||||
@@ -27,7 +28,7 @@ namespace Content.Server.Singularity
|
||||
var entityManager = IoCManager.Resolve<IEntityManager>();
|
||||
foreach (var comp in entityManager.ComponentManager.EntityQuery<EmitterComponent>())
|
||||
{
|
||||
comp.SwitchOn();
|
||||
EntitySystem.Get<EmitterSystem>().SwitchOn(comp);
|
||||
}
|
||||
foreach (var comp in entityManager.ComponentManager.EntityQuery<RadiationCollectorComponent>())
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user