Added functions for execution of OnCollision*() and OnTrigger*()

This commit is contained in:
Kah Wei 2022-10-31 21:38:17 +08:00
parent 60c2c9facb
commit f9810684ed
4 changed files with 109 additions and 12 deletions

View File

@ -80,7 +80,10 @@ namespace SHADE
{
csScriptsExecuteFixedUpdate();
}
void SHScriptEngine::ExecuteCollisionFunctions()
{
csScriptsExecutePhysicsEvents();
}
void SHScriptEngine::Exit()
{
// Do not allow deinitialization if not initialised
@ -377,6 +380,12 @@ namespace SHADE
DEFAULT_CSHARP_NAMESPACE + ".ScriptStore",
"ExecuteLateUpdate"
);
csScriptsExecutePhysicsEvents = dotNet.GetFunctionPtr<CsFuncPtr>
(
DEFAULT_CSHARP_LIB_NAME,
DEFAULT_CSHARP_NAMESPACE + ".ScriptStore",
"ExecuteCollisionFunctions"
);
csScriptsFrameCleanUp = dotNet.GetFunctionPtr<CsFuncPtr>
(
DEFAULT_CSHARP_LIB_NAME,

View File

@ -98,6 +98,11 @@ namespace SHADE
/// </summary>
void ExecuteFixedUpdates();
/// <summary>
/// Executes the OnCollision*()s and OnTrigger*()s of the Scripts that are attached
/// to Entities.
/// </summary>
void ExecuteCollisionFunctions();
/// <summary>
/// Shuts down the DotNetRuntime.
/// </summary>
void Exit() override;
@ -245,6 +250,7 @@ namespace SHADE
CsFuncPtr csScriptsExecuteFixedUpdate = nullptr;
CsFuncPtr csScriptsExecuteUpdate = nullptr;
CsFuncPtr csScriptsExecuteLateUpdate = nullptr;
CsFuncPtr csScriptsExecutePhysicsEvents = nullptr;
CsFuncPtr csScriptsFrameCleanUp = nullptr;
CsScriptManipFuncPtr csScriptsAdd = nullptr;
CsScriptBasicFuncPtr csScriptsRemoveAll = nullptr;

View File

@ -28,6 +28,8 @@ of DigiPen Institute of Technology is prohibited.
#include "Engine/Entity.hxx"
#include "Serialisation/ReflectionUtilities.hxx"
#include "Engine/Application.hxx"
#include "Physics/SHPhysicsSystemInterface.h"
#include "Physics/SHPhysicsUtils.h"
namespace SHADE
{
@ -71,7 +73,7 @@ namespace SHADE
SAFE_NATIVE_CALL_BEGIN
Script^ script;
return AddScriptViaNameWithRef(entity, scriptName, script);
SAFE_NATIVE_CALL_END_N("SHADE.ScriptStore")
SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
return false;
}
@ -301,7 +303,7 @@ namespace SHADE
removeScript(script);
}
scriptList->Clear();
SAFE_NATIVE_CALL_END_N("SHADE.ScriptStore")
SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
}
void ScriptStore::RemoveAllScriptsImmediately(Entity entity, bool callOnDestroy)
{
@ -326,7 +328,7 @@ namespace SHADE
startList.Remove(script);
}
scriptList->Clear();
SAFE_NATIVE_CALL_END_N("SHADE.ScriptStore")
SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
}
/*---------------------------------------------------------------------------------*/
@ -365,7 +367,7 @@ namespace SHADE
startList.AddRange(%inactiveStartList);
inactiveStartList.Clear();
SAFE_NATIVE_CALL_END_N("SHADE.ScriptStore")
SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
}
void ScriptStore::FrameCleanUp()
{
@ -386,7 +388,7 @@ namespace SHADE
scripts.Remove(entity);
}
}
SAFE_NATIVE_CALL_END_N("SHADE.ScriptStore")
SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
}
void ScriptStore::Exit()
{
@ -410,7 +412,7 @@ namespace SHADE
startList.Clear();
disposalQueue.Clear();
scriptTypeList = nullptr;
SAFE_NATIVE_CALL_END_N("SHADE.ScriptStore")
SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
}
/*---------------------------------------------------------------------------------*/
@ -439,7 +441,7 @@ namespace SHADE
script->FixedUpdate();
}
}
SAFE_NATIVE_CALL_END_N("SHADE.ScriptStore")
SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
}
void ScriptStore::ExecuteUpdate()
{
@ -456,7 +458,7 @@ namespace SHADE
script->Update();
}
}
SAFE_NATIVE_CALL_END_N("SHADE.ScriptStore")
SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
}
void ScriptStore::ExecuteLateUpdate()
{
@ -473,7 +475,83 @@ namespace SHADE
script->LateUpdate();
}
}
SAFE_NATIVE_CALL_END_N("SHADE.ScriptStore")
SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
}
void ScriptStore::ExecuteCollisionFunctions()
{
SAFE_NATIVE_CALL_BEGIN
/* Collisions */
const auto& collisions = SHPhysicsSystemInterface::GetCollisionInfo();
for (const auto& collisionInfo : collisions)
{
const EntityID OWNER = collisionInfo.GetEntityA();
// Don't bother if this object has no scripts or is inactive
if (!isEntityActive(OWNER) || !scripts.ContainsKey(OWNER))
continue;
// Construct the collision state object
CollisionInfo info;
info.GameObject = GameObject(OWNER);
// Call all of the script's functions
auto entityScripts = scripts[OWNER];
if (entityScripts->Count > 0)
{
for each (Script^ script in entityScripts)
{
switch (collisionInfo.GetCollisionState())
{
case SHCollisionEvent::State::ENTER:
script->OnCollisionEnter(info);
break;
case SHCollisionEvent::State::STAY:
script->OnCollisionStay(info);
break;
case SHCollisionEvent::State::EXIT:
script->OnCollisionExit(info);
break;
}
}
}
}
/* Triggers */
const auto& triggers = SHPhysicsSystemInterface::GetTriggerInfo();
for (const auto& triggerInfo : triggers)
{
const EntityID OWNER = triggerInfo.GetEntityA();
// Don't bother if this object has no scripts or is inactive
if (!isEntityActive(OWNER) || !scripts.ContainsKey(OWNER))
continue;
// Construct the collision state object
CollisionInfo info;
info.GameObject = GameObject(OWNER);
// Call all of the script's functions
auto entityScripts = scripts[OWNER];
if (entityScripts->Count > 0)
{
for each (Script ^ script in entityScripts)
{
switch (triggerInfo.GetCollisionState())
{
case SHCollisionEvent::State::ENTER:
script->OnTriggerEnter(info);
break;
case SHCollisionEvent::State::STAY:
script->OnTriggerStay(info);
break;
case SHCollisionEvent::State::EXIT:
script->OnTriggerExit(info);
break;
}
}
}
}
SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
}
bool ScriptStore::SerialiseScripts(Entity entity, System::IntPtr yamlNodePtr)
@ -509,7 +587,7 @@ namespace SHADE
}
return true;
SAFE_NATIVE_CALL_END_N("SHADE.ScriptStore")
SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
return false;
}
@ -559,7 +637,7 @@ namespace SHADE
}
return true;
SAFE_NATIVE_CALL_END_N("SHADE.ScriptStore")
SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
return false;
}

View File

@ -233,6 +233,10 @@ namespace SHADE
/// Executes LateUpdate() for all scripts.
/// </summary>
static void ExecuteLateUpdate();
/// <summary>
/// Executes OnCollision*() and OnTrigger*() for all scripts.
/// </summary>
static void ExecuteCollisionFunctions();
/*-----------------------------------------------------------------------------*/
/* Serialisation Functions */