Skip to content

Commit

Permalink
Wow lot more fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
beck-thompson committed Nov 9, 2024
1 parent af587b7 commit dd53309
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 76 deletions.
13 changes: 0 additions & 13 deletions Content.Shared/ReagentOnItem/Components/ReagentOnItemComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,3 @@ public abstract partial class ReagentOnItemComponent : Component
[DataField]
public FixedPoint2 MaxStacks = 15;
}

[Serializable, NetSerializable]
public sealed class ReagentOnItemComponentState : ComponentState
{
public readonly FixedPoint2 EffectStacks;
public readonly FixedPoint2 MaxStacks;

public ReagentOnItemComponentState(FixedPoint2 effectStacks, FixedPoint2 maxStacks)
{
EffectStacks = effectStacks;
MaxStacks = maxStacks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Content.Shared.ReagentOnItem;

[RegisterComponent, NetworkedComponent]
[RegisterComponent, NetworkedComponent, AutoGenerateComponentPause]
public sealed partial class SpaceLubeOnItemComponent : ReagentOnItemComponent
{

Expand All @@ -13,9 +13,24 @@ public sealed partial class SpaceLubeOnItemComponent : ReagentOnItemComponent
public double ChanceToDecreaseReagentOnGrab = .45;

/// <summary>
/// How far will the item be thrown when someone tries to pick it up
/// while it has lube applied to it.
/// How far will the item be thrown when someone tries to pick it up while it has lube applied to it.
/// </summary>
[DataField]
public float PowerOfThrowOnPickup = 10f;
public float PowerOfThrowOnPickup = 5f;

/// <summary>
/// Time that the item can't be picked up after someone tries to grab it.
/// </summary>
/// <remarks>
/// If this is too low, you can try to pick the item up multiple times in "one" click. Do not lower it without
/// testing!
/// </remarks>
[DataField]
public TimeSpan PickupCooldown = TimeSpan.FromSeconds(0.5);

/// <summary>
/// The last time someone tried to pick up the lubed item.
/// </summary>
[DataField, AutoPausedField]
public TimeSpan LastTimeAttemptedPickup;
}
22 changes: 2 additions & 20 deletions Content.Shared/ReagentOnItem/Systems/SpaceGlueOnItemSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,15 @@ public override void Initialize()

SubscribeLocalEvent<SpaceGlueOnItemComponent, GotEquippedHandEvent>(OnHandPickUp);
SubscribeLocalEvent<SpaceGlueOnItemComponent, ExaminedEvent>(OnExamine);
SubscribeLocalEvent<SpaceGlueOnItemComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<SpaceGlueOnItemComponent, MapInitEvent>(MapInit);
SubscribeLocalEvent<SpaceGlueOnItemComponent, RefreshNameModifiersEvent>(OnRefreshNameModifiers);

SubscribeLocalEvent<SpaceGlueOnItemComponent, ComponentGetState>(GetSpaceGlueState);
SubscribeLocalEvent<SpaceGlueOnItemComponent, ComponentHandleState>(HandleSpaceGlueState);
}

private void OnInit(EntityUid uid, SpaceGlueOnItemComponent component, ComponentInit args)
private void MapInit(EntityUid uid, SpaceGlueOnItemComponent component, MapInitEvent args)
{
_nameMod.RefreshNameModifiers(uid);
}


public override void Update(float frameTime)
{
base.Update(frameTime);
Expand Down Expand Up @@ -115,18 +111,4 @@ private void OnRefreshNameModifiers(Entity<SpaceGlueOnItemComponent> entity, ref
{
args.AddModifier("glued-name-prefix");
}

private void HandleSpaceGlueState(EntityUid uid, SpaceGlueOnItemComponent component, ref ComponentHandleState args)
{
if (args.Current is not ReagentOnItemComponentState state)
return;

component.EffectStacks = state.EffectStacks;
component.MaxStacks = state.MaxStacks;
}

private void GetSpaceGlueState(EntityUid uid, SpaceGlueOnItemComponent component, ref ComponentGetState args)
{
args.State = new ReagentOnItemComponentState(component.EffectStacks, component.MaxStacks);
}
}
89 changes: 50 additions & 39 deletions Content.Shared/ReagentOnItem/Systems/SpaceLubeOnItemSystem.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Numerics;
using Content.Shared.IdentityManagement;
using Content.Shared.Popups;
using Content.Shared.Throwing;
Expand All @@ -8,31 +9,33 @@
using Robust.Shared.GameStates;
using Content.Shared.Hands;
using Content.Shared.NameModifier.EntitySystems;
using Robust.Shared.Containers;
using Robust.Shared.Network;
using Robust.Shared.Timing;

namespace Content.Shared.ReagentOnItem;

public sealed class SpaceLubeOnItemSystem : EntitySystem
{
[Dependency] private readonly ThrowingSystem _throwing = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly InventorySystem _inventory = default!;
[Dependency] private readonly SharedTransformSystem _xform = default!;
[Dependency] private readonly NameModifierSystem _nameMod = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly INetManager _net = default!;
[Dependency] private readonly SharedContainerSystem _container = default!;

public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SpaceLubeOnItemComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<SpaceLubeOnItemComponent, MapInitEvent>(MapInit);
SubscribeLocalEvent<SpaceLubeOnItemComponent, GotAttemptedHandPickupEvent>(OnHandPickUp);
SubscribeLocalEvent<SpaceLubeOnItemComponent, ExaminedEvent>(OnExamine);
SubscribeLocalEvent<SpaceLubeOnItemComponent, RefreshNameModifiersEvent>(OnRefreshNameModifiers);

SubscribeLocalEvent<SpaceLubeOnItemComponent, ComponentGetState>(GetSpaceLubeState);
SubscribeLocalEvent<SpaceLubeOnItemComponent, ComponentHandleState>(HandleSpaceLubeState);
}

private void OnInit(EntityUid uid, SpaceLubeOnItemComponent component, ComponentInit args)
private void MapInit(EntityUid uid, SpaceLubeOnItemComponent component, MapInitEvent args)
{
_nameMod.RefreshNameModifiers(uid);
}
Expand All @@ -53,58 +56,66 @@ private void OnHandPickUp(EntityUid uid, SpaceLubeOnItemComponent component, Got
if (HasComp<NonStickSurfaceComponent>(gloves))
return;

if (component.EffectStacks < 1)
{
RemComp<SpaceLubeOnItemComponent>(uid);
_nameMod.RefreshNameModifiers(uid);
args.Cancel();

// I tried so hard but this has got to be server side only trust me
if (!_net.IsServer)
return;
}

args.Cancel();
if (component.LastTimeAttemptedPickup + component.PickupCooldown > _timing.CurTime)
return;

component.LastTimeAttemptedPickup = _timing.CurTime;

// This is so if throwing ever gets predicted it won't lag (Or any of this gets predicted...).
var rand = new System.Random((int)_timing.CurTick.Value);

var randDouble = _random.NextDouble();
var randDouble = rand.NextDouble();
if (randDouble > 1 - component.ChanceToDecreaseReagentOnGrab)
{
component.EffectStacks -= 1;
}

var worldPos = _xform.GetWorldPosition(Transform(entityWhoPickedUp));
var itemPosition = _xform.GetWorldPosition(Transform(uid));
var vecFromObjectToEnt = worldPos - itemPosition;

// This calculates the spread of the crowbar so it won't go in a straight line to the entity picking it up!
var randNegPosOne = 2 * _random.NextDouble() - 1;
var spread = Math.PI / 5;
var rotation = new Angle(spread * randNegPosOne);
if (_container.TryRemoveFromContainer(uid, true, out var wasInContainer) || !wasInContainer)
{
var worldPos = _xform.GetWorldPosition(Transform(entityWhoPickedUp));
var itemPosition = _xform.GetWorldPosition(Transform(uid));
var vecFromObjectToEnt = worldPos - itemPosition;

Vector2 throwDirection;
if (vecFromObjectToEnt == Vector2.Zero)
{
// Just throw it a random direction!
throwDirection = new Vector2(rand.NextFloat(-1,1), rand.NextFloat(-1,1));
}
else
{
// This calculates the spread of the crowbar so it won't go in a straight line to the entity picking it up!
var randNegPosOne = 2 * rand.NextDouble() - 1;
var spread = Math.PI / 5;
var rotation = new Angle(spread * randNegPosOne);
throwDirection = rotation.RotateVec(vecFromObjectToEnt);
}

_throwing.TryThrow(uid, throwDirection.Normalized(), baseThrowSpeed: component.PowerOfThrowOnPickup);
_popup.PopupEntity(Loc.GetString("space-lube-on-item-slip", ("target", Identity.Entity(uid, EntityManager))), entityWhoPickedUp, entityWhoPickedUp, PopupType.MediumCaution);
}

_throwing.TryThrow(uid, rotation.RotateVec(vecFromObjectToEnt.Normalized()), baseThrowSpeed: component.PowerOfThrowOnPickup);
_popup.PopupPredicted(Loc.GetString("space-lube-on-item-slip", ("target", Identity.Entity(uid, EntityManager))), entityWhoPickedUp, entityWhoPickedUp, PopupType.MediumCaution);
if (component.EffectStacks < 1)
{
RemComp<SpaceLubeOnItemComponent>(uid);
_nameMod.RefreshNameModifiers(uid);
}
}

private void OnExamine(EntityUid uid, SpaceLubeOnItemComponent comp, ExaminedEvent args)
{
if (args.IsInDetailsRange)
{
args.PushMarkup(Loc.GetString("space-lube-on-item-inspect"));
}
}

private void OnRefreshNameModifiers(Entity<SpaceLubeOnItemComponent> entity, ref RefreshNameModifiersEvent args)
{
args.AddModifier("lubed-name-prefix");
}

private void HandleSpaceLubeState(EntityUid uid, SpaceLubeOnItemComponent component, ref ComponentHandleState args)
{
if (args.Current is not ReagentOnItemComponentState state)
return;

component.EffectStacks = state.EffectStacks;
component.MaxStacks = state.MaxStacks;
}

private void GetSpaceLubeState(EntityUid uid, SpaceLubeOnItemComponent component, ref ComponentGetState args)
{
args.State = new ReagentOnItemComponentState(component.EffectStacks, component.MaxStacks);
}
}

0 comments on commit dd53309

Please sign in to comment.