rodentia crawling refactor (#2754)
* make draw depth stuff reusable and good * rodentia crawling refactor --------- Co-authored-by: deltanedas <@deltanedas:kde.org>
This commit is contained in:
parent
f95171c168
commit
92a1e0dfc6
|
|
@ -1,44 +0,0 @@
|
|||
using Content.Shared._DV.Abilities;
|
||||
using Content.Shared.Popups;
|
||||
using Robust.Client.GameObjects;
|
||||
using DrawDepth = Content.Shared.DrawDepth.DrawDepth;
|
||||
|
||||
namespace Content.Client._DV.Abilities;
|
||||
|
||||
public sealed partial class HideUnderTableAbilitySystem : SharedCrawlUnderObjectsSystem
|
||||
{
|
||||
[Dependency] private readonly AppearanceSystem _appearance = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<CrawlUnderObjectsComponent, AppearanceChangeEvent>(OnAppearanceChange);
|
||||
}
|
||||
|
||||
private void OnAppearanceChange(EntityUid uid,
|
||||
CrawlUnderObjectsComponent component,
|
||||
AppearanceChangeEvent args)
|
||||
{
|
||||
if (!TryComp<SpriteComponent>(uid, out var sprite))
|
||||
return;
|
||||
|
||||
_appearance.TryGetData(uid, SneakMode.Enabled, out bool enabled);
|
||||
if (enabled)
|
||||
{
|
||||
if (component.OriginalDrawDepth != null)
|
||||
return;
|
||||
|
||||
component.OriginalDrawDepth = sprite.DrawDepth;
|
||||
sprite.DrawDepth = (int) DrawDepth.SmallMobs;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (component.OriginalDrawDepth == null)
|
||||
return;
|
||||
|
||||
sprite.DrawDepth = (int) component.OriginalDrawDepth;
|
||||
component.OriginalDrawDepth = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
using DrawDepth = Content.Shared.DrawDepth.DrawDepth;
|
||||
|
||||
namespace Content.Client._DV.Abilities;
|
||||
|
||||
/// <summary>
|
||||
/// Changes the sprite's draw depth when some appearance data becomes true, reverting it when false.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public sealed partial class DrawDepthVisualizerComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// Appearance key to check.
|
||||
/// </summary>
|
||||
[DataField(required: true)]
|
||||
public Enum Key;
|
||||
|
||||
/// <summary>
|
||||
/// The draw depth to set the sprite to when the appearance data is true.
|
||||
/// </summary>
|
||||
[DataField(required: true)]
|
||||
public DrawDepth Depth;
|
||||
|
||||
[DataField]
|
||||
public int? OriginalDrawDepth;
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
using Content.Shared.DrawDepth;
|
||||
using Robust.Client.GameObjects;
|
||||
|
||||
namespace Content.Client._DV.Abilities;
|
||||
|
||||
/// <summary>
|
||||
/// Changes a sprite's draw depth when some appearance data becomes true.
|
||||
/// </summary>
|
||||
public sealed class DrawDepthVisualizerSystem : EntitySystem
|
||||
{
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<DrawDepthVisualizerComponent, AppearanceChangeEvent>(OnAppearanceChange);
|
||||
}
|
||||
|
||||
private void OnAppearanceChange(Entity<DrawDepthVisualizerComponent> ent, ref AppearanceChangeEvent args)
|
||||
{
|
||||
if (args.Sprite is not {} sprite || !args.AppearanceData.TryGetValue(ent.Comp.Key, out var value))
|
||||
return;
|
||||
|
||||
if (value is true)
|
||||
{
|
||||
if (ent.Comp.OriginalDrawDepth != null)
|
||||
return;
|
||||
|
||||
ent.Comp.OriginalDrawDepth = sprite.DrawDepth;
|
||||
sprite.DrawDepth = (int) ent.Comp.Depth;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ent.Comp.OriginalDrawDepth is not {} original)
|
||||
return;
|
||||
|
||||
sprite.DrawDepth = original;
|
||||
ent.Comp.OriginalDrawDepth = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -19,7 +19,8 @@ namespace Content.Server.Entry
|
|||
"InventorySlots",
|
||||
"LightFade",
|
||||
"HolidayRsiSwap",
|
||||
"OptionsVisualizer"
|
||||
"OptionsVisualizer",
|
||||
"DrawDepthVisualizer" // DeltaV
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,140 +0,0 @@
|
|||
using Content.Shared.Actions;
|
||||
using Content.Shared.Climbing.Components;
|
||||
using Content.Shared.Climbing.Events;
|
||||
using Content.Shared._DV.Abilities;
|
||||
using Content.Shared.Maps;
|
||||
using Content.Shared.Movement.Systems;
|
||||
using Content.Shared.Physics;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Physics.Systems;
|
||||
|
||||
namespace Content.Server._DV.Abilities;
|
||||
|
||||
public sealed partial class CrawlUnderObjectsSystem : SharedCrawlUnderObjectsSystem
|
||||
{
|
||||
[Dependency] private readonly AppearanceSystem _appearance = default!;
|
||||
[Dependency] private readonly SharedActionsSystem _actionsSystem = default!;
|
||||
[Dependency] private readonly MovementSpeedModifierSystem _movespeed = default!;
|
||||
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
|
||||
[Dependency] private readonly TurfSystem _turf = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<CrawlUnderObjectsComponent, ComponentInit>(OnInit);
|
||||
SubscribeLocalEvent<CrawlUnderObjectsComponent, ToggleCrawlingStateEvent>(OnAbilityToggle);
|
||||
SubscribeLocalEvent<CrawlUnderObjectsComponent, AttemptClimbEvent>(OnAttemptClimb);
|
||||
SubscribeLocalEvent<CrawlUnderObjectsComponent, RefreshMovementSpeedModifiersEvent>(OnRefreshMovespeed);
|
||||
}
|
||||
|
||||
private bool IsOnCollidingTile(EntityUid uid)
|
||||
{
|
||||
var xform = Transform(uid);
|
||||
var tile = xform.Coordinates.GetTileRef();
|
||||
if (tile == null)
|
||||
return false;
|
||||
|
||||
return _turf.IsTileBlocked(tile.Value, CollisionGroup.MobMask);
|
||||
}
|
||||
|
||||
private void OnInit(EntityUid uid, CrawlUnderObjectsComponent component, ComponentInit args)
|
||||
{
|
||||
if (component.ToggleHideAction != null)
|
||||
return;
|
||||
|
||||
_actionsSystem.AddAction(uid, ref component.ToggleHideAction, component.ActionProto);
|
||||
}
|
||||
|
||||
private bool EnableSneakMode(EntityUid uid, CrawlUnderObjectsComponent component)
|
||||
{
|
||||
if (component.Enabled
|
||||
|| (TryComp<ClimbingComponent>(uid, out var climbing)
|
||||
&& climbing.IsClimbing == true))
|
||||
return false;
|
||||
|
||||
component.Enabled = true;
|
||||
Dirty(uid, component);
|
||||
RaiseLocalEvent(uid, new CrawlingUpdatedEvent(component.Enabled));
|
||||
|
||||
if (TryComp(uid, out FixturesComponent? fixtureComponent))
|
||||
{
|
||||
foreach (var (key, fixture) in fixtureComponent.Fixtures)
|
||||
{
|
||||
var newMask = (fixture.CollisionMask
|
||||
& (int)~CollisionGroup.HighImpassable
|
||||
& (int)~CollisionGroup.MidImpassable)
|
||||
| (int)CollisionGroup.InteractImpassable;
|
||||
if (fixture.CollisionMask == newMask)
|
||||
continue;
|
||||
|
||||
component.ChangedFixtures.Add((key, fixture.CollisionMask));
|
||||
_physics.SetCollisionMask(uid,
|
||||
key,
|
||||
fixture,
|
||||
newMask,
|
||||
manager: fixtureComponent);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool DisableSneakMode(EntityUid uid, CrawlUnderObjectsComponent component)
|
||||
{
|
||||
if (!component.Enabled
|
||||
|| IsOnCollidingTile(uid)
|
||||
|| (TryComp<ClimbingComponent>(uid, out var climbing)
|
||||
&& climbing.IsClimbing == true))
|
||||
return false;
|
||||
|
||||
component.Enabled = false;
|
||||
Dirty(uid, component);
|
||||
RaiseLocalEvent(uid, new CrawlingUpdatedEvent(component.Enabled));
|
||||
|
||||
// Restore normal collision masks
|
||||
if (TryComp(uid, out FixturesComponent? fixtureComponent))
|
||||
foreach (var (key, originalMask) in component.ChangedFixtures)
|
||||
if (fixtureComponent.Fixtures.TryGetValue(key, out var fixture))
|
||||
_physics.SetCollisionMask(uid, key, fixture, originalMask, fixtureComponent);
|
||||
|
||||
component.ChangedFixtures.Clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
private void OnAbilityToggle(EntityUid uid,
|
||||
CrawlUnderObjectsComponent component,
|
||||
ToggleCrawlingStateEvent args)
|
||||
{
|
||||
if (args.Handled)
|
||||
return;
|
||||
|
||||
bool result;
|
||||
|
||||
if (component.Enabled)
|
||||
result = DisableSneakMode(uid, component);
|
||||
else
|
||||
result = EnableSneakMode(uid, component);
|
||||
|
||||
if (TryComp<AppearanceComponent>(uid, out var app))
|
||||
_appearance.SetData(uid, SneakMode.Enabled, component.Enabled, app);
|
||||
|
||||
_movespeed.RefreshMovementSpeedModifiers(uid);
|
||||
|
||||
args.Handled = result;
|
||||
}
|
||||
|
||||
private void OnAttemptClimb(EntityUid uid,
|
||||
CrawlUnderObjectsComponent component,
|
||||
AttemptClimbEvent args)
|
||||
{
|
||||
if (component.Enabled == true)
|
||||
args.Cancelled = true;
|
||||
}
|
||||
|
||||
private void OnRefreshMovespeed(EntityUid uid, CrawlUnderObjectsComponent component, RefreshMovementSpeedModifiersEvent args)
|
||||
{
|
||||
if (component.Enabled)
|
||||
args.ModifySpeed(component.SneakSpeedModifier, component.SneakSpeedModifier);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,47 +1,43 @@
|
|||
using Content.Shared.Actions;
|
||||
using DrawDepth = Content.Shared.DrawDepth.DrawDepth;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared._DV.Abilities;
|
||||
|
||||
/// <summary>
|
||||
/// Gives the player an action to sneak under tables at a slower move speed.
|
||||
/// </summary>
|
||||
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
|
||||
public sealed partial class CrawlUnderObjectsComponent : Component
|
||||
{
|
||||
[DataField]
|
||||
public EntityUid? ToggleHideAction;
|
||||
|
||||
[DataField]
|
||||
public EntProtoId? ActionProto;
|
||||
[DataField(required: true)]
|
||||
public EntProtoId ActionProto;
|
||||
|
||||
[DataField]
|
||||
public bool Enabled = false;
|
||||
[DataField, AutoNetworkedField]
|
||||
public bool Enabled;
|
||||
|
||||
/// <summary>
|
||||
/// List of fixtures that had their collision mask changed.
|
||||
/// Required for re-adding the collision mask.
|
||||
/// </summary>
|
||||
[DataField, AutoNetworkedField]
|
||||
public List<(string key, int originalMask)> ChangedFixtures = new();
|
||||
|
||||
[DataField]
|
||||
public int? OriginalDrawDepth;
|
||||
public List<(string key, int originalMask)> ChangedFixtures = new();
|
||||
|
||||
[DataField]
|
||||
public float SneakSpeedModifier = 0.7f;
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public enum SneakMode : byte
|
||||
public enum SneakingVisuals : byte
|
||||
{
|
||||
Enabled
|
||||
Sneaking
|
||||
}
|
||||
|
||||
public sealed partial class ToggleCrawlingStateEvent : InstantActionEvent { }
|
||||
public sealed partial class ToggleCrawlingStateEvent : InstantActionEvent;
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed partial class CrawlingUpdatedEvent(bool enabled = false) : EventArgs
|
||||
{
|
||||
public readonly bool Enabled = enabled;
|
||||
}
|
||||
[ByRefEvent]
|
||||
public readonly record struct CrawlingUpdatedEvent(bool Enabled, CrawlUnderObjectsComponent Comp);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,167 @@
|
|||
using Content.Shared.Actions;
|
||||
using Content.Shared.Climbing.Components;
|
||||
using Content.Shared.Climbing.Events;
|
||||
using Content.Shared.Maps;
|
||||
using Content.Shared.Mobs;
|
||||
using Content.Shared.Movement.Systems;
|
||||
using Content.Shared.Physics;
|
||||
using Content.Shared.Popups;
|
||||
using Content.Shared.Standing;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Physics.Systems;
|
||||
|
||||
namespace Content.Shared._DV.Abilities;
|
||||
|
||||
/// <summary>
|
||||
/// Not to be confused with laying down, <see cref="CrawlUnderObjectsComponent"/> lets you move under tables.
|
||||
/// </summary>
|
||||
public sealed class CrawlUnderObjectsSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly MovementSpeedModifierSystem _moveSpeed = default!;
|
||||
[Dependency] private readonly SharedActionsSystem _actions = default!;
|
||||
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
|
||||
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
|
||||
[Dependency] private readonly SharedPopupSystem _popup = default!;
|
||||
[Dependency] private readonly StandingStateSystem _standing = default!;
|
||||
[Dependency] private readonly TurfSystem _turf = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<CrawlUnderObjectsComponent, MapInitEvent>(OnMapInit);
|
||||
SubscribeLocalEvent<CrawlUnderObjectsComponent, ToggleCrawlingStateEvent>(OnToggleCrawling);
|
||||
SubscribeLocalEvent<CrawlUnderObjectsComponent, AttemptClimbEvent>(OnAttemptClimb);
|
||||
SubscribeLocalEvent<CrawlUnderObjectsComponent, DownAttemptEvent>(CancelWhenSneaking);
|
||||
SubscribeLocalEvent<CrawlUnderObjectsComponent, StandAttemptEvent>(CancelWhenSneaking);
|
||||
SubscribeLocalEvent<CrawlUnderObjectsComponent, RefreshMovementSpeedModifiersEvent>(OnRefreshMoveSpeed);
|
||||
SubscribeLocalEvent<CrawlUnderObjectsComponent, MobStateChangedEvent>(OnMobStateChanged);
|
||||
|
||||
SubscribeLocalEvent<FixturesComponent, CrawlingUpdatedEvent>(OnCrawlingUpdated);
|
||||
}
|
||||
|
||||
private void OnMapInit(Entity<CrawlUnderObjectsComponent> ent, ref MapInitEvent args)
|
||||
{
|
||||
if (ent.Comp.ToggleHideAction != null)
|
||||
return;
|
||||
|
||||
_actions.AddAction(ent, ref ent.Comp.ToggleHideAction, ent.Comp.ActionProto);
|
||||
}
|
||||
|
||||
private void OnToggleCrawling(Entity<CrawlUnderObjectsComponent> ent, ref ToggleCrawlingStateEvent args)
|
||||
{
|
||||
if (args.Handled)
|
||||
return;
|
||||
|
||||
args.Handled = TryToggle(ent);
|
||||
}
|
||||
|
||||
private void OnAttemptClimb(Entity<CrawlUnderObjectsComponent> ent, ref AttemptClimbEvent args)
|
||||
{
|
||||
if (ent.Comp.Enabled)
|
||||
args.Cancelled = true;
|
||||
}
|
||||
|
||||
private void CancelWhenSneaking<TEvent>(Entity<CrawlUnderObjectsComponent> ent, ref TEvent args) where TEvent : CancellableEntityEventArgs
|
||||
{
|
||||
if (ent.Comp.Enabled)
|
||||
args.Cancel();
|
||||
}
|
||||
|
||||
private void OnRefreshMoveSpeed(Entity<CrawlUnderObjectsComponent> ent, ref RefreshMovementSpeedModifiersEvent args)
|
||||
{
|
||||
if (ent.Comp.Enabled)
|
||||
args.ModifySpeed(ent.Comp.SneakSpeedModifier, ent.Comp.SneakSpeedModifier);
|
||||
}
|
||||
|
||||
private void OnMobStateChanged(Entity<CrawlUnderObjectsComponent> ent, ref MobStateChangedEvent args)
|
||||
{
|
||||
if (args.OldMobState != MobState.Alive || !ent.Comp.Enabled)
|
||||
return;
|
||||
|
||||
// crawling prevents downing, so when you go crit/die stop crawling and force downing
|
||||
SetEnabled(ent, false);
|
||||
_standing.Down(ent);
|
||||
}
|
||||
|
||||
private void OnCrawlingUpdated(Entity<FixturesComponent> ent, ref CrawlingUpdatedEvent args)
|
||||
{
|
||||
if (args.Enabled)
|
||||
{
|
||||
foreach (var (key, fixture) in ent.Comp.Fixtures)
|
||||
{
|
||||
var newMask = (fixture.CollisionMask
|
||||
& (int)~CollisionGroup.HighImpassable
|
||||
& (int)~CollisionGroup.MidImpassable)
|
||||
| (int)CollisionGroup.InteractImpassable;
|
||||
if (fixture.CollisionMask == newMask)
|
||||
continue;
|
||||
|
||||
args.Comp.ChangedFixtures.Add((key, fixture.CollisionMask));
|
||||
_physics.SetCollisionMask(ent,
|
||||
key,
|
||||
fixture,
|
||||
newMask,
|
||||
manager: ent.Comp);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var (key, originalMask) in args.Comp.ChangedFixtures)
|
||||
{
|
||||
if (ent.Comp.Fixtures.TryGetValue(key, out var fixture))
|
||||
_physics.SetCollisionMask(ent, key, fixture, originalMask, ent.Comp);
|
||||
}
|
||||
|
||||
args.Comp.ChangedFixtures.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to enable or disable sneaking
|
||||
/// </summary>
|
||||
public bool TrySetEnabled(Entity<CrawlUnderObjectsComponent> ent, bool enabled)
|
||||
{
|
||||
if (ent.Comp.Enabled == enabled || IsOnCollidingTile(ent) || _standing.IsDown(ent))
|
||||
return false;
|
||||
|
||||
if (TryComp<ClimbingComponent>(ent, out var climbing) && climbing.IsClimbing)
|
||||
return false;
|
||||
|
||||
SetEnabled(ent, enabled);
|
||||
|
||||
var msg = Loc.GetString("crawl-under-objects-toggle-" + (enabled ? "on" : "off"));
|
||||
_popup.PopupPredicted(msg, ent, ent);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void SetEnabled(Entity<CrawlUnderObjectsComponent> ent, bool enabled)
|
||||
{
|
||||
ent.Comp.Enabled = enabled;
|
||||
Dirty(ent);
|
||||
|
||||
_appearance.SetData(ent, SneakingVisuals.Sneaking, enabled);
|
||||
|
||||
_moveSpeed.RefreshMovementSpeedModifiers(ent);
|
||||
|
||||
var ev = new CrawlingUpdatedEvent(enabled, ent.Comp);
|
||||
RaiseLocalEvent(ent, ref ev);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to toggle sneaking
|
||||
/// </summary>
|
||||
public bool TryToggle(Entity<CrawlUnderObjectsComponent> ent)
|
||||
{
|
||||
return TrySetEnabled(ent, !ent.Comp.Enabled);
|
||||
}
|
||||
|
||||
private bool IsOnCollidingTile(EntityUid uid)
|
||||
{
|
||||
if (Transform(uid).Coordinates.GetTileRef() is not {} tile)
|
||||
return false;
|
||||
|
||||
return _turf.IsTileBlocked(tile, CollisionGroup.MobMask);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
|
||||
using Content.Shared.Popups;
|
||||
|
||||
namespace Content.Shared._DV.Abilities;
|
||||
public abstract class SharedCrawlUnderObjectsSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly SharedPopupSystem _popup = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<CrawlUnderObjectsComponent, CrawlingUpdatedEvent>(OnCrawlingUpdated);
|
||||
}
|
||||
|
||||
private void OnCrawlingUpdated(EntityUid uid,
|
||||
CrawlUnderObjectsComponent component,
|
||||
CrawlingUpdatedEvent args)
|
||||
{
|
||||
if (args.Enabled)
|
||||
_popup.PopupEntity(Loc.GetString("crawl-under-objects-toggle-on"), uid);
|
||||
else
|
||||
_popup.PopupEntity(Loc.GetString("crawl-under-objects-toggle-off"), uid);
|
||||
}
|
||||
}
|
||||
|
|
@ -114,6 +114,9 @@
|
|||
spitDamageThreshold: 3
|
||||
- type: CrawlUnderObjects
|
||||
actionProto: ActionToggleSneakMode
|
||||
- type: DrawDepthVisualizer
|
||||
key: enum.SneakingVisuals.Sneaking
|
||||
depth: SmallMobs
|
||||
|
||||
- type: entity
|
||||
save: false
|
||||
|
|
|
|||
Loading…
Reference in New Issue