Skip to content

Commit

Permalink
Only trigger melee attacks when they would actually hit something
Browse files Browse the repository at this point in the history
  • Loading branch information
fholger committed Jan 20, 2025
1 parent 5ba1b20 commit 9868d46
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
8 changes: 4 additions & 4 deletions Code/Melee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ const char *CMelee::GetDamageType() const


//------------------------------------------------------------------------
bool CMelee::PerformRayTest(const Vec3 &pos, const Vec3 &dir, float strength, bool remote)
bool CMelee::PerformRayTest(const Vec3 &pos, const Vec3 &dir, float strength, bool remote, bool execute)
{
IEntity *pOwner = m_pWeapon->GetOwner();
IPhysicalEntity *pIgnore = pOwner?pOwner->GetPhysics():0;
Expand All @@ -359,7 +359,7 @@ bool CMelee::PerformRayTest(const Vec3 &pos, const Vec3 &dir, float strength, bo
}
//=================================================================================

if (n>0)
if (n>0 && execute)
{
Hit(&hit, dir, strength, remote);
Impulse(hit.pt, dir, hit.n, hit.pCollider, hit.partid, hit.ipart, hit.surface_idx, strength);
Expand All @@ -369,7 +369,7 @@ bool CMelee::PerformRayTest(const Vec3 &pos, const Vec3 &dir, float strength, bo
}

//------------------------------------------------------------------------
bool CMelee::PerformCylinderTest(const Vec3 &pos, const Vec3 &dir, float strength, bool remote)
bool CMelee::PerformCylinderTest(const Vec3 &pos, const Vec3 &dir, float strength, bool remote, bool execute)
{
IEntity *pOwner = m_pWeapon->GetOwner();
IPhysicalEntity *pIgnore = pOwner?pOwner->GetPhysics():0;
Expand Down Expand Up @@ -436,7 +436,7 @@ bool CMelee::PerformCylinderTest(const Vec3 &pos, const Vec3 &dir, float strengt
}


if (closestc)
if (closestc && execute)
{
IPhysicalEntity *pCollider = gEnv->pPhysicalWorld->GetPhysicalEntityById(closestc->iPrim[0]);

Expand Down
8 changes: 4 additions & 4 deletions Code/Melee.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Description: Beam Fire Mode Implementation
-------------------------------------------------------------------------
History:
- 23:3:2006 13:02 : Created by Márcio Martins
- 23:3:2006 13:02 : Created by M�rcio Martins
*************************************************************************/
#ifndef __MELEE_H__
Expand Down Expand Up @@ -187,8 +187,8 @@ class CMelee :
virtual void Unlock() {};
//~IFireMode

virtual bool PerformRayTest(const Vec3 &pos, const Vec3 &dir, float strength, bool remote);
virtual bool PerformCylinderTest(const Vec3 &pos, const Vec3 &dir, float strength, bool remote);
virtual bool PerformRayTest(const Vec3 &pos, const Vec3 &dir, float strength, bool remote, bool execute = true);
virtual bool PerformCylinderTest(const Vec3 &pos, const Vec3 &dir, float strength, bool remote, bool execute = true);
virtual void Hit(ray_hit *hit, const Vec3 &dir, float damageScale, bool remote);
virtual void Hit(geom_contact *contact, const Vec3 &dir, float damageScale, bool remote);
virtual void Hit(const Vec3 &pt, const Vec3 &dir, const Vec3 &normal, IPhysicalEntity *pCollider, int partId, int ipart, int surfaceIdx, float damageScale, bool remote);
Expand Down Expand Up @@ -225,4 +225,4 @@ class CMelee :
};


#endif //__MELEE_H__
#endif //__MELEE_H__
10 changes: 9 additions & 1 deletion Code/VR/OpenXRInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "GameCVars.h"
#include "imgui.h"
#include "IPlayerInput.h"
#include "Melee.h"
#include "OpenXRRuntime.h"
#include "Player.h"
#include "VRManager.h"
Expand Down Expand Up @@ -519,7 +520,14 @@ void OpenXRInput::UpdateMeleeAttacks()
// also, only consider movements that are somewhat in the direction of where you look at
if (GetGripAmount(i) >= .95f && controllerVelocity.GetLength() > g_pGameCVars->vr_melee_trigger_velocity && controllerVelocity.Dot(hmdForward) > 0.7f)
{
weapon->MeleeAttack();
// check if the attack would actually hit something, otherwise don't perform one
SMovementState info;
player->GetMovementController()->GetMovementState(info);
Vec3 pos = info.eyePosition;
Vec3 dir = info.eyeDirection;
CMelee* melee = static_cast<CMelee*>(weapon->GetMeleeFireMode());
if (melee->PerformRayTest(pos, dir, 1.f, false, false) || melee->PerformCylinderTest(pos, dir, 1.f, false, false))
weapon->MeleeAttack();
break;
}
}
Expand Down

0 comments on commit 9868d46

Please sign in to comment.