Skip to content

Commit 148684c

Browse files
committed
Add marker click detection
1 parent 4ba63eb commit 148684c

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

Client/mods/deathmatch/logic/CClientGame.cpp

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ CVector g_vecBulletFireEndPosition;
7373
#define DOUBLECLICK_TIMEOUT 330
7474
#define DOUBLECLICK_MOVE_THRESHOLD 10.0f
7575

76+
// Ray casting constants for marker click detection
77+
constexpr float MARKER_CLICK_RAY_DEPTH = 300.0f; // Screen-to-world ray projection depth
78+
constexpr float MARKER_CLICK_MAX_DISTANCE = 99999.9f; // Maximum distance for closest marker comparison
79+
7680
static constexpr long long TIME_DISCORD_UPDATE_RATE = 15000;
7781

7882
CClientGame::CClientGame(bool bLocalPlay) : m_ServerInfo(new CServerInfo())
@@ -2325,7 +2329,7 @@ void CClientGame::ProcessServerControlBind(CControlFunctionBind* pBind)
23252329
m_pNetAPI->RPC(KEY_BIND, bitStream.pBitStream);
23262330
}
23272331

2328-
CClientEntity* CClientGame::CheckClientSideEntityClick(float fScreenX, float fScreenY)
2332+
CClientMarker* CClientGame::CheckMarkerClick(float fScreenX, float fScreenY, float& fDistance)
23292333
{
23302334
if (!m_pMarkerManager)
23312335
return nullptr;
@@ -2335,14 +2339,14 @@ CClientEntity* CClientGame::CheckClientSideEntityClick(float fScreenX, float fSc
23352339
pCamera->GetMatrix(&matCamera);
23362340
CVector vecOrigin = matCamera.vPos;
23372341

2338-
CVector vecTarget, vecScreen(fScreenX, fScreenY, 300.0f);
2342+
CVector vecTarget, vecScreen(fScreenX, fScreenY, MARKER_CLICK_RAY_DEPTH);
23392343
g_pCore->GetGraphics()->CalcWorldCoors(&vecScreen, &vecTarget);
23402344

23412345
CVector vecRayDir = vecTarget - vecOrigin;
23422346
vecRayDir.Normalize();
23432347

23442348
CClientMarker* pClosestMarker = nullptr;
2345-
float fClosestDist = 99999.9f;
2349+
float fClosestDist = MARKER_CLICK_MAX_DISTANCE;
23462350

23472351
for (auto* pMarker : m_pMarkerManager->m_Markers)
23482352
{
@@ -2370,6 +2374,9 @@ CClientEntity* CClientGame::CheckClientSideEntityClick(float fScreenX, float fSc
23702374
}
23712375
}
23722376

2377+
if (pClosestMarker)
2378+
fDistance = fClosestDist;
2379+
23732380
return pClosestMarker;
23742381
}
23752382

@@ -2441,9 +2448,6 @@ bool CClientGame::ProcessMessageForCursorEvents(HWND hwnd, UINT uMsg, WPARAM wPa
24412448
CVector vecCollision;
24422449
ElementID CollisionEntityID = INVALID_ELEMENT_ID;
24432450
CClientEntity* pCollisionEntity = nullptr;
2444-
float fObjectDistance = 99999.9f;
2445-
2446-
CClientEntity* pClientSideEntity = CheckClientSideEntityClick(static_cast<float>(iX), static_cast<float>(iY));
24472451

24482452
if (bCollision && pColPoint)
24492453
{
@@ -2454,7 +2458,6 @@ bool CClientGame::ProcessMessageForCursorEvents(HWND hwnd, UINT uMsg, WPARAM wPa
24542458
CClientEntity* pEntity = pPools->GetClientEntity((DWORD*)pGameEntity->GetInterface());
24552459
if (pEntity)
24562460
{
2457-
fObjectDistance = (vecCollision - vecOrigin).Length();
24582461
pCollisionEntity = pEntity;
24592462
if (!pEntity->IsLocalEntity())
24602463
CollisionEntityID = pEntity->GetID();
@@ -2470,22 +2473,6 @@ bool CClientGame::ProcessMessageForCursorEvents(HWND hwnd, UINT uMsg, WPARAM wPa
24702473
if (pColPoint)
24712474
pColPoint->Destroy();
24722475

2473-
if (pClientSideEntity)
2474-
{
2475-
CVector vecMarkerPos;
2476-
pClientSideEntity->GetPosition(vecMarkerPos);
2477-
float fMarkerDistance = (vecMarkerPos - vecOrigin).Length();
2478-
2479-
if (fMarkerDistance < fObjectDistance)
2480-
{
2481-
pCollisionEntity = pClientSideEntity;
2482-
if (!pClientSideEntity->IsLocalEntity())
2483-
CollisionEntityID = pClientSideEntity->GetID();
2484-
2485-
vecCollision = vecMarkerPos;
2486-
}
2487-
}
2488-
24892476
const char* szButton = NULL;
24902477
const char* szState = NULL;
24912478
switch (ucButtonHit)
@@ -2524,6 +2511,25 @@ bool CClientGame::ProcessMessageForCursorEvents(HWND hwnd, UINT uMsg, WPARAM wPa
25242511
if (std::isnan(vecCollision.fZ))
25252512
vecCollision.fZ = 0;
25262513

2514+
float fMarkerDistance = 0.0f;
2515+
CClientMarker* pClickedMarker = CheckMarkerClick(static_cast<float>(iX), static_cast<float>(iY), fMarkerDistance);
2516+
if (pClickedMarker)
2517+
{
2518+
CVector vecMarkerPosition;
2519+
pClickedMarker->GetPosition(vecMarkerPosition);
2520+
2521+
CLuaArguments MarkerArguments;
2522+
MarkerArguments.PushString(szButton);
2523+
MarkerArguments.PushString(szState);
2524+
MarkerArguments.PushNumber(vecCursorPosition.fX);
2525+
MarkerArguments.PushNumber(vecCursorPosition.fY);
2526+
MarkerArguments.PushNumber(vecMarkerPosition.fX);
2527+
MarkerArguments.PushNumber(vecMarkerPosition.fY);
2528+
MarkerArguments.PushNumber(vecMarkerPosition.fZ);
2529+
MarkerArguments.PushNumber(fMarkerDistance);
2530+
pClickedMarker->CallEvent("onClientMarkerClick", MarkerArguments, false);
2531+
}
2532+
25272533
// Call the event for the client
25282534
CLuaArguments Arguments;
25292535
Arguments.PushString(szButton);
@@ -2607,7 +2613,7 @@ bool CClientGame::ProcessMessageForCursorEvents(HWND hwnd, UINT uMsg, WPARAM wPa
26072613
CVector2D vecResolution = g_pCore->GetGUI()->GetResolution();
26082614
CVector2D vecCursorPosition(((float)iX) / vecResolution.fX, ((float)iY) / vecResolution.fY);
26092615

2610-
CVector vecTarget, vecScreen((float)iX, (float)iY, 300.0f);
2616+
CVector vecTarget, vecScreen((float)iX, (float)iY, MARKER_CLICK_RAY_DEPTH);
26112617
g_pCore->GetGraphics()->CalcWorldCoors(&vecScreen, &vecTarget);
26122618

26132619
// Call the onClientCursorMove event
@@ -2806,6 +2812,7 @@ void CClientGame::AddBuiltInEvents()
28062812
// Marker events
28072813
m_Events.AddEvent("onClientMarkerHit", "entity, matchingDimension", nullptr, false);
28082814
m_Events.AddEvent("onClientMarkerLeave", "entity, matchingDimension", nullptr, false);
2815+
m_Events.AddEvent("onClientMarkerClick", "button, state, screenX, screenY, worldX, worldY, worldZ, distance", nullptr, false);
28092816

28102817
m_Events.AddEvent("onClientPlayerMarkerHit", "marker, matchingDimension", nullptr, false);
28112818
m_Events.AddEvent("onClientPlayerMarkerLeave", "marker, matchingDimension", nullptr, false);

Client/mods/deathmatch/logic/CClientGame.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ class CClientGame
381381
void ProcessServerControlBind(CControlFunctionBind* pBind);
382382

383383
bool ProcessMessageForCursorEvents(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
384-
CClientEntity* CheckClientSideEntityClick(float fScreenX, float fScreenY);
384+
CClientMarker* CheckMarkerClick(float fScreenX, float fScreenY, float& fDistance);
385385
bool AreCursorEventsEnabled() { return m_bCursorEventsEnabled; }
386386
void SetCursorEventsEnabled(bool bCursorEventsEnabled) { m_bCursorEventsEnabled = bCursorEventsEnabled; }
387387

Client/mods/deathmatch/logic/CClientMarker.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,6 @@ void CClientMarker::SetSize(float fSize)
327327
break;
328328
}
329329
}
330-
331330
m_pMarker->SetSize(fSize);
332331
}
333332

0 commit comments

Comments
 (0)