Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Content.Client/Actions/ActionsSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@ public override void Initialize()
SubscribeLocalEvent<EntityWorldTargetActionComponent, ComponentHandleState>(OnEntityWorldTargetHandleState);
}

public override void FrameUpdate(float frameTime)
{
base.FrameUpdate(frameTime);

var worldActionQuery = EntityQueryEnumerator<WorldTargetActionComponent>();
while (worldActionQuery.MoveNext(out var uid, out var action))
{
UpdateAction(uid, action);
}

var instantActionQuery = EntityQueryEnumerator<InstantActionComponent>();
while (instantActionQuery.MoveNext(out var uid, out var action))
{
UpdateAction(uid, action);
}

var entityActionQuery = EntityQueryEnumerator<EntityTargetActionComponent>();
while (entityActionQuery.MoveNext(out var uid, out var action))
{
UpdateAction(uid, action);
}
}

private void OnInstantHandleState(EntityUid uid, InstantActionComponent component, ref ComponentHandleState args)
{
if (args.Current is not InstantActionComponentState state)
Expand Down Expand Up @@ -95,6 +118,8 @@ private void BaseHandleState<T>(EntityUid uid, BaseActionComponent component, Ba
component.Icon = state.Icon;
component.IconOn = state.IconOn;
component.IconColor = state.IconColor;
component.OriginalIconColor = state.OriginalIconColor;
component.DisabledIconColor = state.DisabledIconColor;
component.Keywords.Clear();
component.Keywords.UnionWith(state.Keywords);
component.Enabled = state.Enabled;
Expand Down Expand Up @@ -125,6 +150,8 @@ public override void UpdateAction(EntityUid? actionId, BaseActionComponent? acti
if (!ResolveActionData(actionId, ref action))
return;

action.IconColor = action.Charges < 1 ? action.DisabledIconColor : action.OriginalIconColor;

base.UpdateAction(actionId, action);
if (_playerManager.LocalEntity != action.AttachedEntity)
return;
Expand Down
14 changes: 14 additions & 0 deletions Content.Shared/Actions/BaseActionComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ public abstract partial class BaseActionComponent : Component
/// </remarks>
[DataField("iconColor")] public Color IconColor = Color.White;

/// <summary>
/// The original <see cref="IconColor"/> this action was.
/// </summary>
[DataField] public Color OriginalIconColor;

/// <summary>
/// The color the action should turn to when disabled
/// </summary>
[DataField] public Color DisabledIconColor = Color.DimGray;

/// <summary>
/// Keywords that can be used to search for this action in the action menu.
/// </summary>
Expand Down Expand Up @@ -179,6 +189,8 @@ public abstract class BaseActionComponentState : ComponentState
public SpriteSpecifier? Icon;
public SpriteSpecifier? IconOn;
public Color IconColor;
public Color OriginalIconColor;
public Color DisabledIconColor;
public HashSet<string> Keywords;
public bool Enabled;
public bool Toggled;
Expand Down Expand Up @@ -209,6 +221,8 @@ protected BaseActionComponentState(BaseActionComponent component, IEntityManager
Icon = component.Icon;
IconOn = component.IconOn;
IconColor = component.IconColor;
OriginalIconColor = component.OriginalIconColor;
DisabledIconColor = component.DisabledIconColor;
Keywords = component.Keywords;
Enabled = component.Enabled;
Toggled = component.Toggled;
Expand Down
64 changes: 58 additions & 6 deletions Content.Shared/Actions/SharedActionsSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,42 @@ public override void Initialize()
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;

Expand Down Expand Up @@ -326,14 +360,18 @@ public void RemoveCharges(EntityUid? actionId, int? removeCharges)
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)
Expand Down Expand Up @@ -386,13 +424,12 @@ private void OnActionRequest(RequestPerformActionEvent ev, EntitySessionEventArg
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;

Expand Down Expand Up @@ -1072,4 +1109,19 @@ public void SetEntityIcon(EntityUid uid, EntityUid? icon, BaseActionComponent? a
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 };
}
}
Loading