mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Added Entity event OnCollisionEnter2D
This commit is contained in:
@@ -2,6 +2,8 @@ using NetHostBeef;
|
||||
using System;
|
||||
using System.Interop;
|
||||
using GlitchyEngine.Core;
|
||||
using GlitchyEngine.Scripting.Classes;
|
||||
|
||||
using static GlitchyEngine.Scripting.ScriptGlue;
|
||||
|
||||
namespace GlitchyEngine.Scripting;
|
||||
@@ -11,6 +13,7 @@ public struct ScriptFunctionPointers
|
||||
public function void(UUID entityId) OnCreate;
|
||||
public function void(UUID entityId, float deltaTime) OnUpdate;
|
||||
public function void(UUID entityId, bool callEntity) OnDestroy;
|
||||
public function void(UUID entityId, Collision2D collision) OnCollisionEnter2D;
|
||||
}
|
||||
|
||||
static class CoreClrHelper
|
||||
@@ -124,6 +127,7 @@ static class CoreClrHelper
|
||||
GetFunctionPointerUnmanagedCallersOnly("GlitchyEngine.ScriptGlue, ScriptCore", "InvokeEntityOnCreate", out _entityScriptFunctions.OnCreate);
|
||||
GetFunctionPointerUnmanagedCallersOnly("GlitchyEngine.ScriptGlue, ScriptCore", "InvokeEntityOnUpdate", out _entityScriptFunctions.OnUpdate);
|
||||
GetFunctionPointerUnmanagedCallersOnly("GlitchyEngine.ScriptGlue, ScriptCore", "InvokeEntityOnDestroy", out _entityScriptFunctions.OnDestroy);
|
||||
GetFunctionPointerUnmanagedCallersOnly("GlitchyEngine.ScriptGlue, ScriptCore", "InvokeEntityOnCollisionEnter2D", out _entityScriptFunctions.OnCollisionEnter2D);
|
||||
}
|
||||
|
||||
public static void LoadAppAssembly(Span<uint8> appAssemblyData, Span<uint8> pdbData)
|
||||
|
||||
@@ -100,4 +100,10 @@ class NewScriptInstance : RefCounter
|
||||
|
||||
ScriptEngine.UnregisterScriptInstance(_entityId);
|
||||
}
|
||||
|
||||
public void InvokeOnCollisionEnter2D(Collision2D collision)
|
||||
{
|
||||
if (_scriptClass.Methods.HasFlag(.OnCollisionEnter2D))
|
||||
CoreClrHelper._entityScriptFunctions.OnCollisionEnter2D(_entityId, collision);
|
||||
}
|
||||
}
|
||||
@@ -392,6 +392,7 @@ static class ScriptEngine
|
||||
OnCreate = 0x1,
|
||||
OnUpdate = 0x2,
|
||||
OnDestroy = 0x4,
|
||||
OnCollisionEnter2D = 0x8,
|
||||
}
|
||||
|
||||
struct ScriptClassInfo
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace GlitchyEngine;
|
||||
/// <summary>
|
||||
/// The base class for all entities and scripts in the current world.
|
||||
/// </summary>
|
||||
public class Entity : EngineObject
|
||||
public partial class Entity : EngineObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the name of the <see cref="Entity"/>.
|
||||
@@ -451,4 +451,10 @@ public class Entity : EngineObject
|
||||
/// Will be called once when the entity is being destroyed.
|
||||
/// </summary>
|
||||
protected internal virtual void OnDestroy() { }
|
||||
|
||||
/// <summary>
|
||||
/// Will be called
|
||||
/// </summary>
|
||||
/// <param name="collision"></param>
|
||||
protected internal virtual void OnCollisionEnter2D(Collision2D collision) { }
|
||||
}
|
||||
|
||||
@@ -152,7 +152,8 @@ internal static unsafe partial class ScriptGlue
|
||||
None = 0,
|
||||
OnCreate = 0x1,
|
||||
OnUpdate = 0x2,
|
||||
OnDestroy = 0x4
|
||||
OnDestroy = 0x4,
|
||||
OnCollisionEnter2D = 0x8
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -210,6 +211,7 @@ internal static unsafe partial class ScriptGlue
|
||||
methods |= HasMethod(type, nameof(Entity.OnCreate), ScriptMethods.OnCreate);
|
||||
methods |= HasMethod(type, nameof(Entity.OnUpdate), ScriptMethods.OnUpdate);
|
||||
methods |= HasMethod(type, nameof(Entity.OnDestroy), ScriptMethods.OnDestroy);
|
||||
methods |= HasMethod(type, nameof(Entity.OnCollisionEnter2D), ScriptMethods.OnCollisionEnter2D);
|
||||
|
||||
bool runInEditMode = type.HasCustomAttribute<RunInEditModeAttribute>();
|
||||
|
||||
@@ -310,6 +312,20 @@ internal static unsafe partial class ScriptGlue
|
||||
}
|
||||
}
|
||||
|
||||
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
|
||||
public static void InvokeEntityOnCollisionEnter2D(UUID entityId, Collision2D collision)
|
||||
{
|
||||
try
|
||||
{
|
||||
(Entity entity, Type type) = EntityScriptInstances[entityId];
|
||||
entity.OnCollisionEnter2D(collision);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Exception(e);
|
||||
}
|
||||
}
|
||||
|
||||
[UnmanagedCallersOnly]
|
||||
public static void InvokeEntityOnDestroy(UUID entityId, byte callDestroy)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user