Adds real-time charge & disabled action information to Actions (#31821)

This commit is contained in:
keronshb
2024-09-25 10:27:28 -04:00
committed by GitHub
parent 0dedc9d91a
commit 7168959929
3 changed files with 99 additions and 6 deletions

View File

@@ -69,8 +69,42 @@ public abstract class SharedActionsSystem : EntitySystem
SubscribeAllEvent<RequestPerformActionEvent>(OnActionRequest);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var worldActionQuery = EntityQueryEnumerator<WorldTargetActionComponent>();
while (worldActionQuery.MoveNext(out var uid, out var action))
{
if (IsCooldownActive(action) || !ShouldResetCharges(action))
continue;
ResetCharges(uid, dirty: true);
}
var instantActionQuery = EntityQueryEnumerator<InstantActionComponent>();
while (instantActionQuery.MoveNext(out var uid, out var action))
{
if (IsCooldownActive(action) || !ShouldResetCharges(action))
continue;
ResetCharges(uid, dirty: true);
}
var entityActionQuery = EntityQueryEnumerator<EntityTargetActionComponent>();
while (entityActionQuery.MoveNext(out var uid, out var action))
{
if (IsCooldownActive(action) || !ShouldResetCharges(action))
continue;
ResetCharges(uid, dirty: true);
}
}
private void OnActionMapInit(EntityUid uid, BaseActionComponent component, MapInitEvent args)
{
component.OriginalIconColor = component.IconColor;
if (component.Charges == null)
return;
@@ -326,14 +360,18 @@ public abstract class SharedActionsSystem : EntitySystem
Dirty(actionId.Value, action);
}
public void ResetCharges(EntityUid? actionId)
public void ResetCharges(EntityUid? actionId, bool update = false, bool dirty = false)
{
if (!TryGetActionData(actionId, out var action))
return;
action.Charges = action.MaxCharges;
UpdateAction(actionId, action);
Dirty(actionId.Value, action);
if (update)
UpdateAction(actionId, action);
if (dirty)
Dirty(actionId.Value, action);
}
private void OnActionsGetState(EntityUid uid, ActionsComponent component, ref ComponentGetState args)
@@ -386,13 +424,12 @@ public abstract class SharedActionsSystem : EntitySystem
return;
var curTime = GameTiming.CurTime;
// TODO: Check for charge recovery timer
if (action.Cooldown.HasValue && action.Cooldown.Value.End > curTime)
if (IsCooldownActive(action, curTime))
return;
// TODO: Replace with individual charge recovery when we have the visuals to aid it
if (action is { Charges: < 1, RenewCharges: true })
ResetCharges(actionEnt);
ResetCharges(actionEnt, true, true);
BaseActionEvent? performEvent = null;
@@ -1072,4 +1109,19 @@ public abstract class SharedActionsSystem : EntitySystem
action.EntityIcon = icon;
Dirty(uid, action);
}
/// <summary>
/// Checks if the action has a cooldown and if it's still active
/// </summary>
protected bool IsCooldownActive(BaseActionComponent action, TimeSpan? curTime = null)
{
curTime ??= GameTiming.CurTime;
// TODO: Check for charge recovery timer
return action.Cooldown.HasValue && action.Cooldown.Value.End > curTime;
}
protected bool ShouldResetCharges(BaseActionComponent action)
{
return action is { Charges: < 1, RenewCharges: true };
}
}