AnchorableComponent light cleanup. (#4009)
* Remove awful commands * Unanchoring is a word. * Fix disposal tests.... * Slight anchorable cleanup
This commit is contained in:
committed by
GitHub
parent
efbe129d61
commit
dc03f1f545
@@ -1,6 +1,5 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.GameObjects.Components.Interactable;
|
||||
using Content.Server.GameObjects.Components.Pulling;
|
||||
@@ -14,6 +13,7 @@ using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.GameObjects.Components
|
||||
{
|
||||
// TODO: Move this component's logic to an EntitySystem.
|
||||
[RegisterComponent]
|
||||
public class AnchorableComponent : Component, IInteractUsing
|
||||
{
|
||||
@@ -32,49 +32,40 @@ namespace Content.Server.GameObjects.Components
|
||||
[DataField("snap")]
|
||||
public bool Snap { get; private set; } = true;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
Owner.EnsureComponent<PhysicsComponent>(out _physicsComponent);
|
||||
}
|
||||
|
||||
/// <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, can be null if forcing it</param>
|
||||
/// <param name="force">Whether or not to check if the tool is valid</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, [NotNullWhen(true)] bool force = false)
|
||||
private async Task<bool> Valid(IEntity user, IEntity utilizing, bool anchoring)
|
||||
{
|
||||
if (!Owner.HasComponent<IPhysBody>())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (user != null && !force)
|
||||
{
|
||||
if (utilizing == null ||
|
||||
!utilizing.TryGetComponent(out ToolComponent? tool) ||
|
||||
!(await tool.UseTool(user, Owner, 0.5f, Tool)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
BaseAnchoredAttemptEvent attempt =
|
||||
anchoring ? new AnchorAttemptEvent(user, utilizing) : new UnanchorAttemptEvent(user, utilizing);
|
||||
|
||||
return true;
|
||||
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, attempt, false);
|
||||
|
||||
if (attempt.Cancelled)
|
||||
return false;
|
||||
|
||||
return utilizing.TryGetComponent(out ToolComponent? tool) && await tool.UseTool(user, Owner, 0.5f, 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, if any</param>
|
||||
/// <param name="force">Whether or not to ignore valid tool checks</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 = null, bool force = false)
|
||||
public async Task<bool> TryAnchor(IEntity user, IEntity utilizing)
|
||||
{
|
||||
if (!(await Valid(user, utilizing, force)))
|
||||
if (!(await Valid(user, utilizing, true)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -82,13 +73,6 @@ namespace Content.Server.GameObjects.Components
|
||||
if (_physicsComponent == null)
|
||||
return false;
|
||||
|
||||
var attempt = new AnchorAttemptMessage();
|
||||
|
||||
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, attempt, false);
|
||||
|
||||
if (attempt.Cancelled)
|
||||
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);
|
||||
@@ -106,7 +90,7 @@ namespace Content.Server.GameObjects.Components
|
||||
|
||||
_physicsComponent.BodyType = BodyType.Static;
|
||||
|
||||
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, new AnchoredMessage(), false);
|
||||
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, new AnchoredEvent(user, utilizing), false);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -118,9 +102,9 @@ namespace Content.Server.GameObjects.Components
|
||||
/// <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 = null, bool force = false)
|
||||
public async Task<bool> TryUnAnchor(IEntity user, IEntity utilizing)
|
||||
{
|
||||
if (!(await Valid(user, utilizing, force)))
|
||||
if (!(await Valid(user, utilizing, false)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -128,16 +112,9 @@ namespace Content.Server.GameObjects.Components
|
||||
if (_physicsComponent == null)
|
||||
return false;
|
||||
|
||||
var attempt = new UnanchorAttemptMessage();
|
||||
|
||||
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, attempt, false);
|
||||
|
||||
if (attempt.Cancelled)
|
||||
return false;
|
||||
|
||||
_physicsComponent.BodyType = BodyType.Dynamic;
|
||||
|
||||
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, new UnanchoredMessage(), false);
|
||||
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, new UnanchoredEvent(user, utilizing), false);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -146,17 +123,16 @@ namespace Content.Server.GameObjects.Components
|
||||
/// 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, if any</param>
|
||||
/// <param name="force">Whether or not to ignore valid tool checks</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 = null, bool force = false)
|
||||
public async Task<bool> TryToggleAnchor(IEntity user, IEntity utilizing)
|
||||
{
|
||||
if (_physicsComponent == null)
|
||||
return false;
|
||||
|
||||
return _physicsComponent.BodyType == BodyType.Static ?
|
||||
await TryUnAnchor(user, utilizing, force) :
|
||||
await TryAnchor(user, utilizing, force);
|
||||
await TryUnAnchor(user, utilizing) :
|
||||
await TryAnchor(user, utilizing);
|
||||
}
|
||||
|
||||
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
|
||||
@@ -165,9 +141,47 @@ namespace Content.Server.GameObjects.Components
|
||||
}
|
||||
}
|
||||
|
||||
public class AnchorAttemptMessage : CancellableEntityEventArgs { }
|
||||
public class UnanchorAttemptMessage : CancellableEntityEventArgs { }
|
||||
public abstract class BaseAnchoredAttemptEvent : CancellableEntityEventArgs
|
||||
{
|
||||
public IEntity User { get; }
|
||||
public IEntity Tool { get; }
|
||||
|
||||
public class AnchoredMessage : EntityEventArgs {}
|
||||
public class UnanchoredMessage : EntityEventArgs {}
|
||||
protected BaseAnchoredAttemptEvent(IEntity user, IEntity tool)
|
||||
{
|
||||
User = user;
|
||||
Tool = tool;
|
||||
}
|
||||
}
|
||||
|
||||
public class AnchorAttemptEvent : BaseAnchoredAttemptEvent
|
||||
{
|
||||
public AnchorAttemptEvent(IEntity user, IEntity tool) : base(user, tool) { }
|
||||
}
|
||||
|
||||
public class UnanchorAttemptEvent : BaseAnchoredAttemptEvent
|
||||
{
|
||||
public UnanchorAttemptEvent(IEntity user, IEntity tool) : base(user, tool) { }
|
||||
}
|
||||
|
||||
public abstract class BaseAnchoredEvent : EntityEventArgs
|
||||
{
|
||||
public IEntity User { get; }
|
||||
public IEntity Tool { get; }
|
||||
|
||||
protected BaseAnchoredEvent(IEntity user, IEntity tool)
|
||||
{
|
||||
User = user;
|
||||
Tool = tool;
|
||||
}
|
||||
}
|
||||
|
||||
public class AnchoredEvent : BaseAnchoredEvent
|
||||
{
|
||||
public AnchoredEvent(IEntity user, IEntity tool) : base(user, tool) { }
|
||||
}
|
||||
|
||||
public class UnanchoredEvent : BaseAnchoredEvent
|
||||
{
|
||||
public UnanchoredEvent(IEntity user, IEntity tool) : base(user, tool) { }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user