Blocking and Shields (#8584)

* Blocking

* Fixes Bodytype bug

* Blocking Damage Modifier

* Storing bodytype

* Consolidates Stop Blocking code

* Consolidates more methods

* Some cleanup, hitbox fix

* Shield Textures

* Passive blocking modifier check

* Localization, popups, and more cleanup

* Small cleanup

* Relay event

* Fixes a shutdown bug, adds specific containers and sets

* Popups and sounds

* Fixes typo

* Removes whitespace, adds comment

* Some requested changes

* Remove Shared

* Audio fix

* More changes

* More requested changes

* Properly remove on shutdown

* Adds riot shields to seclathes

* SecTech Riot shield

* Constant variable

* Relay transfer to user blocking system

* More destruction behavior

* Adds a shape field

* Riot shield cleanup

* More requested changes.

* Prevents blocking attempt where a user cannot be anchored

* Listen for anchor change

* Unused using cleanup

* More shields.

* Buckler

* Construction

* Linter fix
This commit is contained in:
keronshb
2022-07-04 02:31:12 -04:00
committed by GitHub
parent 22d228fd11
commit d65601f024
53 changed files with 981 additions and 1 deletions

View File

@@ -0,0 +1,194 @@
using Content.Shared.Actions;
using Content.Shared.Actions.ActionTypes;
using Content.Shared.Hands;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Physics;
using Content.Shared.Popups;
using Content.Shared.Toggleable;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Dynamics;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
namespace Content.Shared.Blocking;
public sealed class BlockingSystem : EntitySystem
{
[Dependency] private readonly SharedActionsSystem _actionsSystem = default!;
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly SharedTransformSystem _transformSystem = default!;
[Dependency] private readonly FixtureSystem _fixtureSystem = default!;
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
[Dependency] private readonly SharedPopupSystem _popupSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<BlockingComponent, GotEquippedHandEvent>(OnEquip);
SubscribeLocalEvent<BlockingComponent, GotUnequippedHandEvent>(OnUnequip);
SubscribeLocalEvent<BlockingComponent, GetItemActionsEvent>(OnGetActions);
SubscribeLocalEvent<BlockingComponent, ToggleActionEvent>(OnToggleAction);
SubscribeLocalEvent<BlockingComponent, ComponentShutdown>(OnShutdown);
}
private void OnEquip(EntityUid uid, BlockingComponent component, GotEquippedHandEvent args)
{
component.User = args.User;
//To make sure that this bodytype doesn't get set as anything but the original
if (TryComp<PhysicsComponent>(args.User, out var physicsComponent) && physicsComponent.BodyType != BodyType.Static
&& !TryComp<BlockingUserComponent>(args.User, out var blockingUserComponent))
{
var userComp = EnsureComp<BlockingUserComponent>(args.User);
userComp.BlockingItem = uid;
userComp.OriginalBodyType = physicsComponent.BodyType;
}
}
private void OnUnequip(EntityUid uid, BlockingComponent component, GotUnequippedHandEvent args)
{
BlockingShutdownHelper(uid, component, args.User);
}
private void OnGetActions(EntityUid uid, BlockingComponent component, GetItemActionsEvent args)
{
if (component.BlockingToggleAction == null
&& _proto.TryIndex(component.BlockingToggleActionId, out InstantActionPrototype? act))
{
component.BlockingToggleAction = new(act);
}
if (component.BlockingToggleAction != null)
args.Actions.Add(component.BlockingToggleAction);
}
private void OnToggleAction(EntityUid uid, BlockingComponent component, ToggleActionEvent args)
{
if(args.Handled)
return;
if (component.IsBlocking)
StopBlocking(uid, component, args.Performer);
else
StartBlocking(uid, component, args.Performer);
args.Handled = true;
}
private void OnShutdown(EntityUid uid, BlockingComponent component, ComponentShutdown args)
{
//In theory the user should not be null when this fires off
if (component.User != null)
{
_actionsSystem.RemoveProvidedActions(component.User.Value, uid);
BlockingShutdownHelper(uid, component, component.User.Value);
}
}
/// <summary>
/// Called where you want the user to start blocking
/// Creates a new hard fixture to bodyblock
/// Also makes the user static to prevent prediction issues
/// </summary>
/// <param name="uid"> The entity with the blocking component</param>
/// <param name="component"> The <see cref="BlockingComponent"/></param>
/// <param name="user"> The entity who's using the item to block</param>
/// <returns></returns>
public bool StartBlocking(EntityUid item, BlockingComponent component, EntityUid user)
{
if (component.IsBlocking) return false;
var xform = Transform(user);
var shieldName = Name(item);
var msgUser = Loc.GetString("action-popup-blocking-user", ("shield", shieldName));
var msgOther = Loc.GetString("action-popup-blocking-other", ("blockerName", Name(user)), ("shield", shieldName));
if (component.BlockingToggleAction != null)
{
_transformSystem.AnchorEntity(xform);
if (!xform.Anchored)
{
var msgError = Loc.GetString("action-popup-blocking-user-cant-block");
_popupSystem.PopupEntity(msgError, user, Filter.Entities(user));
return false;
}
_actionsSystem.SetToggled(component.BlockingToggleAction, true);
_popupSystem.PopupEntity(msgUser, user, Filter.Entities(user));
_popupSystem.PopupEntity(msgOther, user, Filter.Pvs(user).RemoveWhereAttachedEntity(e => e == user));
}
if (TryComp<PhysicsComponent>(user, out var physicsComponent))
{
var fixture = new Fixture(physicsComponent, component.Shape)
{
ID = BlockingComponent.BlockFixtureID,
Hard = true,
CollisionLayer = (int) CollisionGroup.WallLayer
};
_fixtureSystem.TryCreateFixture(physicsComponent, fixture);
}
component.IsBlocking = true;
return true;
}
/// <summary>
/// Called where you want the user to stop blocking.
/// </summary>
/// <param name="item"> The entity with the blocking component</param>
/// <param name="component"> The <see cref="BlockingComponent"/></param>
/// <param name="user"> The entity who's using the item to block</param>
/// <returns></returns>
public bool StopBlocking(EntityUid item, BlockingComponent component, EntityUid user)
{
if (!component.IsBlocking) return false;
var xform = Transform(user);
var shieldName = Name(item);
var msgUser = Loc.GetString("action-popup-blocking-disabling-user", ("shield", shieldName));
var msgOther = Loc.GetString("action-popup-blocking-disabling-other", ("blockerName", Name(user)), ("shield", shieldName));
//If the component blocking toggle isn't null, grab the users SharedBlockingUserComponent and PhysicsComponent
//then toggle the action to false, unanchor the user, remove the hard fixture
//and set the users bodytype back to their original type
if (component.BlockingToggleAction != null && TryComp<BlockingUserComponent>(user, out var blockingUserComponent)
&& TryComp<PhysicsComponent>(user, out var physicsComponent))
{
_transformSystem.Unanchor(xform);
_actionsSystem.SetToggled(component.BlockingToggleAction, false);
_fixtureSystem.DestroyFixture(physicsComponent, BlockingComponent.BlockFixtureID);
physicsComponent.BodyType = blockingUserComponent.OriginalBodyType;
_popupSystem.PopupEntity(msgUser, user, Filter.Entities(user));
_popupSystem.PopupEntity(msgOther, user, Filter.Pvs(user).RemoveWhereAttachedEntity(e => e == user));
}
component.IsBlocking = false;
return true;
}
/// <summary>
/// Called where you want someone to stop blocking and to remove the <see cref="BlockingUserComponent"/> from them
/// </summary>
/// <param name="uid"> The item the component is attached to</param>
/// <param name="component"> The <see cref="BlockingComponent"/> </param>
/// <param name="user"> The person holding the blocking item </param>
private void BlockingShutdownHelper(EntityUid uid, BlockingComponent component, EntityUid user)
{
if (component.IsBlocking)
StopBlocking(uid, component, user);
RemComp<BlockingUserComponent>(user);
component.User = null;
}
}

View File

@@ -0,0 +1,60 @@
using Content.Shared.Audio;
using Content.Shared.Damage;
using Content.Shared.Damage.Prototypes;
using Content.Shared.Hands.EntitySystems;
using Robust.Shared.Audio;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
namespace Content.Shared.Blocking;
public sealed class BlockingUserSystem : EntitySystem
{
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly BlockingSystem _blockingSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<BlockingUserComponent, DamageChangedEvent>(OnDamageChanged);
SubscribeLocalEvent<BlockingUserComponent, DamageModifyEvent>(OnUserDamageModified);
SubscribeLocalEvent<BlockingUserComponent, AnchorStateChangedEvent>(OnAnchorChanged);
}
private void OnAnchorChanged(EntityUid uid, BlockingUserComponent component, ref AnchorStateChangedEvent args)
{
if (!args.Anchored)
return;
if (TryComp<BlockingComponent>(component.BlockingItem, out var blockComp) && blockComp.IsBlocking)
{
_blockingSystem.StopBlocking(component.BlockingItem.Value, blockComp, uid);
}
}
private void OnDamageChanged(EntityUid uid, BlockingUserComponent component, DamageChangedEvent args)
{
if (component.BlockingItem != null)
{
RaiseLocalEvent(component.BlockingItem.Value, args);
}
}
private void OnUserDamageModified(EntityUid uid, BlockingUserComponent component, DamageModifyEvent args)
{
if (TryComp<BlockingComponent>(component.BlockingItem, out var blockingComponent))
{
if (_proto.TryIndex(blockingComponent.PassiveBlockDamageModifer, out DamageModifierSetPrototype? passiveblockModifier) && !blockingComponent.IsBlocking)
args.Damage = DamageSpecifier.ApplyModifierSet(args.Damage, passiveblockModifier);
if (_proto.TryIndex(blockingComponent.ActiveBlockDamageModifier, out DamageModifierSetPrototype? activeBlockModifier) && blockingComponent.IsBlocking)
{
args.Damage = DamageSpecifier.ApplyModifierSet(args.Damage, activeBlockModifier);
SoundSystem.Play(blockingComponent.BlockSound.GetSound(), Filter.Pvs(component.Owner, entityManager: EntityManager), component.Owner, AudioHelpers.WithVariation(0.2f));
}
}
}
}

View File

@@ -0,0 +1,64 @@
using Content.Shared.Actions.ActionTypes;
using Content.Shared.Sound;
using Robust.Shared.Physics.Collision.Shapes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Shared.Blocking;
/// <summary>
/// This component goes on an item that you want to use to block
/// </summary>
[RegisterComponent]
public sealed class BlockingComponent : Component
{
/// <summary>
/// The entity that's blocking
/// </summary>
[ViewVariables]
public EntityUid? User;
/// <summary>
/// Is it currently blocking?
/// </summary>
[ViewVariables]
public bool IsBlocking;
/// <summary>
/// The ID for the fixture that's dynamically created when blocking
/// </summary>
public const string BlockFixtureID = "blocking-active";
/// <summary>
/// The shape of the blocking fixture that will be dynamically spawned
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("shape")]
public IPhysShape Shape = new PhysShapeCircle {Radius = 0.5F};
/// <summary>
/// The damage modifer to use while passively blocking
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("passiveBlockModifier")]
public string PassiveBlockDamageModifer = "Metallic";
/// <summary>
/// The damage modifier to use while actively blocking.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("activeBlockModifier")]
public string ActiveBlockDamageModifier = "Metallic";
[DataField("blockingToggleActionId", customTypeSerializer:typeof(PrototypeIdSerializer<InstantActionPrototype>))]
public string BlockingToggleActionId = "ToggleBlock";
[DataField("blockingToggleAction")]
public InstantAction? BlockingToggleAction;
/// <summary>
/// The sound to be played when you get hit while actively blocking
/// </summary>
[ViewVariables]
[DataField("blockSound")]
public SoundSpecifier BlockSound = new SoundPathSpecifier("/Audio/Weapons/block_metal1.ogg");
}

View File

@@ -0,0 +1,31 @@
using Content.Shared.Damage;
using Robust.Shared.Physics;
namespace Content.Shared.Blocking;
/// <summary>
/// This component gets dynamically added to an Entity via the <see cref="BlockingSystem"/>
/// </summary>
[RegisterComponent]
public sealed class BlockingUserComponent : Component
{
/// <summary>
/// The entity that's being used to block
/// </summary>
[ViewVariables]
[DataField("blockingItem")]
public EntityUid? BlockingItem;
[ViewVariables]
[DataField("modifiers")]
public DamageModifierSet Modifiers = default!;
/// <summary>
/// Stores the entities original bodytype
/// Used so that it can be put back to what it was after anchoring
/// </summary>
[ViewVariables]
[DataField("originalBodyType")]
public BodyType OriginalBodyType;
}