mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
ScriptGlue: Added Camera component and throwing mono exceptions from beef
This commit is contained in:
@@ -69,6 +69,7 @@ static class ScriptGlue
|
||||
|
||||
RegisterComponent<TransformComponent>("GlitchyEngine.Core.Transform");
|
||||
RegisterComponent<Rigidbody2DComponent>("GlitchyEngine.Physics.Rigidbody2D");
|
||||
RegisterComponent<CameraComponent>("GlitchyEngine.Core.Camera");
|
||||
}
|
||||
|
||||
[RegisterMethod]
|
||||
@@ -107,6 +108,65 @@ static class ScriptGlue
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the component of the specified type that is attached to the given entity. Or null, if the entity doesn't exist or doesn't have the specified component.
|
||||
static T* GetComponentSafe<T>(UUID entityId) where T: struct, new
|
||||
{
|
||||
Result<Entity> foundEntity = ScriptEngine.Context.GetEntityByID(entityId);
|
||||
|
||||
if (foundEntity case .Ok(let entity))
|
||||
{
|
||||
if (entity.TryGetComponent<T>(let component))
|
||||
{
|
||||
return component;
|
||||
}
|
||||
else
|
||||
{
|
||||
ThrowArgumentException(null, scope $"The entity has no component of type {typeof(T)} or it was deleted.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ThrowArgumentException(null, "The entity doesn't exist or was deleted.");
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the component of the specified type that is attached to the given entity. Or null, if the entity doesn't exist or doesn't have the specified component.
|
||||
static bool TryGetComponentSafe<T>(UUID entityId, out T* component) where T: struct, new
|
||||
{
|
||||
Result<Entity> foundEntity = ScriptEngine.Context.GetEntityByID(entityId);
|
||||
|
||||
if (foundEntity case .Ok(let entity))
|
||||
{
|
||||
if (entity.TryGetComponent<T>(out component))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.ClientLogger.Error($"Entity {entityId} has no {nameof(T)}.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.ClientLogger.Error($"No entity with ID {entityId} found.");
|
||||
}
|
||||
|
||||
component = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
#region Exception Helpers
|
||||
|
||||
/// Throws an argument exception.
|
||||
[NoReturn]
|
||||
static void ThrowArgumentException(char8* argument, char8* message)
|
||||
{
|
||||
MonoException* exception = Mono.mono_get_exception_argument(argument, message);
|
||||
Mono.mono_raise_exception(exception);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Log
|
||||
|
||||
[RegisterCall("ScriptGlue::Log_LogMessage")]
|
||||
@@ -229,9 +289,19 @@ static class ScriptGlue
|
||||
{
|
||||
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);
|
||||
Result<Entity> foundEntity = ScriptEngine.Context.GetEntityByID(entityId);
|
||||
|
||||
if (foundEntity case .Ok(let entity))
|
||||
{
|
||||
if (s_HasComponentMethods.TryGetValue(type, let hasMethod))
|
||||
return hasMethod(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.ClientLogger.Warning($"No entity found with the given id \"{entityId}\".");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Log.EngineLogger.AssertDebug(false, "No managed component with the given type registered.");
|
||||
|
||||
@@ -319,11 +389,16 @@ static class ScriptGlue
|
||||
[RegisterCall("ScriptGlue::Entity_GetName")]
|
||||
static MonoString* Entity_GetName(UUID entityId)
|
||||
{
|
||||
Entity entity = ScriptEngine.Context.GetEntityByID(entityId);
|
||||
Result<Entity> foundEntity = ScriptEngine.Context.GetEntityByID(entityId);
|
||||
|
||||
MonoString* name = Mono.mono_string_new_len(ScriptEngine.[Friend]s_AppDomain, entity.Name.Ptr, (.)entity.Name.Length);
|
||||
if (foundEntity case .Ok(let entity))
|
||||
{
|
||||
MonoString* name = Mono.mono_string_new_len(ScriptEngine.[Friend]s_AppDomain, entity.Name.Ptr, (.)entity.Name.Length);
|
||||
|
||||
return name;
|
||||
return name;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
[RegisterCall("ScriptGlue::Entity_SetName")]
|
||||
@@ -472,6 +547,138 @@ static class ScriptGlue
|
||||
|
||||
#endregion Rigidbody2D
|
||||
|
||||
#region Camera
|
||||
|
||||
[RegisterCall("ScriptGlue::Camera_GetProjectionType")]
|
||||
static void Camera_GetProjectionType(UUID entityId, out SceneCamera.ProjectionType projectionType)
|
||||
{
|
||||
CameraComponent* camera = GetComponentSafe<CameraComponent>(entityId);
|
||||
projectionType = camera.Camera.ProjectionType;
|
||||
}
|
||||
|
||||
[RegisterCall("ScriptGlue::Camera_SetProjectionType")]
|
||||
static void Camera_SetProjectionType(UUID entityId, in SceneCamera.ProjectionType projectionType)
|
||||
{
|
||||
CameraComponent* camera = GetComponentSafe<CameraComponent>(entityId);
|
||||
camera.Camera.ProjectionType = projectionType;
|
||||
}
|
||||
|
||||
// Native Implementierungen in Beef für die Kamera-Properties
|
||||
|
||||
[RegisterCall("ScriptGlue::Camera_GetPerspectiveFovY")]
|
||||
static void Camera_GetPerspectiveFovY(UUID entityId, out float fovY)
|
||||
{
|
||||
CameraComponent* camera = GetComponentSafe<CameraComponent>(entityId);
|
||||
fovY = camera.Camera.PerspectiveFovY;
|
||||
}
|
||||
|
||||
[RegisterCall("ScriptGlue::Camera_SetPerspectiveFovY")]
|
||||
static void Camera_SetPerspectiveFovY(UUID entityId, float fovY)
|
||||
{
|
||||
CameraComponent* camera = GetComponentSafe<CameraComponent>(entityId);
|
||||
camera.Camera.PerspectiveFovY = fovY;
|
||||
}
|
||||
|
||||
[RegisterCall("ScriptGlue::Camera_GetPerspectiveNearPlane")]
|
||||
static void Camera_GetPerspectiveNearPlane(UUID entityId, out float nearPlane)
|
||||
{
|
||||
CameraComponent* camera = GetComponentSafe<CameraComponent>(entityId);
|
||||
nearPlane = camera.Camera.PerspectiveNearPlane;
|
||||
}
|
||||
|
||||
[RegisterCall("ScriptGlue::Camera_SetPerspectiveNearPlane")]
|
||||
static void Camera_SetPerspectiveNearPlane(UUID entityId, float nearPlane)
|
||||
{
|
||||
CameraComponent* camera = GetComponentSafe<CameraComponent>(entityId);
|
||||
camera.Camera.PerspectiveNearPlane = nearPlane;
|
||||
}
|
||||
|
||||
[RegisterCall("ScriptGlue::Camera_GetPerspectiveFarPlane")]
|
||||
static void Camera_GetPerspectiveFarPlane(UUID entityId, out float farPlane)
|
||||
{
|
||||
CameraComponent* camera = GetComponentSafe<CameraComponent>(entityId);
|
||||
farPlane = camera.Camera.PerspectiveFarPlane;
|
||||
}
|
||||
|
||||
[RegisterCall("ScriptGlue::Camera_SetPerspectiveFarPlane")]
|
||||
static void Camera_SetPerspectiveFarPlane(UUID entityId, float farPlane)
|
||||
{
|
||||
CameraComponent* camera = GetComponentSafe<CameraComponent>(entityId);
|
||||
camera.Camera.PerspectiveFarPlane = farPlane;
|
||||
}
|
||||
|
||||
[RegisterCall("ScriptGlue::Camera_GetOrthographicHeight")]
|
||||
static void Camera_GetOrthographicHeight(UUID entityId, out float height)
|
||||
{
|
||||
CameraComponent* camera = GetComponentSafe<CameraComponent>(entityId);
|
||||
height = camera.Camera.OrthographicHeight;
|
||||
}
|
||||
|
||||
[RegisterCall("ScriptGlue::Camera_SetOrthographicHeight")]
|
||||
static void Camera_SetOrthographicHeight(UUID entityId, float height)
|
||||
{
|
||||
CameraComponent* camera = GetComponentSafe<CameraComponent>(entityId);
|
||||
camera.Camera.OrthographicHeight = height;
|
||||
}
|
||||
|
||||
[RegisterCall("ScriptGlue::Camera_SetOrthographicNearPlane")]
|
||||
static void Camera_SetOrthographicNearPlane(UUID entityId, float nearPlane)
|
||||
{
|
||||
CameraComponent* camera = GetComponentSafe<CameraComponent>(entityId);
|
||||
camera.Camera.OrthographicNearPlane = nearPlane;
|
||||
}
|
||||
|
||||
[RegisterCall("ScriptGlue::Camera_GetOrthographicNearPlane")]
|
||||
static void Camera_GetOrthographicNearPlane(UUID entityId, out float nearPlane)
|
||||
{
|
||||
CameraComponent* camera = GetComponentSafe<CameraComponent>(entityId);
|
||||
nearPlane = camera.Camera.OrthographicNearPlane;
|
||||
}
|
||||
|
||||
[RegisterCall("ScriptGlue::Camera_SetOrthographicFarPlane")]
|
||||
static void Camera_SetOrthographicFarPlane(UUID entityId, float farPlane)
|
||||
{
|
||||
CameraComponent* camera = GetComponentSafe<CameraComponent>(entityId);
|
||||
camera.Camera.OrthographicFarPlane = farPlane;
|
||||
}
|
||||
|
||||
[RegisterCall("ScriptGlue::Camera_GetOrthographicFarPlane")]
|
||||
static void Camera_GetOrthographicFarPlane(UUID entityId, out float farPlane)
|
||||
{
|
||||
CameraComponent* camera = GetComponentSafe<CameraComponent>(entityId);
|
||||
farPlane = camera.Camera.OrthographicFarPlane;
|
||||
}
|
||||
|
||||
[RegisterCall("ScriptGlue::Camera_SetAspectRatio")]
|
||||
static void Camera_SetAspectRatio(UUID entityId, float aspectRatio)
|
||||
{
|
||||
CameraComponent* camera = GetComponentSafe<CameraComponent>(entityId);
|
||||
camera.Camera.AspectRatio = aspectRatio;
|
||||
}
|
||||
|
||||
[RegisterCall("ScriptGlue::Camera_GetAspectRatio")]
|
||||
static void Camera_GetAspectRatio(UUID entityId, out float aspectRatio)
|
||||
{
|
||||
CameraComponent* camera = GetComponentSafe<CameraComponent>(entityId);
|
||||
aspectRatio = camera.Camera.AspectRatio;
|
||||
}
|
||||
|
||||
[RegisterCall("ScriptGlue::Camera_SetFixedAspectRatio")]
|
||||
static void Camera_SetFixedAspectRatio(UUID entityId, bool fixedAspectRatio)
|
||||
{
|
||||
CameraComponent* camera = GetComponentSafe<CameraComponent>(entityId);
|
||||
camera.Camera.FixedAspectRatio = fixedAspectRatio;
|
||||
}
|
||||
|
||||
[RegisterCall("ScriptGlue::Camera_GetFixedAspectRatio")]
|
||||
static void Camera_GetFixedAspectRatio(UUID entityId, out bool fixedAspectRatio)
|
||||
{
|
||||
CameraComponent* camera = GetComponentSafe<CameraComponent>(entityId);
|
||||
fixedAspectRatio = camera.Camera.FixedAspectRatio;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Physics2D
|
||||
|
||||
// TODO: We need a wrapper class!
|
||||
|
||||
@@ -279,9 +279,22 @@ static class Mono
|
||||
|
||||
#endregion
|
||||
|
||||
#region Exception
|
||||
|
||||
[LinkName(.C), NoReturn]
|
||||
public static extern void mono_raise_exception(MonoException *ex);
|
||||
|
||||
[LinkName(.C)]
|
||||
public static extern char8* mono_exception_get_managed_backtrace(MonoException* exc);
|
||||
|
||||
[LinkName(.C)]
|
||||
public static extern MonoException* mono_exception_from_name_msg(MonoImage *image, char8* name_space, char8* name, char8* msg);
|
||||
|
||||
[LinkName(.C)]
|
||||
public static extern MonoException* mono_get_exception_argument(char8* arg, char8* msg);
|
||||
|
||||
#endregion
|
||||
|
||||
[LinkName(.C)]
|
||||
public static extern MonoClass* mono_object_get_class(MonoObject* obj);
|
||||
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
using System;
|
||||
|
||||
namespace GlitchyEngine.Core;
|
||||
|
||||
/// <summary>
|
||||
/// The type of projection used by a camera.
|
||||
/// </summary>
|
||||
public enum ProjectionType : byte
|
||||
{
|
||||
/// <summary>
|
||||
/// An orthographic projection.
|
||||
/// </summary>
|
||||
Orthographic = 0,
|
||||
/// <summary>
|
||||
/// A perspective projection.
|
||||
/// </summary>
|
||||
Perspective = 1,
|
||||
/// <summary>
|
||||
/// A perspective projection where the far plane is infinitely far away from the camera.
|
||||
/// </summary>
|
||||
InfinitePerspective = 2
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A component representing a camera that is attached to the entity.
|
||||
/// </summary>
|
||||
public class Camera : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// The projection type of the <see cref="Camera"/>.
|
||||
/// </summary>
|
||||
public ProjectionType ProjectionType
|
||||
{
|
||||
get
|
||||
{
|
||||
ScriptGlue.Camera_GetProjectionType(_uuid, out ProjectionType projectionType);
|
||||
|
||||
return projectionType;
|
||||
}
|
||||
set => ScriptGlue.Camera_SetProjectionType(_uuid, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Y field of view in perspective mode.
|
||||
/// </summary>
|
||||
public float PerspectiveFovY
|
||||
{
|
||||
get
|
||||
{
|
||||
ScriptGlue.Camera_GetPerspectiveFovY(_uuid, out float fovY);
|
||||
return fovY;
|
||||
}
|
||||
set => ScriptGlue.Camera_SetPerspectiveFovY(_uuid, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the X field of view in perspective mode.
|
||||
/// </summary>
|
||||
public float PerspectiveFovX
|
||||
{
|
||||
get => PerspectiveFovY * AspectRatio;
|
||||
set => PerspectiveFovY = value / AspectRatio;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the near plane distance in perspective mode.
|
||||
/// </summary>
|
||||
public float PerspectiveNearPlane
|
||||
{
|
||||
get
|
||||
{
|
||||
ScriptGlue.Camera_GetPerspectiveNearPlane(_uuid, out float nearPlane);
|
||||
return nearPlane;
|
||||
}
|
||||
set => ScriptGlue.Camera_SetPerspectiveNearPlane(_uuid, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the far plane distance in perspective mode.
|
||||
/// </summary>
|
||||
public float PerspectiveFarPlane
|
||||
{
|
||||
get
|
||||
{
|
||||
ScriptGlue.Camera_GetPerspectiveFarPlane(_uuid, out float farPlane);
|
||||
return farPlane;
|
||||
}
|
||||
set => ScriptGlue.Camera_SetPerspectiveFarPlane(_uuid, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the orthographic height.
|
||||
/// </summary>
|
||||
public float OrthographicHeight
|
||||
{
|
||||
get
|
||||
{
|
||||
ScriptGlue.Camera_GetOrthographicHeight(_uuid, out float height);
|
||||
return height;
|
||||
}
|
||||
set => ScriptGlue.Camera_SetOrthographicHeight(_uuid, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the near plane distance in orthographic mode.
|
||||
/// </summary>
|
||||
public float OrthographicNearPlane
|
||||
{
|
||||
get
|
||||
{
|
||||
ScriptGlue.Camera_GetOrthographicNearPlane(_uuid, out float nearPlane);
|
||||
return nearPlane;
|
||||
}
|
||||
set => ScriptGlue.Camera_SetOrthographicNearPlane(_uuid, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the far plane distance in orthographic mode.
|
||||
/// </summary>
|
||||
public float OrthographicFarPlane
|
||||
{
|
||||
get
|
||||
{
|
||||
ScriptGlue.Camera_GetOrthographicFarPlane(_uuid, out float farPlane);
|
||||
return farPlane;
|
||||
}
|
||||
set => ScriptGlue.Camera_SetOrthographicFarPlane(_uuid, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the aspect ratio.
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentException">The setter throws an <see cref="ArgumentException"/>, when <see langword="value"/> is zero.</exception>
|
||||
public float AspectRatio
|
||||
{
|
||||
get
|
||||
{
|
||||
ScriptGlue.Camera_GetAspectRatio(_uuid, out float aspectRatio);
|
||||
return aspectRatio;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value == 0)
|
||||
throw new ArgumentException($"{AspectRatio} must not be zero.");
|
||||
|
||||
ScriptGlue.Camera_SetAspectRatio(_uuid, value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the aspect ratio is fixed.
|
||||
/// </summary>
|
||||
public bool FixedAspectRatio
|
||||
{
|
||||
get
|
||||
{
|
||||
ScriptGlue.Camera_GetFixedAspectRatio(_uuid, out bool fixedAspectRatio);
|
||||
return fixedAspectRatio;
|
||||
}
|
||||
set => ScriptGlue.Camera_SetFixedAspectRatio(_uuid, value);
|
||||
}
|
||||
}
|
||||
@@ -109,6 +109,57 @@ internal static class ScriptGlue
|
||||
|
||||
#endregion RigidBody2D
|
||||
|
||||
#region Camera
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Camera_GetProjectionType(UUID entityId, out ProjectionType projectionType);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Camera_SetProjectionType(UUID entityId, in ProjectionType projectionType);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Camera_GetPerspectiveFovY(UUID entityId, out float fovY);
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Camera_SetPerspectiveFovY(UUID entityId, float fovY);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Camera_GetPerspectiveNearPlane(UUID entityId, out float nearPlane);
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Camera_SetPerspectiveNearPlane(UUID entityId, float nearPlane);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Camera_GetPerspectiveFarPlane(UUID entityId, out float farPlane);
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Camera_SetPerspectiveFarPlane(UUID entityId, float farPlane);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Camera_GetOrthographicHeight(UUID entityId, out float height);
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Camera_SetOrthographicHeight(UUID entityId, float height);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Camera_GetOrthographicNearPlane(UUID entityId, out float nearPlane);
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Camera_SetOrthographicNearPlane(UUID entityId, float nearPlane);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Camera_GetOrthographicFarPlane(UUID entityId, out float farPlane);
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Camera_SetOrthographicFarPlane(UUID entityId, float farPlane);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Camera_GetAspectRatio(UUID entityId, out float aspectRatio);
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Camera_SetAspectRatio(UUID entityId, float aspectRatio);
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Camera_GetFixedAspectRatio(UUID entityId, out bool fixedAspectRatio);
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
internal static extern void Camera_SetFixedAspectRatio(UUID entityId, bool fixedAspectRatio);
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Physics2D
|
||||
|
||||
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||
|
||||
Reference in New Issue
Block a user