Fix tabletop error (#7830)

This commit is contained in:
Leon Friedrich
2022-04-28 19:57:51 +12:00
committed by GitHub
parent 0a51fbff1e
commit c2b4a4acef
9 changed files with 54 additions and 121 deletions

View File

@@ -1,16 +0,0 @@
using Content.Shared.Tabletop.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.Network;
using Robust.Shared.ViewVariables;
namespace Content.Client.Tabletop.Components
{
[RegisterComponent]
[ComponentReference(typeof(SharedTabletopDraggableComponent))]
public sealed class TabletopDraggableComponent : SharedTabletopDraggableComponent
{
// The player dragging the piece
[ViewVariables]
public NetUserId? DraggingPlayer;
}
}

View File

@@ -1,7 +1,7 @@
using Content.Client.Tabletop.Components;
using Content.Client.Tabletop.UI; using Content.Client.Tabletop.UI;
using Content.Client.Viewport; using Content.Client.Viewport;
using Content.Shared.Tabletop; using Content.Shared.Tabletop;
using Content.Shared.Tabletop.Components;
using Content.Shared.Tabletop.Events; using Content.Shared.Tabletop.Events;
using JetBrains.Annotations; using JetBrains.Annotations;
using Robust.Client.GameObjects; using Robust.Client.GameObjects;
@@ -10,14 +10,10 @@ using Robust.Client.Input;
using Robust.Client.Player; using Robust.Client.Player;
using Robust.Client.UserInterface; using Robust.Client.UserInterface;
using Robust.Client.UserInterface.CustomControls; using Robust.Client.UserInterface.CustomControls;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates; using Robust.Shared.GameStates;
using Robust.Shared.Input; using Robust.Shared.Input;
using Robust.Shared.Input.Binding; using Robust.Shared.Input.Binding;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Map; using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Timing; using Robust.Shared.Timing;
using static Robust.Shared.Input.Binding.PointerInputCmdHandler; using static Robust.Shared.Input.Binding.PointerInputCmdHandler;
using DrawDepth = Content.Shared.DrawDepth.DrawDepth; using DrawDepth = Content.Shared.DrawDepth.DrawDepth;
@@ -51,6 +47,13 @@ namespace Content.Client.Tabletop
SubscribeNetworkEvent<TabletopPlayEvent>(OnTabletopPlay); SubscribeNetworkEvent<TabletopPlayEvent>(OnTabletopPlay);
SubscribeLocalEvent<TabletopDraggableComponent, ComponentHandleState>(HandleComponentState); SubscribeLocalEvent<TabletopDraggableComponent, ComponentHandleState>(HandleComponentState);
SubscribeLocalEvent<TabletopDraggableComponent, ComponentRemove>(HandleDraggableRemoved);
}
private void HandleDraggableRemoved(EntityUid uid, TabletopDraggableComponent component, ComponentRemove args)
{
if (_draggedEntity == uid)
StopDragging(false);
} }
public override void Update(float frameTime) public override void Update(float frameTime)
@@ -59,13 +62,11 @@ namespace Content.Client.Tabletop
if (!_gameTiming.IsFirstTimePredicted) if (!_gameTiming.IsFirstTimePredicted)
return; return;
// If there is no player entity, return if (_window == null)
if (_playerManager.LocalPlayer is not {ControlledEntity: { } playerEntity}) return; return;
if (StunnedOrNoHands(playerEntity)) // If there is no player entity, return
{ if (_playerManager.LocalPlayer is not { ControlledEntity: { } playerEntity }) return;
StopDragging();
}
if (!CanSeeTable(playerEntity, _table)) if (!CanSeeTable(playerEntity, _table))
{ {
@@ -77,8 +78,11 @@ namespace Content.Client.Tabletop
// If no entity is being dragged or no viewport is clicked, return // If no entity is being dragged or no viewport is clicked, return
if (_draggedEntity == null || _viewport == null) return; if (_draggedEntity == null || _viewport == null) return;
// Make sure the dragged entity has a draggable component if (!CanDrag(playerEntity, _draggedEntity.Value, out var draggableComponent))
if (!EntityManager.TryGetComponent<TabletopDraggableComponent>(_draggedEntity.Value, out var draggableComponent)) return; {
StopDragging();
return;
}
// If the dragged entity has another dragging player, drop the item // If the dragged entity has another dragging player, drop the item
// This should happen if the local player is dragging an item, and another player grabs it out of their hand // This should happen if the local player is dragging an item, and another player grabs it out of their hand
@@ -179,21 +183,7 @@ namespace Content.Client.Tabletop
return false; return false;
// Return if can not see table or stunned/no hands // Return if can not see table or stunned/no hands
if (!CanSeeTable(playerEntity, _table) || StunnedOrNoHands(playerEntity)) if (!CanSeeTable(playerEntity, _table) || !CanDrag(playerEntity, args.EntityUid, out _))
{
return false;
}
var draggedEntity = args.EntityUid;
// Set the entity being dragged and the viewport under the mouse
if (!EntityManager.EntityExists(draggedEntity))
{
return false;
}
// Make sure that entity can be dragged
if (!EntityManager.HasComponent<TabletopDraggableComponent>(draggedEntity))
{ {
return false; return false;
} }
@@ -204,7 +194,7 @@ namespace Content.Client.Tabletop
return false; return false;
} }
StartDragging(draggedEntity, viewport); StartDragging(args.EntityUid, viewport);
return true; return true;
} }

View File

@@ -1,28 +0,0 @@
using Content.Shared.Tabletop.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.Network;
using Robust.Shared.Players;
using Robust.Shared.ViewVariables;
using static Content.Shared.Tabletop.SharedTabletopSystem;
namespace Content.Server.Tabletop.Components
{
[RegisterComponent]
[ComponentReference(typeof(SharedTabletopDraggableComponent))]
public sealed class TabletopDraggableComponent : SharedTabletopDraggableComponent
{
private NetUserId? _draggingPlayer;
// The player dragging the piece
[ViewVariables]
public NetUserId? DraggingPlayer
{
get => _draggingPlayer;
set
{
_draggingPlayer = value;
Dirty();
}
}
}
}

View File

@@ -1,11 +1,10 @@
using Content.Server.Tabletop.Components; using Content.Server.Tabletop.Components;
using Content.Shared.Tabletop; using Content.Shared.Tabletop;
using Content.Shared.Tabletop.Components;
using Content.Shared.Tabletop.Events; using Content.Shared.Tabletop.Events;
using Robust.Server.Player; using Robust.Server.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates; using Robust.Shared.GameStates;
using Robust.Shared.Map; using Robust.Shared.Map;
using Robust.Shared.Maths;
using DrawDepth = Content.Shared.DrawDepth.DrawDepth; using DrawDepth = Content.Shared.DrawDepth.DrawDepth;
namespace Content.Server.Tabletop namespace Content.Server.Tabletop
@@ -34,18 +33,7 @@ namespace Content.Server.Tabletop
if (!session.Players.ContainsKey(playerSession)) if (!session.Players.ContainsKey(playerSession))
return; return;
// Return if can not see table or stunned/no hands if (!CanSeeTable(playerEntity, msg.TableUid) || !CanDrag(playerEntity, msg.MovedEntityUid, out _))
if (!EntityManager.EntityExists(msg.TableUid))
return;
if (!CanSeeTable(playerEntity, msg.TableUid) || StunnedOrNoHands(playerEntity))
return;
// Check if moved entity exists and has tabletop draggable component
if (!EntityManager.EntityExists(msg.MovedEntityUid))
return;
if (!EntityManager.HasComponent<TabletopDraggableComponent>(msg.MovedEntityUid))
return; return;
// TODO: some permission system, disallow movement if you're not permitted to move the item // TODO: some permission system, disallow movement if you're not permitted to move the item
@@ -63,6 +51,7 @@ namespace Content.Server.Tabletop
if (!EntityManager.TryGetComponent<TabletopDraggableComponent?>(dragged, out var draggableComponent)) return; if (!EntityManager.TryGetComponent<TabletopDraggableComponent?>(dragged, out var draggableComponent)) return;
draggableComponent.DraggingPlayer = msg.IsDragging ? args.SenderSession.UserId : null; draggableComponent.DraggingPlayer = msg.IsDragging ? args.SenderSession.UserId : null;
Dirty(draggableComponent);
if (!EntityManager.TryGetComponent<AppearanceComponent?>(dragged, out var appearance)) return; if (!EntityManager.TryGetComponent<AppearanceComponent?>(dragged, out var appearance)) return;

View File

@@ -103,11 +103,8 @@ namespace Content.Server.Tabletop
var gamerUid = (gamer).Owner; var gamerUid = (gamer).Owner;
if (actor.PlayerSession.Status > SessionStatus.Connected || CanSeeTable(gamerUid, gamer.Tabletop) if (actor.PlayerSession.Status != SessionStatus.InGame || !CanSeeTable(gamerUid, gamer.Tabletop))
|| !StunnedOrNoHands(gamerUid)) CloseSessionFor(actor.PlayerSession, gamer.Tabletop);
continue;
CloseSessionFor(actor.PlayerSession, gamer.Tabletop);
} }
} }
} }

View File

@@ -1,13 +0,0 @@
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
namespace Content.Shared.Tabletop.Components
{
/// <summary>
/// Allows an entity to be dragged around by the mouse. The position is updated for all player while dragging.
/// </summary>
[NetworkedComponent]
public abstract class SharedTabletopDraggableComponent : Component
{
}
}

View File

@@ -0,0 +1,16 @@
using Robust.Shared.GameStates;
using Robust.Shared.Network;
namespace Content.Shared.Tabletop.Components;
/// <summary>
/// Allows an entity to be dragged around by the mouse. The position is updated for all player while dragging.
/// </summary>
[NetworkedComponent]
[RegisterComponent]
public sealed class TabletopDraggableComponent : Component
{
// The player dragging the piece
[ViewVariables]
public NetUserId? DraggingPlayer;
}

View File

@@ -8,7 +8,7 @@ namespace Content.Shared.Tabletop.Events
{ {
/// <summary> /// <summary>
/// An event that is sent to the server every so often by the client to tell where an entity with a /// An event that is sent to the server every so often by the client to tell where an entity with a
/// <see cref="SharedTabletopDraggableComponent"/> has been moved. /// <see cref="TabletopDraggableComponent"/> has been moved.
/// </summary> /// </summary>
[Serializable, NetSerializable] [Serializable, NetSerializable]
public sealed class TabletopMoveEvent : EntityEventArgs public sealed class TabletopMoveEvent : EntityEventArgs

View File

@@ -2,8 +2,10 @@ using Content.Shared.ActionBlocker;
using Content.Shared.Hands.Components; using Content.Shared.Hands.Components;
using Content.Shared.Interaction; using Content.Shared.Interaction;
using Content.Shared.Stunnable; using Content.Shared.Stunnable;
using Content.Shared.Tabletop.Components;
using Robust.Shared.Network; using Robust.Shared.Network;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
using System.Diagnostics.CodeAnalysis;
namespace Content.Shared.Tabletop namespace Content.Shared.Tabletop
{ {
@@ -32,15 +34,10 @@ namespace Content.Shared.Tabletop
/// <param name="table">The table entity to check.</param> /// <param name="table">The table entity to check.</param>
protected bool CanSeeTable(EntityUid playerEntity, EntityUid? table) protected bool CanSeeTable(EntityUid playerEntity, EntityUid? table)
{ {
if (table == null) // Table may have been deleted, hence TryComp
return false; if (!TryComp(table, out MetaDataComponent? meta)
|| meta.EntityLifeStage >= EntityLifeStage.Terminating
if (EntityManager.GetComponent<TransformComponent>(table.Value).Parent?.Owner is not { } parent) || (meta.Flags & MetaDataFlags.InContainer) == MetaDataFlags.InContainer)
{
return false;
}
if (!EntityManager.HasComponent<MapComponent>(parent) && !EntityManager.HasComponent<IMapGridComponent>(parent))
{ {
return false; return false;
} }
@@ -48,15 +45,16 @@ namespace Content.Shared.Tabletop
return _interactionSystem.InRangeUnobstructed(playerEntity, table.Value) && _actionBlockerSystem.CanInteract(playerEntity, table); return _interactionSystem.InRangeUnobstructed(playerEntity, table.Value) && _actionBlockerSystem.CanInteract(playerEntity, table);
} }
protected bool StunnedOrNoHands(EntityUid playerEntity) protected bool CanDrag(EntityUid playerEntity, EntityUid target, [NotNullWhen(true)] out TabletopDraggableComponent? draggable)
{ {
var stunned = EntityManager.HasComponent<StunnedComponent>(playerEntity); if (!TryComp(target, out draggable))
var hasHand = EntityManager.TryGetComponent<SharedHandsComponent>(playerEntity, out var handsComponent) && return false;
handsComponent.Hands.Count > 0;
return stunned || !hasHand; // CanSeeTable checks interaction action blockers. So no need to check them here.
// If this ever changes, so that ghosts can spectate games, then the check needs to be moved here.
return TryComp(playerEntity, out SharedHandsComponent? hands) && hands.Hands.Count > 0;
} }
#endregion #endregion
} }
} }