Files
crystall-punk-14/Content.Shared/Projectiles/SharedProjectileSystem.cs
Ed 9d6e023bd0 Merge remote-tracking branch 'upstream/stable' into ed-311-03-2025-upstream-2
# Conflicts:
#	Content.Client/Administration/AdminNameOverlay.cs
#	Content.Client/Administration/UI/Bwoink/BwoinkControl.xaml
#	Content.Client/Guidebook/Controls/GuideReagentReaction.xaml
#	Content.Client/Options/UI/Tabs/KeyRebindTab.xaml.cs
#	Content.Client/SubFloor/SubFloorHideSystem.cs
#	Content.Server/Administration/Systems/AdminVerbSystem.Antags.cs
#	Content.Server/Antag/AntagSelectionSystem.cs
#	Content.Server/Cloning/CloningSystem.cs
#	Content.Server/GameTicking/Rules/Components/ParadoxCloneRuleComponent.cs
#	Content.Server/GameTicking/Rules/ParadoxCloneRuleSystem.cs
#	Content.Server/Roles/ParadoxCloneRoleComponent.cs
#	Content.Shared.Database/LogType.cs
#	Content.Shared/CCVar/CCVars.Interface.cs
#	Content.Shared/Cloning/CloningEvents.cs
#	Content.Shared/Cloning/CloningSettingsPrototype.cs
#	Content.Shared/Humanoid/NamingSystem.cs
#	Content.Shared/Humanoid/Prototypes/SpeciesPrototype.cs
#	Content.Shared/Light/Components/SunShadowCycleComponent.cs
#	Content.Shared/Storage/StorageComponent.cs
#	Resources/Changelog/Admin.yml
#	Resources/Changelog/Changelog.yml
#	Resources/Credits/GitHub.txt
#	Resources/Locale/en-US/paradox-clone/role.ftl
#	Resources/Maps/bagel.yml
#	Resources/Maps/loop.yml
#	Resources/Prototypes/Chemistry/mixing_types.yml
#	Resources/Prototypes/Datasets/Names/last.yml
#	Resources/Prototypes/Entities/Effects/puddle.yml
#	Resources/Prototypes/Entities/Mobs/Player/clone.yml
#	Resources/Prototypes/Entities/Mobs/Species/base.yml
#	Resources/Prototypes/Entities/Objects/Deliveries/deliveries_tables.yml
#	Resources/Prototypes/Entities/Objects/Devices/pda.yml
#	Resources/Prototypes/Entities/Objects/Tools/handheld_mass_scanner.yml
#	Resources/Prototypes/GameRules/events.yml
#	Resources/Prototypes/Maps/Pools/default.yml
#	Resources/Prototypes/Objectives/paradoxClone.yml
#	Resources/Prototypes/Reagents/Consumable/Drink/alcohol.yml
#	Resources/Textures/Clothing/Eyes/Glasses/jensen.rsi/equipped-EYES-arachnid.png
2025-03-31 12:41:37 +03:00

238 lines
8.4 KiB
C#

using System.Numerics;
using Content.Shared.CombatMode.Pacification;
using Content.Shared.Damage;
using Content.Shared.DoAfter;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Interaction;
using Content.Shared.Mobs.Components;
using Content.Shared.Throwing;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Map;
using Robust.Shared.Network;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Dynamics;
using Robust.Shared.Physics.Events;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
namespace Content.Shared.Projectiles;
public abstract partial class SharedProjectileSystem : EntitySystem
{
public const string ProjectileFixture = "projectile";
[Dependency] private readonly INetManager _net = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedDoAfterSystem _doAfter = default!;
[Dependency] private readonly SharedHandsSystem _hands = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ProjectileComponent, PreventCollideEvent>(PreventCollision);
SubscribeLocalEvent<EmbeddableProjectileComponent, ProjectileHitEvent>(OnEmbedProjectileHit);
SubscribeLocalEvent<EmbeddableProjectileComponent, ThrowDoHitEvent>(OnEmbedThrowDoHit);
SubscribeLocalEvent<EmbeddableProjectileComponent, ActivateInWorldEvent>(OnEmbedActivate);
SubscribeLocalEvent<EmbeddableProjectileComponent, RemoveEmbeddedProjectileEvent>(OnEmbedRemove);
SubscribeLocalEvent<EmbeddedContainerComponent, EntityTerminatingEvent>(OnEmbeddableTermination);
}
private void OnEmbedActivate(Entity<EmbeddableProjectileComponent> embeddable, ref ActivateInWorldEvent args)
{
// Unremovable embeddables moment
if (embeddable.Comp.RemovalTime == null)
return;
if (args.Handled || !args.Complex || !TryComp<PhysicsComponent>(embeddable, out var physics) ||
physics.BodyType != BodyType.Static)
return;
args.Handled = true;
_doAfter.TryStartDoAfter(new DoAfterArgs(EntityManager,
args.User,
embeddable.Comp.RemovalTime.Value,
new RemoveEmbeddedProjectileEvent(),
eventTarget: embeddable,
target: embeddable));
}
private void OnEmbedRemove(Entity<EmbeddableProjectileComponent> embeddable, ref RemoveEmbeddedProjectileEvent args)
{
// Whacky prediction issues.
if (args.Cancelled || _net.IsClient)
return;
EmbedDetach(embeddable, embeddable.Comp, args.User);
// try place it in the user's hand
_hands.TryPickupAnyHand(args.User, embeddable);
}
private void OnEmbedThrowDoHit(Entity<EmbeddableProjectileComponent> embeddable, ref ThrowDoHitEvent args)
{
if (!embeddable.Comp.EmbedOnThrow)
return;
EmbedAttach(embeddable, args.Target, null, embeddable.Comp);
}
private void OnEmbedProjectileHit(Entity<EmbeddableProjectileComponent> embeddable, ref ProjectileHitEvent args)
{
EmbedAttach(embeddable, args.Target, args.Shooter, embeddable.Comp);
// Raise a specific event for projectiles.
if (TryComp(embeddable, out ProjectileComponent? projectile) && projectile.Shooter != null && projectile.Weapon != null) //CP14 Null safe checks
{
var ev = new ProjectileEmbedEvent(projectile.Shooter.Value, projectile.Weapon.Value, args.Target);
RaiseLocalEvent(embeddable, ref ev);
}
}
private void EmbedAttach(EntityUid uid, EntityUid target, EntityUid? user, EmbeddableProjectileComponent component)
{
TryComp<PhysicsComponent>(uid, out var physics);
_physics.SetLinearVelocity(uid, Vector2.Zero, body: physics);
_physics.SetBodyType(uid, BodyType.Static, body: physics);
var xform = Transform(uid);
_transform.SetParent(uid, xform, target);
if (component.Offset != Vector2.Zero)
{
var rotation = xform.LocalRotation;
if (TryComp<ThrowingAngleComponent>(uid, out var throwingAngleComp))
rotation += throwingAngleComp.Angle;
_transform.SetLocalPosition(uid, xform.LocalPosition + rotation.RotateVec(component.Offset), xform);
}
_audio.PlayPredicted(component.Sound, uid, null);
component.EmbeddedIntoUid = target;
var ev = new EmbedEvent(user, target);
RaiseLocalEvent(uid, ref ev);
Dirty(uid, component);
EnsureComp<EmbeddedContainerComponent>(target, out var embeddedContainer);
//Assert that this entity not embed
DebugTools.AssertEqual(embeddedContainer.EmbeddedObjects.Contains(uid), false);
embeddedContainer.EmbeddedObjects.Add(uid);
}
public void EmbedDetach(EntityUid uid, EmbeddableProjectileComponent? component, EntityUid? user = null)
{
if (!Resolve(uid, ref component))
return;
if (component.DeleteOnRemove)
{
QueueDel(uid);
return;
}
if (component.EmbeddedIntoUid is not null)
{
if (TryComp<EmbeddedContainerComponent>(component.EmbeddedIntoUid.Value, out var embeddedContainer))
embeddedContainer.EmbeddedObjects.Remove(uid);
}
var xform = Transform(uid);
if (TerminatingOrDeleted(xform.GridUid) && TerminatingOrDeleted(xform.MapUid))
return;
TryComp<PhysicsComponent>(uid, out var physics);
_physics.SetBodyType(uid, BodyType.Dynamic, body: physics, xform: xform);
_transform.AttachToGridOrMap(uid, xform);
component.EmbeddedIntoUid = null;
Dirty(uid, component);
// Reset whether the projectile has damaged anything if it successfully was removed
if (TryComp<ProjectileComponent>(uid, out var projectile))
{
projectile.Shooter = null;
projectile.Weapon = null;
projectile.ProjectileSpent = false;
Dirty(uid, projectile);
}
if (user != null)
{
// Land it just coz uhhh yeah
var landEv = new LandEvent(user, true);
RaiseLocalEvent(uid, ref landEv);
}
_physics.WakeBody(uid, body: physics);
}
private void OnEmbeddableTermination(Entity<EmbeddedContainerComponent> container, ref EntityTerminatingEvent args)
{
DetachAllEmbedded(container);
}
public void DetachAllEmbedded(Entity<EmbeddedContainerComponent> container)
{
foreach (var embedded in container.Comp.EmbeddedObjects)
{
if (!TryComp<EmbeddableProjectileComponent>(embedded, out var embeddedComp))
continue;
EmbedDetach(embedded, embeddedComp);
}
}
private void PreventCollision(EntityUid uid, ProjectileComponent component, ref PreventCollideEvent args)
{
if (component.IgnoreShooter && (args.OtherEntity == component.Shooter || args.OtherEntity == component.Weapon))
{
args.Cancelled = true;
}
}
public void SetShooter(EntityUid id, ProjectileComponent component, EntityUid shooterId)
{
if (component.Shooter == shooterId)
return;
component.Shooter = shooterId;
Dirty(id, component);
}
[Serializable, NetSerializable]
private sealed partial class RemoveEmbeddedProjectileEvent : DoAfterEvent
{
public override DoAfterEvent Clone() => this;
}
}
[Serializable, NetSerializable]
public sealed class ImpactEffectEvent : EntityEventArgs
{
public string Prototype;
public NetCoordinates Coordinates;
public ImpactEffectEvent(string prototype, NetCoordinates coordinates)
{
Prototype = prototype;
Coordinates = coordinates;
}
}
/// <summary>
/// Raised when an entity is just about to be hit with a projectile but can reflect it
/// </summary>
[ByRefEvent]
public record struct ProjectileReflectAttemptEvent(EntityUid ProjUid, ProjectileComponent Component, bool Cancelled);
/// <summary>
/// Raised when a projectile hits an entity
/// </summary>
[ByRefEvent]
public record struct ProjectileHitEvent(DamageSpecifier Damage, EntityUid Target, EntityUid? Shooter = null);