2022-01-05 17:53:08 +13:00
|
|
|
using Content.Shared.Actions.Components;
|
|
|
|
|
using Content.Shared.Hands;
|
2022-01-31 20:07:03 +13:00
|
|
|
using Content.Shared.Inventory.Events;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.GameObjects;
|
2022-01-05 17:53:08 +13:00
|
|
|
using System;
|
2020-12-13 14:28:20 -08:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Shared.Actions
|
2020-12-13 14:28:20 -08:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Evicts action states with expired cooldowns.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class SharedActionSystem : EntitySystem
|
|
|
|
|
{
|
|
|
|
|
private const float CooldownCheckIntervalSeconds = 10;
|
|
|
|
|
private float _timeSinceCooldownCheck;
|
|
|
|
|
|
2021-12-30 03:12:04 +01:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
|
|
|
|
UpdatesOutsidePrediction = true;
|
2022-01-31 20:07:03 +13:00
|
|
|
SubscribeLocalEvent<ItemActionsComponent, GotEquippedEvent>(OnGotEquipped);
|
2022-01-05 17:53:08 +13:00
|
|
|
SubscribeLocalEvent<ItemActionsComponent, EquippedHandEvent>(OnHandEquipped);
|
2022-01-31 20:07:03 +13:00
|
|
|
SubscribeLocalEvent<ItemActionsComponent, UnequippedHandEvent>((uid, comp, _) => OnUnequipped(uid, comp));
|
|
|
|
|
SubscribeLocalEvent<ItemActionsComponent, GotUnequippedEvent>((uid, comp, _) => OnUnequipped(uid, comp));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnGotEquipped(EntityUid uid, ItemActionsComponent component, GotEquippedEvent args)
|
|
|
|
|
{
|
|
|
|
|
if (!TryComp(args.Equipee, out SharedActionsComponent? actionsComponent))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
component.Holder = args.Equipee;
|
|
|
|
|
component.HolderActionsComponent = actionsComponent;
|
|
|
|
|
component.IsEquipped = true;
|
|
|
|
|
component.GrantOrUpdateAllToHolder();
|
2022-01-05 17:53:08 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnHandEquipped(EntityUid uid, ItemActionsComponent component, EquippedHandEvent args)
|
|
|
|
|
{
|
2022-01-31 20:07:03 +13:00
|
|
|
if (!TryComp(args.User, out SharedActionsComponent? actionsComponent))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
component.Holder = args.User;
|
|
|
|
|
component.HolderActionsComponent = actionsComponent;
|
|
|
|
|
component.IsEquipped = true;
|
|
|
|
|
component.InHand = args.Hand;
|
|
|
|
|
component.GrantOrUpdateAllToHolder();
|
2022-01-05 17:53:08 +13:00
|
|
|
}
|
|
|
|
|
|
2022-01-31 20:07:03 +13:00
|
|
|
private void OnUnequipped(EntityUid uid, ItemActionsComponent component)
|
2022-01-05 17:53:08 +13:00
|
|
|
{
|
2022-01-31 20:07:03 +13:00
|
|
|
component.RevokeAllFromHolder();
|
|
|
|
|
component.Holder = null;
|
|
|
|
|
component.HolderActionsComponent = null;
|
|
|
|
|
component.IsEquipped = false;
|
|
|
|
|
component.InHand = null;
|
2021-12-30 03:12:04 +01:00
|
|
|
}
|
2020-12-13 14:28:20 -08:00
|
|
|
|
|
|
|
|
public override void Update(float frameTime)
|
|
|
|
|
{
|
|
|
|
|
base.Update(frameTime);
|
|
|
|
|
|
|
|
|
|
_timeSinceCooldownCheck += frameTime;
|
|
|
|
|
if (_timeSinceCooldownCheck < CooldownCheckIntervalSeconds) return;
|
|
|
|
|
|
2021-09-28 13:35:29 +02:00
|
|
|
foreach (var comp in EntityManager.EntityQuery<SharedActionsComponent>(false))
|
2020-12-13 14:28:20 -08:00
|
|
|
{
|
|
|
|
|
comp.ExpireCooldowns();
|
|
|
|
|
}
|
|
|
|
|
_timeSinceCooldownCheck -= CooldownCheckIntervalSeconds;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|