Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit 9d1aaf0

Browse files
committed
Added support for OnCollideWithMO and OnCollideWithTerrain to MovableObject
Added TODO about const_cast Added comment about lambda method in MovableObject::ReloadScripts
1 parent edb5d32 commit 9d1aaf0

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

Entities/MovableObject.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,9 @@ int MovableObject::ReloadScripts() {
543543
return 0;
544544
}
545545

546+
/// <summary>
547+
/// Internal lambda method to clear a given object's script configurations, and then load them all again in order to reset them.
548+
/// </summary>
546549
auto clearScriptConfigurationAndLoadPreexistingScripts = [](MovableObject *object) {
547550
std::vector<std::pair<std::string, bool>> loadedScriptsCopy = object->m_LoadedScripts;
548551
object->m_LoadedScripts.clear();
@@ -562,6 +565,7 @@ int MovableObject::ReloadScripts() {
562565
if (status >= 0) {
563566
return status;
564567
}
568+
//TODO consider getting rid of this const_cast. It would require either code duplication or creating some none const methods (specifically of PresetMan::GetEntityPreset, which may be unsafe. Could be this gross exceptional handling is the best way to go.
565569
MovableObject *pPreset = const_cast<MovableObject *>(dynamic_cast<const MovableObject *>(g_PresetMan.GetEntityPreset(GetClassName(), GetPresetName(), GetModuleID())));
566570
if (pPreset && pPreset != this) {
567571
status = clearScriptConfigurationAndLoadPreexistingScripts(pPreset);
@@ -782,9 +786,18 @@ bool MovableObject::IsAtRest()
782786

783787
bool MovableObject::OnMOHit(HitData &hd)
784788
{
789+
if (hd.pBody[HITOR] == this || hd.pBody[HITEE] == this) {
790+
RunScriptedFunctionInAppropriateScripts("OnCollideWithMO", false, false, {hd.pBody[hd.pBody[HITOR] == this ? HITEE : HITOR]});
791+
}
785792
return hd.terminate[hd.pRootBody[HITOR] == this ? HITOR : HITEE] = OnMOHit(hd.pRootBody[hd.pRootBody[HITOR] == this ? HITEE : HITOR]);
786793
}
787794

795+
void MovableObject::SetHitWhatTerrMaterial(unsigned char matID) {
796+
m_TerrainMatHit = matID;
797+
m_LastCollisionSimFrameNumber = g_MovableMan.GetSimUpdateFrameNumber();
798+
RunScriptedFunctionInAppropriateScripts("OnCollideWithTerrain", false, false, {}, {std::to_string(m_TerrainMatHit)});
799+
}
800+
788801

789802
//////////////////////////////////////////////////////////////////////////////////////////
790803
// Virtual method: ApplyForces

Entities/MovableObject.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,8 +1780,7 @@ ENTITYALLOCATION(MovableObject)
17801780
// Arguments: The ID of the material, if any, that this MO hit during the last Travel.
17811781
// Return value: None.
17821782

1783-
virtual void SetHitWhatTerrMaterial(unsigned char matID) { m_TerrainMatHit = matID; m_LastCollisionSimFrameNumber = g_MovableMan.GetSimUpdateFrameNumber(); }
1784-
1783+
virtual void SetHitWhatTerrMaterial(unsigned char matID);
17851784

17861785
virtual bool ProvidesPieMenuContext() const { return m_ProvidesPieMenuContext; }
17871786

0 commit comments

Comments
 (0)