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:
@@ -52,6 +52,8 @@ static class ScriptEngine
|
||||
|
||||
LoadAssembly("resources/scripts/ScriptCore.dll");
|
||||
|
||||
ScriptGlue.RegisterManagedComponents();
|
||||
|
||||
//Samples();
|
||||
}
|
||||
|
||||
|
||||
@@ -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,14 +139,55 @@ 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);
|
||||
@@ -113,8 +195,8 @@ static class ScriptGlue
|
||||
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
|
||||
{
|
||||
|
||||
@@ -123,6 +123,12 @@ static class Mono
|
||||
|
||||
[LinkName(.C)]
|
||||
public static extern void mono_field_set_value(MonoObject* obj, MonoClassField* field, void* value);
|
||||
|
||||
[LinkName(.C)]
|
||||
public static extern MonoType* mono_reflection_type_from_name(char8* name, MonoImage* image);
|
||||
|
||||
[LinkName(.C)]
|
||||
public static extern MonoType* mono_reflection_type_get_type(MonoReflectionType* reflectionType);
|
||||
}
|
||||
|
||||
struct MonoDomain;
|
||||
@@ -145,6 +151,10 @@ struct MonoException;
|
||||
|
||||
struct MonoClassField;
|
||||
|
||||
struct MonoType;
|
||||
|
||||
struct MonoReflectionType;
|
||||
|
||||
enum MonoImageOpenStatus
|
||||
{
|
||||
Ok,
|
||||
|
||||
@@ -2,7 +2,7 @@ using GlitchyEngine.Core;
|
||||
|
||||
namespace GlitchyEngine;
|
||||
|
||||
public class Component : EngineObject
|
||||
public abstract class Component
|
||||
{
|
||||
|
||||
public Entity Entity { get; internal set; }
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using GlitchyEngine.Math;
|
||||
|
||||
namespace GlitchyEngine;
|
||||
|
||||
public class RigidBody2D : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// Apply a force at a world point.
|
||||
/// If the force is not applied at the center of mass, it will generate a torque and affect the angular velocity.
|
||||
/// This wakes up the body.
|
||||
/// </summary>
|
||||
/// <param name="force">The world force vector, usually in Newtons (N).</param>
|
||||
/// <param name="point">The world position of the point of application.</param>
|
||||
/// <param name="wakeUp">Wake up the body</param>
|
||||
public void ApplyForce(Vector2 force, Vector2 point, bool wakeUp = true)
|
||||
{
|
||||
ScriptGlue.RigidBody2D_ApplyForce(Entity._uuid, force, point, wakeUp);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Apply a force to the center of mass.
|
||||
/// This wakes up the body.
|
||||
/// </summary>
|
||||
/// <param name="force">The world force vector, usually in Newtons (N).</param>
|
||||
/// <param name="wakeUp">Wake up the body</param>
|
||||
public void ApplyForceToCenter(Vector2 force, bool wakeUp = true)
|
||||
{
|
||||
ScriptGlue.RigidBody2D_ApplyForceToCenter(Entity._uuid, force, wakeUp);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,20 @@
|
||||
using GlitchyEngine.Core;
|
||||
using GlitchyEngine.Math;
|
||||
|
||||
namespace GlitchyEngine;
|
||||
|
||||
public class Transform : EngineObject
|
||||
public class Transform : Component
|
||||
{
|
||||
|
||||
public Vector3 Translation
|
||||
{
|
||||
get
|
||||
{
|
||||
ScriptGlue.Transform_GetTranslation(Entity.UUID, out Vector3 translation);
|
||||
return translation;
|
||||
}
|
||||
set
|
||||
{
|
||||
ScriptGlue.Transform_SetTranslation(Entity.UUID, in value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+99
-9
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using GlitchyEngine.Core;
|
||||
using GlitchyEngine.Math;
|
||||
|
||||
@@ -27,22 +28,111 @@ public abstract class Entity : EngineObject
|
||||
// _uuid = uuid;
|
||||
//}
|
||||
|
||||
public T GetComponent<T>() where T : Component
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool HasComponent<T>() => HasComponent(typeof(T));
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool HasComponent(Type type) => ScriptGlue.Entity_HasComponent(_uuid, type);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the component with the specified type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the component to get.</typeparam>
|
||||
/// <returns>The component or null, if the entity doesn't have a component of the specified type.</returns>
|
||||
public T GetComponent<T>() where T : Component, new()
|
||||
{
|
||||
Log.Info("Trying to get component");
|
||||
|
||||
if (HasComponent<T>())
|
||||
{
|
||||
return new T
|
||||
{
|
||||
Entity = this
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
//public Transform Transform => new (){_uuid = _uuid};
|
||||
/// <summary>
|
||||
/// Gets the component with the given type.
|
||||
/// </summary>
|
||||
/// <param name="componentType">The type of the component to get.</param>
|
||||
/// <returns>The component or null, if the entity doesn't have a component of the given type.</returns>
|
||||
/// <remarks>For performance reasons it is recommended to use <see cref="GetComponent{T}"/> if possible.</remarks>
|
||||
public Component GetComponent(Type componentType)
|
||||
{
|
||||
// TODO: probably throw!
|
||||
if (!componentType.IsSubclassOf(typeof(Component))) return null;
|
||||
|
||||
if (HasComponent(componentType))
|
||||
{
|
||||
return Activator.CreateInstance(componentType) as Component;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the component with the specified component type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the component to add.</typeparam>
|
||||
/// <returns>The component.</returns>
|
||||
public T AddComponent<T>() where T : Component, new()
|
||||
{
|
||||
ScriptGlue.Entity_AddComponent(_uuid, typeof(T));
|
||||
|
||||
return new T
|
||||
{
|
||||
Entity = this
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the component with the given component type.
|
||||
/// </summary>
|
||||
/// <param name="componentType">The type of the component to add.</param>
|
||||
/// <returns>The component or null, if the given type is not a valid component type.</returns>
|
||||
/// <remarks>For performance reasons it is recommended to use <see cref="AddComponent{T}"/> if possible.</remarks>
|
||||
public Component AddComponent(Type componentType)
|
||||
{
|
||||
// TODO: probably throw!
|
||||
if (!componentType.IsSubclassOf(typeof(Component))) return null;
|
||||
|
||||
ScriptGlue.Entity_AddComponent(_uuid, componentType);
|
||||
|
||||
Component component = Activator.CreateInstance(componentType) as Component;
|
||||
return component;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the component with the specified component type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the component to remove.</typeparam>
|
||||
public void RemoveComponent<T>() where T : Component, new()
|
||||
{
|
||||
ScriptGlue.Entity_RemoveComponent(_uuid, typeof(T));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the component with the given component type.
|
||||
/// </summary>
|
||||
/// <param name="componentType">The type of the component to remove.</param>
|
||||
/// <remarks>For performance reasons it is recommended to use <see cref="RemoveComponent{T}"/> if possible.</remarks>
|
||||
public void RemoveComponent(Type componentType)
|
||||
{
|
||||
// TODO: probably throw!
|
||||
if (!componentType.IsSubclassOf(typeof(Component))) return;
|
||||
|
||||
ScriptGlue.Entity_RemoveComponent(_uuid, componentType);
|
||||
}
|
||||
|
||||
public Transform Transform => GetComponent<Transform>();
|
||||
|
||||
public Vector3 Translation
|
||||
{
|
||||
get
|
||||
{
|
||||
ScriptGlue.Entity_GetTranslation(_uuid, out Vector3 translation);
|
||||
return translation;
|
||||
}
|
||||
|
||||
set => ScriptGlue.Entity_SetTranslation(_uuid, value);
|
||||
get => Transform.Translation;
|
||||
set => Transform.Translation = value;
|
||||
}
|
||||
|
||||
// Will be executed once after the entity as be created.
|
||||
|
||||
@@ -121,6 +121,17 @@ public struct Vector3
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
var hashCode = X.GetHashCode();
|
||||
hashCode = (hashCode * 397) ^ Y.GetHashCode();
|
||||
hashCode = (hashCode * 397) ^ Z.GetHashCode();
|
||||
return hashCode;
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"X:{X}, Y:{Y}, Z:{Z}";
|
||||
|
||||
+17
-10
@@ -5,7 +5,7 @@ namespace GlitchyEngine;
|
||||
|
||||
class MyTestEntity : Entity
|
||||
{
|
||||
//Rigidbody2D _rigidBody;
|
||||
RigidBody2D _rigidBody;
|
||||
|
||||
/// <summary>
|
||||
/// Called after the script component was created. (The entity might not be fully created yet)
|
||||
@@ -14,7 +14,7 @@ class MyTestEntity : Entity
|
||||
{
|
||||
Log.Info($"Create! {UUID}");
|
||||
|
||||
//_rigidBody = GetComponent<Rigidbody2D>();
|
||||
_rigidBody = GetComponent<RigidBody2D>();
|
||||
}
|
||||
|
||||
///// <summary>
|
||||
@@ -31,18 +31,25 @@ class MyTestEntity : Entity
|
||||
/// <param name="deltaTime"></param>
|
||||
void OnUpdate(float deltaTime)
|
||||
{
|
||||
Vector2 force = Vector2.Zero;
|
||||
|
||||
if (Input.IsKeyPressed(Key.A))
|
||||
{
|
||||
Log.Info($"HALLO!");
|
||||
|
||||
Vector3 translation = Translation;
|
||||
translation.Y += 1.0f * deltaTime;
|
||||
|
||||
Translation = translation;
|
||||
|
||||
//_rigidBody.ApplyImpulse(new Vector2(0, 10));
|
||||
force.X -= 1000 * deltaTime;
|
||||
}
|
||||
|
||||
if (Input.IsKeyPressed(Key.D))
|
||||
{
|
||||
force.X += 1000 * deltaTime;
|
||||
}
|
||||
|
||||
if (Input.IsKeyPressing(Key.Space))
|
||||
{
|
||||
force.Y += 2000;
|
||||
}
|
||||
|
||||
_rigidBody.ApplyForceToCenter(force);
|
||||
|
||||
if (Input.IsMouseButtonReleasing(MouseButton.LeftButton))
|
||||
{
|
||||
Log.Info("Ouha!");
|
||||
|
||||
@@ -44,6 +44,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Components\Component.cs" />
|
||||
<Compile Include="Components\RigidBody2D.cs" />
|
||||
<Compile Include="Components\Transform.cs" />
|
||||
<Compile Include="Core\EngineObject.cs" />
|
||||
<Compile Include="Core\UUID.cs" />
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using GlitchyEngine.Core;
|
||||
using GlitchyEngine.Math;
|
||||
@@ -9,8 +10,37 @@ namespace GlitchyEngine;
|
||||
/// </summary>
|
||||
internal static class ScriptGlue
|
||||
{
|
||||
#region Entity
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Entity_GetTranslation(UUID entityId, out Vector3 translation);
|
||||
internal static extern void Entity_AddComponent(UUID entityId, Type componentType);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Entity_SetTranslation(UUID entityId, in Vector3 translation);
|
||||
internal static extern bool Entity_HasComponent(UUID entityId, Type componentType);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Entity_RemoveComponent(UUID entityId, Type componentType);
|
||||
|
||||
#endregion Entity
|
||||
|
||||
#region TransformComponent
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Transform_GetTranslation(UUID entityId, out Vector3 translation);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Transform_SetTranslation(UUID entityId, in Vector3 translation);
|
||||
|
||||
#endregion TransformComponent
|
||||
|
||||
#region RigidBody2D
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void RigidBody2D_ApplyForce(UUID entityId, in Vector2 force, Vector2 point, bool wakeUp);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void RigidBody2D_ApplyForceToCenter(UUID entityId, in Vector2 force, bool wakeUp);
|
||||
|
||||
#endregion RigidBody2D
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user