2019-11-11 16:21:29 -05:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2020-05-23 01:23:36 +02:00
|
|
|
|
using System.Linq;
|
2019-09-26 22:32:32 +02:00
|
|
|
|
using Content.Server.GameObjects.EntitySystems;
|
2019-07-31 15:02:36 +02:00
|
|
|
|
using Content.Shared.GameObjects;
|
2019-11-12 01:43:11 +01:00
|
|
|
|
using Content.Shared.GameObjects.Components.Items;
|
2019-09-26 22:32:32 +02:00
|
|
|
|
using Robust.Server.GameObjects.EntitySystems;
|
2019-07-31 15:02:36 +02:00
|
|
|
|
using Robust.Shared.GameObjects;
|
2020-05-31 12:40:36 -05:00
|
|
|
|
using Robust.Shared.GameObjects.Systems;
|
2019-09-26 22:32:32 +02:00
|
|
|
|
using Robust.Shared.Interfaces.GameObjects;
|
2019-07-31 15:02:36 +02:00
|
|
|
|
using Robust.Shared.Interfaces.Map;
|
2019-11-17 21:46:39 +01:00
|
|
|
|
using Robust.Shared.Interfaces.Physics;
|
2019-11-11 16:21:29 -05:00
|
|
|
|
using Robust.Shared.Interfaces.Timing;
|
2019-04-15 21:11:38 -06:00
|
|
|
|
using Robust.Shared.IoC;
|
|
|
|
|
|
using Robust.Shared.Maths;
|
|
|
|
|
|
using Robust.Shared.Serialization;
|
2019-09-26 22:32:32 +02:00
|
|
|
|
using Robust.Shared.ViewVariables;
|
2018-04-05 17:32:51 -05:00
|
|
|
|
|
|
|
|
|
|
namespace Content.Server.GameObjects.Components.Weapon.Melee
|
|
|
|
|
|
{
|
2019-07-31 15:02:36 +02:00
|
|
|
|
[RegisterComponent]
|
2019-06-30 00:01:41 +02:00
|
|
|
|
public class MeleeWeaponComponent : Component, IAttack
|
2018-04-05 17:32:51 -05:00
|
|
|
|
{
|
2019-09-26 22:32:32 +02:00
|
|
|
|
public override string Name => "MeleeWeapon";
|
2019-11-11 16:21:29 -05:00
|
|
|
|
private TimeSpan _lastAttackTime;
|
2019-09-26 22:32:32 +02:00
|
|
|
|
|
2019-04-20 16:20:18 -07:00
|
|
|
|
#pragma warning disable 649
|
|
|
|
|
|
[Dependency] private readonly IMapManager _mapManager;
|
2019-09-26 22:32:32 +02:00
|
|
|
|
[Dependency] private readonly IEntitySystemManager _entitySystemManager;
|
2019-11-17 21:46:39 +01:00
|
|
|
|
[Dependency] private readonly IPhysicsManager _physicsManager;
|
2019-04-20 16:20:18 -07:00
|
|
|
|
#pragma warning restore 649
|
|
|
|
|
|
|
2020-05-11 13:54:31 +02:00
|
|
|
|
private int _damage;
|
|
|
|
|
|
private float _range;
|
|
|
|
|
|
private float _arcWidth;
|
2019-09-26 22:32:32 +02:00
|
|
|
|
private string _arc;
|
|
|
|
|
|
private string _hitSound;
|
2020-05-11 13:54:31 +02:00
|
|
|
|
private float _cooldownTime;
|
2019-09-26 22:32:32 +02:00
|
|
|
|
|
|
|
|
|
|
[ViewVariables(VVAccess.ReadWrite)]
|
|
|
|
|
|
public string Arc
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _arc;
|
|
|
|
|
|
set => _arc = value;
|
|
|
|
|
|
}
|
2018-04-05 17:32:51 -05:00
|
|
|
|
|
2019-09-26 22:32:32 +02:00
|
|
|
|
[ViewVariables(VVAccess.ReadWrite)]
|
|
|
|
|
|
public float ArcWidth
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _arcWidth;
|
|
|
|
|
|
set => _arcWidth = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[ViewVariables(VVAccess.ReadWrite)]
|
|
|
|
|
|
public float Range
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _range;
|
|
|
|
|
|
set => _range = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[ViewVariables(VVAccess.ReadWrite)]
|
|
|
|
|
|
public int Damage
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _damage;
|
|
|
|
|
|
set => _damage = value;
|
|
|
|
|
|
}
|
2018-04-05 17:32:51 -05:00
|
|
|
|
|
2018-07-26 23:38:16 +02:00
|
|
|
|
public override void ExposeData(ObjectSerializer serializer)
|
2018-04-05 17:32:51 -05:00
|
|
|
|
{
|
|
|
|
|
|
base.ExposeData(serializer);
|
|
|
|
|
|
|
2019-09-26 22:32:32 +02:00
|
|
|
|
serializer.DataField(ref _damage, "damage", 5);
|
|
|
|
|
|
serializer.DataField(ref _range, "range", 1);
|
|
|
|
|
|
serializer.DataField(ref _arcWidth, "arcwidth", 90);
|
|
|
|
|
|
serializer.DataField(ref _arc, "arc", "default");
|
|
|
|
|
|
serializer.DataField(ref _hitSound, "hitSound", "/Audio/weapons/genhit1.ogg");
|
2019-11-11 16:21:29 -05:00
|
|
|
|
serializer.DataField(ref _cooldownTime, "cooldownTime", 1f);
|
2018-04-05 17:32:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-05-21 19:40:08 +02:00
|
|
|
|
public virtual bool OnHitEntities(IReadOnlyList<IEntity> entities)
|
2020-05-13 20:21:03 +02:00
|
|
|
|
{
|
2020-05-21 19:40:08 +02:00
|
|
|
|
return false;
|
2020-05-13 20:21:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-30 00:01:41 +02:00
|
|
|
|
void IAttack.Attack(AttackEventArgs eventArgs)
|
2018-04-05 17:32:51 -05:00
|
|
|
|
{
|
2019-11-11 16:21:29 -05:00
|
|
|
|
var curTime = IoCManager.Resolve<IGameTiming>().CurTime;
|
|
|
|
|
|
var span = curTime - _lastAttackTime;
|
|
|
|
|
|
if(span.TotalSeconds < _cooldownTime) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2019-04-20 16:20:18 -07:00
|
|
|
|
var location = eventArgs.User.Transform.GridPosition;
|
2020-01-25 01:39:14 -08:00
|
|
|
|
var angle = new Angle(eventArgs.ClickLocation.ToMapPos(_mapManager) - location.ToMapPos(_mapManager));
|
2019-09-26 22:32:32 +02:00
|
|
|
|
|
|
|
|
|
|
// This should really be improved. GetEntitiesInArc uses pos instead of bounding boxes.
|
2019-11-17 21:46:39 +01:00
|
|
|
|
var entities = ArcRayCast(eventArgs.User.Transform.WorldPosition, angle, eventArgs.User);
|
2018-04-05 17:32:51 -05:00
|
|
|
|
|
2019-09-26 22:32:32 +02:00
|
|
|
|
var hitEntities = new List<IEntity>();
|
2018-05-27 16:44:50 +02:00
|
|
|
|
foreach (var entity in entities)
|
2018-04-05 17:32:51 -05:00
|
|
|
|
{
|
2019-04-20 16:20:18 -07:00
|
|
|
|
if (!entity.Transform.IsMapTransform || entity == eventArgs.User)
|
2018-04-05 17:32:51 -05:00
|
|
|
|
continue;
|
2018-05-27 16:44:50 +02:00
|
|
|
|
|
2019-06-30 00:01:41 +02:00
|
|
|
|
if (entity.TryGetComponent(out DamageableComponent damageComponent))
|
2018-04-05 17:32:51 -05:00
|
|
|
|
{
|
2020-02-13 15:57:40 +01:00
|
|
|
|
damageComponent.TakeDamage(DamageType.Brute, Damage, Owner, eventArgs.User);
|
2019-09-26 22:32:32 +02:00
|
|
|
|
hitEntities.Add(entity);
|
2018-04-05 17:32:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-09-26 22:32:32 +02:00
|
|
|
|
|
2020-05-21 19:40:08 +02:00
|
|
|
|
if(OnHitEntities(hitEntities)) return;
|
2020-05-13 20:21:03 +02:00
|
|
|
|
|
2020-05-31 12:40:36 -05:00
|
|
|
|
var audioSystem = EntitySystem.Get<AudioSystem>();
|
2020-02-08 18:25:40 +01:00
|
|
|
|
var emitter = hitEntities.Count == 0 ? eventArgs.User : hitEntities[0];
|
2020-06-07 08:55:15 -05:00
|
|
|
|
audioSystem.PlayFromEntity(hitEntities.Count > 0 ? _hitSound : "/Audio/weapons/punchmiss.ogg", emitter);
|
2019-09-26 22:32:32 +02:00
|
|
|
|
|
|
|
|
|
|
if (Arc != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var sys = _entitySystemManager.GetEntitySystem<MeleeWeaponSystem>();
|
|
|
|
|
|
sys.SendAnimation(Arc, angle, eventArgs.User, hitEntities);
|
|
|
|
|
|
}
|
2019-11-12 01:43:11 +01:00
|
|
|
|
|
2019-11-11 16:21:29 -05:00
|
|
|
|
_lastAttackTime = IoCManager.Resolve<IGameTiming>().CurTime;
|
2019-11-12 01:43:11 +01:00
|
|
|
|
|
|
|
|
|
|
if (Owner.TryGetComponent(out ItemCooldownComponent cooldown))
|
|
|
|
|
|
{
|
|
|
|
|
|
cooldown.CooldownStart = _lastAttackTime;
|
|
|
|
|
|
cooldown.CooldownEnd = _lastAttackTime + TimeSpan.FromSeconds(_cooldownTime);
|
|
|
|
|
|
}
|
2018-04-05 17:32:51 -05:00
|
|
|
|
}
|
2019-11-17 21:46:39 +01:00
|
|
|
|
|
|
|
|
|
|
private HashSet<IEntity> ArcRayCast(Vector2 position, Angle angle, IEntity ignore)
|
|
|
|
|
|
{
|
|
|
|
|
|
var widthRad = Angle.FromDegrees(ArcWidth);
|
2020-05-11 13:54:31 +02:00
|
|
|
|
var increments = 1 + (35 * (int) Math.Ceiling(widthRad / (2 * Math.PI)));
|
2020-01-15 14:51:01 +01:00
|
|
|
|
var increment = widthRad / increments;
|
2019-11-17 21:46:39 +01:00
|
|
|
|
var baseAngle = angle - widthRad / 2;
|
|
|
|
|
|
|
|
|
|
|
|
var resSet = new HashSet<IEntity>();
|
|
|
|
|
|
|
2020-01-25 12:15:04 -08:00
|
|
|
|
var mapId = Owner.Transform.MapID;
|
2020-01-15 14:51:01 +01:00
|
|
|
|
for (var i = 0; i < increments; i++)
|
2019-11-17 21:46:39 +01:00
|
|
|
|
{
|
|
|
|
|
|
var castAngle = new Angle(baseAngle + increment * i);
|
2020-05-23 17:46:50 +02:00
|
|
|
|
var res = _physicsManager.IntersectRay(mapId, new CollisionRay(position, castAngle.ToVec(), 23), _range, ignore).FirstOrDefault();
|
2019-11-17 21:46:39 +01:00
|
|
|
|
if (res.HitEntity != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
resSet.Add(res.HitEntity);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return resSet;
|
|
|
|
|
|
}
|
2018-04-05 17:32:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|