Files
tbd-station-14/Content.Server/GameObjects/Components/Weapon/Melee/MeleeWeaponComponent.cs
metalgearsloth 4d064abcd7 Physics (#3485)
* Content side new physics structure

* BroadPhase outline done

* But we need to fix WorldAABB

* Fix static pvs AABB

* Fix import

* Rando fixes

* B is for balloon

* Change human mob hitbox to circle

* Decent movement

* Start adding friction to player controller

I think it's the best way to go about it to keep other objects somewhat consistent for physics.

* This baby can fit so many physics bugs in it.

* Slight mob mover optimisations.

* Player mover kinda works okay.

* Beginnings of testbed

* More testbed

* Circlestack bed

* Namespaces

* BB fixes

* Pull WorldAABB

* Joint pulling

* Semi-decent movement I guess.

* Pulling better

* Bullet controller + old movement

* im too dumb for this shit

* Use kinematic mob controller again

It's probably for the best TBH

* Stashed shitcode

* Remove SlipController

* In which movement code is entirely refactored

* Singularity fix

* Fix ApplyLinearImpulse

* MoveRelay fix

* Fix door collisions

* Disable subfloor collisions

Saves on broadphase a fair bit

* Re-implement ClimbController

* Zumzum's pressure

* Laggy item throwing

* Minor atmos change

* Some caching

* Optimise controllers

* Optimise CollideWith to hell and back

* Re-do throwing and tile friction

* Landing too

* Optimise controllers

* Move CCVars and other stuff swept is beautiful

* Cleanup a bunch of controllers

* Fix shooting and high pressure movement controller

* Flashing improvements

* Stuff and things

* Combat collisions

* Combat mode collisions

* Pulling distance joint again

* Cleanup physics interfaces

* More like scuffedularity

* Shit's fucked

* Haha tests go green

* Bigmoneycrab

* Fix dupe pulling

* Zumzum's based fix

* Don't run tile friction for non-predicted bodies

* Experimental pulling improvement

* Everything's a poly now

* Optimise AI region debugging a bit

Could still be better but should improve default performance a LOT

* Mover no updater

* Crazy kinematic body idea

* Good collisions

* KinematicController

* Fix aghost

* Throwing refactor

* Pushing cleanup

* Fix throwing and footstep sounds

* Frametime in ICollideBehavior

* Fix stuff

* Actually fix weightlessness

* Optimise collision behaviors a lot

* Make open lockers still collide with walls

* powwweeerrrrr

* Merge master proper

* AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

* AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

* AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

* Ch ch ch changesss

* SHIP IT

* Fix #if DEBUG

* Fix vaulting and item locker collision

* Fix throwing

* Editing yaml by hand what can go wrong

* on

* Last yaml fixes

* Okay now it's fixed

* Linter

Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
Co-authored-by: Vera Aguilera Puerto <zddm@outlook.es>
2021-03-08 04:09:59 +11:00

252 lines
8.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.Damage;
using Content.Shared.GameObjects.Components.Damage;
using Content.Shared.GameObjects.Components.Items;
using Content.Shared.Interfaces.GameObjects.Components;
using Content.Shared.Physics;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Physics;
using Robust.Shared.Prototypes;
using Robust.Shared.Physics.Broadphase;
using Robust.Shared.Serialization;
using Robust.Shared.Timing;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Weapon.Melee
{
[RegisterComponent]
public class MeleeWeaponComponent : Component, IAttack, IHandSelected
{
[Dependency] private readonly IPhysicsManager _physicsManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
public override string Name => "MeleeWeapon";
private TimeSpan _lastAttackTime;
private TimeSpan _cooldownEnd;
[DataField("hitSound")]
private string _hitSound = "/Audio/Weapons/genhit1.ogg";
[DataField("missSound")]
private string _missSound = "/Audio/Weapons/punchmiss.ogg";
[DataField("arcCooldownTime")]
public float ArcCooldownTime { get; private set; } = 1f;
[DataField("cooldownTime")]
public float CooldownTime { get; private set; } = 1f;
[ViewVariables(VVAccess.ReadWrite)]
[DataField("clickArc")]
public string ClickArc { get; set; } = "punch";
[ViewVariables(VVAccess.ReadWrite)]
[DataField("arc")]
public string Arc { get; set; } = "default";
[ViewVariables(VVAccess.ReadWrite)] [DataField("arcwidth")] public float ArcWidth { get; set; } = 90;
[ViewVariables(VVAccess.ReadWrite)]
[DataField("range")]
public float Range { get; set; } = 1;
[ViewVariables(VVAccess.ReadWrite)]
[DataField("damage")]
public int Damage { get; set; } = 5;
[ViewVariables(VVAccess.ReadWrite)]
[DataField("damageType")]
public DamageType DamageType { get; set; } = DamageType.Blunt;
[ViewVariables(VVAccess.ReadWrite)] [DataField("clickAttackEffect")] public bool ClickAttackEffect { get; set; } = true;
protected virtual bool OnHitEntities(IReadOnlyList<IEntity> entities, AttackEventArgs eventArgs)
{
return true;
}
bool IAttack.WideAttack(AttackEventArgs eventArgs)
{
if (!eventArgs.WideAttack) return true;
var curTime = _gameTiming.CurTime;
if (curTime < _cooldownEnd)
return true;
var location = eventArgs.User.Transform.Coordinates;
var diff = eventArgs.ClickLocation.ToMapPos(Owner.EntityManager) - location.ToMapPos(Owner.EntityManager);
var angle = Angle.FromWorldVec(diff);
// This should really be improved. GetEntitiesInArc uses pos instead of bounding boxes.
var entities = ArcRayCast(eventArgs.User.Transform.WorldPosition, angle, eventArgs.User);
var audioSystem = EntitySystem.Get<AudioSystem>();
if (entities.Count != 0)
{
audioSystem.PlayFromEntity(_hitSound, entities.First());
}
else
{
audioSystem.PlayFromEntity(_missSound, eventArgs.User);
}
var hitEntities = new List<IEntity>();
foreach (var entity in entities)
{
if (!entity.Transform.IsMapTransform || entity == eventArgs.User)
continue;
if (entity.TryGetComponent(out IDamageableComponent damageComponent))
{
damageComponent.ChangeDamage(DamageType, Damage, false, Owner);
hitEntities.Add(entity);
}
}
SendMessage(new MeleeHitMessage(hitEntities));
if (!OnHitEntities(hitEntities, eventArgs)) return false;
if (Arc != null)
{
var sys = EntitySystem.Get<MeleeWeaponSystem>();
sys.SendAnimation(Arc, angle, eventArgs.User, Owner, hitEntities);
}
_lastAttackTime = curTime;
_cooldownEnd = _lastAttackTime + TimeSpan.FromSeconds(ArcCooldownTime);
RefreshItemCooldown();
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.Coordinates;
var diff = eventArgs.ClickLocation.ToMapPos(Owner.EntityManager) - location.ToMapPos(Owner.EntityManager);
var angle = Angle.FromWorldVec(diff);
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);
}
SendMessage(new MeleeHitMessage(new List<IEntity> { target }));
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, false);
}
_lastAttackTime = curTime;
_cooldownEnd = _lastAttackTime + TimeSpan.FromSeconds(CooldownTime);
RefreshItemCooldown();
return true;
}
private HashSet<IEntity> ArcRayCast(Vector2 position, Angle angle, IEntity ignore)
{
var widthRad = Angle.FromDegrees(ArcWidth);
var increments = 1 + 35 * (int) Math.Ceiling(widthRad / (2 * Math.PI));
var increment = widthRad / increments;
var baseAngle = angle - widthRad / 2;
var resSet = new HashSet<IEntity>();
var mapId = Owner.Transform.MapID;
for (var i = 0; i < increments; i++)
{
var castAngle = new Angle(baseAngle + increment * i);
var res = EntitySystem.Get<SharedBroadPhaseSystem>().IntersectRay(mapId,
new CollisionRay(position, castAngle.ToVec(),
(int) (CollisionGroup.Impassable | CollisionGroup.MobImpassable)), Range, ignore).ToList();
if (res.Count != 0)
{
resSet.Add(res[0].HitEntity);
}
}
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
{
public readonly List<IEntity> HitEntities;
public MeleeHitMessage(List<IEntity> hitEntities)
{
HitEntities = hitEntities;
}
}
}