Add cooldown to melee weapons on pickup/selected

Fixes #3149
Fixes kitchen knives not having ItemCooldown component.
This commit is contained in:
Vera Aguilera Puerto
2021-02-12 11:27:11 +01:00
parent cc8458f23c
commit 2b10ee6b8f
2 changed files with 37 additions and 11 deletions

View File

@@ -19,7 +19,7 @@ using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Weapon.Melee
{
[RegisterComponent]
public class MeleeWeaponComponent : Component, IAttack
public class MeleeWeaponComponent : Component, IAttack, IHandSelected
{
[Dependency] private readonly IPhysicsManager _physicsManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
@@ -126,11 +126,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Melee
_lastAttackTime = curTime;
_cooldownEnd = _lastAttackTime + TimeSpan.FromSeconds(ArcCooldownTime);
if (Owner.TryGetComponent(out ItemCooldownComponent cooldown))
{
cooldown.CooldownStart = _lastAttackTime;
cooldown.CooldownEnd = _cooldownEnd;
}
RefreshItemCooldown();
return true;
}
@@ -180,11 +176,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Melee
_lastAttackTime = curTime;
_cooldownEnd = _lastAttackTime + TimeSpan.FromSeconds(CooldownTime);
if (Owner.TryGetComponent(out ItemCooldownComponent cooldown))
{
cooldown.CooldownStart = _lastAttackTime;
cooldown.CooldownEnd = _cooldownEnd;
}
RefreshItemCooldown();
return true;
}
@@ -211,6 +203,39 @@ namespace Content.Server.GameObjects.Components.Weapon.Melee
return resSet;
}
void IHandSelected.HandSelected(HandSelectedEventArgs eventArgs)
{
var curTime = _gameTiming.CurTime;
var cool = TimeSpan.FromSeconds(CooldownTime * 0.5f);
if (curTime < _cooldownEnd)
{
if (_cooldownEnd - curTime < cool)
{
_lastAttackTime = curTime;
_cooldownEnd += cool;
}
else
return;
}
else
{
_lastAttackTime = curTime;
_cooldownEnd = curTime + cool;
}
RefreshItemCooldown();
}
private void RefreshItemCooldown()
{
if (Owner.TryGetComponent(out ItemCooldownComponent cooldown))
{
cooldown.CooldownStart = _lastAttackTime;
cooldown.CooldownEnd = _cooldownEnd;
}
}
}
public class MeleeHitMessage : ComponentMessage

View File

@@ -6,6 +6,7 @@
- type: Utensil
types:
- Knife
- type: ItemCooldown
- type: MeleeWeapon
hitSound: /Audio/Weapons/bladeslice.ogg
damage: 12