Files
tbd-station-14/Content.Server/GameObjects/Components/Strap/StrapComponent.cs
DrSmugleaf 602dac393e Add Buckling (#1155)
* Create BuckleableComponent.cs

* Add strap component and keybind to buckle targeted entity

* Remove buckle keybind, turn it into a verb

* Add moving and attaching the buckled entity to the strap

* Fix reality collapsing when clicking on a buckled entity

* Add strap position to buckle a mob in the standing or down position

* Add new default strap position that makes no change to the mob's standing state

* Add Strap component to office chairs and stools

* Add Strap component to the pilot chair

* Add buckled status effect icon

* Add status effect click behaviour

* Add buckling and unbuckling sounds

* Change Buckle verb to only appear when an entity can be currently buckled

* Rotate buckled entity in the direction of the seat

* Disable entity rotation when buckled

* Fix buckle rotation on beds

* Buckling now finds the closest strap to the buckleable entity

* Fix rotation when unbuckling an entity

* Move buckle verb to StrapComponent

* Added buckled entity unbuckle verb, range and interaction checks

* Add checks for currently occupied straps

* Add unbuckling entity if its respective strap component is removed

* Add Clickable, InteractionOutline and Collidable components to bed

* Add rotation property to strap component

* Rename Buckleable to Buckle

* Add Buckle and Strap sizes to buckle multiple entities in the same strap

* Remove out of range popup message from strap verb GetData

* Move BuckledTo setter logic to its methods

* Fix Strap BuckledEntities being public

* Fix not updating status when Buckle component is removed

* Change BuckleComponent.BuckledTo to be of type StrapComponent

* Fix NRE when unbuckling

* Add buckle perspective messages

* Fix not equals comparison in strap verb

* Add added check to Strap TryAdd

* Change buckle.ogg and unbuckle.ogg from stereo to mono

* Remove -2f volume on buckle and unbuckle sounds

* Add summary to Strap TryAdd and Remove methods

* Make buckled entities unable to fall

* Fix default strap position not rotating the buckled entity

* Add downing after unbuckling an entity if it is knocked down

* Prevent an entity from buckling onto itself

Fixes stack overflow error

* Disable recursive buckling

* Add buckling onto straps by clicking them with an empty hand

* Add recursive buckle check to the trybuckle method as well

* Fix being able to click on a different strap to unbuckle from the current one

* Merge TryUnbuckle and ForceUnbuckle with a force argument

* Remove explicit unimplemented status effect clicking cases

* Add documentation to EffectBlockerSystem and ActionBlockerSystem
2020-06-25 15:52:24 +02:00

216 lines
6.9 KiB
C#

using System.Collections.Generic;
using Content.Server.GameObjects.Components.Mobs;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.GameObjects;
using Content.Shared.GameObjects.Components.Strap;
using Content.Shared.GameObjects.EntitySystems;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Strap
{
[RegisterComponent]
public class StrapComponent : SharedStrapComponent, IInteractHand
{
private StrapPosition _position;
private string _buckleSound;
private string _unbuckleSound;
private int _rotation;
private int _size;
/// <summary>
/// The entity that is currently buckled here, synced from <see cref="BuckleComponent.BuckledTo"/>
/// </summary>
private HashSet<IEntity> BuckledEntities { get; set; }
/// <summary>
/// The change in position to the strapped mob
/// </summary>
public override StrapPosition Position
{
get => _position;
set
{
_position = value;
Dirty();
}
}
/// <summary>
/// The sound to be played when a mob is buckled
/// </summary>
[ViewVariables]
public string BuckleSound => _buckleSound;
/// <summary>
/// The sound to be played when a mob is unbuckled
/// </summary>
[ViewVariables]
public string UnbuckleSound => _unbuckleSound;
/// <summary>
/// The angle in degrees to rotate the player by when they get strapped
/// </summary>
[ViewVariables]
public int Rotation => _rotation;
/// <summary>
/// The size of the strap which is compared against when buckling entities
/// </summary>
[ViewVariables]
public int Size => _size;
/// <summary>
/// The sum of the sizes of all the buckled entities in this strap
/// </summary>
[ViewVariables]
public int OccupiedSize { get; private set; }
public bool HasSpace(BuckleComponent buckle)
{
return OccupiedSize + buckle.Size <= _size;
}
/// <summary>
/// Adds a buckled entity. Called from <see cref="BuckleComponent.TryBuckle"/>
/// </summary>
/// <param name="buckle">The component to add</param>
/// <param name="force">Whether or not to check if the strap has enough space</param>
/// <returns>True if added, false otherwise</returns>
public bool TryAdd(BuckleComponent buckle, bool force = false)
{
if (!force && !HasSpace(buckle))
{
return false;
}
if (!BuckledEntities.Add(buckle.Owner))
{
return false;
}
OccupiedSize += buckle.Size;
if (buckle.Owner.TryGetComponent(out AppearanceComponent appearance))
{
appearance.SetData(StrapVisuals.RotationAngle, _rotation);
}
return true;
}
/// <summary>
/// Removes a buckled entity. Called from <see cref="BuckleComponent.TryUnbuckle"/>
/// </summary>
/// <param name="buckle">The component to remove</param>
public void Remove(BuckleComponent buckle)
{
if (BuckledEntities.Remove(buckle.Owner))
{
OccupiedSize -= buckle.Size;
}
}
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _position, "position", StrapPosition.None);
serializer.DataField(ref _buckleSound, "buckleSound", "/Audio/effects/buckle.ogg");
serializer.DataField(ref _unbuckleSound, "unbuckleSound", "/Audio/effects/unbuckle.ogg");
serializer.DataField(ref _rotation, "rotation", 0);
var defaultSize = 100;
serializer.DataField(ref _size, "size", defaultSize);
BuckledEntities = new HashSet<IEntity>(_size / defaultSize);
OccupiedSize = 0;
}
public override void OnRemove()
{
base.OnRemove();
foreach (var entity in BuckledEntities)
{
if (entity.TryGetComponent(out BuckleComponent buckle))
{
buckle.TryUnbuckle(entity, true);
}
}
BuckledEntities.Clear();
OccupiedSize = 0;
}
[Verb]
private sealed class StrapVerb : Verb<StrapComponent>
{
protected override void GetData(IEntity user, StrapComponent component, VerbData data)
{
data.Visibility = VerbVisibility.Invisible;
if (!ActionBlockerSystem.CanInteract(component.Owner) ||
!user.TryGetComponent(out BuckleComponent buckle) ||
buckle.BuckledTo != null && buckle.BuckledTo != component ||
user == component.Owner)
{
return;
}
var parent = component.Owner.Transform.Parent;
while (parent != null)
{
if (parent == user.Transform)
{
return;
}
parent = parent.Parent;
}
var userPosition = user.Transform.MapPosition;
var strapPosition = component.Owner.Transform.MapPosition;
var range = SharedInteractionSystem.InteractionRange / 2;
var inRange = EntitySystem.Get<SharedInteractionSystem>()
.InRangeUnobstructed(userPosition, strapPosition, range,
predicate: entity => entity == user || entity == component.Owner);
if (!inRange)
{
return;
}
data.Visibility = VerbVisibility.Visible;
data.Text = buckle.BuckledTo == null ? Loc.GetString("Buckle") : Loc.GetString("Unbuckle");
}
protected override void Activate(IEntity user, StrapComponent component)
{
if (!user.TryGetComponent(out BuckleComponent buckle))
{
return;
}
buckle.ToggleBuckle(user, component.Owner);
}
}
bool IInteractHand.InteractHand(InteractHandEventArgs eventArgs)
{
if (!eventArgs.User.TryGetComponent(out BuckleComponent buckle))
{
return false;
}
return buckle.ToggleBuckle(eventArgs.User, Owner);
}
}
}