Collision callback now returns native instance if possible
This commit is contained in:
@@ -39,7 +39,7 @@ namespace Nuake {
|
|||||||
return scene->GetEntity(entityName).GetHandle();
|
return scene->GetEntity(entityName).GetHandle();
|
||||||
}
|
}
|
||||||
|
|
||||||
Coral::ManagedObject GetEntityScript(Coral::String entityName)
|
Coral::ManagedObject GetEntityScriptFromName(Coral::String entityName)
|
||||||
{
|
{
|
||||||
auto scene = Engine::GetCurrentScene();
|
auto scene = Engine::GetCurrentScene();
|
||||||
if (!scene->EntityExists(entityName))
|
if (!scene->EntityExists(entityName))
|
||||||
@@ -57,6 +57,24 @@ namespace Nuake {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Coral::ManagedObject GetEntityScriptFromHandle(int entityHandle)
|
||||||
|
{
|
||||||
|
auto scene = Engine::GetCurrentScene();
|
||||||
|
|
||||||
|
Entity entity = { (entt::entity)(entityHandle), scene.get()};
|
||||||
|
if (!entity.IsValid())
|
||||||
|
{
|
||||||
|
return Coral::ManagedObject(); // Error code: entity not found.
|
||||||
|
}
|
||||||
|
|
||||||
|
auto& scriptingEngine = ScriptingEngineNet::Get();
|
||||||
|
if (scriptingEngine.HasEntityScriptInstance(entity))
|
||||||
|
{
|
||||||
|
auto instance = scriptingEngine.GetEntityScript(entity);
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static enum ComponentTypes
|
static enum ComponentTypes
|
||||||
{
|
{
|
||||||
Unknown = -1,
|
Unknown = -1,
|
||||||
@@ -121,6 +139,17 @@ namespace Nuake {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool EntityHasManagedInstance(int handle)
|
||||||
|
{
|
||||||
|
Entity entity = { (entt::entity)(handle), Engine::GetCurrentScene().get() };
|
||||||
|
if (!entity.IsValid())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ScriptingEngineNet::Get().HasEntityScriptInstance(entity);
|
||||||
|
}
|
||||||
|
|
||||||
void TransformSetPosition(int entityId, float x, float y, float z)
|
void TransformSetPosition(int entityId, float x, float y, float z)
|
||||||
{
|
{
|
||||||
Entity entity = { (entt::entity)(entityId), Engine::GetCurrentScene().get() };
|
Entity entity = { (entt::entity)(entityId), Engine::GetCurrentScene().get() };
|
||||||
@@ -247,8 +276,10 @@ namespace Nuake {
|
|||||||
void Nuake::SceneNetAPI::RegisterMethods()
|
void Nuake::SceneNetAPI::RegisterMethods()
|
||||||
{
|
{
|
||||||
RegisterMethod("Entity.EntityHasComponentIcall", &EntityHasComponent);
|
RegisterMethod("Entity.EntityHasComponentIcall", &EntityHasComponent);
|
||||||
|
RegisterMethod("Entity.EntityHasManagedInstanceIcall", &EntityHasManagedInstance);
|
||||||
RegisterMethod("Scene.GetEntityIcall", &GetEntity);
|
RegisterMethod("Scene.GetEntityIcall", &GetEntity);
|
||||||
RegisterMethod("Scene.GetEntityScriptIcall", &GetEntityScript);
|
RegisterMethod("Scene.GetEntityScriptFromNameIcall", &GetEntityScriptFromName);
|
||||||
|
RegisterMethod("Scene.GetEntityScriptFromHandleIcall", &GetEntityScriptFromHandle);
|
||||||
|
|
||||||
// Components
|
// Components
|
||||||
RegisterMethod("TransformComponent.SetPositionIcall", &TransformSetPosition);
|
RegisterMethod("TransformComponent.SetPositionIcall", &TransformSetPosition);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using Coral.Managed.Interop;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
@@ -18,6 +19,7 @@ namespace Nuake.Net
|
|||||||
public class Entity
|
public class Entity
|
||||||
{
|
{
|
||||||
internal static unsafe delegate*<int, int, bool> EntityHasComponentIcall;
|
internal static unsafe delegate*<int, int, bool> EntityHasComponentIcall;
|
||||||
|
internal static unsafe delegate*<int, bool> EntityHasManagedInstanceIcall;
|
||||||
|
|
||||||
public enum ComponentTypes
|
public enum ComponentTypes
|
||||||
{
|
{
|
||||||
@@ -59,7 +61,22 @@ namespace Nuake.Net
|
|||||||
// Physics
|
// Physics
|
||||||
public void OnCollisionInternal(int entity1, int entity2)
|
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>()
|
protected static Dictionary<Type, ComponentTypes> MappingTypeEnum = new Dictionary<Type, ComponentTypes>()
|
||||||
|
|||||||
@@ -6,12 +6,26 @@ namespace Nuake.Net
|
|||||||
public class Scene
|
public class Scene
|
||||||
{
|
{
|
||||||
internal static unsafe delegate*<NativeString, int> GetEntityIcall;
|
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
|
public static T? GetEntity<T>(string entityName) where T : class
|
||||||
{
|
{
|
||||||
NativeInstance<Entity> handle;
|
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();
|
Entity? entity = handle.Get();
|
||||||
if (entity != null && entity is T)
|
if (entity != null && entity is T)
|
||||||
|
|||||||
Reference in New Issue
Block a user