Added hook for CBaseAnimating::HandleAnimEvent()

This commit is contained in:
Blixibon 2021-03-12 15:33:48 -06:00
parent 97116a5eca
commit 3bb9a92ed8
5 changed files with 107 additions and 0 deletions

View File

@ -427,6 +427,11 @@ void CAnimationLayer::DispatchAnimEvents( CBaseAnimating *eventHandler, CBaseAni
event.eventtime = pOwner->m_flAnimTime + (flCycle - m_flCycle) / flCycleRate + pOwner->GetAnimTimeInterval();
}
#ifdef MAPBASE_VSCRIPT
if (eventHandler->m_ScriptScope.IsInitialized() && eventHandler->ScriptHookHandleAnimEvent( &event ) == false)
continue;
#endif
// Msg( "dispatch %d (%d : %.2f)\n", index - 1, event.event, event.eventtime );
eventHandler->HandleAnimEvent( &event );
}

View File

@ -284,6 +284,7 @@ END_SEND_TABLE()
#ifdef MAPBASE_VSCRIPT
ScriptHook_t CBaseAnimating::g_Hook_OnServerRagdoll;
ScriptHook_t CBaseAnimating::g_Hook_HandleAnimEvent;
#endif
BEGIN_ENT_SCRIPTDESC( CBaseAnimating, CBaseEntity, "Animating models" )
@ -342,6 +343,10 @@ BEGIN_ENT_SCRIPTDESC( CBaseAnimating, CBaseEntity, "Animating models" )
DEFINE_SCRIPTHOOK_PARAM( "ragdoll", FIELD_HSCRIPT )
DEFINE_SCRIPTHOOK_PARAM( "submodel", FIELD_BOOLEAN )
END_SCRIPTHOOK()
BEGIN_SCRIPTHOOK( CBaseAnimating::g_Hook_HandleAnimEvent, "HandleAnimEvent", FIELD_BOOLEAN, "Called when handling animation events. Return false to cancel base handling." )
DEFINE_SCRIPTHOOK_PARAM( "event", FIELD_HSCRIPT )
END_SCRIPTHOOK()
#endif
END_SCRIPTDESC();
@ -1243,6 +1248,11 @@ void CBaseAnimating::DispatchAnimEvents ( CBaseAnimating *eventHandler )
event.eventtime = m_flAnimTime + (flCycle - GetCycle()) / flCycleRate + GetAnimTimeInterval();
}
#ifdef MAPBASE_VSCRIPT
if (eventHandler->ScriptHookHandleAnimEvent( &event ) == false)
continue;
#endif
/*
if (m_debugOverlays & OVERLAY_NPC_SELECTED_BIT)
{
@ -1273,6 +1283,29 @@ void CBaseAnimating::DispatchAnimEvents ( CBaseAnimating *eventHandler )
}
}
#ifdef MAPBASE_VSCRIPT
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CBaseAnimating::ScriptHookHandleAnimEvent( animevent_t *pEvent )
{
if (m_ScriptScope.IsInitialized() && g_Hook_HandleAnimEvent.CanRunInScope(m_ScriptScope))
{
HSCRIPT hEvent = g_pScriptVM->RegisterInstance( pEvent );
// event
ScriptVariant_t args[] = { hEvent };
ScriptVariant_t returnValue = true;
g_Hook_HandleAnimEvent.Call( m_ScriptScope, &returnValue, args );
g_pScriptVM->RemoveInstance( hEvent );
return returnValue.m_bool;
}
return true;
}
#endif
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------

View File

@ -144,6 +144,9 @@ public:
bool HasAnimEvent( int nSequence, int nEvent );
virtual void DispatchAnimEvents ( CBaseAnimating *eventHandler ); // Handle events that have happend since last time called up until X seconds into the future
virtual void HandleAnimEvent( animevent_t *pEvent );
#ifdef MAPBASE_VSCRIPT
bool ScriptHookHandleAnimEvent( animevent_t *pEvent );
#endif
int LookupPoseParameter( CStudioHdr *pStudioHdr, const char *szName );
inline int LookupPoseParameter( const char *szName ) { return LookupPoseParameter(GetModelPtr(), szName); }
@ -211,6 +214,7 @@ public:
void SetSkin( int iSkin ) { m_nSkin = iSkin; }
static ScriptHook_t g_Hook_OnServerRagdoll;
static ScriptHook_t g_Hook_HandleAnimEvent;
#endif
// These return the attachment in the space of the entity

View File

@ -26,6 +26,7 @@
#include "con_nprint.h"
#include "particle_parse.h"
#include "npcevent.h"
#include "vscript_funcs_shared.h"
#include "vscript_singletons.h"
@ -501,6 +502,61 @@ FireBulletsInfo_t *GetFireBulletsInfoFromInfo( HSCRIPT hBulletsInfo )
return HScriptToClass<FireBulletsInfo_t>( hBulletsInfo );
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
CAnimEventTInstanceHelper g_AnimEventTInstanceHelper;
BEGIN_SCRIPTDESC_ROOT( animevent_t, "Handle for accessing animevent_t info." )
DEFINE_SCRIPT_INSTANCE_HELPER( &g_AnimEventTInstanceHelper )
END_SCRIPTDESC();
bool CAnimEventTInstanceHelper::Get( void *p, const char *pszKey, ScriptVariant_t &variant )
{
animevent_t *ani = ((animevent_t *)p);
if (FStrEq( pszKey, "event" ))
variant = ani->event;
else if (FStrEq( pszKey, "options" ))
variant = ani->options;
else if (FStrEq( pszKey, "cycle" ))
variant = ani->cycle;
else if (FStrEq( pszKey, "eventtime" ))
variant = ani->eventtime;
else if (FStrEq( pszKey, "type" ))
variant = ani->type;
else if (FStrEq( pszKey, "source" ))
variant = ToHScript(ani->pSource);
else
return false;
return true;
}
bool CAnimEventTInstanceHelper::Set( void *p, const char *pszKey, ScriptVariant_t &variant )
{
animevent_t *ani = ((animevent_t *)p);
if (FStrEq( pszKey, "event" ))
ani->event = variant;
else if (FStrEq( pszKey, "options" ))
ani->options = variant;
else if (FStrEq( pszKey, "cycle" ))
ani->cycle = variant;
else if (FStrEq( pszKey, "eventtime" ))
ani->eventtime = variant;
else if (FStrEq( pszKey, "type" ))
ani->type = variant;
else if (FStrEq( pszKey, "source" ))
{
CBaseEntity *pEnt = ToEnt( variant.m_hScript );
if (pEnt)
ani->pSource = pEnt->GetBaseAnimating();
}
else
return false;
return true;
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------

View File

@ -121,4 +121,13 @@ private:
HSCRIPT m_planeAccessor;
};
//-----------------------------------------------------------------------------
// Exposes animevent_t to VScript
//-----------------------------------------------------------------------------
class CAnimEventTInstanceHelper : public IScriptInstanceHelper
{
bool Get( void *p, const char *pszKey, ScriptVariant_t &variant );
bool Set( void *p, const char *pszKey, ScriptVariant_t &variant );
};
#endif