Cooldown for status effects (#1109)

This commit is contained in:
Víctor Aguilera Puerto
2020-06-12 16:22:36 +02:00
committed by GitHub
parent 1587efbe29
commit cb5acf7cd3
9 changed files with 163 additions and 59 deletions

View File

@@ -7,12 +7,15 @@ using Robust.Client.GameObjects;
using Robust.Client.Interfaces.ResourceManagement; using Robust.Client.Interfaces.ResourceManagement;
using Robust.Client.Interfaces.UserInterface; using Robust.Client.Interfaces.UserInterface;
using Robust.Client.Player; using Robust.Client.Player;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls; using Robust.Client.UserInterface.Controls;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Network; using Robust.Shared.Interfaces.Network;
using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.Log; using Robust.Shared.Log;
using Robust.Shared.Maths;
using Robust.Shared.Players; using Robust.Shared.Players;
namespace Content.Client.GameObjects.Components.Mobs namespace Content.Client.GameObjects.Components.Mobs
@@ -25,10 +28,12 @@ namespace Content.Client.GameObjects.Components.Mobs
[Dependency] private readonly IPlayerManager _playerManager; [Dependency] private readonly IPlayerManager _playerManager;
[Dependency] private readonly IResourceCache _resourceCache; [Dependency] private readonly IResourceCache _resourceCache;
[Dependency] private readonly IUserInterfaceManager _userInterfaceManager; [Dependency] private readonly IUserInterfaceManager _userInterfaceManager;
[Dependency] private readonly IGameTiming _gameTiming;
#pragma warning restore 649 #pragma warning restore 649
private StatusEffectsUI _ui; private StatusEffectsUI _ui;
private IDictionary<StatusEffect, string> _icons = new Dictionary<StatusEffect, string>(); private Dictionary<StatusEffect, StatusEffectStatus> _status = new Dictionary<StatusEffect, StatusEffectStatus>();
private Dictionary<StatusEffect, CooldownGraphic> _cooldown = new Dictionary<StatusEffect, CooldownGraphic>();
/// <summary> /// <summary>
/// Allows calculating if we need to act due to this component being controlled by the current mob /// Allows calculating if we need to act due to this component being controlled by the current mob
@@ -58,9 +63,9 @@ namespace Content.Client.GameObjects.Components.Mobs
public override void HandleComponentState(ComponentState curState, ComponentState nextState) public override void HandleComponentState(ComponentState curState, ComponentState nextState)
{ {
base.HandleComponentState(curState, nextState); base.HandleComponentState(curState, nextState);
if (!(curState is StatusEffectComponentState state) || _icons == state.StatusEffects) return; if (!(curState is StatusEffectComponentState state) || _status == state.StatusEffects) return;
_icons = state.StatusEffects; _status = state.StatusEffects;
UpdateIcons(); UpdateStatusEffects();
} }
private void PlayerAttached() private void PlayerAttached()
@@ -71,7 +76,7 @@ namespace Content.Client.GameObjects.Components.Mobs
} }
_ui = new StatusEffectsUI(); _ui = new StatusEffectsUI();
_userInterfaceManager.StateRoot.AddChild(_ui); _userInterfaceManager.StateRoot.AddChild(_ui);
UpdateIcons(); UpdateStatusEffects();
} }
private void PlayerDetached() private void PlayerDetached()
@@ -80,32 +85,68 @@ namespace Content.Client.GameObjects.Components.Mobs
_ui = null; _ui = null;
} }
public void UpdateIcons() public void UpdateStatusEffects()
{ {
if (!CurrentlyControlled || _ui == null) if (!CurrentlyControlled || _ui == null)
{ {
return; return;
} }
_cooldown.Clear();
_ui.VBox.DisposeAllChildren(); _ui.VBox.DisposeAllChildren();
foreach (var effect in _icons.OrderBy(x => (int) x.Key)) foreach (var (key, statusEffect) in _status.OrderBy(x => (int) x.Key))
{ {
TextureRect newIcon = new TextureRect var status = new Control()
{ {
TextureScale = (2, 2), Children =
Texture = _resourceCache.GetTexture(effect.Value) {
new TextureRect
{
TextureScale = (2, 2),
Texture = _resourceCache.GetTexture(statusEffect.Icon)
},
}
}; };
newIcon.Texture = _resourceCache.GetTexture(effect.Value); if (statusEffect.Cooldown.HasValue)
_ui.VBox.AddChild(newIcon); {
var cooldown = new CooldownGraphic();
status.Children.Add(cooldown);
_cooldown[key] = cooldown;
}
_ui.VBox.AddChild(status);
} }
} }
public void RemoveIcon(StatusEffect name) public void RemoveStatusEffect(StatusEffect name)
{ {
_icons.Remove(name); _status.Remove(name);
UpdateIcons(); UpdateStatusEffects();
Logger.InfoS("statuseffects", $"Removed icon {name}"); }
public void FrameUpdate(float frameTime)
{
foreach (var (effect, cooldownGraphic) in _cooldown)
{
var status = _status[effect];
if (!status.Cooldown.HasValue)
{
cooldownGraphic.Progress = 0;
cooldownGraphic.Visible = false;
continue;
}
var start = status.Cooldown.Value.Item1;
var end = status.Cooldown.Value.Item2;
var length = (end - start).TotalSeconds;
var progress = (_gameTiming.CurTime - start).TotalSeconds / length;
var ratio = (progress <= 1 ? (1 - progress) : (_gameTiming.CurTime - end).TotalSeconds * -5);
cooldownGraphic.Progress = (float)ratio.Clamp(-1, 1);
cooldownGraphic.Visible = ratio > -1f;
}
} }
} }
} }

View File

@@ -0,0 +1,34 @@
using Content.Client.GameObjects.Components.Mobs;
using Content.Shared.GameObjects.Components.Mobs;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC;
namespace Content.Client.GameObjects.EntitySystems
{
public class StatusEffectsSystem : EntitySystem
{
#pragma warning disable 649
[Dependency] private IGameTiming _gameTiming;
#pragma warning restore 649
public StatusEffectsSystem()
{
EntityQuery = new TypeEntityQuery(typeof(ClientStatusEffectsComponent));
}
public override void FrameUpdate(float frameTime)
{
base.FrameUpdate(frameTime);
if (!_gameTiming.IsFirstTimePredicted)
return;
foreach (var entity in RelevantEntities)
{
entity.GetComponent<ClientStatusEffectsComponent>().FrameUpdate(frameTime);
}
}
}
}

View File

@@ -66,21 +66,21 @@ namespace Content.Server.GameObjects
throw new InvalidOperationException(); //these should all be below the crit value, possibly going over multiple thresholds at once? throw new InvalidOperationException(); //these should all be below the crit value, possibly going over multiple thresholds at once?
} }
var modifier = totaldamage / (critvalue / normalstates); //integer division floors towards zero var modifier = totaldamage / (critvalue / normalstates); //integer division floors towards zero
statusEffectsComponent?.ChangeStatus(StatusEffect.Health, statusEffectsComponent?.ChangeStatusEffectIcon(StatusEffect.Health,
"/Textures/Mob/UI/Human/human" + modifier + ".png"); "/Textures/Mob/UI/Human/human" + modifier + ".png");
overlayComponent?.ChangeOverlay(ScreenEffects.None); overlayComponent?.ChangeOverlay(ScreenEffects.None);
return; return;
case ThresholdType.Critical: case ThresholdType.Critical:
statusEffectsComponent?.ChangeStatus( statusEffectsComponent?.ChangeStatusEffectIcon(
StatusEffect.Health, StatusEffect.Health,
"/Textures/Mob/UI/Human/humancrit-0.png"); "/Textures/Mob/UI/Human/humancrit-0.png");
overlayComponent?.ChangeOverlay(ScreenEffects.GradientCircleMask); overlayComponent?.ChangeOverlay(ScreenEffects.GradientCircleMask);
return; return;
case ThresholdType.Death: case ThresholdType.Death:
statusEffectsComponent?.ChangeStatus( statusEffectsComponent?.ChangeStatusEffectIcon(
StatusEffect.Health, StatusEffect.Health,
"/Textures/Mob/UI/Human/humandead.png"); "/Textures/Mob/UI/Human/humandead.png");
overlayComponent?.ChangeOverlay(ScreenEffects.CircleMask); overlayComponent?.ChangeOverlay(ScreenEffects.CircleMask);

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using Content.Shared.GameObjects.Components.Mobs; using Content.Shared.GameObjects.Components.Mobs;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
@@ -8,25 +9,48 @@ namespace Content.Server.GameObjects.Components.Mobs
[ComponentReference(typeof(SharedStatusEffectsComponent))] [ComponentReference(typeof(SharedStatusEffectsComponent))]
public sealed class ServerStatusEffectsComponent : SharedStatusEffectsComponent public sealed class ServerStatusEffectsComponent : SharedStatusEffectsComponent
{ {
private readonly Dictionary<StatusEffect, string> _statusEffects = new Dictionary<StatusEffect, string>(); private readonly Dictionary<StatusEffect, StatusEffectStatus> _statusEffects = new Dictionary<StatusEffect, StatusEffectStatus>();
public override ComponentState GetComponentState() public override ComponentState GetComponentState()
{ {
return new StatusEffectComponentState(_statusEffects); return new StatusEffectComponentState(_statusEffects);
} }
public void ChangeStatus(StatusEffect effect, string icon) public void ChangeStatusEffectIcon(StatusEffect effect, string icon)
{ {
if (_statusEffects.TryGetValue(effect, out string value) && value == icon) if (_statusEffects.TryGetValue(effect, out var value) && value.Icon == icon)
{ {
return; return;
} }
_statusEffects[effect] = icon; _statusEffects[effect] = new StatusEffectStatus()
{Icon = icon, Cooldown = value.Cooldown};
Dirty(); Dirty();
} }
public void RemoveStatus(StatusEffect effect) public void ChangeStatusEffectCooldown(StatusEffect effect, ValueTuple<TimeSpan, TimeSpan> cooldown)
{
if (_statusEffects.TryGetValue(effect, out var value)
&& value.Cooldown == cooldown)
{
return;
}
_statusEffects[effect] = new StatusEffectStatus()
{
Icon = value.Icon, Cooldown = cooldown
};
Dirty();
}
public void ChangeStatusEffect(StatusEffect effect, string icon, ValueTuple<TimeSpan, TimeSpan>? cooldown)
{
_statusEffects[effect] = new StatusEffectStatus()
{Icon = icon, Cooldown = cooldown};
Dirty();
}
public void RemoveStatusEffect(StatusEffect effect)
{ {
if (!_statusEffects.Remove(effect)) if (!_statusEffects.Remove(effect))
{ {

View File

@@ -75,7 +75,7 @@ namespace Content.Server.GameObjects
{ {
base.OnRemove(); base.OnRemove();
Owner.TryGetComponent(out ServerStatusEffectsComponent statusEffectsComponent); Owner.TryGetComponent(out ServerStatusEffectsComponent statusEffectsComponent);
statusEffectsComponent?.RemoveStatus(StatusEffect.Health); statusEffectsComponent?.RemoveStatusEffect(StatusEffect.Health);
Owner.TryGetComponent(out ServerOverlayEffectsComponent overlayEffectsComponent); Owner.TryGetComponent(out ServerOverlayEffectsComponent overlayEffectsComponent);
overlayEffectsComponent?.ChangeOverlay(ScreenEffects.None); overlayEffectsComponent?.ChangeOverlay(ScreenEffects.None);

View File

@@ -6,6 +6,7 @@ using Content.Server.Interfaces.GameObjects;
using Content.Server.Mobs; using Content.Server.Mobs;
using Content.Shared.Audio; using Content.Shared.Audio;
using Content.Shared.GameObjects.Components.Mobs; using Content.Shared.GameObjects.Components.Mobs;
using Microsoft.Extensions.Logging;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using Robust.Server.GameObjects.EntitySystems; using Robust.Server.GameObjects.EntitySystems;
using Robust.Shared.Audio; using Robust.Shared.Audio;
@@ -15,6 +16,7 @@ using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Timers; using Robust.Shared.Interfaces.Timers;
using Robust.Shared.Interfaces.Timing; using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Maths; using Robust.Shared.Maths;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables; using Robust.Shared.ViewVariables;
@@ -39,7 +41,7 @@ namespace Content.Server.GameObjects.Components.Mobs
[ViewVariables] [ViewVariables]
public TimeSpan? StunEnd => _lastStun == null public TimeSpan? StunEnd => _lastStun == null
? (TimeSpan?) null ? (TimeSpan?) null
: _gameTiming.CurTime + TimeSpan.FromSeconds(_stunnedTimer + _knockdownTimer + _slowdownTimer); : _gameTiming.CurTime + (TimeSpan.FromSeconds(Math.Max(_stunnedTimer, Math.Max(_knockdownTimer, _slowdownTimer))));
private const int StunLevels = 8; private const int StunLevels = 8;
@@ -56,7 +58,8 @@ namespace Content.Server.GameObjects.Components.Mobs
private float _walkModifierOverride = 0f; private float _walkModifierOverride = 0f;
private float _runModifierOverride = 0f; private float _runModifierOverride = 0f;
private readonly string[] _texturesStunOverlay = new string[StunLevels]; private string _stunTexture;
private CancellationTokenSource _statusRemoveCancellation = new CancellationTokenSource();
[ViewVariables] public bool Stunned => _stunnedTimer > 0f; [ViewVariables] public bool Stunned => _stunnedTimer > 0f;
[ViewVariables] public bool KnockedDown => _knockdownTimer > 0f; [ViewVariables] public bool KnockedDown => _knockdownTimer > 0f;
@@ -73,16 +76,7 @@ namespace Content.Server.GameObjects.Components.Mobs
serializer.DataField(ref _slowdownCap, "slowdownCap", 20f); serializer.DataField(ref _slowdownCap, "slowdownCap", 20f);
serializer.DataField(ref _helpInterval, "helpInterval", 1f); serializer.DataField(ref _helpInterval, "helpInterval", 1f);
serializer.DataField(ref _helpKnockdownRemove, "helpKnockdownRemove", 1f); serializer.DataField(ref _helpKnockdownRemove, "helpKnockdownRemove", 1f);
} serializer.DataField(ref _stunTexture, "stunTexture", "/Textures/Objects/Melee/stunbaton.rsi/stunbaton_off.png");
public override void Initialize()
{
base.Initialize();
for (var i = 0; i < StunLevels; i++)
{
_texturesStunOverlay[i] = $"/Textures/UserInterface/Inventory/cooldown-{i}.png";
}
} }
/// <summary> /// <summary>
@@ -100,6 +94,8 @@ namespace Content.Server.GameObjects.Components.Mobs
_stunnedTimer = seconds; _stunnedTimer = seconds;
_lastStun = _gameTiming.CurTime; _lastStun = _gameTiming.CurTime;
SetStatusEffect();
} }
/// <summary> /// <summary>
@@ -117,6 +113,8 @@ namespace Content.Server.GameObjects.Components.Mobs
_knockdownTimer = seconds; _knockdownTimer = seconds;
_lastStun = _gameTiming.CurTime; _lastStun = _gameTiming.CurTime;
SetStatusEffect();
} }
/// <summary> /// <summary>
@@ -150,6 +148,8 @@ namespace Content.Server.GameObjects.Components.Mobs
if(Owner.TryGetComponent(out MovementSpeedModifierComponent movement)) if(Owner.TryGetComponent(out MovementSpeedModifierComponent movement))
movement.RefreshMovementSpeedModifiers(); movement.RefreshMovementSpeedModifiers();
SetStatusEffect();
} }
/// <summary> /// <summary>
@@ -174,9 +174,21 @@ namespace Content.Server.GameObjects.Components.Mobs
_knockdownTimer -= _helpKnockdownRemove; _knockdownTimer -= _helpKnockdownRemove;
SetStatusEffect();
return true; return true;
} }
private void SetStatusEffect()
{
if (!Owner.TryGetComponent(out ServerStatusEffectsComponent status))
return;
status.ChangeStatusEffect(StatusEffect.Stun, _stunTexture, (StunStart == null || StunEnd == null) ? default : (StunStart.Value, StunEnd.Value));
_statusRemoveCancellation.Cancel();
_statusRemoveCancellation = new CancellationTokenSource();
}
public void Update(float delta) public void Update(float delta)
{ {
if (Stunned) if (Stunned)
@@ -214,33 +226,20 @@ namespace Content.Server.GameObjects.Components.Mobs
} }
} }
if (!_lastStun.HasValue || !StunEnd.HasValue || !Owner.TryGetComponent(out ServerStatusEffectsComponent status)) if (!StunStart.HasValue || !StunEnd.HasValue || !Owner.TryGetComponent(out ServerStatusEffectsComponent status))
return; return;
var start = _lastStun.Value; var start = StunStart.Value;
var end = StunEnd.Value; var end = StunEnd.Value;
var length = (end - start).TotalSeconds; var length = (end - start).TotalSeconds;
var progress = (_gameTiming.CurTime - start).TotalSeconds; var progress = (_gameTiming.CurTime - start).TotalSeconds;
var ratio = (float)(progress / length);
var textureIndex = CalculateStunLevel(ratio); if (progress >= length)
if (textureIndex == StunLevels)
{ {
Timer.Spawn(250, () => status.RemoveStatusEffect(StatusEffect.Stun), _statusRemoveCancellation.Token);
_lastStun = null; _lastStun = null;
status.RemoveStatus(StatusEffect.Stun);
} }
else
{
status.ChangeStatus(StatusEffect.Stun, _texturesStunOverlay[textureIndex]);
}
}
private static int CalculateStunLevel(float stunValue)
{
var val = stunValue.Clamp(0, 1);
val *= StunLevels;
return (int)Math.Floor(val);
} }
#region ActionBlockers #region ActionBlockers

View File

@@ -78,8 +78,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
// Update UI // Update UI
Owner.TryGetComponent(out ServerStatusEffectsComponent statusEffectsComponent); Owner.TryGetComponent(out ServerStatusEffectsComponent statusEffectsComponent);
statusEffectsComponent?.ChangeStatus(StatusEffect.Hunger, _hungerThresholdImages[ (int)_currentHungerThreshold ]); statusEffectsComponent?.ChangeStatusEffectIcon(StatusEffect.Hunger, _hungerThresholdImages[ (int)_currentHungerThreshold ]);
switch (_currentHungerThreshold) switch (_currentHungerThreshold)
{ {
case HungerThreshold.Overfed: case HungerThreshold.Overfed:

View File

@@ -77,7 +77,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
// Update UI // Update UI
Owner.TryGetComponent(out ServerStatusEffectsComponent statusEffectsComponent); Owner.TryGetComponent(out ServerStatusEffectsComponent statusEffectsComponent);
statusEffectsComponent?.ChangeStatus(StatusEffect.Thirst, "/Textures/Mob/UI/Thirst/" + statusEffectsComponent?.ChangeStatusEffectIcon(StatusEffect.Thirst, "/Textures/Mob/UI/Thirst/" +
_currentThirstThreshold + ".png"); _currentThirstThreshold + ".png");
switch (_currentThirstThreshold) switch (_currentThirstThreshold)

View File

@@ -18,14 +18,21 @@ namespace Content.Shared.GameObjects.Components.Mobs
[Serializable, NetSerializable] [Serializable, NetSerializable]
public class StatusEffectComponentState : ComponentState public class StatusEffectComponentState : ComponentState
{ {
public Dictionary<StatusEffect, string> StatusEffects; public Dictionary<StatusEffect, StatusEffectStatus> StatusEffects;
public StatusEffectComponentState(Dictionary<StatusEffect, string> statusEffects) : base(ContentNetIDs.STATUSEFFECTS) public StatusEffectComponentState(Dictionary<StatusEffect, StatusEffectStatus> statusEffects) : base(ContentNetIDs.STATUSEFFECTS)
{ {
StatusEffects = statusEffects; StatusEffects = statusEffects;
} }
} }
[Serializable, NetSerializable]
public struct StatusEffectStatus
{
public string Icon;
public ValueTuple<TimeSpan, TimeSpan>? Cooldown;
}
// Each status effect is assumed to be unique // Each status effect is assumed to be unique
public enum StatusEffect public enum StatusEffect
{ {