mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Added basic component access in Scripts
This commit is contained in:
@@ -6,11 +6,16 @@ using GlitchyEngine.Events;
|
||||
using GlitchyEngine.Math;
|
||||
using GlitchyEngine.World;
|
||||
using GlitchyEngine.Core;
|
||||
using System.Collections;
|
||||
|
||||
namespace GlitchyEngine.Scripting;
|
||||
|
||||
static class ScriptGlue
|
||||
{
|
||||
private static Dictionary<MonoType*, function void(Entity entityId)> s_AddComponentMethods = new .() ~ delete _;
|
||||
private static Dictionary<MonoType*, function bool(Entity entityId)> s_HasComponentMethods = new .() ~ delete _;
|
||||
private static Dictionary<MonoType*, function void(Entity entityId)> s_RemoveComponentMethods = new .() ~ delete _;
|
||||
|
||||
struct RegisterCallAttribute : Attribute
|
||||
{
|
||||
public String MethodName;
|
||||
@@ -47,12 +52,48 @@ static class ScriptGlue
|
||||
RegisterCalls();
|
||||
}
|
||||
|
||||
public static void RegisterManagedComponents()
|
||||
{
|
||||
RegisterComponent<TransformComponent>("GlitchyEngine.Transform");
|
||||
RegisterComponent<Rigidbody2DComponent>("GlitchyEngine.RigidBody2D");
|
||||
}
|
||||
|
||||
[RegisterMethod]
|
||||
private static void RegisterCalls()
|
||||
{
|
||||
// Generated at CompTime
|
||||
}
|
||||
|
||||
private static void RegisterComponent<T>(StringView cSharpClassName = "") where T : struct, new
|
||||
{
|
||||
String className;
|
||||
|
||||
if (cSharpClassName.IsWhiteSpace)
|
||||
{
|
||||
className = scope:: $"GlitchyEngine.";
|
||||
typeof(T).GetName(className);
|
||||
}
|
||||
else
|
||||
{
|
||||
className = scope:: String(cSharpClassName);
|
||||
}
|
||||
|
||||
className.EnsureNullTerminator();
|
||||
|
||||
MonoType* managedType = Mono.mono_reflection_type_from_name(className.CStr(), ScriptEngine.[Friend]s_CoreAssemblyImage);
|
||||
|
||||
if (managedType != null)
|
||||
{
|
||||
s_AddComponentMethods[managedType] = (entity) => entity.AddComponent<T>();
|
||||
s_HasComponentMethods[managedType] = (entity) => entity.HasComponent<T>();
|
||||
s_RemoveComponentMethods[managedType] = (entity) => entity.RemoveComponent<T>();
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.EngineLogger.AssertDebug(managedType != null, scope $"No C# component with name \"{{className}}\" found for Beef type \"{typeof(T)}\"");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[RegisterCall("Log::LogMessage_Impl")]
|
||||
static void Log(int32 logLevel, MonoString* message)
|
||||
@@ -98,23 +139,64 @@ static class ScriptGlue
|
||||
|
||||
#region Scene/Entity stuff
|
||||
|
||||
[RegisterCall("ScriptGlue::Entity_AddComponent")]
|
||||
static void Entity_AddComponent(UUID entityId, MonoReflectionType* componentType)
|
||||
{
|
||||
MonoType* type = Mono.mono_reflection_type_get_type(componentType);
|
||||
|
||||
Entity entity = ScriptEngine.Context.GetEntityByID(entityId);
|
||||
if (s_AddComponentMethods.TryGetValue(type, let addMethod))
|
||||
addMethod(entity);
|
||||
|
||||
Log.EngineLogger.AssertDebug(false, "No managed component with the given type registered.");
|
||||
}
|
||||
|
||||
[RegisterCall("ScriptGlue::Entity_HasComponent")]
|
||||
static bool Entity_HasComponent(UUID entityId, MonoReflectionType* componentType)
|
||||
{
|
||||
MonoType* type = Mono.mono_reflection_type_get_type(componentType);
|
||||
|
||||
Entity entity = ScriptEngine.Context.GetEntityByID(entityId);
|
||||
if (s_HasComponentMethods.TryGetValue(type, let hasMethod))
|
||||
return hasMethod(entity);
|
||||
|
||||
Log.EngineLogger.AssertDebug(false, "No managed component with the given type registered.");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
[RegisterCall("ScriptGlue::Entity_RemoveComponent")]
|
||||
static void Entity_RemoveComponent(UUID entityId, MonoReflectionType* componentType)
|
||||
{
|
||||
MonoType* type = Mono.mono_reflection_type_get_type(componentType);
|
||||
|
||||
Entity entity = ScriptEngine.Context.GetEntityByID(entityId);
|
||||
if (s_RemoveComponentMethods.TryGetValue(type, let removeMethod))
|
||||
removeMethod(entity);
|
||||
|
||||
Log.EngineLogger.AssertDebug(false, "No managed component with the given type registered.");
|
||||
}
|
||||
/*[RegisterCall("Input::IsMouseButtonReleasing")]
|
||||
static void Destroy()
|
||||
{
|
||||
// TODO:
|
||||
}*/
|
||||
|
||||
[RegisterCall("ScriptGlue::Entity_GetTranslation")]
|
||||
static void Entity_GetTranslation(UUID entityId, ref Vector3 translation)
|
||||
#endregion
|
||||
|
||||
#region TransformComponent
|
||||
|
||||
[RegisterCall("ScriptGlue::Transform_GetTranslation")]
|
||||
static void Transform_GetTranslation(UUID entityId, ref Vector3 translation)
|
||||
{
|
||||
Scene scene = ScriptEngine.Context;
|
||||
Entity entity = scene.GetEntityByID(entityId);
|
||||
|
||||
translation = entity.Transform.Position;
|
||||
}
|
||||
|
||||
[RegisterCall("ScriptGlue::Entity_SetTranslation")]
|
||||
static void Entity_SetTranslation(UUID entityId, ref Vector3 translation)
|
||||
|
||||
[RegisterCall("ScriptGlue::Transform_SetTranslation")]
|
||||
static void Transform_SetTranslation(UUID entityId, ref Vector3 translation)
|
||||
{
|
||||
Scene scene = ScriptEngine.Context;
|
||||
Entity entity = scene.GetEntityByID(entityId);
|
||||
@@ -122,7 +204,31 @@ static class ScriptGlue
|
||||
entity.Transform.Position = translation;
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endregion TransformComponent
|
||||
|
||||
#region RigidBody2D
|
||||
|
||||
[RegisterCall("ScriptGlue::RigidBody2D_ApplyForce")]
|
||||
static void RigidBody2D_ApplyForce(UUID entityId, ref Vector2 force, ref Vector2 point, bool wakeUp)
|
||||
{
|
||||
Scene scene = ScriptEngine.Context;
|
||||
Entity entity = scene.GetEntityByID(entityId);
|
||||
|
||||
var rigidBody = entity.GetComponent<Rigidbody2DComponent>();
|
||||
Box2D.Body.ApplyForce(rigidBody.[Friend]RuntimeBody, ref *(Box2D.b2Vec2*)&force, ref *(Box2D.b2Vec2*)&point, wakeUp);
|
||||
}
|
||||
|
||||
[RegisterCall("ScriptGlue::RigidBody2D_ApplyForceToCenter")]
|
||||
static void RigidBody2D_ApplyForceToCenter(UUID entityId, ref Vector2 force, bool wakeUp)
|
||||
{
|
||||
Scene scene = ScriptEngine.Context;
|
||||
Entity entity = scene.GetEntityByID(entityId);
|
||||
|
||||
var rigidBody = entity.GetComponent<Rigidbody2DComponent>();
|
||||
Box2D.Body.ApplyForceToCenter(rigidBody.[Friend]RuntimeBody, ref *(Box2D.b2Vec2*)&force, wakeUp);
|
||||
}
|
||||
|
||||
#endregion RigidBody2D
|
||||
|
||||
private static void RegisterCall<T>(String name, T method) where T : var
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user