Added support for collision and trigger events in code properly

This commit is contained in:
Kah Wei 2022-11-01 00:11:09 +08:00
parent b7aca5b118
commit e4cb8ede5a
3 changed files with 97 additions and 49 deletions

View File

@ -21,6 +21,7 @@
#include "Math/SHMathHelpers.h"
#include "Scene/SHSceneManager.h"
#include "Math/Transform/SHTransformComponent.h"
#include "Scripting/SHScriptEngine.h"
namespace SHADE
{
@ -353,7 +354,17 @@ namespace SHADE
{
system->SyncTransforms();
// TODO(Kah Wei): Take Collision & Trigger messages here
// Collision & Trigger messages
auto scriptSys = SHSystemManager::GetSystem<SHScriptEngine>();
if (scriptSys)
{
scriptSys->ExecuteFixedUpdates();
scriptSys->ExecuteCollisionFunctions();
}
else
{
SHLOG_WARNING("[SHPhysicsSystem] Unable to invoke collision and trigger script events due to missing SHScriptEngine!");
}
system->ClearInvalidCollisions();
}

View File

@ -485,18 +485,23 @@ namespace SHADE
const auto& collisions = SHPhysicsSystemInterface::GetCollisionInfo();
for (const auto& collisionInfo : collisions)
{
const EntityID OWNER = collisionInfo.GetEntityA();
auto entities =
{
std::make_pair(collisionInfo.GetEntityA(), collisionInfo.GetEntityB()),
std::make_pair(collisionInfo.GetEntityB(), collisionInfo.GetEntityA())
};
for (auto entity : entities)
{
// Don't bother if this object has no scripts or is inactive
if (!isEntityActive(OWNER) || !scripts.ContainsKey(OWNER))
if (!isEntityActive(entity.first) || !scripts.ContainsKey(entity.first))
continue;
// Construct the collision state object
CollisionInfo info;
info.GameObject = GameObject(OWNER);
info.GameObject = GameObject(entity.second);
// Call all of the script's functions
auto entityScripts = scripts[OWNER];
auto entityScripts = scripts[entity.first];
if (entityScripts->Count > 0)
{
for each (Script ^ script in entityScripts)
@ -516,22 +521,28 @@ namespace SHADE
}
}
}
}
/* Triggers */
const auto& triggers = SHPhysicsSystemInterface::GetTriggerInfo();
for (const auto& triggerInfo : triggers)
{
const EntityID OWNER = triggerInfo.GetEntityA();
auto entities =
{
std::make_pair(triggerInfo.GetEntityA(), triggerInfo.GetEntityB()),
std::make_pair(triggerInfo.GetEntityB(), triggerInfo.GetEntityA())
};
for (auto entity : entities)
{
// Don't bother if this object has no scripts or is inactive
if (!isEntityActive(OWNER) || !scripts.ContainsKey(OWNER))
if (!isEntityActive(entity.first) || !scripts.ContainsKey(entity.first))
continue;
// Construct the collision state object
CollisionInfo info;
info.GameObject = GameObject(OWNER);
info.GameObject = GameObject(entity.second);
// Call all of the script's functions
auto entityScripts = scripts[OWNER];
auto entityScripts = scripts[entity.first];
if (entityScripts->Count > 0)
{
for each (Script ^ script in entityScripts)
@ -551,6 +562,7 @@ namespace SHADE
}
}
}
}
SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
}

View File

@ -40,4 +40,29 @@ public class PhysicsTest : Script
}
Debug.Log($"{Transform.LocalPosition.y}");
}
protected override void onCollisionEnter(CollisionInfo info)
{
Debug.Log($"Collision Enter: {info.GameObject.Name}");
}
protected override void onCollisionStay(CollisionInfo info)
{
Debug.Log($"Collision Stay: {info.GameObject.Name}");
}
protected override void onCollisionExit(CollisionInfo info)
{
Debug.Log($"Collision Exit: {info.GameObject.Name}");
}
protected override void onTriggerEnter(CollisionInfo info)
{
Debug.Log($"Trigger Enter: {info.GameObject.Name}");
}
protected override void onTriggerStay(CollisionInfo info)
{
Debug.Log($"Trigger Stay: {info.GameObject.Name}");
}
protected override void onTriggerExit(CollisionInfo info)
{
Debug.Log($"Trigger Exit: {info.GameObject.Name}");
}
}