Adds click attacks (#1972)

click attacking goes brrrr
This commit is contained in:
Víctor Aguilera Puerto
2020-09-02 15:34:09 +02:00
committed by GitHub
7 changed files with 170 additions and 69 deletions

View File

@@ -1,10 +1,14 @@
using Content.Client.GameObjects.Components.Mobs; using System;
using Content.Client.GameObjects.Components.Mobs;
using Content.Client.GameObjects.Components.Weapons.Melee; using Content.Client.GameObjects.Components.Weapons.Melee;
using Content.Shared.GameObjects.Components.Weapons.Melee; using Content.Shared.GameObjects.Components.Weapons.Melee;
using JetBrains.Annotations; using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Client.Interfaces.GameObjects.Components; using Robust.Client.Interfaces.GameObjects.Components;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.EntitySystemMessages;
using Robust.Shared.GameObjects.Systems; using Robust.Shared.GameObjects.Systems;
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.Maths;
@@ -18,6 +22,7 @@ namespace Content.Client.GameObjects.EntitySystems
public sealed class MeleeWeaponSystem : EntitySystem public sealed class MeleeWeaponSystem : EntitySystem
{ {
[Dependency] private readonly IPrototypeManager _prototypeManager = default!; [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
public override void Initialize() public override void Initialize()
{ {
@@ -53,6 +58,26 @@ namespace Content.Client.GameObjects.EntitySystems
var weaponArcAnimation = entity.GetComponent<MeleeWeaponArcAnimationComponent>(); var weaponArcAnimation = entity.GetComponent<MeleeWeaponArcAnimationComponent>();
weaponArcAnimation.SetData(weaponArc, msg.Angle, attacker); weaponArcAnimation.SetData(weaponArc, msg.Angle, attacker);
// Due to ISpriteComponent limitations, weapons that don't use an RSI won't have this effect.
if (EntityManager.TryGetEntity(msg.Source, out var source) && msg.TextureEffect && source.TryGetComponent(out ISpriteComponent sourceSprite)
&& sourceSprite.BaseRSI?.Path != null)
{
var sys = Get<EffectSystem>();
var curTime = _gameTiming.CurTime;
var effect = new EffectSystemMessage
{
EffectSprite = sourceSprite.BaseRSI.Path.ToString(),
RsiState = sourceSprite.LayerGetState(0).Name,
Coordinates = attacker.Transform.GridPosition,
Color = Vector4.Multiply(new Vector4(255, 255, 255, 125), 1.0f),
ColorDelta = Vector4.Multiply(new Vector4(0, 0, 0, -10), 1.0f),
Velocity = msg.Angle.ToVec(),
Acceleration = msg.Angle.ToVec() * 5f,
Born = curTime,
DeathTime = curTime.Add(TimeSpan.FromMilliseconds(300f)),
};
sys.CreateEffect(effect);
}
foreach (var uid in msg.Hits) foreach (var uid in msg.Hits)
{ {

View File

@@ -15,7 +15,7 @@ namespace Content.Server.AI.Utility.Considerations.Combat.Melee
return 0.0f; return 0.0f;
} }
return meleeWeaponComponent.CooldownTime / 10.0f; return meleeWeaponComponent.ArcCooldownTime / 10.0f;
} }
} }
} }

View File

@@ -17,6 +17,8 @@ using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables; using Robust.Shared.ViewVariables;
using Content.Shared.Damage; using Content.Shared.Damage;
using Content.Shared.Interfaces.GameObjects.Components; using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects.EntitySystemMessages;
namespace Content.Server.GameObjects.Components.Weapon.Melee namespace Content.Server.GameObjects.Components.Weapon.Melee
{ {
@@ -24,59 +26,54 @@ namespace Content.Server.GameObjects.Components.Weapon.Melee
public class MeleeWeaponComponent : Component, IAttack public class MeleeWeaponComponent : Component, IAttack
{ {
[Dependency] private readonly IMapManager _mapManager = default!; [Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly IEntitySystemManager _entitySystemManager = default!;
[Dependency] private readonly IPhysicsManager _physicsManager = default!; [Dependency] private readonly IPhysicsManager _physicsManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!; [Dependency] private readonly IGameTiming _gameTiming = default!;
public override string Name => "MeleeWeapon"; public override string Name => "MeleeWeapon";
private TimeSpan _lastAttackTime; private TimeSpan _lastAttackTime;
private TimeSpan _cooldownEnd;
private int _damage;
private float _range;
private float _arcWidth;
private string _arc;
private string _hitSound; private string _hitSound;
public float CooldownTime => _cooldownTime; private string _missSound;
private float _cooldownTime = 1f; public float ArcCooldownTime { get; private set; } = 1f;
public float CooldownTime { get; private set; } = 0.5f;
[ViewVariables(VVAccess.ReadWrite)] [ViewVariables(VVAccess.ReadWrite)]
public string Arc public string ClickArc { get; set; }
{
get => _arc;
set => _arc = value;
}
[ViewVariables(VVAccess.ReadWrite)] [ViewVariables(VVAccess.ReadWrite)]
public float ArcWidth public string Arc { get; set; }
{
get => _arcWidth;
set => _arcWidth = value;
}
[ViewVariables(VVAccess.ReadWrite)] [ViewVariables(VVAccess.ReadWrite)]
public float Range public float ArcWidth { get; set; }
{
get => _range;
set => _range = value;
}
[ViewVariables(VVAccess.ReadWrite)] [ViewVariables(VVAccess.ReadWrite)]
public int Damage public float Range { get; set; }
{
get => _damage; [ViewVariables(VVAccess.ReadWrite)]
set => _damage = value; public int Damage { get; set; }
}
[ViewVariables(VVAccess.ReadWrite)]
public DamageType DamageType { get; set; }
[ViewVariables(VVAccess.ReadWrite)]
public bool ClickAttackEffect { get; set; }
public override void ExposeData(ObjectSerializer serializer) public override void ExposeData(ObjectSerializer serializer)
{ {
base.ExposeData(serializer); base.ExposeData(serializer);
serializer.DataField(ref _damage, "damage", 5); serializer.DataField(this, x => x.Damage, "damage", 5);
serializer.DataField(ref _range, "range", 1); serializer.DataField(this, x => x.Range, "range", 1);
serializer.DataField(ref _arcWidth, "arcwidth", 90); serializer.DataField(this, x => x.ArcWidth, "arcwidth", 90);
serializer.DataField(ref _arc, "arc", "default"); serializer.DataField(this, x => x.Arc, "arc", "default");
serializer.DataField(ref _hitSound, "hitSound", "/Audio/Weapons/genhit1.ogg"); serializer.DataField(this, x => x.ClickArc, "clickArc", "punch");
serializer.DataField(ref _cooldownTime, "cooldownTime", 1f); serializer.DataField(this, x => x._hitSound, "hitSound", "/Audio/Weapons/genhit1.ogg");
serializer.DataField(this, x => x._missSound, "hitSound", "/Audio/Weapons/punchmiss.ogg");
serializer.DataField(this, x => x.ArcCooldownTime, "arcCooldownTime", 1f);
serializer.DataField(this, x => x.CooldownTime, "cooldownTime", 1f);
serializer.DataField(this, x => x.DamageType, "damageType", DamageType.Blunt);
serializer.DataField(this, x => x.ClickAttackEffect, "clickAttackEffect", true);
} }
protected virtual bool OnHitEntities(IReadOnlyList<IEntity> entities, AttackEventArgs eventArgs) protected virtual bool OnHitEntities(IReadOnlyList<IEntity> entities, AttackEventArgs eventArgs)
@@ -84,13 +81,15 @@ namespace Content.Server.GameObjects.Components.Weapon.Melee
return true; return true;
} }
void IAttack.Attack(AttackEventArgs eventArgs) bool IAttack.WideAttack(AttackEventArgs eventArgs)
{ {
if (!eventArgs.WideAttack) return true;
var curTime = _gameTiming.CurTime; var curTime = _gameTiming.CurTime;
var span = curTime - _lastAttackTime;
if(span.TotalSeconds < _cooldownTime) { if(curTime < _cooldownEnd)
return; return true;
}
var location = eventArgs.User.Transform.GridPosition; var location = eventArgs.User.Transform.GridPosition;
var angle = new Angle(eventArgs.ClickLocation.ToMapPos(_mapManager) - location.ToMapPos(_mapManager)); var angle = new Angle(eventArgs.ClickLocation.ToMapPos(_mapManager) - location.ToMapPos(_mapManager));
@@ -104,7 +103,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Melee
} }
else else
{ {
audioSystem.PlayFromEntity("/Audio/Weapons/punchmiss.ogg", eventArgs.User); audioSystem.PlayFromEntity(_missSound, eventArgs.User);
} }
var hitEntities = new List<IEntity>(); var hitEntities = new List<IEntity>();
@@ -115,26 +114,82 @@ namespace Content.Server.GameObjects.Components.Weapon.Melee
if (entity.TryGetComponent(out IDamageableComponent damageComponent)) if (entity.TryGetComponent(out IDamageableComponent damageComponent))
{ {
damageComponent.ChangeDamage(DamageType.Blunt, Damage, false, Owner); damageComponent.ChangeDamage(DamageType, Damage, false, Owner);
hitEntities.Add(entity); hitEntities.Add(entity);
} }
} }
if(!OnHitEntities(hitEntities, eventArgs)) return; if(!OnHitEntities(hitEntities, eventArgs)) return false;
if (Arc != null) if (Arc != null)
{ {
var sys = _entitySystemManager.GetEntitySystem<MeleeWeaponSystem>(); var sys = EntitySystem.Get<MeleeWeaponSystem>();
sys.SendAnimation(Arc, angle, eventArgs.User, hitEntities); sys.SendAnimation(Arc, angle, eventArgs.User, Owner, hitEntities);
} }
_lastAttackTime = _gameTiming.CurTime; _lastAttackTime = curTime;
_cooldownEnd = _lastAttackTime + TimeSpan.FromSeconds(ArcCooldownTime);
if (Owner.TryGetComponent(out ItemCooldownComponent cooldown)) if (Owner.TryGetComponent(out ItemCooldownComponent cooldown))
{ {
cooldown.CooldownStart = _lastAttackTime; cooldown.CooldownStart = _lastAttackTime;
cooldown.CooldownEnd = _lastAttackTime + TimeSpan.FromSeconds(_cooldownTime); cooldown.CooldownEnd = _cooldownEnd;
} }
return true;
}
bool IAttack.ClickAttack(AttackEventArgs eventArgs)
{
if (eventArgs.WideAttack) return false;
var curTime = _gameTiming.CurTime;
if(curTime < _cooldownEnd || !eventArgs.Target.IsValid())
return true;
var target = eventArgs.TargetEntity;
var location = eventArgs.User.Transform.GridPosition;
var angle = new Angle(eventArgs.ClickLocation.ToMapPos(_mapManager) - location.ToMapPos(_mapManager));
var audioSystem = EntitySystem.Get<AudioSystem>();
if (target != null)
{
audioSystem.PlayFromEntity( _hitSound, target);
}
else
{
audioSystem.PlayFromEntity(_missSound, eventArgs.User);
return false;
}
if (target.TryGetComponent(out IDamageableComponent damageComponent))
{
damageComponent.ChangeDamage(DamageType, Damage, false, Owner);
}
var targets = new[] {target};
if (!OnHitEntities(targets, eventArgs))
return false;
if (ClickArc != null)
{
var sys = EntitySystem.Get<MeleeWeaponSystem>();
sys.SendAnimation(ClickArc, angle, eventArgs.User, Owner, targets, ClickAttackEffect);
}
_lastAttackTime = curTime;
_cooldownEnd = _lastAttackTime + TimeSpan.FromSeconds(CooldownTime);
if (Owner.TryGetComponent(out ItemCooldownComponent cooldown))
{
cooldown.CooldownStart = _lastAttackTime;
cooldown.CooldownEnd = _cooldownEnd;
}
return true;
} }
private HashSet<IEntity> ArcRayCast(Vector2 position, Angle angle, IEntity ignore) private HashSet<IEntity> ArcRayCast(Vector2 position, Angle angle, IEntity ignore)
@@ -150,7 +205,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Melee
for (var i = 0; i < increments; i++) for (var i = 0; i < increments; i++)
{ {
var castAngle = new Angle(baseAngle + increment * i); var castAngle = new Angle(baseAngle + increment * i);
var res = _physicsManager.IntersectRay(mapId, new CollisionRay(position, castAngle.ToVec(), 23), _range, ignore).FirstOrDefault(); var res = _physicsManager.IntersectRay(mapId, new CollisionRay(position, castAngle.ToVec(), 23), Range, ignore).FirstOrDefault();
if (res.HitEntity != null) if (res.HitEntity != null)
{ {
resSet.Add(res.HitEntity); resSet.Add(res.HitEntity);

View File

@@ -176,7 +176,7 @@ namespace Content.Server.GameObjects.EntitySystems.Click
if (userEntity.TryGetComponent(out CombatModeComponent combatMode) && combatMode.IsInCombatMode) if (userEntity.TryGetComponent(out CombatModeComponent combatMode) && combatMode.IsInCombatMode)
{ {
DoAttack(userEntity, coords); DoAttack(userEntity, coords, true);
} }
return true; return true;
@@ -198,7 +198,7 @@ namespace Content.Server.GameObjects.EntitySystems.Click
if (entity.TryGetComponent(out CombatModeComponent combatMode) && combatMode.IsInCombatMode) if (entity.TryGetComponent(out CombatModeComponent combatMode) && combatMode.IsInCombatMode)
{ {
DoAttack(entity, coords); DoAttack(entity, coords, false);
} }
else else
{ {
@@ -229,7 +229,10 @@ namespace Content.Server.GameObjects.EntitySystems.Click
return true; return true;
} }
UserInteraction(userEntity, coords, uid); if(userEntity.TryGetComponent(out CombatModeComponent combat) && combat.IsInCombatMode)
DoAttack(userEntity, coords, false, uid);
else
UserInteraction(userEntity, coords, uid);
return true; return true;
} }
@@ -790,7 +793,7 @@ namespace Content.Server.GameObjects.EntitySystems.Click
} }
} }
private void DoAttack(IEntity player, GridCoordinates coordinates) private void DoAttack(IEntity player, GridCoordinates coordinates, bool wideAttack, EntityUid target = default)
{ {
// Verify player is on the same map as the entity he clicked on // Verify player is on the same map as the entity he clicked on
if (_mapManager.GetGrid(coordinates.GridID).ParentMapId != player.Transform.MapID) if (_mapManager.GetGrid(coordinates.GridID).ParentMapId != player.Transform.MapID)
@@ -800,12 +803,13 @@ namespace Content.Server.GameObjects.EntitySystems.Click
return; return;
} }
if (!ActionBlockerSystem.CanAttack(player)) if (!ActionBlockerSystem.CanAttack(player) ||
(!wideAttack && !InRangeUnobstructed(player.Transform.MapPosition, coordinates.ToMap(_mapManager), ignoreInsideBlocker:true)))
{ {
return; return;
} }
var eventArgs = new AttackEventArgs(player, coordinates); var eventArgs = new AttackEventArgs(player, coordinates, wideAttack, target);
// Verify player has a hand, and find what object he is currently holding in his active hand // Verify player has a hand, and find what object he is currently holding in his active hand
if (player.TryGetComponent<IHandsComponent>(out var hands)) if (player.TryGetComponent<IHandsComponent>(out var hands))
@@ -814,22 +818,20 @@ namespace Content.Server.GameObjects.EntitySystems.Click
if (item != null) if (item != null)
{ {
var attacked = false;
foreach (var attackComponent in item.GetAllComponents<IAttack>()) foreach (var attackComponent in item.GetAllComponents<IAttack>())
{ {
attackComponent.Attack(eventArgs); if(wideAttack ? attackComponent.WideAttack(eventArgs) : attackComponent.ClickAttack(eventArgs))
attacked = true; return;
}
if (attacked)
{
return;
} }
} }
} }
foreach (var attackComponent in player.GetAllComponents<IAttack>()) foreach (var attackComponent in player.GetAllComponents<IAttack>())
{ {
attackComponent.Attack(eventArgs); if (wideAttack)
attackComponent.WideAttack(eventArgs);
else
attackComponent.ClickAttack(eventArgs);
} }
} }
} }

View File

@@ -9,10 +9,10 @@ namespace Content.Server.GameObjects.EntitySystems
{ {
public sealed class MeleeWeaponSystem : EntitySystem public sealed class MeleeWeaponSystem : EntitySystem
{ {
public void SendAnimation(string arc, Angle angle, IEntity attacker, IEnumerable<IEntity> hits) public void SendAnimation(string arc, Angle angle, IEntity attacker, IEntity source, IEnumerable<IEntity> hits, bool textureEffect = false)
{ {
RaiseNetworkEvent(new MeleeWeaponSystemMessages.PlayMeleeWeaponAnimationMessage(arc, angle, attacker.Uid, RaiseNetworkEvent(new MeleeWeaponSystemMessages.PlayMeleeWeaponAnimationMessage(arc, angle, attacker.Uid, source.Uid,
hits.Select(e => e.Uid).ToList())); hits.Select(e => e.Uid).ToList(), textureEffect));
} }
} }
} }

View File

@@ -11,18 +11,22 @@ namespace Content.Shared.GameObjects.EntitySystemMessages
[Serializable, NetSerializable] [Serializable, NetSerializable]
public sealed class PlayMeleeWeaponAnimationMessage : EntitySystemMessage public sealed class PlayMeleeWeaponAnimationMessage : EntitySystemMessage
{ {
public PlayMeleeWeaponAnimationMessage(string arcPrototype, Angle angle, EntityUid attacker, List<EntityUid> hits) public PlayMeleeWeaponAnimationMessage(string arcPrototype, Angle angle, EntityUid attacker, EntityUid source, List<EntityUid> hits, bool textureEffect = false)
{ {
ArcPrototype = arcPrototype; ArcPrototype = arcPrototype;
Angle = angle; Angle = angle;
Attacker = attacker; Attacker = attacker;
Source = source;
Hits = hits; Hits = hits;
TextureEffect = textureEffect;
} }
public string ArcPrototype { get; } public string ArcPrototype { get; }
public Angle Angle { get; } public Angle Angle { get; }
public EntityUid Attacker { get; } public EntityUid Attacker { get; }
public EntityUid Source { get; }
public List<EntityUid> Hits { get; } public List<EntityUid> Hits { get; }
public bool TextureEffect { get; }
} }
} }
} }

View File

@@ -1,5 +1,9 @@
using System; #nullable enable
using System;
using Content.Shared.GameObjects.Components.Research;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map; using Robust.Shared.Map;
namespace Content.Shared.Interfaces.GameObjects.Components namespace Content.Shared.Interfaces.GameObjects.Components
@@ -9,18 +13,29 @@ namespace Content.Shared.Interfaces.GameObjects.Components
/// </summary> /// </summary>
public interface IAttack public interface IAttack
{ {
void Attack(AttackEventArgs eventArgs); // Redirects to ClickAttack by default.
bool WideAttack(AttackEventArgs eventArgs) => ClickAttack(eventArgs);
bool ClickAttack(AttackEventArgs eventArgs);
} }
public class AttackEventArgs : EventArgs public class AttackEventArgs : EventArgs
{ {
public AttackEventArgs(IEntity user, GridCoordinates clickLocation) public AttackEventArgs(IEntity user, GridCoordinates clickLocation, bool wideAttack, EntityUid target = default)
{ {
User = user; User = user;
ClickLocation = clickLocation; ClickLocation = clickLocation;
WideAttack = wideAttack;
Target = target;
IEntity? targetEntity = null;
IoCManager.Resolve<IEntityManager>()?.TryGetEntity(Target, out targetEntity);
TargetEntity = targetEntity;
} }
public IEntity User { get; } public IEntity User { get; }
public GridCoordinates ClickLocation { get; } public GridCoordinates ClickLocation { get; }
public bool WideAttack { get; }
public EntityUid Target { get; }
public IEntity? TargetEntity { get; }
} }
} }