Added collision and trigger event functions for scripts and double multiplication with vectors #148

Merged
Pycorax merged 11 commits from SP3-6-c-scripting into main 2022-11-01 00:39:08 +08:00
18 changed files with 603 additions and 20 deletions

View File

@ -327,6 +327,11 @@ namespace SHADE
void SHPhysicsSystem::PhysicsFixedUpdate::Execute(double dt) noexcept void SHPhysicsSystem::PhysicsFixedUpdate::Execute(double dt) noexcept
{ {
auto* physicsSystem = reinterpret_cast<SHPhysicsSystem*>(GetSystem()); auto* physicsSystem = reinterpret_cast<SHPhysicsSystem*>(GetSystem());
auto scriptSys = SHSystemManager::GetSystem<SHScriptEngine>();
if (!scriptSys)
{
SHLOG_WARNING("[SHPhysicsSystem] Unable to invoke FixedUpdate() on scripts due to missing SHScriptEngine!");
}
fixedTimeStep = 1.0 / physicsSystem->fixedDT; fixedTimeStep = 1.0 / physicsSystem->fixedDT;
accumulatedTime += dt; accumulatedTime += dt;
@ -334,6 +339,10 @@ namespace SHADE
int count = 0; int count = 0;
while (accumulatedTime > fixedTimeStep) while (accumulatedTime > fixedTimeStep)
{ {
if (scriptSys)
{
scriptSys->ExecuteFixedUpdates();
}
physicsSystem->world->update(static_cast<rp3d::decimal>(fixedTimeStep)); physicsSystem->world->update(static_cast<rp3d::decimal>(fixedTimeStep));
accumulatedTime -= fixedTimeStep; accumulatedTime -= fixedTimeStep;
@ -352,10 +361,23 @@ namespace SHADE
// Interpolate transforms for rendering // Interpolate transforms for rendering
if (physicsSystem->worldUpdated) if (physicsSystem->worldUpdated)
{
physicsSystem->SyncTransforms(); physicsSystem->SyncTransforms();
// Collision & Trigger messages
auto scriptSys = SHSystemManager::GetSystem<SHScriptEngine>();
if (scriptSys)
{
scriptSys->ExecuteCollisionFunctions();
}
else
{
SHLOG_WARNING("[SHPhysicsSystem] Unable to invoke collision and trigger script events due to missing SHScriptEngine!");
}
physicsSystem->ClearInvalidCollisions(); physicsSystem->ClearInvalidCollisions();
} }
}
void SHPhysicsSystem::onContact(const CallbackData& callbackData) void SHPhysicsSystem::onContact(const CallbackData& callbackData)
{ {

View File

@ -0,0 +1,65 @@
/************************************************************************************//*!
\file SHPhysicsSystemInterface.cpp
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 31, 2022
\brief Contains the definitions of the functions of the static
SHPhysicsSystemInterface class.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
// Precompiled Headers
#include "SHpch.h"
// Primary Header
#include "SHPhysicsSystemInterface.h"
// Project Includes
#include "ECS_Base/Managers/SHSystemManager.h"
#include "Physics/SHPhysicsSystem.h"
#include "Physics/SHPhysicsUtils.h"
namespace SHADE
{
/*-----------------------------------------------------------------------------------*/
/* Static Usage Functions */
/*-----------------------------------------------------------------------------------*/
const std::vector<SHCollisionEvent>& SHPhysicsSystemInterface::GetCollisionInfo() noexcept
{
static std::vector<SHCollisionEvent> emptyVec;
auto phySystem = SHSystemManager::GetSystem<SHPhysicsSystem>();
if (phySystem)
{
return phySystem->GetCollisionInfo();
}
SHLOG_WARNING("[SHPhysicsSystemInterface] Failed to get collision events. Empty vector returned instead.");
return emptyVec;
}
const std::vector<SHCollisionEvent>& SHPhysicsSystemInterface::GetTriggerInfo() noexcept
{
static std::vector<SHCollisionEvent> emptyVec;
auto phySystem = SHSystemManager::GetSystem<SHPhysicsSystem>();
if (phySystem)
{
return phySystem->GetTriggerInfo();
}
SHLOG_WARNING("[SHPhysicsSystemInterface] Failed to get trigger events. Empty vector returned instead.");
return emptyVec;
}
double SHPhysicsSystemInterface::GetFixedDT() noexcept
{
auto phySystem = SHSystemManager::GetSystem<SHPhysicsSystem>();
if (phySystem)
{
return phySystem->GetFixedDT();
}
SHLOG_WARNING("[SHPhysicsSystemInterface] Failed to get fixed delta time. 0.0 returned instead.");
return 0.0;
}
}

View File

@ -0,0 +1,46 @@
/************************************************************************************//*!
\file SHPhysicsSystemInterface.h
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 31, 2022
\brief Contains the definition of the SHGraphicsSystemInterface static class.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
#pragma once
// STL Includes
#include <vector>
namespace SHADE
{
/*-----------------------------------------------------------------------------------*/
/* Forward Declarations */
/*-----------------------------------------------------------------------------------*/
class SHCollisionEvent;
/*-----------------------------------------------------------------------------------*/
/* Type Definitions */
/*-----------------------------------------------------------------------------------*/
/// <summary>
/// Static class that wraps up certain functions in the SHPhysicsSystem so that
/// accessing it from SHADE_Managed would not cause issues due to C++20 features.
/// </summary>
class SH_API SHPhysicsSystemInterface final
{
public:
/*---------------------------------------------------------------------------------*/
/* Constructor */
/*---------------------------------------------------------------------------------*/
SHPhysicsSystemInterface() = delete;
/*---------------------------------------------------------------------------------*/
/* Static Usage Functions */
/*---------------------------------------------------------------------------------*/
[[nodiscard]] static const std::vector<SHCollisionEvent>& GetCollisionInfo() noexcept;
[[nodiscard]] static const std::vector<SHCollisionEvent>& GetTriggerInfo() noexcept;
[[nodiscard]] static double GetFixedDT() noexcept;
};
}

View File

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

View File

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

View File

@ -14,6 +14,9 @@ of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/ *//*************************************************************************************/
// Precompiled Headers // Precompiled Headers
#include "SHpch.h" #include "SHpch.h"
// External Dependencies
#include "FRC/SHFramerateController.h"
#include "Physics/SHPhysicsSystemInterface.h"
// Primary Header // Primary Header
#include "Time.hxx" #include "Time.hxx"
@ -26,4 +29,8 @@ namespace SHADE
{ {
return SHFrameRateController::GetRawDeltaTime(); return SHFrameRateController::GetRawDeltaTime();
} }
double Time::FixedDeltaTime::get()
{
return SHPhysicsSystemInterface::GetFixedDT();
}
} }

View File

@ -14,8 +14,6 @@ of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/ *//*************************************************************************************/
#pragma once #pragma once
#include "FRC/SHFramerateController.h"
namespace SHADE namespace SHADE
{ {
/// <summary> /// <summary>
@ -29,13 +27,18 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/// <summary> /// <summary>
/// Time taken to process the previous frame. /// Time taken to process the previous frame.
/// Note, is affected by TimeScale. Use UnscaledDeltaTime if you wish to retrieve
/// real world time. This is also affected by MaxDeltaTime clamping that
/// UnscaledDeltaTime is subject to.
/// </summary> /// </summary>
static property double DeltaTime static property double DeltaTime
{ {
double get(); double get();
} }
/// <summary>
/// Time taken for Physics simulations. You should use this for operations
/// within Script.FixedUpdate()
/// </summary>
static property double FixedDeltaTime
{
double get();
}
}; };
} }

View File

@ -236,6 +236,22 @@ namespace SHADE
lhs.y * rhs.y lhs.y * rhs.y
); );
} }
Vector2 Vector2::operator*(Vector2 lhs, double rhs)
{
return Vector2
(
lhs.x * static_cast<float>(rhs),
lhs.y * static_cast<float>(rhs)
);
}
Vector2 Vector2::operator/(Vector2 lhs, double rhs)
{
return Vector2
(
lhs.x / static_cast<float>(rhs),
lhs.y / static_cast<float>(rhs)
);
}
Vector2 Vector2::operator*(Vector2 lhs, float rhs) Vector2 Vector2::operator*(Vector2 lhs, float rhs)
{ {
return Vector2 return Vector2

View File

@ -361,6 +361,22 @@ namespace SHADE
/// <param name="lhs">Vector2 to multiply with.</param> /// <param name="lhs">Vector2 to multiply with.</param>
/// <param name="rhs">Scalar to multiply with.</param> /// <param name="rhs">Scalar to multiply with.</param>
/// <returns>The result of the scalar multiplication.</returns> /// <returns>The result of the scalar multiplication.</returns>
static Vector2 operator*(Vector2 lhs, double rhs);
/// <summary>
/// Calculates the division of a Vector2 with a scalar value and returns
/// the result.
/// </summary>
/// <param name="lhs">Scalar to divide with.</param>
/// <param name="rhs">Vector2 to divide with.</param>
/// <returns>The result of the scalar division.</returns>
static Vector2 operator/(Vector2 lhs, double rhs);
/// <summary>
/// Calculates the multiplication of a Vector2 with a scalar value and returns
/// the result.
/// </summary>
/// <param name="lhs">Vector2 to multiply with.</param>
/// <param name="rhs">Scalar to multiply with.</param>
/// <returns>The result of the scalar multiplication.</returns>
static Vector2 operator*(Vector2 lhs, float rhs); static Vector2 operator*(Vector2 lhs, float rhs);
/// <summary> /// <summary>
/// Calculates the division of a Vector2 with a scalar value and returns /// Calculates the division of a Vector2 with a scalar value and returns

View File

@ -237,6 +237,24 @@ namespace SHADE
lhs.z * rhs.z lhs.z * rhs.z
); );
} }
Vector3 Vector3::operator*(Vector3 lhs, double rhs)
{
return Vector3
(
lhs.x * static_cast<float>(rhs),
lhs.y * static_cast<float>(rhs),
lhs.z * static_cast<float>(rhs)
);
}
Vector3 Vector3::operator/(Vector3 lhs, double rhs)
{
return Vector3
(
lhs.x / static_cast<float>(rhs),
lhs.y / static_cast<float>(rhs),
lhs.z / static_cast<float>(rhs)
);
}
Vector3 Vector3::operator*(Vector3 lhs, float rhs) Vector3 Vector3::operator*(Vector3 lhs, float rhs)
{ {
return Vector3 return Vector3

View File

@ -375,6 +375,22 @@ namespace SHADE
/// <param name="lhs">Vector3 to multiply with.</param> /// <param name="lhs">Vector3 to multiply with.</param>
/// <param name="rhs">Scalar to multiply with.</param> /// <param name="rhs">Scalar to multiply with.</param>
/// <returns>The result of the scalar multiplication.</returns> /// <returns>The result of the scalar multiplication.</returns>
static Vector3 operator*(Vector3 lhs, double rhs);
/// <summary>
/// Calculates the division of a Vector3 with a scalar value and returns
/// the result.
/// </summary>
/// <param name="lhs">Scalar to divide with.</param>
/// <param name="rhs">Vector3 to divide with.</param>
/// <returns>The result of the scalar division.</returns>
static Vector3 operator/(Vector3 lhs, double rhs);
/// <summary>
/// Calculates the multiplication of a Vector3 with a scalar value and returns
/// the result.
/// </summary>
/// <param name="lhs">Vector3 to multiply with.</param>
/// <param name="rhs">Scalar to multiply with.</param>
/// <returns>The result of the scalar multiplication.</returns>
static Vector3 operator*(Vector3 lhs, float rhs); static Vector3 operator*(Vector3 lhs, float rhs);
/// <summary> /// <summary>
/// Calculates the division of a Vector3 with a scalar value and returns /// Calculates the division of a Vector3 with a scalar value and returns

View File

@ -0,0 +1,36 @@
/************************************************************************************//*!
\file CollisionInfo.cxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 31, 2022
\brief Contains the definition of the functions of the managed CollisionInfo
struct.
Note: This file is written in C++17/CLI.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
#include "SHpch.h"
#include "CollisionInfo.hxx"
#include "Components/RigidBody.hxx"
#include "Components/Collider.hxx"
namespace SHADE
{
Collider^ CollisionInfo::Collider::get()
{
return GameObject.GetComponent<SHADE::Collider^>();
}
CollisionShape^ CollisionInfo::CollisionShape::get()
{
throw gcnew System::NotImplementedException();
}
RigidBody^ CollisionInfo::RigidBody::get()
{
return GameObject.GetComponent<SHADE::RigidBody^>();
}
}

View File

@ -0,0 +1,66 @@
/************************************************************************************//*!
\file CollisionInfo.hxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 31, 2022
\brief Contains the definition of the managed CollisionInfo struct with the
definition of its properties and declaration of functions.
Note: This file is written in C++17/CLI.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
#pragma once
// Project Includes
#include "Engine/GameObject.hxx"
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Forward Declarations */
/*---------------------------------------------------------------------------------*/
ref class RigidBody;
ref class Collider;
ref class CollisionShape;
/*---------------------------------------------------------------------------------*/
/* Type Definitions */
/*---------------------------------------------------------------------------------*/
/// <summary>
/// Struct that describes a collision
/// </summary>
public value struct CollisionInfo
{
public:
/*-----------------------------------------------------------------------------*/
/* Properties */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// The GameObject whose collider you are colliding with.
/// </summary>
property GameObject GameObject;
/// <summary>
/// The Collider that you are colliding with.
/// </summary>
property Collider^ Collider
{
SHADE::Collider^ get();
}
/// <summary>
/// The CollisionShape of the Collider that you are colliding with.
/// </summary>
property CollisionShape^ CollisionShape
{
SHADE::CollisionShape^ get();
}
/// <summary>
/// The RigidBody that you are colliding with.
/// </summary>
property RigidBody^ RigidBody
{
SHADE::RigidBody^ get();
}
};
}

View File

@ -147,6 +147,48 @@ namespace SHADE
SAFE_NATIVE_CALL_END(this) SAFE_NATIVE_CALL_END(this)
} }
void Script::OnCollisionEnter(CollisionInfo collision)
{
SAFE_NATIVE_CALL_BEGIN
onCollisionEnter(collision);
SAFE_NATIVE_CALL_END(this)
}
void Script::OnCollisionStay(CollisionInfo collision)
{
SAFE_NATIVE_CALL_BEGIN
onCollisionStay(collision);
SAFE_NATIVE_CALL_END(this)
}
void Script::OnCollisionExit(CollisionInfo collision)
{
SAFE_NATIVE_CALL_BEGIN
onCollisionExit(collision);
SAFE_NATIVE_CALL_END(this)
}
void Script::OnTriggerEnter(CollisionInfo collision)
{
SAFE_NATIVE_CALL_BEGIN
onTriggerEnter(collision);
SAFE_NATIVE_CALL_END(this)
}
void Script::OnTriggerStay(CollisionInfo collision)
{
SAFE_NATIVE_CALL_BEGIN
onTriggerStay(collision);
SAFE_NATIVE_CALL_END(this)
}
void Script::OnTriggerExit(CollisionInfo collision)
{
SAFE_NATIVE_CALL_BEGIN
onTriggerExit(collision);
SAFE_NATIVE_CALL_END(this)
}
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Constructors */ /* Constructors */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
@ -169,4 +211,14 @@ namespace SHADE
void Script::update() {} void Script::update() {}
void Script::lateUpdate() {} void Script::lateUpdate() {}
void Script::onDestroy() {} void Script::onDestroy() {}
}// namespace PlushieAPI
/*---------------------------------------------------------------------------------*/
/* Virtual Event Functions */
/*---------------------------------------------------------------------------------*/
void Script::onTriggerEnter(CollisionInfo) {}
void Script::onTriggerStay(CollisionInfo) {}
void Script::onTriggerExit(CollisionInfo) {}
void Script::onCollisionEnter(CollisionInfo) {}
void Script::onCollisionStay(CollisionInfo) {}
void Script::onCollisionExit(CollisionInfo) {}
}

View File

@ -15,6 +15,7 @@ of DigiPen Institute of Technology is prohibited.
// Project Includes // Project Includes
#include "Engine/GameObject.hxx" #include "Engine/GameObject.hxx"
#include "Physics/CollisionInfo.hxx"
namespace SHADE namespace SHADE
{ {
@ -213,6 +214,46 @@ namespace SHADE
/// </summary> /// </summary>
void OnDestroy(); void OnDestroy();
/*-----------------------------------------------------------------------------*/
/* Event Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Used to call onCollisionEnter(). This should be called when a collision is
/// detected between the attached GameObject and another GameObject.
/// </summary>
/// <param name="collision">Information on the collision event.</param>
void OnCollisionEnter(CollisionInfo collision);
/// <summary>
/// Used to call onCollisionStay(). This should be called when a collision is
/// persistent between the attached GameObject and another GameObject.
/// </summary>
/// <param name="collision">Information on the collision event.</param>
void OnCollisionStay(CollisionInfo collision);
/// <summary>
/// Used to call onCollisionExit(). This should be called when a collision ends
/// between the attached GameObject and another GameObject.
/// </summary>
/// <param name="collision">Information on the collision event.</param>
void OnCollisionExit(CollisionInfo collision);
/// <summary>
/// Used to call onTriggerEnter(). This should be called when a trigger-type
/// collision is detected between the attached GameObject and another GameObject.
/// </summary>
/// <param name="collision">Information on the collision event.</param>
void OnTriggerEnter(CollisionInfo collision);
/// <summary>
/// Used to call onTriggerStay(). This should be called when a trigger-type
/// collision is detected between the attached GameObject and another GameObject.
/// </summary>
/// <param name="collision">Information on the collision event.</param>
void OnTriggerStay(CollisionInfo collision);
/// <summary>
/// Used to call onTriggerExit(). This should be called when a trigger-type
/// collision is detected between the attached GameObject and another GameObject.
/// </summary>
/// <param name="collision">Information on the collision event.</param>
void OnTriggerExit(CollisionInfo collision);
protected: protected:
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Constructors */ /* Constructors */
@ -273,6 +314,46 @@ namespace SHADE
/// </summary> /// </summary>
virtual void onDestroy(); virtual void onDestroy();
/*-----------------------------------------------------------------------------*/
/* Virtual Event Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Called when the attached GameObject has a trigger Collider and collides with
/// another GameObject with a Collider in the first frame of collision.
/// </summary>
/// <param name="info">Information on the collision event.</param>
virtual void onTriggerEnter(CollisionInfo info);
/// <summary>
/// Called when the attached GameObject has a trigger Collider and collides with
/// another GameObject with a Collider in subsequent frames of collision.
/// </summary>
/// <param name="info">Information on the collision event.</param>
virtual void onTriggerStay(CollisionInfo info);
/// <summary>
/// Called when the attached GameObject has a trigger Collider and leaves a
/// collision with another GameObject with a Collider2D.
/// </summary>
/// <param name="info">Information on the collision event.</param>
virtual void onTriggerExit(CollisionInfo info);
/// <summary>
/// Called when the attached GameObject has a Collider and collides with
/// another GameObject with a Collider in the first frame of collision.
/// </summary>
/// <param name="info">Information on the collision event.</param>
virtual void onCollisionEnter(CollisionInfo info);
/// <summary>
/// Called when the attached GameObject has a Collider and collides with
/// another GameObject with a Collider in subsequent frames of collision.
/// </summary>
/// <param name="info">Information on the collision event.</param>
virtual void onCollisionStay(CollisionInfo info);
/// <summary>
/// Called when the attached GameObject has a Collider and leaves a
/// collision with another GameObject with a Collider2D.
/// </summary>
/// <param name="info">Information on the collision event.</param>
virtual void onCollisionExit(CollisionInfo info);
private: private:
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Data Members */ /* Data Members */
@ -280,4 +361,4 @@ namespace SHADE
GameObject owner; GameObject owner;
}; };
} // namespace PlushieAPI }

View File

@ -28,6 +28,8 @@ of DigiPen Institute of Technology is prohibited.
#include "Engine/Entity.hxx" #include "Engine/Entity.hxx"
#include "Serialisation/ReflectionUtilities.hxx" #include "Serialisation/ReflectionUtilities.hxx"
#include "Engine/Application.hxx" #include "Engine/Application.hxx"
#include "Physics/SHPhysicsSystemInterface.h"
#include "Physics/SHPhysicsUtils.h"
namespace SHADE namespace SHADE
{ {
@ -71,7 +73,7 @@ namespace SHADE
SAFE_NATIVE_CALL_BEGIN SAFE_NATIVE_CALL_BEGIN
Script^ script; Script^ script;
return AddScriptViaNameWithRef(entity, scriptName, script); return AddScriptViaNameWithRef(entity, scriptName, script);
SAFE_NATIVE_CALL_END_N("SHADE.ScriptStore") SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
return false; return false;
} }
@ -301,7 +303,7 @@ namespace SHADE
removeScript(script); removeScript(script);
} }
scriptList->Clear(); scriptList->Clear();
SAFE_NATIVE_CALL_END_N("SHADE.ScriptStore") SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
} }
void ScriptStore::RemoveAllScriptsImmediately(Entity entity, bool callOnDestroy) void ScriptStore::RemoveAllScriptsImmediately(Entity entity, bool callOnDestroy)
{ {
@ -326,7 +328,7 @@ namespace SHADE
startList.Remove(script); startList.Remove(script);
} }
scriptList->Clear(); 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); startList.AddRange(%inactiveStartList);
inactiveStartList.Clear(); inactiveStartList.Clear();
SAFE_NATIVE_CALL_END_N("SHADE.ScriptStore") SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
} }
void ScriptStore::FrameCleanUp() void ScriptStore::FrameCleanUp()
{ {
@ -386,7 +388,7 @@ namespace SHADE
scripts.Remove(entity); scripts.Remove(entity);
} }
} }
SAFE_NATIVE_CALL_END_N("SHADE.ScriptStore") SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
} }
void ScriptStore::Exit() void ScriptStore::Exit()
{ {
@ -410,7 +412,7 @@ namespace SHADE
startList.Clear(); startList.Clear();
disposalQueue.Clear(); disposalQueue.Clear();
scriptTypeList = nullptr; 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(); script->FixedUpdate();
} }
} }
SAFE_NATIVE_CALL_END_N("SHADE.ScriptStore") SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
} }
void ScriptStore::ExecuteUpdate() void ScriptStore::ExecuteUpdate()
{ {
@ -456,7 +458,7 @@ namespace SHADE
script->Update(); script->Update();
} }
} }
SAFE_NATIVE_CALL_END_N("SHADE.ScriptStore") SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
} }
void ScriptStore::ExecuteLateUpdate() void ScriptStore::ExecuteLateUpdate()
{ {
@ -473,7 +475,95 @@ namespace SHADE
script->LateUpdate(); 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)
{
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(entity.first) || !scripts.ContainsKey(entity.first))
continue;
// Construct the collision state object
CollisionInfo info;
info.GameObject = GameObject(entity.second);
// Call all of the script's functions
auto entityScripts = scripts[entity.first];
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)
{
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(entity.first) || !scripts.ContainsKey(entity.first))
continue;
// Construct the collision state object
CollisionInfo info;
info.GameObject = GameObject(entity.second);
// Call all of the script's functions
auto entityScripts = scripts[entity.first];
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) bool ScriptStore::SerialiseScripts(Entity entity, System::IntPtr yamlNodePtr)
@ -509,7 +599,7 @@ namespace SHADE
} }
return true; return true;
SAFE_NATIVE_CALL_END_N("SHADE.ScriptStore") SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
return false; return false;
} }
@ -559,7 +649,7 @@ namespace SHADE
} }
return true; return true;
SAFE_NATIVE_CALL_END_N("SHADE.ScriptStore") SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
return false; return false;
} }

View File

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

View File

@ -40,4 +40,34 @@ public class PhysicsTest : Script
} }
Debug.Log($"{Transform.LocalPosition.y}"); Debug.Log($"{Transform.LocalPosition.y}");
} }
protected override void fixedUpdate()
{
Debug.Log("Fixed Update");
}
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}");
}
} }