The power of componentdependencies (#2211)

* stuff

* bucklemeup

* powerreceiver

* things

* Update RobustToolbox

* Fix nullability errors

Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com>
This commit is contained in:
Paul Ritter
2020-10-13 21:51:54 +02:00
committed by GitHub
parent 47ba7fc690
commit 844e4f6e50
6 changed files with 83 additions and 78 deletions

View File

@@ -1,8 +1,11 @@
using Content.Shared.GameObjects.Components.ActionBlocking; #nullable enable
using Content.Shared.GameObjects.Components.ActionBlocking;
using Content.Shared.Preferences.Appearance; using Content.Shared.Preferences.Appearance;
using Robust.Client.GameObjects; using Robust.Client.GameObjects;
using Robust.Client.Graphics; using Robust.Client.Graphics;
using Robust.Client.Interfaces.GameObjects.Components;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.ComponentDependencies;
using Robust.Shared.Utility; using Robust.Shared.Utility;
using Robust.Shared.ViewVariables; using Robust.Shared.ViewVariables;
@@ -12,9 +15,11 @@ namespace Content.Client.GameObjects.Components.ActionBlocking
public class CuffableComponent : SharedCuffableComponent public class CuffableComponent : SharedCuffableComponent
{ {
[ViewVariables] [ViewVariables]
private string _currentRSI = default; private string _currentRSI = default!;
public override void HandleComponentState(ComponentState curState, ComponentState nextState) [ViewVariables] [ComponentDependency] private readonly SpriteComponent? _spriteComponent = null;
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{ {
if (!(curState is CuffableComponentState cuffState)) if (!(curState is CuffableComponentState cuffState))
{ {
@@ -23,21 +28,21 @@ namespace Content.Client.GameObjects.Components.ActionBlocking
CanStillInteract = cuffState.CanStillInteract; CanStillInteract = cuffState.CanStillInteract;
if (Owner.TryGetComponent<SpriteComponent>(out var sprite)) if (_spriteComponent != null)
{ {
sprite.LayerSetVisible(HumanoidVisualLayers.Handcuffs, cuffState.NumHandsCuffed > 0); _spriteComponent.LayerSetVisible(HumanoidVisualLayers.Handcuffs, cuffState.NumHandsCuffed > 0);
sprite.LayerSetColor(HumanoidVisualLayers.Handcuffs, cuffState.Color); _spriteComponent.LayerSetColor(HumanoidVisualLayers.Handcuffs, cuffState.Color);
if (cuffState.NumHandsCuffed > 0) if (cuffState.NumHandsCuffed > 0)
{ {
if (_currentRSI != cuffState.RSI) // we don't want to keep loading the same RSI if (_currentRSI != cuffState.RSI) // we don't want to keep loading the same RSI
{ {
_currentRSI = cuffState.RSI; _currentRSI = cuffState.RSI;
sprite.LayerSetState(HumanoidVisualLayers.Handcuffs, new RSI.StateId(cuffState.IconState), new ResourcePath(cuffState.RSI)); _spriteComponent.LayerSetState(HumanoidVisualLayers.Handcuffs, new RSI.StateId(cuffState.IconState), new ResourcePath(cuffState.RSI));
} }
else else
{ {
sprite.LayerSetState(HumanoidVisualLayers.Handcuffs, new RSI.StateId(cuffState.IconState)); // TODO: safety check to see if RSI contains the state? _spriteComponent.LayerSetState(HumanoidVisualLayers.Handcuffs, new RSI.StateId(cuffState.IconState)); // TODO: safety check to see if RSI contains the state?
} }
} }
} }
@@ -47,10 +52,7 @@ namespace Content.Client.GameObjects.Components.ActionBlocking
{ {
base.OnRemove(); base.OnRemove();
if (Owner.TryGetComponent<SpriteComponent>(out var sprite)) _spriteComponent?.LayerSetVisible(HumanoidVisualLayers.Handcuffs, false);
{
sprite.LayerSetVisible(HumanoidVisualLayers.Handcuffs, false);
}
} }
} }
} }

View File

@@ -1,6 +1,9 @@
using Content.Server.Atmos; #nullable enable
using Content.Server.Atmos;
using Content.Server.GameObjects.Components.Temperature; using Content.Server.GameObjects.Components.Temperature;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.ComponentDependencies;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Atmos namespace Content.Server.GameObjects.Components.Atmos
{ {
@@ -13,29 +16,31 @@ namespace Content.Server.GameObjects.Components.Atmos
{ {
public override string Name => "AtmosExposed"; public override string Name => "AtmosExposed";
[ViewVariables]
[ComponentDependency] private readonly TemperatureComponent? _temperatureComponent = null;
[ViewVariables]
[ComponentDependency] private readonly BarotraumaComponent? _barotraumaComponent = null;
[ViewVariables]
[ComponentDependency] private readonly FlammableComponent? _flammableComponent = null;
public void Update(TileAtmosphere tile, float frameDelta) public void Update(TileAtmosphere tile, float frameDelta)
{ {
if (Owner.TryGetComponent<TemperatureComponent>(out var temperatureComponent)) if (_temperatureComponent != null)
{ {
if (tile.Air != null) if (tile.Air != null)
{ {
var temperatureDelta = tile.Air.Temperature - temperatureComponent.CurrentTemperature; var temperatureDelta = tile.Air.Temperature - _temperatureComponent.CurrentTemperature;
var heat = temperatureDelta * (tile.Air.HeatCapacity * temperatureComponent.HeatCapacity / (tile.Air.HeatCapacity + temperatureComponent.HeatCapacity)); var heat = temperatureDelta * (tile.Air.HeatCapacity * _temperatureComponent.HeatCapacity / (tile.Air.HeatCapacity + _temperatureComponent.HeatCapacity));
temperatureComponent.ReceiveHeat(heat); _temperatureComponent.ReceiveHeat(heat);
} }
temperatureComponent.Update(); _temperatureComponent.Update();
} }
if (Owner.TryGetComponent<BarotraumaComponent>(out var barotraumaComponent)) _barotraumaComponent?.Update(tile.Air?.Pressure ?? 0);
{
barotraumaComponent.Update(tile.Air?.Pressure ?? 0);
}
if (Owner.TryGetComponent<FlammableComponent>(out var flammableComponent)) _flammableComponent?.Update(tile);
{
flammableComponent.Update(tile);
} }
} }
}
} }

View File

@@ -19,6 +19,7 @@ using Robust.Server.GameObjects.EntitySystemMessages;
using Robust.Server.GameObjects.EntitySystems; using Robust.Server.GameObjects.EntitySystems;
using Robust.Shared.Containers; using Robust.Shared.Containers;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.ComponentDependencies;
using Robust.Shared.GameObjects.Components.Transform; using Robust.Shared.GameObjects.Components.Transform;
using Robust.Shared.GameObjects.Systems; using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
@@ -39,6 +40,11 @@ namespace Content.Server.GameObjects.Components.Buckle
[Dependency] private readonly IEntitySystemManager _entitySystem = default!; [Dependency] private readonly IEntitySystemManager _entitySystem = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!; [Dependency] private readonly IGameTiming _gameTiming = default!;
[ComponentDependency] public readonly AppearanceComponent? AppearanceComponent = null;
[ComponentDependency] private readonly ServerStatusEffectsComponent? _serverStatusEffectsComponent = null;
[ComponentDependency] private readonly StunnableComponent? _stunnableComponent = null;
[ComponentDependency] private readonly MobStateManagerComponent? _mobStateManagerComponent = null;
private int _size; private int _size;
/// <summary> /// <summary>
@@ -109,15 +115,15 @@ namespace Content.Server.GameObjects.Components.Buckle
/// </summary> /// </summary>
private void BuckleStatus() private void BuckleStatus()
{ {
if (Owner.TryGetComponent(out ServerStatusEffectsComponent? status)) if (_serverStatusEffectsComponent != null)
{ {
if (Buckled) if (Buckled)
{ {
status.ChangeStatusEffectIcon(StatusEffect.Buckled, BuckledTo!.BuckledIcon); _serverStatusEffectsComponent.ChangeStatusEffectIcon(StatusEffect.Buckled, BuckledTo!.BuckledIcon);
} }
else else
{ {
status.RemoveStatusEffect(StatusEffect.Buckled); _serverStatusEffectsComponent.RemoveStatusEffect(StatusEffect.Buckled);
} }
} }
} }
@@ -280,10 +286,7 @@ namespace Content.Server.GameObjects.Components.Buckle
return false; return false;
} }
if (Owner.TryGetComponent(out AppearanceComponent? appearance)) AppearanceComponent?.SetData(BuckleVisuals.Buckled, true);
{
appearance.SetData(BuckleVisuals.Buckled, true);
}
BuckledTo = strap; BuckledTo = strap;
@@ -345,12 +348,9 @@ namespace Content.Server.GameObjects.Components.Buckle
Owner.Transform.WorldRotation = oldBuckledTo.Owner.Transform.WorldRotation; Owner.Transform.WorldRotation = oldBuckledTo.Owner.Transform.WorldRotation;
} }
if (Owner.TryGetComponent(out AppearanceComponent? appearance)) AppearanceComponent?.SetData(BuckleVisuals.Buckled, false);
{
appearance.SetData(BuckleVisuals.Buckled, false);
}
if (Owner.TryGetComponent(out StunnableComponent? stunnable) && stunnable.KnockedDown) if (_stunnableComponent != null && _stunnableComponent.KnockedDown)
{ {
EntitySystem.Get<StandingStateSystem>().Down(Owner); EntitySystem.Get<StandingStateSystem>().Down(Owner);
} }
@@ -359,19 +359,16 @@ namespace Content.Server.GameObjects.Components.Buckle
EntitySystem.Get<StandingStateSystem>().Standing(Owner); EntitySystem.Get<StandingStateSystem>().Standing(Owner);
} }
if (Owner.TryGetComponent(out MobStateManagerComponent? stateManager)) if (_mobStateManagerComponent != null)
{ {
stateManager.CurrentMobState.EnterState(Owner); _mobStateManagerComponent.CurrentMobState.EnterState(Owner);
} }
BuckleStatus(); BuckleStatus();
if (oldBuckledTo.Owner.TryGetComponent(out StrapComponent? strap)) oldBuckledTo.Remove(this);
{
strap.Remove(this);
_entitySystem.GetEntitySystem<AudioSystem>() _entitySystem.GetEntitySystem<AudioSystem>()
.PlayFromEntity(strap.UnbuckleSound, Owner); .PlayFromEntity(oldBuckledTo.UnbuckleSound, Owner);
}
SendMessage(new UnbuckleMessage(Owner, oldBuckledTo.Owner)); SendMessage(new UnbuckleMessage(Owner, oldBuckledTo.Owner));
@@ -520,11 +517,7 @@ namespace Content.Server.GameObjects.Components.Buckle
_entityManager.EventBus.UnsubscribeEvents(this); _entityManager.EventBus.UnsubscribeEvents(this);
if (BuckledTo != null && BuckledTo?.Remove(this);
BuckledTo.Owner.TryGetComponent(out StrapComponent? strap))
{
strap.Remove(this);
}
TryUnbuckle(Owner, true); TryUnbuckle(Owner, true);
@@ -538,9 +531,9 @@ namespace Content.Server.GameObjects.Components.Buckle
if (BuckledTo != null && if (BuckledTo != null &&
Owner.Transform.WorldRotation.GetCardinalDir() == Direction.North && Owner.Transform.WorldRotation.GetCardinalDir() == Direction.North &&
BuckledTo.Owner.TryGetComponent(out SpriteComponent? strapSprite)) BuckledTo.SpriteComponent != null)
{ {
drawDepth = strapSprite.DrawDepth - 1; drawDepth = BuckledTo.SpriteComponent.DrawDepth - 1;
} }
return new BuckleComponentState(Buckled, drawDepth); return new BuckleComponentState(Buckled, drawDepth);

View File

@@ -1,10 +1,12 @@
using System; #nullable enable
using System;
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups; using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
using Content.Shared.GameObjects.Components.Power; using Content.Shared.GameObjects.Components.Power;
using Content.Shared.GameObjects.EntitySystems; using Content.Shared.GameObjects.EntitySystems;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using Robust.Server.Interfaces.GameObjects; using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.ComponentDependencies;
using Robust.Shared.GameObjects.Components; using Robust.Shared.GameObjects.Components;
using Robust.Shared.Interfaces.Map; using Robust.Shared.Interfaces.Map;
using Robust.Shared.IoC; using Robust.Shared.IoC;
@@ -23,9 +25,11 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
{ {
[Dependency] private readonly IServerEntityManager _serverEntityManager = default!; [Dependency] private readonly IServerEntityManager _serverEntityManager = default!;
[ViewVariables] [ComponentDependency] private readonly IPhysicsComponent? _collidableComponent = null;
public override string Name => "PowerReceiver"; public override string Name => "PowerReceiver";
public event EventHandler<PowerStateEventArgs> OnPowerStateChanged; public event EventHandler<PowerStateEventArgs>? OnPowerStateChanged;
[ViewVariables] [ViewVariables]
public bool Powered => (HasApcPower || !NeedsPower) && !PowerDisabled; public bool Powered => (HasApcPower || !NeedsPower) && !PowerDisabled;
@@ -50,7 +54,7 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
/// </summary> /// </summary>
public bool Connectable => Anchored; public bool Connectable => Anchored;
private bool Anchored => !Owner.TryGetComponent<IPhysicsComponent>(out var physics) || physics.Anchored; private bool Anchored => _collidableComponent == null || _collidableComponent.Anchored;
[ViewVariables] [ViewVariables]
public bool NeedsProvider { get; private set; } = true; public bool NeedsProvider { get; private set; } = true;
@@ -92,18 +96,18 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
{ {
TryFindAndSetProvider(); TryFindAndSetProvider();
} }
if (Owner.TryGetComponent<IPhysicsComponent>(out var physics)) if (_collidableComponent != null)
{ {
AnchorUpdate(); AnchorUpdate();
physics.AnchoredChanged += AnchorUpdate; _collidableComponent.AnchoredChanged += AnchorUpdate;
} }
} }
public override void OnRemove() public override void OnRemove()
{ {
if (Owner.TryGetComponent<IPhysicsComponent>(out var physics)) if (_collidableComponent != null)
{ {
physics.AnchoredChanged -= AnchorUpdate; _collidableComponent.AnchoredChanged -= AnchorUpdate;
} }
_provider.RemoveReceiver(this); _provider.RemoveReceiver(this);
base.OnRemove(); base.OnRemove();
@@ -139,7 +143,7 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
} }
} }
} }
foundProvider = default; foundProvider = default!;
return false; return false;
} }
@@ -204,7 +208,7 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
private void OnNewPowerState() private void OnNewPowerState()
{ {
OnPowerStateChanged?.Invoke(this, new PowerStateEventArgs(Powered)); OnPowerStateChanged?.Invoke(this, new PowerStateEventArgs(Powered));
if (Owner.TryGetComponent(out AppearanceComponent appearance)) if (Owner.TryGetComponent<AppearanceComponent>(out var appearance))
{ {
appearance.SetData(PowerDeviceVisuals.Powered, Powered); appearance.SetData(PowerDeviceVisuals.Powered, Powered);
} }

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic; #nullable enable
using System.Collections.Generic;
using Content.Server.GameObjects.Components.Buckle; using Content.Server.GameObjects.Components.Buckle;
using Content.Shared.GameObjects.Components.Strap; using Content.Shared.GameObjects.Components.Strap;
using Content.Shared.GameObjects.EntitySystems; using Content.Shared.GameObjects.EntitySystems;
@@ -7,6 +8,7 @@ using Content.Shared.Interfaces.GameObjects.Components;
using Content.Shared.Utility; using Content.Shared.Utility;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.ComponentDependencies;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Localization; using Robust.Shared.Localization;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
@@ -17,11 +19,13 @@ namespace Content.Server.GameObjects.Components.Strap
[RegisterComponent] [RegisterComponent]
public class StrapComponent : SharedStrapComponent, IInteractHand public class StrapComponent : SharedStrapComponent, IInteractHand
{ {
private HashSet<IEntity> _buckledEntities; [ComponentDependency] public readonly SpriteComponent? SpriteComponent = null;
private HashSet<IEntity> _buckledEntities = null!;
private StrapPosition _position; private StrapPosition _position;
private string _buckleSound; private string _buckleSound = null!;
private string _unbuckleSound; private string _unbuckleSound = null!;
private string _buckledIcon; private string _buckledIcon = null!;
/// <summary> /// <summary>
/// The angle in degrees to rotate the player by when they get strapped /// The angle in degrees to rotate the player by when they get strapped
@@ -103,10 +107,7 @@ namespace Content.Server.GameObjects.Components.Strap
_occupiedSize += buckle.Size; _occupiedSize += buckle.Size;
if (buckle.Owner.TryGetComponent(out AppearanceComponent appearance)) buckle.AppearanceComponent?.SetData(StrapVisuals.RotationAngle, _rotation);
{
appearance.SetData(StrapVisuals.RotationAngle, _rotation);
}
SendMessage(new StrapMessage(buckle.Owner, Owner)); SendMessage(new StrapMessage(buckle.Owner, Owner));
@@ -151,7 +152,7 @@ namespace Content.Server.GameObjects.Components.Strap
foreach (var entity in _buckledEntities) foreach (var entity in _buckledEntities)
{ {
if (entity.TryGetComponent(out BuckleComponent buckle)) if (entity.TryGetComponent<BuckleComponent>(out var buckle))
{ {
buckle.TryUnbuckle(entity, true); buckle.TryUnbuckle(entity, true);
} }
@@ -168,7 +169,7 @@ namespace Content.Server.GameObjects.Components.Strap
bool IInteractHand.InteractHand(InteractHandEventArgs eventArgs) bool IInteractHand.InteractHand(InteractHandEventArgs eventArgs)
{ {
if (!eventArgs.User.TryGetComponent(out BuckleComponent buckle)) if (!eventArgs.User.TryGetComponent<BuckleComponent>(out var buckle))
{ {
return false; return false;
} }
@@ -184,7 +185,7 @@ namespace Content.Server.GameObjects.Components.Strap
data.Visibility = VerbVisibility.Invisible; data.Visibility = VerbVisibility.Invisible;
if (!ActionBlockerSystem.CanInteract(component.Owner) || if (!ActionBlockerSystem.CanInteract(component.Owner) ||
!user.TryGetComponent(out BuckleComponent buckle) || !user.TryGetComponent<BuckleComponent>(out var buckle) ||
buckle.BuckledTo != null && buckle.BuckledTo != component || buckle.BuckledTo != null && buckle.BuckledTo != component ||
user == component.Owner) user == component.Owner)
{ {
@@ -215,7 +216,7 @@ namespace Content.Server.GameObjects.Components.Strap
protected override void Activate(IEntity user, StrapComponent component) protected override void Activate(IEntity user, StrapComponent component)
{ {
if (!user.TryGetComponent(out BuckleComponent buckle)) if (!user.TryGetComponent<BuckleComponent>(out var buckle))
{ {
return; return;
} }