Collision callback now returns native instance if possible

This commit is contained in:
Antoine Pilote
2024-04-18 23:09:56 -04:00
parent 41c07bf6d8
commit c981b45cac
3 changed files with 68 additions and 6 deletions

View File

@@ -1,4 +1,5 @@
using System;
using Coral.Managed.Interop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
@@ -18,6 +19,7 @@ namespace Nuake.Net
public class Entity
{
internal static unsafe delegate*<int, int, bool> EntityHasComponentIcall;
internal static unsafe delegate*<int, bool> EntityHasManagedInstanceIcall;
public enum ComponentTypes
{
@@ -59,7 +61,22 @@ namespace Nuake.Net
// Physics
public void OnCollisionInternal(int entity1, int entity2)
{
OnCollision(new Entity { ECSHandle = entity1 }, new Entity { ECSHandle = entity2 });
Entity entityInstance;
unsafe
{
bool hasInstance = EntityHasManagedInstanceIcall(entity2);
if (hasInstance)
{
entityInstance = Scene.GetEntity<Entity>(entity2);
}
else
{
entityInstance = new Entity { ECSHandle = entity2 };
}
}
OnCollision(new Entity { ECSHandle = entity1 }, entityInstance);
}
protected static Dictionary<Type, ComponentTypes> MappingTypeEnum = new Dictionary<Type, ComponentTypes>()

View File

@@ -6,12 +6,26 @@ namespace Nuake.Net
public class Scene
{
internal static unsafe delegate*<NativeString, int> GetEntityIcall;
internal static unsafe delegate*<NativeString, NativeInstance<Entity>> GetEntityScriptIcall;
internal static unsafe delegate*<NativeString, NativeInstance<Entity>> GetEntityScriptFromNameIcall;
internal static unsafe delegate*<int, NativeInstance<Entity>> GetEntityScriptFromHandleIcall;
public static T? GetEntity<T>(string entityName) where T : class
{
NativeInstance<Entity> handle;
unsafe { handle = GetEntityScriptIcall(entityName); }
unsafe { handle = GetEntityScriptFromNameIcall(entityName); }
Entity? entity = handle.Get();
if (entity != null && entity is T)
{
return entity as T;
}
return null;
}
public static T? GetEntity<T>(int entityHandle) where T : class
{
NativeInstance<Entity> handle;
unsafe { handle = GetEntityScriptFromHandleIcall(entityHandle); }
Entity? entity = handle.Get();
if (entity != null && entity is T)