Add spear embedding (#18578)

* Add spear embedding

* fuck this copy-paste

* Juicier

* the river
This commit is contained in:
metalgearsloth
2023-08-04 18:56:39 +10:00
committed by GitHub
parent dda3848a98
commit 321f337572
10 changed files with 159 additions and 26 deletions

View File

@@ -435,7 +435,6 @@ public sealed partial class ExplosionSystem : EntitySystem
physics, physics,
xform, xform,
projectileQuery, projectileQuery,
tagQuery,
throwForce); throwForce);
} }

View File

@@ -1,12 +1,36 @@
using System.Numerics;
using Robust.Shared.GameStates; using Robust.Shared.GameStates;
namespace Content.Shared.Weapons.Ranged.Components; namespace Content.Shared.Projectiles;
/// <summary> /// <summary>
/// Embeds this entity inside of the hit target. /// Embeds this entity inside of the hit target.
/// </summary> /// </summary>
[RegisterComponent, NetworkedComponent] [RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
public sealed class EmbeddableProjectileComponent : Component public sealed partial class EmbeddableProjectileComponent : Component
{ {
/// <summary>
/// Minimum speed of the projectile to embed.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("minimumSpeed"), AutoNetworkedField]
public float MinimumSpeed = 5f;
/// <summary>
/// Delete the entity on embedded removal?
/// Does nothing if there's no RemovalTime.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("deleteOnRemove"), AutoNetworkedField]
public bool DeleteOnRemove;
/// <summary>
/// How long it takes to remove the embedded object.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("removalTime"), AutoNetworkedField]
public float? RemovalTime = 3f;
/// <summary>
/// How far into the entity should we offset (0 is wherever we collided).
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("offset"), AutoNetworkedField]
public Vector2 Offset = Vector2.Zero;
} }

View File

@@ -1,8 +1,14 @@
using System.Numerics; using System.Numerics;
using Content.Shared.DoAfter;
using Content.Shared.Interaction;
using Content.Shared.Projectiles; using Content.Shared.Projectiles;
using Content.Shared.Sound.Components;
using Content.Shared.Throwing;
using Content.Shared.Weapons.Ranged.Components; using Content.Shared.Weapons.Ranged.Components;
using Robust.Shared.Map; using Robust.Shared.Map;
using Robust.Shared.Network;
using Robust.Shared.Physics; using Robust.Shared.Physics;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Events; using Robust.Shared.Physics.Events;
using Robust.Shared.Physics.Systems; using Robust.Shared.Physics.Systems;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
@@ -13,6 +19,8 @@ namespace Content.Shared.Projectiles
{ {
public const string ProjectileFixture = "projectile"; public const string ProjectileFixture = "projectile";
[Dependency] private readonly INetManager _netManager = default!;
[Dependency] private readonly SharedDoAfterSystem _doAfter = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!; [Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!; [Dependency] private readonly SharedTransformSystem _transform = default!;
@@ -20,19 +28,77 @@ namespace Content.Shared.Projectiles
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<ProjectileComponent, PreventCollideEvent>(PreventCollision); SubscribeLocalEvent<ProjectileComponent, PreventCollideEvent>(PreventCollision);
SubscribeLocalEvent<EmbeddableProjectileComponent, StartCollideEvent>(OnEmbedCollide); SubscribeLocalEvent<EmbeddableProjectileComponent, ProjectileCollideEvent>(OnEmbedProjectileCollide);
SubscribeLocalEvent<EmbeddableProjectileComponent, ThrowDoHitEvent>(OnEmbedThrowDoHit);
SubscribeLocalEvent<EmbeddableProjectileComponent, ActivateInWorldEvent>(OnEmbedActivate);
SubscribeLocalEvent<EmbeddableProjectileComponent, RemoveEmbeddedProjectileEvent>(OnEmbedRemove);
} }
private void OnEmbedCollide(EntityUid uid, EmbeddableProjectileComponent component, ref StartCollideEvent args) private void OnEmbedActivate(EntityUid uid, EmbeddableProjectileComponent component, ActivateInWorldEvent args)
{ {
if (!TryComp<ProjectileComponent>(uid, out var projectile)) // Nuh uh
if (component.RemovalTime == null)
return; return;
_physics.SetLinearVelocity(uid, Vector2.Zero, body: args.OurBody); _doAfter.TryStartDoAfter(new DoAfterArgs(args.User, component.RemovalTime.Value,
_physics.SetBodyType(uid, BodyType.Static, body: args.OurBody); new RemoveEmbeddedProjectileEvent(), eventTarget: uid, target: uid)
_transform.SetParent(uid, args.OtherEntity); {
var ev = new ProjectileEmbedEvent(projectile.Shooter, projectile.Weapon, args.OtherEntity); DistanceThreshold = SharedInteractionSystem.InteractionRange,
RaiseLocalEvent(uid, ref ev); });
}
private void OnEmbedRemove(EntityUid uid, EmbeddableProjectileComponent component, RemoveEmbeddedProjectileEvent args)
{
// Whacky prediction issues.
if (args.Cancelled || _netManager.IsClient)
return;
if (component.DeleteOnRemove)
{
QueueDel(uid);
return;
}
var xform = Transform(uid);
TryComp<PhysicsComponent>(uid, out var physics);
_physics.SetBodyType(uid, BodyType.Dynamic, body: physics, xform: xform);
_transform.AttachToGridOrMap(uid, xform);
// Land it just coz uhhh yeah
var landEv = new LandEvent(args.User, true);
RaiseLocalEvent(uid, ref landEv);
_physics.WakeBody(uid, body: physics);
}
private void OnEmbedThrowDoHit(EntityUid uid, EmbeddableProjectileComponent component, ThrowDoHitEvent args)
{
Embed(uid, args.Target, component);
}
private void OnEmbedProjectileCollide(EntityUid uid, EmbeddableProjectileComponent component, ref ProjectileCollideEvent args)
{
Embed(uid, args.OtherEntity, component);
// Raise a specific event for projectiles.
if (TryComp<ProjectileComponent>(uid, out var projectile))
{
var ev = new ProjectileEmbedEvent(projectile.Shooter, projectile.Weapon, args.OtherEntity);
RaiseLocalEvent(uid, ref ev);
}
}
private void Embed(EntityUid uid, EntityUid target, 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)
{
_transform.SetLocalPosition(xform, xform.LocalPosition + xform.LocalRotation.RotateVec(component.Offset));
}
} }
private void PreventCollision(EntityUid uid, ProjectileComponent component, ref PreventCollideEvent args) private void PreventCollision(EntityUid uid, ProjectileComponent component, ref PreventCollideEvent args)
@@ -49,7 +115,13 @@ namespace Content.Shared.Projectiles
return; return;
component.Shooter = uid; component.Shooter = uid;
Dirty(component); Dirty(uid, component);
}
[Serializable, NetSerializable]
private sealed class RemoveEmbeddedProjectileEvent : DoAfterEvent
{
public override DoAfterEvent Clone() => this;
} }
} }

View File

@@ -1,5 +1,3 @@
using JetBrains.Annotations;
namespace Content.Shared.Throwing namespace Content.Shared.Throwing
{ {
/// <summary> /// <summary>

View File

@@ -0,0 +1,19 @@
using Robust.Shared.GameStates;
namespace Content.Shared.Throwing;
/// <summary>
/// When thrown applies a specific angle to the thrown entity.
/// </summary>
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
public sealed partial class ThrowingAngleComponent : Component
{
/// <summary>
/// Do we apply throwing spin to the entity.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("angularVelocity"), AutoNetworkedField]
public bool AngularVelocity;
[ViewVariables(VVAccess.ReadWrite), DataField("angle"), AutoNetworkedField]
public Angle Angle;
}

View File

@@ -75,7 +75,6 @@ public sealed class ThrowingSystem : EntitySystem
physics, physics,
Transform(uid), Transform(uid),
projectileQuery, projectileQuery,
tagQuery,
strength, strength,
user, user,
pushbackRatio, pushbackRatio,
@@ -94,7 +93,6 @@ public sealed class ThrowingSystem : EntitySystem
PhysicsComponent physics, PhysicsComponent physics,
TransformComponent transform, TransformComponent transform,
EntityQuery<ProjectileComponent> projectileQuery, EntityQuery<ProjectileComponent> projectileQuery,
EntityQuery<TagComponent> tagQuery,
float strength = 1.0f, float strength = 1.0f,
EntityUid? user = null, EntityUid? user = null,
float pushbackRatio = PushbackDefault, float pushbackRatio = PushbackDefault,
@@ -105,7 +103,7 @@ public sealed class ThrowingSystem : EntitySystem
if ((physics.BodyType & (BodyType.Dynamic | BodyType.KinematicController)) == 0x0) if ((physics.BodyType & (BodyType.Dynamic | BodyType.KinematicController)) == 0x0)
{ {
Logger.Warning($"Tried to throw entity {ToPrettyString(uid)} but can't throw {physics.BodyType} bodies!"); Log.Warning($"Tried to throw entity {ToPrettyString(uid)} but can't throw {physics.BodyType} bodies!");
return; return;
} }
@@ -114,12 +112,21 @@ public sealed class ThrowingSystem : EntitySystem
var comp = EnsureComp<ThrownItemComponent>(uid); var comp = EnsureComp<ThrownItemComponent>(uid);
comp.Thrower = user; comp.Thrower = user;
ThrowingAngleComponent? throwingAngle = null;
// Give it a l'il spin. // Give it a l'il spin.
if (physics.InvI > 0f && (!tagQuery.TryGetComponent(uid, out var tag) || !_tagSystem.HasTag(tag, "NoSpinOnThrow"))) if (physics.InvI > 0f && (!TryComp(uid, out throwingAngle) || throwingAngle.AngularVelocity))
{
_physics.ApplyAngularImpulse(uid, ThrowAngularImpulse / physics.InvI, body: physics); _physics.ApplyAngularImpulse(uid, ThrowAngularImpulse / physics.InvI, body: physics);
}
else else
transform.LocalRotation = direction.ToWorldAngle() - Math.PI; {
Resolve(uid, ref throwingAngle, false);
var gridRot = _transform.GetWorldRotation(transform.ParentUid);
var angle = direction.ToWorldAngle() - gridRot;
var offset = throwingAngle?.Angle ?? Angle.Zero;
_transform.SetLocalRotation(uid, angle + offset);
}
if (user != null) if (user != null)
_interactionSystem.ThrownInteraction(user.Value, uid); _interactionSystem.ThrownInteraction(user.Value, uid);

View File

@@ -25,7 +25,6 @@
count: 4 count: 4
- type: Tag - type: Tag
tags: tags:
- NoSpinOnThrow
- Pie - Pie
- type: entity - type: entity
@@ -50,9 +49,6 @@
Quantity: 1.2 Quantity: 1.2
- ReagentId: Vitamin - ReagentId: Vitamin
Quantity: 1 Quantity: 1
- type: Tag
tags:
- NoSpinOnThrow
# Pie # Pie

View File

@@ -705,7 +705,9 @@
noSpawn: true noSpawn: true
components: components:
- type: EmbeddableProjectile - type: EmbeddableProjectile
deleteOnRemove: true
- type: Clickable - type: Clickable
- type: InteractionOutline
- type: Sprite - type: Sprite
noRot: false noRot: false
sprite: Objects/Weapons/Guns/Launchers/grappling_gun.rsi sprite: Objects/Weapons/Guns/Launchers/grappling_gun.rsi

View File

@@ -4,9 +4,27 @@
id: Spear id: Spear
description: Definition of a Classic. Keeping murder affordable since 200,000 BCE. description: Definition of a Classic. Keeping murder affordable since 200,000 BCE.
components: components:
- type: EmbeddableProjectile
offset: 0.15,0.15
- type: ThrowingAngle
angle: 225
- type: Tag - type: Tag
tags: tags:
- Spear - Spear
- type: Fixtures
fixtures:
fix1:
shape: !type:PolygonShape
vertices:
- -0.40,-0.30
- -0.30,-0.40
- 0.40,0.30
- 0.30,0.40
density: 20
mask:
- ItemMask
restitution: 0.3
friction: 0.2
- type: Sharp - type: Sharp
- type: Sprite - type: Sprite
sprite: Objects/Weapons/Melee/spear.rsi sprite: Objects/Weapons/Melee/spear.rsi

View File

@@ -589,8 +589,6 @@
- type: Tag - type: Tag
id: Multitool id: Multitool
- type: Tag
id: NoSpinOnThrow
- type: Tag - type: Tag
id: NoBlockAnchoring id: NoBlockAnchoring