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 Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.Interfaces.GameObjects.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.ComponentDependencies;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
@@ -12,9 +15,11 @@ namespace Content.Client.GameObjects.Components.ActionBlocking
public class CuffableComponent : SharedCuffableComponent
{
[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))
{
@@ -23,21 +28,21 @@ namespace Content.Client.GameObjects.Components.ActionBlocking
CanStillInteract = cuffState.CanStillInteract;
if (Owner.TryGetComponent<SpriteComponent>(out var sprite))
if (_spriteComponent != null)
{
sprite.LayerSetVisible(HumanoidVisualLayers.Handcuffs, cuffState.NumHandsCuffed > 0);
sprite.LayerSetColor(HumanoidVisualLayers.Handcuffs, cuffState.Color);
_spriteComponent.LayerSetVisible(HumanoidVisualLayers.Handcuffs, cuffState.NumHandsCuffed > 0);
_spriteComponent.LayerSetColor(HumanoidVisualLayers.Handcuffs, cuffState.Color);
if (cuffState.NumHandsCuffed > 0)
{
if (_currentRSI != cuffState.RSI) // we don't want to keep loading the same 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
{
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();
if (Owner.TryGetComponent<SpriteComponent>(out var sprite))
{
sprite.LayerSetVisible(HumanoidVisualLayers.Handcuffs, false);
}
_spriteComponent?.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 Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.ComponentDependencies;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Atmos
{
@@ -13,29 +16,31 @@ namespace Content.Server.GameObjects.Components.Atmos
{
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)
{
if (Owner.TryGetComponent<TemperatureComponent>(out var temperatureComponent))
if (_temperatureComponent != null)
{
if (tile.Air != null)
{
var temperatureDelta = tile.Air.Temperature - temperatureComponent.CurrentTemperature;
var heat = temperatureDelta * (tile.Air.HeatCapacity * temperatureComponent.HeatCapacity / (tile.Air.HeatCapacity + temperatureComponent.HeatCapacity));
temperatureComponent.ReceiveHeat(heat);
var temperatureDelta = tile.Air.Temperature - _temperatureComponent.CurrentTemperature;
var heat = temperatureDelta * (tile.Air.HeatCapacity * _temperatureComponent.HeatCapacity / (tile.Air.HeatCapacity + _temperatureComponent.HeatCapacity));
_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.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.ComponentDependencies;
using Robust.Shared.GameObjects.Components.Transform;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
@@ -39,6 +40,11 @@ namespace Content.Server.GameObjects.Components.Buckle
[Dependency] private readonly IEntitySystemManager _entitySystem = 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;
/// <summary>
@@ -109,15 +115,15 @@ namespace Content.Server.GameObjects.Components.Buckle
/// </summary>
private void BuckleStatus()
{
if (Owner.TryGetComponent(out ServerStatusEffectsComponent? status))
if (_serverStatusEffectsComponent != null)
{
if (Buckled)
{
status.ChangeStatusEffectIcon(StatusEffect.Buckled, BuckledTo!.BuckledIcon);
_serverStatusEffectsComponent.ChangeStatusEffectIcon(StatusEffect.Buckled, BuckledTo!.BuckledIcon);
}
else
{
status.RemoveStatusEffect(StatusEffect.Buckled);
_serverStatusEffectsComponent.RemoveStatusEffect(StatusEffect.Buckled);
}
}
}
@@ -280,10 +286,7 @@ namespace Content.Server.GameObjects.Components.Buckle
return false;
}
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
{
appearance.SetData(BuckleVisuals.Buckled, true);
}
AppearanceComponent?.SetData(BuckleVisuals.Buckled, true);
BuckledTo = strap;
@@ -345,12 +348,9 @@ namespace Content.Server.GameObjects.Components.Buckle
Owner.Transform.WorldRotation = oldBuckledTo.Owner.Transform.WorldRotation;
}
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
{
appearance.SetData(BuckleVisuals.Buckled, false);
}
AppearanceComponent?.SetData(BuckleVisuals.Buckled, false);
if (Owner.TryGetComponent(out StunnableComponent? stunnable) && stunnable.KnockedDown)
if (_stunnableComponent != null && _stunnableComponent.KnockedDown)
{
EntitySystem.Get<StandingStateSystem>().Down(Owner);
}
@@ -359,19 +359,16 @@ namespace Content.Server.GameObjects.Components.Buckle
EntitySystem.Get<StandingStateSystem>().Standing(Owner);
}
if (Owner.TryGetComponent(out MobStateManagerComponent? stateManager))
if (_mobStateManagerComponent != null)
{
stateManager.CurrentMobState.EnterState(Owner);
_mobStateManagerComponent.CurrentMobState.EnterState(Owner);
}
BuckleStatus();
if (oldBuckledTo.Owner.TryGetComponent(out StrapComponent? strap))
{
strap.Remove(this);
_entitySystem.GetEntitySystem<AudioSystem>()
.PlayFromEntity(strap.UnbuckleSound, Owner);
}
oldBuckledTo.Remove(this);
_entitySystem.GetEntitySystem<AudioSystem>()
.PlayFromEntity(oldBuckledTo.UnbuckleSound, Owner);
SendMessage(new UnbuckleMessage(Owner, oldBuckledTo.Owner));
@@ -520,11 +517,7 @@ namespace Content.Server.GameObjects.Components.Buckle
_entityManager.EventBus.UnsubscribeEvents(this);
if (BuckledTo != null &&
BuckledTo.Owner.TryGetComponent(out StrapComponent? strap))
{
strap.Remove(this);
}
BuckledTo?.Remove(this);
TryUnbuckle(Owner, true);
@@ -538,9 +531,9 @@ namespace Content.Server.GameObjects.Components.Buckle
if (BuckledTo != null &&
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);

View File

@@ -1,10 +1,12 @@
using System;
#nullable enable
using System;
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
using Content.Shared.GameObjects.Components.Power;
using Content.Shared.GameObjects.EntitySystems;
using Robust.Server.GameObjects;
using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.ComponentDependencies;
using Robust.Shared.GameObjects.Components;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.IoC;
@@ -23,9 +25,11 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
{
[Dependency] private readonly IServerEntityManager _serverEntityManager = default!;
[ViewVariables] [ComponentDependency] private readonly IPhysicsComponent? _collidableComponent = null;
public override string Name => "PowerReceiver";
public event EventHandler<PowerStateEventArgs> OnPowerStateChanged;
public event EventHandler<PowerStateEventArgs>? OnPowerStateChanged;
[ViewVariables]
public bool Powered => (HasApcPower || !NeedsPower) && !PowerDisabled;
@@ -50,7 +54,7 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
/// </summary>
public bool Connectable => Anchored;
private bool Anchored => !Owner.TryGetComponent<IPhysicsComponent>(out var physics) || physics.Anchored;
private bool Anchored => _collidableComponent == null || _collidableComponent.Anchored;
[ViewVariables]
public bool NeedsProvider { get; private set; } = true;
@@ -92,18 +96,18 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
{
TryFindAndSetProvider();
}
if (Owner.TryGetComponent<IPhysicsComponent>(out var physics))
if (_collidableComponent != null)
{
AnchorUpdate();
physics.AnchoredChanged += AnchorUpdate;
_collidableComponent.AnchoredChanged += AnchorUpdate;
}
}
public override void OnRemove()
{
if (Owner.TryGetComponent<IPhysicsComponent>(out var physics))
if (_collidableComponent != null)
{
physics.AnchoredChanged -= AnchorUpdate;
_collidableComponent.AnchoredChanged -= AnchorUpdate;
}
_provider.RemoveReceiver(this);
base.OnRemove();
@@ -139,7 +143,7 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
}
}
}
foundProvider = default;
foundProvider = default!;
return false;
}
@@ -204,7 +208,7 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
private void OnNewPowerState()
{
OnPowerStateChanged?.Invoke(this, new PowerStateEventArgs(Powered));
if (Owner.TryGetComponent(out AppearanceComponent appearance))
if (Owner.TryGetComponent<AppearanceComponent>(out var appearance))
{
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.Shared.GameObjects.Components.Strap;
using Content.Shared.GameObjects.EntitySystems;
@@ -7,6 +8,7 @@ using Content.Shared.Interfaces.GameObjects.Components;
using Content.Shared.Utility;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.ComponentDependencies;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Serialization;
@@ -17,11 +19,13 @@ namespace Content.Server.GameObjects.Components.Strap
[RegisterComponent]
public class StrapComponent : SharedStrapComponent, IInteractHand
{
private HashSet<IEntity> _buckledEntities;
[ComponentDependency] public readonly SpriteComponent? SpriteComponent = null;
private HashSet<IEntity> _buckledEntities = null!;
private StrapPosition _position;
private string _buckleSound;
private string _unbuckleSound;
private string _buckledIcon;
private string _buckleSound = null!;
private string _unbuckleSound = null!;
private string _buckledIcon = null!;
/// <summary>
/// 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;
if (buckle.Owner.TryGetComponent(out AppearanceComponent appearance))
{
appearance.SetData(StrapVisuals.RotationAngle, _rotation);
}
buckle.AppearanceComponent?.SetData(StrapVisuals.RotationAngle, _rotation);
SendMessage(new StrapMessage(buckle.Owner, Owner));
@@ -151,7 +152,7 @@ namespace Content.Server.GameObjects.Components.Strap
foreach (var entity in _buckledEntities)
{
if (entity.TryGetComponent(out BuckleComponent buckle))
if (entity.TryGetComponent<BuckleComponent>(out var buckle))
{
buckle.TryUnbuckle(entity, true);
}
@@ -168,7 +169,7 @@ namespace Content.Server.GameObjects.Components.Strap
bool IInteractHand.InteractHand(InteractHandEventArgs eventArgs)
{
if (!eventArgs.User.TryGetComponent(out BuckleComponent buckle))
if (!eventArgs.User.TryGetComponent<BuckleComponent>(out var buckle))
{
return false;
}
@@ -184,7 +185,7 @@ namespace Content.Server.GameObjects.Components.Strap
data.Visibility = VerbVisibility.Invisible;
if (!ActionBlockerSystem.CanInteract(component.Owner) ||
!user.TryGetComponent(out BuckleComponent buckle) ||
!user.TryGetComponent<BuckleComponent>(out var buckle) ||
buckle.BuckledTo != null && buckle.BuckledTo != component ||
user == component.Owner)
{
@@ -215,7 +216,7 @@ namespace Content.Server.GameObjects.Components.Strap
protected override void Activate(IEntity user, StrapComponent component)
{
if (!user.TryGetComponent(out BuckleComponent buckle))
if (!user.TryGetComponent<BuckleComponent>(out var buckle))
{
return;
}