Add Enabled property to SubFloorHideComponent, make it networked. (#4404)

This commit is contained in:
Vera Aguilera Puerto
2021-08-02 13:53:33 +02:00
committed by GitHub
parent 331604cc66
commit 7e3d5f6cf1
2 changed files with 71 additions and 10 deletions

View File

@@ -1,4 +1,8 @@
using System;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Players;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
@@ -10,6 +14,7 @@ namespace Content.Shared.SubFloor
/// (plating).
/// </summary>
/// <seealso cref="P:Content.Shared.Maps.ContentTileDefinition.IsSubFloor" />
[NetworkedComponent]
[RegisterComponent]
public sealed class SubFloorHideComponent : Component
{
@@ -17,10 +22,35 @@ namespace Content.Shared.SubFloor
public override string Name => "SubFloorHide";
/// <summary>
/// This entity needs to be anchored to be hid in the subfloor.
/// Whether the entity will be hid when not in subfloor.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("enabled")]
public bool Enabled { get; set; } = true;
/// <summary>
/// This entity needs to be anchored to be hid when not in subfloor.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("requireAnchored")]
public bool RequireAnchored { get; set; } = true;
public override ComponentState GetComponentState(ICommonSession player)
{
return new SubFloorHideComponentState(Enabled, RequireAnchored);
}
}
[Serializable, NetSerializable]
public class SubFloorHideComponentState : ComponentState
{
public bool Enabled { get; }
public bool RequireAnchored { get; }
public SubFloorHideComponentState(bool enabled, bool requireAnchored)
{
Enabled = enabled;
RequireAnchored = requireAnchored;
}
}
}

View File

@@ -2,6 +2,7 @@ using System;
using Content.Shared.Maps;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
@@ -44,6 +45,7 @@ namespace Content.Shared.SubFloor
SubscribeLocalEvent<SubFloorHideComponent, ComponentStartup>(OnSubFloorStarted);
SubscribeLocalEvent<SubFloorHideComponent, ComponentShutdown>(OnSubFloorTerminating);
SubscribeLocalEvent<SubFloorHideComponent, AnchorStateChangedEvent>(HandleAnchorChanged);
SubscribeLocalEvent<SubFloorHideComponent, ComponentHandleState>(HandleComponentState);
}
public override void Shutdown()
@@ -54,6 +56,20 @@ namespace Content.Shared.SubFloor
_mapManager.TileChanged -= MapManagerOnTileChanged;
}
public void SetEnabled(SubFloorHideComponent subFloor, bool enabled)
{
subFloor.Enabled = enabled;
subFloor.Dirty();
UpdateEntity(subFloor.Owner.Uid);
}
public void SetRequireAnchoring(SubFloorHideComponent subFloor, bool requireAnchored)
{
subFloor.RequireAnchored = requireAnchored;
subFloor.Dirty();
UpdateEntity(subFloor.Owner.Uid);
}
private void OnSubFloorStarted(EntityUid uid, SubFloorHideComponent component, ComponentStartup _)
{
UpdateEntity(uid);
@@ -73,6 +89,16 @@ namespace Content.Shared.SubFloor
UpdateEntity(uid);
}
private void HandleComponentState(EntityUid uid, SubFloorHideComponent component, ComponentHandleState args)
{
if (args.Current is not SubFloorHideComponentState state)
return;
component.Enabled = state.Enabled;
component.RequireAnchored = state.RequireAnchored;
UpdateEntity(uid);
}
private void MapManagerOnTileChanged(object? sender, TileChangedEventArgs e)
{
UpdateTile(_mapManager.GetGrid(e.NewTile.GridIndex), e.NewTile.GridIndices);
@@ -136,16 +162,21 @@ namespace Content.Shared.SubFloor
if (subFloorHideEvent.Handled)
return;
// This might look weird, but basically we only need to query the SubFloorHide and Transform components
// if we are gonna hide the entity and we require it to be anchored to be hidden. Because getting components
// is "expensive", we have a slow path where we query them, and a fast path where we don't.
if (!subFloor
&& ComponentManager.TryGetComponent(uid, out SubFloorHideComponent? subFloorHideComponent) &&
subFloorHideComponent.RequireAnchored
&& ComponentManager.TryGetComponent(uid, out ITransformComponent? transformComponent))
// We only need to query the subfloor component to check if it's enabled or not when we're not on subfloor.
// Getting components is expensive, after all.
if (!subFloor && ComponentManager.TryGetComponent(uid, out SubFloorHideComponent? subFloorHideComponent))
{
// If we require the entity to be anchored but it's not, this will set subfloor to true, unhiding it.
subFloor = !transformComponent.Anchored;
// If the component isn't enabled, then subfloor will always be true, and the entity will be shown.
if (!subFloorHideComponent.Enabled)
{
subFloor = true;
}
// We only need to query the TransformComp if the SubfloorHide is enabled and requires anchoring.
else if (subFloorHideComponent.RequireAnchored && ComponentManager.TryGetComponent(uid, out ITransformComponent? transformComponent))
{
// If we require the entity to be anchored but it's not, this will set subfloor to true, unhiding it.
subFloor = !transformComponent.Anchored;
}
}
// Whether to show this entity as visible, visually.