Makes Anchorable ECS. (#5222)

This commit is contained in:
Vera Aguilera Puerto
2021-11-09 08:08:30 +01:00
committed by GitHub
parent 31d622f941
commit fe6590bbe6
3 changed files with 171 additions and 139 deletions

View File

@@ -1,15 +1,7 @@
using System;
using System.Threading.Tasks;
using Content.Server.Coordinates.Helpers;
using Content.Server.Pulling;
using Content.Server.Tools;
using Content.Server.Tools.Components;
using Content.Shared.Interaction;
using Content.Shared.Tools;
using Content.Shared.Tools.Components;
using Content.Shared.Pulling.Components;
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects;
using Robust.Shared.Physics;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
using Robust.Shared.ViewVariables;
@@ -17,137 +9,23 @@ using Robust.Shared.ViewVariables;
namespace Content.Server.Construction.Components
{
// TODO: Move this component's logic to an EntitySystem.
[RegisterComponent]
public class AnchorableComponent : Component, IInteractUsing
[RegisterComponent, Friend(typeof(AnchorableSystem))]
public class AnchorableComponent : Component
{
public override string Name => "Anchorable";
[ViewVariables]
[DataField("tool", customTypeSerializer:typeof(PrototypeIdSerializer<ToolQualityPrototype>))]
public string Tool { get; private set; } = "Anchoring";
[ViewVariables]
int IInteractUsing.Priority => 1;
[ViewVariables(VVAccess.ReadWrite)]
[DataField("snap")]
[ViewVariables(VVAccess.ReadWrite)]
public bool Snap { get; private set; } = true;
/// <summary>
/// Checks if a tool can change the anchored status.
/// </summary>
/// <param name="user">The user doing the action</param>
/// <param name="utilizing">The tool being used</param>
/// <param name="anchoring">True if we're anchoring, and false if we're unanchoring.</param>
/// <returns>true if it is valid, false otherwise</returns>
private async Task<bool> Valid(IEntity user, IEntity utilizing, bool anchoring)
{
if (!Owner.HasComponent<IPhysBody>())
{
return false;
}
if (!utilizing.TryGetComponent(out ToolComponent? tool))
return false;
BaseAnchoredAttemptEvent attempt =
anchoring ? new AnchorAttemptEvent(user, utilizing) : new UnanchorAttemptEvent(user, utilizing);
// Need to cast the event or it will be raised as BaseAnchoredAttemptEvent.
if (anchoring)
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, (AnchorAttemptEvent) attempt, false);
else
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, (UnanchorAttemptEvent) attempt, false);
if (attempt.Cancelled)
return false;
return await EntitySystem.Get<ToolSystem>().UseTool(utilizing.Uid, user.Uid, Owner.Uid, 0f, 0.5f + attempt.Delay, Tool, toolComponent:tool);
}
/// <summary>
/// Tries to anchor the owner of this component.
/// </summary>
/// <param name="user">The entity doing the anchoring</param>
/// <param name="utilizing">The tool being used</param>
/// <returns>true if anchored, false otherwise</returns>
public async Task<bool> TryAnchor(IEntity user, IEntity utilizing)
{
if (!(await Valid(user, utilizing, true)))
{
return false;
}
// Snap rotation to cardinal (multiple of 90)
var rot = Owner.Transform.LocalRotation;
Owner.Transform.LocalRotation = Math.Round(rot / (Math.PI / 2)) * (Math.PI / 2);
if (Owner.TryGetComponent(out SharedPullableComponent? pullableComponent))
{
if (pullableComponent.Puller != null)
{
EntitySystem.Get<PullingSystem>().TryStopPull(pullableComponent);
}
}
if (Snap)
Owner.SnapToGrid(Owner.EntityManager);
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, new BeforeAnchoredEvent(user, utilizing), false);
Owner.Transform.Anchored = true;
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, new AnchoredEvent(user, utilizing), false);
return true;
}
/// <summary>
/// Tries to unanchor the owner of this component.
/// </summary>
/// <param name="user">The entity doing the unanchoring</param>
/// <param name="utilizing">The tool being used, if any</param>
/// <param name="force">Whether or not to ignore valid tool checks</param>
/// <returns>true if unanchored, false otherwise</returns>
public async Task<bool> TryUnAnchor(IEntity user, IEntity utilizing)
{
if (!(await Valid(user, utilizing, false)))
{
return false;
}
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, new BeforeUnanchoredEvent(user, utilizing), false);
Owner.Transform.Anchored = false;
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, new UnanchoredEvent(user, utilizing), false);
return true;
}
/// <summary>
/// Tries to toggle the anchored status of this component's owner.
/// </summary>
/// <param name="user">The entity doing the unanchoring</param>
/// <param name="utilizing">The tool being used</param>
/// <returns>true if toggled, false otherwise</returns>
public async Task<bool> TryToggleAnchor(IEntity user, IEntity utilizing)
{
return Owner.Transform.Anchored ?
await TryUnAnchor(user, utilizing) :
await TryAnchor(user, utilizing);
}
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
{
return await TryToggleAnchor(eventArgs.User, eventArgs.Using);
}
}
public abstract class BaseAnchoredAttemptEvent : CancellableEntityEventArgs
{
public IEntity User { get; }
public IEntity Tool { get; }
public EntityUid User { get; }
public EntityUid Tool { get; }
/// <summary>
/// Extra delay to add to the do_after.
@@ -156,7 +34,7 @@ namespace Content.Server.Construction.Components
/// </summary>
public float Delay { get; set; } = 0f;
protected BaseAnchoredAttemptEvent(IEntity user, IEntity tool)
protected BaseAnchoredAttemptEvent(EntityUid user, EntityUid tool)
{
User = user;
Tool = tool;
@@ -165,20 +43,20 @@ namespace Content.Server.Construction.Components
public class AnchorAttemptEvent : BaseAnchoredAttemptEvent
{
public AnchorAttemptEvent(IEntity user, IEntity tool) : base(user, tool) { }
public AnchorAttemptEvent(EntityUid user, EntityUid tool) : base(user, tool) { }
}
public class UnanchorAttemptEvent : BaseAnchoredAttemptEvent
{
public UnanchorAttemptEvent(IEntity user, IEntity tool) : base(user, tool) { }
public UnanchorAttemptEvent(EntityUid user, EntityUid tool) : base(user, tool) { }
}
public abstract class BaseAnchoredEvent : EntityEventArgs
{
public IEntity User { get; }
public IEntity Tool { get; }
public EntityUid User { get; }
public EntityUid Tool { get; }
protected BaseAnchoredEvent(IEntity user, IEntity tool)
protected BaseAnchoredEvent(EntityUid user, EntityUid tool)
{
User = user;
Tool = tool;
@@ -190,12 +68,12 @@ namespace Content.Server.Construction.Components
/// </summary>
public class BeforeAnchoredEvent : BaseAnchoredEvent
{
public BeforeAnchoredEvent(IEntity user, IEntity tool) : base(user, tool) { }
public BeforeAnchoredEvent(EntityUid user, EntityUid tool) : base(user, tool) { }
}
public class AnchoredEvent : BaseAnchoredEvent
{
public AnchoredEvent(IEntity user, IEntity tool) : base(user, tool) { }
public AnchoredEvent(EntityUid user, EntityUid tool) : base(user, tool) { }
}
/// <summary>
@@ -203,11 +81,11 @@ namespace Content.Server.Construction.Components
/// </summary>
public class BeforeUnanchoredEvent : BaseAnchoredEvent
{
public BeforeUnanchoredEvent(IEntity user, IEntity tool) : base(user, tool) { }
public BeforeUnanchoredEvent(EntityUid user, EntityUid tool) : base(user, tool) { }
}
public class UnanchoredEvent : BaseAnchoredEvent
{
public UnanchoredEvent(IEntity user, IEntity tool) : base(user, tool) { }
public UnanchoredEvent(EntityUid user, EntityUid tool) : base(user, tool) { }
}
}