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.UserInterface;
using Robust.Client.Player;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Network;
using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Maths;
using Robust.Shared.Players;
namespace Content.Client.GameObjects.Components.Mobs
@@ -25,10 +28,12 @@ namespace Content.Client.GameObjects.Components.Mobs
[Dependency] private readonly IPlayerManager _playerManager;
[Dependency] private readonly IResourceCache _resourceCache;
[Dependency] private readonly IUserInterfaceManager _userInterfaceManager;
[Dependency] private readonly IGameTiming _gameTiming;
#pragma warning restore 649
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>
/// 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)
{
base.HandleComponentState(curState, nextState);
if (!(curState is StatusEffectComponentState state) || _icons == state.StatusEffects) return;
_icons = state.StatusEffects;
UpdateIcons();
if (!(curState is StatusEffectComponentState state) || _status == state.StatusEffects) return;
_status = state.StatusEffects;
UpdateStatusEffects();
}
private void PlayerAttached()
@@ -71,7 +76,7 @@ namespace Content.Client.GameObjects.Components.Mobs
}
_ui = new StatusEffectsUI();
_userInterfaceManager.StateRoot.AddChild(_ui);
UpdateIcons();
UpdateStatusEffects();
}
private void PlayerDetached()
@@ -80,32 +85,68 @@ namespace Content.Client.GameObjects.Components.Mobs
_ui = null;
}
public void UpdateIcons()
public void UpdateStatusEffects()
{
if (!CurrentlyControlled || _ui == null)
{
return;
}
_cooldown.Clear();
_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),
Texture = _resourceCache.GetTexture(effect.Value)
Children =
{
new TextureRect
{
TextureScale = (2, 2),
Texture = _resourceCache.GetTexture(statusEffect.Icon)
},
}
};
newIcon.Texture = _resourceCache.GetTexture(effect.Value);
_ui.VBox.AddChild(newIcon);
if (statusEffect.Cooldown.HasValue)
{
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);
UpdateIcons();
Logger.InfoS("statuseffects", $"Removed icon {name}");
_status.Remove(name);
UpdateStatusEffects();
}
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?
}
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");
overlayComponent?.ChangeOverlay(ScreenEffects.None);
return;
case ThresholdType.Critical:
statusEffectsComponent?.ChangeStatus(
statusEffectsComponent?.ChangeStatusEffectIcon(
StatusEffect.Health,
"/Textures/Mob/UI/Human/humancrit-0.png");
overlayComponent?.ChangeOverlay(ScreenEffects.GradientCircleMask);
return;
case ThresholdType.Death:
statusEffectsComponent?.ChangeStatus(
statusEffectsComponent?.ChangeStatusEffectIcon(
StatusEffect.Health,
"/Textures/Mob/UI/Human/humandead.png");
overlayComponent?.ChangeOverlay(ScreenEffects.CircleMask);

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using Content.Shared.GameObjects.Components.Mobs;
using Robust.Shared.GameObjects;
@@ -8,25 +9,48 @@ namespace Content.Server.GameObjects.Components.Mobs
[ComponentReference(typeof(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()
{
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;
}
_statusEffects[effect] = icon;
_statusEffects[effect] = new StatusEffectStatus()
{Icon = icon, Cooldown = value.Cooldown};
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))
{

View File

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

View File

@@ -6,6 +6,7 @@ using Content.Server.Interfaces.GameObjects;
using Content.Server.Mobs;
using Content.Shared.Audio;
using Content.Shared.GameObjects.Components.Mobs;
using Microsoft.Extensions.Logging;
using Robust.Server.GameObjects;
using Robust.Server.GameObjects.EntitySystems;
using Robust.Shared.Audio;
@@ -15,6 +16,7 @@ using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Timers;
using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Maths;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
@@ -39,7 +41,7 @@ namespace Content.Server.GameObjects.Components.Mobs
[ViewVariables]
public TimeSpan? StunEnd => _lastStun == 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;
@@ -56,7 +58,8 @@ namespace Content.Server.GameObjects.Components.Mobs
private float _walkModifierOverride = 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 KnockedDown => _knockdownTimer > 0f;
@@ -73,16 +76,7 @@ namespace Content.Server.GameObjects.Components.Mobs
serializer.DataField(ref _slowdownCap, "slowdownCap", 20f);
serializer.DataField(ref _helpInterval, "helpInterval", 1f);
serializer.DataField(ref _helpKnockdownRemove, "helpKnockdownRemove", 1f);
}
public override void Initialize()
{
base.Initialize();
for (var i = 0; i < StunLevels; i++)
{
_texturesStunOverlay[i] = $"/Textures/UserInterface/Inventory/cooldown-{i}.png";
}
serializer.DataField(ref _stunTexture, "stunTexture", "/Textures/Objects/Melee/stunbaton.rsi/stunbaton_off.png");
}
/// <summary>
@@ -100,6 +94,8 @@ namespace Content.Server.GameObjects.Components.Mobs
_stunnedTimer = seconds;
_lastStun = _gameTiming.CurTime;
SetStatusEffect();
}
/// <summary>
@@ -117,6 +113,8 @@ namespace Content.Server.GameObjects.Components.Mobs
_knockdownTimer = seconds;
_lastStun = _gameTiming.CurTime;
SetStatusEffect();
}
/// <summary>
@@ -150,6 +148,8 @@ namespace Content.Server.GameObjects.Components.Mobs
if(Owner.TryGetComponent(out MovementSpeedModifierComponent movement))
movement.RefreshMovementSpeedModifiers();
SetStatusEffect();
}
/// <summary>
@@ -174,9 +174,21 @@ namespace Content.Server.GameObjects.Components.Mobs
_knockdownTimer -= _helpKnockdownRemove;
SetStatusEffect();
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)
{
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;
var start = _lastStun.Value;
var start = StunStart.Value;
var end = StunEnd.Value;
var length = (end - start).TotalSeconds;
var progress = (_gameTiming.CurTime - start).TotalSeconds;
var ratio = (float)(progress / length);
var textureIndex = CalculateStunLevel(ratio);
if (textureIndex == StunLevels)
if (progress >= length)
{
Timer.Spawn(250, () => status.RemoveStatusEffect(StatusEffect.Stun), _statusRemoveCancellation.Token);
_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

View File

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

View File

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

View File

@@ -18,14 +18,21 @@ namespace Content.Shared.GameObjects.Components.Mobs
[Serializable, NetSerializable]
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;
}
}
[Serializable, NetSerializable]
public struct StatusEffectStatus
{
public string Icon;
public ValueTuple<TimeSpan, TimeSpan>? Cooldown;
}
// Each status effect is assumed to be unique
public enum StatusEffect
{