Adds AttemptEntity(Uns|S)tickEvent. (#20728)

* try-stick

* convert spider charge to attempt-stick-events
This commit is contained in:
TemporalOroboros
2023-10-24 18:45:42 -07:00
committed by GitHub
parent f0c5aa15b8
commit 3007200832
3 changed files with 63 additions and 12 deletions

View File

@@ -23,7 +23,7 @@ public sealed class SpiderChargeSystem : EntitySystem
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<SpiderChargeComponent, BeforeRangedInteractEvent>(BeforePlant); SubscribeLocalEvent<SpiderChargeComponent, AttemptEntityStickEvent>(OnAttemptStick);
SubscribeLocalEvent<SpiderChargeComponent, EntityStuckEvent>(OnStuck); SubscribeLocalEvent<SpiderChargeComponent, EntityStuckEvent>(OnStuck);
SubscribeLocalEvent<SpiderChargeComponent, TriggerEvent>(OnExplode); SubscribeLocalEvent<SpiderChargeComponent, TriggerEvent>(OnExplode);
} }
@@ -31,14 +31,17 @@ public sealed class SpiderChargeSystem : EntitySystem
/// <summary> /// <summary>
/// Require that the planter is a ninja and the charge is near the target warp point. /// Require that the planter is a ninja and the charge is near the target warp point.
/// </summary> /// </summary>
private void BeforePlant(EntityUid uid, SpiderChargeComponent comp, BeforeRangedInteractEvent args) private void OnAttemptStick(EntityUid uid, SpiderChargeComponent comp, AttemptEntityStickEvent args)
{ {
if (args.Cancelled)
return;
var user = args.User; var user = args.User;
if (!_mind.TryGetRole<NinjaRoleComponent>(user, out var role)) if (!_mind.TryGetRole<NinjaRoleComponent>(user, out var role))
{ {
_popup.PopupEntity(Loc.GetString("spider-charge-not-ninja"), user, user); _popup.PopupEntity(Loc.GetString("spider-charge-not-ninja"), user, user);
args.Handled = true; args.Cancelled = true;
return; return;
} }
@@ -47,12 +50,14 @@ public sealed class SpiderChargeSystem : EntitySystem
return; return;
// assumes warp point still exists // assumes warp point still exists
var target = Transform(role.SpiderChargeTarget.Value).MapPosition; var targetXform = Transform(role.SpiderChargeTarget.Value);
var coords = args.ClickLocation.ToMap(EntityManager, _transform); var locXform = Transform(args.Target);
if (!coords.InRange(target, comp.Range)) if (locXform.MapID != targetXform.MapID ||
(_transform.GetWorldPosition(locXform) - _transform.GetWorldPosition(targetXform)).LengthSquared() > comp.Range * comp.Range)
{ {
_popup.PopupEntity(Loc.GetString("spider-charge-too-far"), user, user); _popup.PopupEntity(Loc.GetString("spider-charge-too-far"), user, user);
args.Handled = true; args.Cancelled = true;
return;
} }
} }

View File

@@ -1,5 +1,28 @@
namespace Content.Server.Sticky.Events; namespace Content.Server.Sticky.Events;
/// <summary>
/// Risen on sticky entity to see if it can stick to another entity.
/// </summary>
[ByRefEvent]
public record struct AttemptEntityStickEvent(EntityUid Target, EntityUid User)
{
public readonly EntityUid Target = Target;
public readonly EntityUid User = User;
public bool Cancelled = false;
}
/// <summary>
/// Risen on sticky entity to see if it can unstick from another entity.
/// </summary>
[ByRefEvent]
public record struct AttemptEntityUnstickEvent(EntityUid Target, EntityUid User)
{
public readonly EntityUid Target = Target;
public readonly EntityUid User = User;
public bool Cancelled = false;
}
/// <summary> /// <summary>
/// Risen on sticky entity when it was stuck to other entity. /// Risen on sticky entity when it was stuck to other entity.
/// </summary> /// </summary>

View File

@@ -56,7 +56,7 @@ public sealed class StickySystem : EntitySystem
{ {
DoContactInteraction = true, DoContactInteraction = true,
Text = Loc.GetString("comp-sticky-unstick-verb-text"), Text = Loc.GetString("comp-sticky-unstick-verb-text"),
Icon = new SpriteSpecifier.Texture(new ("/Textures/Interface/VerbIcons/eject.svg.192dpi.png")), Icon = new SpriteSpecifier.Texture(new("/Textures/Interface/VerbIcons/eject.svg.192dpi.png")),
Act = () => StartUnsticking(uid, args.User, component) Act = () => StartUnsticking(uid, args.User, component)
}); });
} }
@@ -72,6 +72,11 @@ public sealed class StickySystem : EntitySystem
if (component.Blacklist != null && component.Blacklist.IsValid(target)) if (component.Blacklist != null && component.Blacklist.IsValid(target))
return false; return false;
var attemptEv = new AttemptEntityStickEvent(target, user);
RaiseLocalEvent(uid, ref attemptEv);
if (attemptEv.Cancelled)
return false;
// check if delay is not zero to start do after // check if delay is not zero to start do after
var delay = (float) component.StickDelay.TotalSeconds; var delay = (float) component.StickDelay.TotalSeconds;
if (delay > 0) if (delay > 0)
@@ -120,6 +125,14 @@ public sealed class StickySystem : EntitySystem
if (!Resolve(uid, ref component)) if (!Resolve(uid, ref component))
return; return;
if (component.StuckTo is not { } stuckTo)
return;
var attemptEv = new AttemptEntityUnstickEvent(stuckTo, user);
RaiseLocalEvent(uid, ref attemptEv);
if (attemptEv.Cancelled)
return;
var delay = (float) component.UnstickDelay.TotalSeconds; var delay = (float) component.UnstickDelay.TotalSeconds;
if (delay > 0) if (delay > 0)
{ {
@@ -152,6 +165,11 @@ public sealed class StickySystem : EntitySystem
if (!Resolve(uid, ref component)) if (!Resolve(uid, ref component))
return; return;
var attemptEv = new AttemptEntityStickEvent(target, user);
RaiseLocalEvent(uid, ref attemptEv);
if (attemptEv.Cancelled)
return;
// add container to entity and insert sticker into it // add container to entity and insert sticker into it
var container = _containerSystem.EnsureContainer<Container>(target, StickerSlotId); var container = _containerSystem.EnsureContainer<Container>(target, StickerSlotId);
container.ShowContents = true; container.ShowContents = true;
@@ -179,12 +197,17 @@ public sealed class StickySystem : EntitySystem
{ {
if (!Resolve(uid, ref component)) if (!Resolve(uid, ref component))
return; return;
if (component.StuckTo == null)
if (component.StuckTo is not { } stuckTo)
return;
var attemptEv = new AttemptEntityUnstickEvent(stuckTo, user);
RaiseLocalEvent(uid, ref attemptEv);
if (attemptEv.Cancelled)
return; return;
// try to remove sticky item from target container // try to remove sticky item from target container
var target = component.StuckTo.Value; if (!_containerSystem.TryGetContainer(stuckTo, StickerSlotId, out var container) || !container.Remove(uid))
if (!_containerSystem.TryGetContainer(target, StickerSlotId, out var container) || !container.Remove(uid))
return; return;
// delete container if it's now empty // delete container if it's now empty
if (container.ContainedEntities.Count == 0) if (container.ContainedEntities.Count == 0)
@@ -207,6 +230,6 @@ public sealed class StickySystem : EntitySystem
} }
component.StuckTo = null; component.StuckTo = null;
RaiseLocalEvent(uid, new EntityUnstuckEvent(target, user), true); RaiseLocalEvent(uid, new EntityUnstuckEvent(stuckTo, user), true);
} }
} }