Scripting: Added Set and GetPosition to Rigidbody2D

+ Vectors: Hide swizzles in Debugger + Generate ToString
This commit is contained in:
Simon Lübeß
2023-08-21 13:20:12 +02:00
parent d3c19dd8da
commit 3affb8a32f
4 changed files with 67 additions and 3 deletions
+20
View File
@@ -260,6 +260,26 @@ static class ScriptGlue
Box2D.Body.ApplyForceToCenter(rigidBody.[Friend]RuntimeBody, force, wakeUp); Box2D.Body.ApplyForceToCenter(rigidBody.[Friend]RuntimeBody, force, wakeUp);
} }
[RegisterCall("ScriptGlue::RigidBody2D_SetPosition")]
static void RigidBody2D_SetPosition(UUID entityId, ref float2 position)
{
Scene scene = ScriptEngine.Context;
Entity entity = scene.GetEntityByID(entityId);
var rigidBody = entity.GetComponent<Rigidbody2DComponent>();
rigidBody.SetPosition(position);
}
[RegisterCall("ScriptGlue::RigidBody2D_GetPosition")]
static void RigidBody2D_GetPosition(UUID entityId, ref float2 position)
{
Scene scene = ScriptEngine.Context;
Entity entity = scene.GetEntityByID(entityId);
var rigidBody = entity.GetComponent<Rigidbody2DComponent>();
position = rigidBody.GetPosition();
}
#endregion RigidBody2D #endregion RigidBody2D
#region Physics2D #region Physics2D
+16
View File
@@ -27,4 +27,20 @@ public class Rigidbody2D : Component
{ {
ScriptGlue.RigidBody2D_ApplyForceToCenter(Entity._uuid, force, wakeUp); ScriptGlue.RigidBody2D_ApplyForceToCenter(Entity._uuid, force, wakeUp);
} }
/// <summary>
/// Sets the position of the rigidbody.
/// </summary>
/// <param name="position">The position of the rigidbody.</param>
public void SetPosition(float2 position)
{
ScriptGlue.RigidBody2D_SetPosition(Entity._uuid, position);
}
public float2 GetPosition()
{
ScriptGlue.RigidBody2D_GetPosition(Entity._uuid, out float2 position);
return position;
}
} }
+6 -1
View File
@@ -46,6 +46,12 @@ internal static class ScriptGlue
[MethodImpl(MethodImplOptions.InternalCall)] [MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void RigidBody2D_ApplyForceToCenter(UUID entityId, in float2 force, bool wakeUp); internal static extern void RigidBody2D_ApplyForceToCenter(UUID entityId, in float2 force, bool wakeUp);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void RigidBody2D_SetPosition(UUID entityId, in float2 position);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void RigidBody2D_GetPosition(UUID entityId, out float2 position);
#endregion RigidBody2D #endregion RigidBody2D
@@ -74,5 +80,4 @@ internal static class ScriptGlue
internal static extern float4 modf_float4(float4 x, out float4 integerPart); internal static extern float4 modf_float4(float4 x, out float4 integerPart);
#endregion #endregion
} }
+25 -2
View File
@@ -66,6 +66,9 @@ public class VectorGenerator : ISourceGenerator
builder.AppendLine(vector.Struct.GetParent<CompilationUnitSyntax>().Usings.ToFullString()); builder.AppendLine(vector.Struct.GetParent<CompilationUnitSyntax>().Usings.ToFullString());
builder.Append($$""" builder.Append($$"""
// For "DebuggerBrowsable" and "DebuggerBrowsableState"
using System.Diagnostics;
namespace {{vector.Struct.GetNamespace()}}; namespace {{vector.Struct.GetNamespace()}};
public partial struct {{vector.VectorName}} public partial struct {{vector.VectorName}}
@@ -102,15 +105,32 @@ public class VectorGenerator : ISourceGenerator
GenerateCastOperator(cast, vector, toVectorDefinition, builder); GenerateCastOperator(cast, vector, toVectorDefinition, builder);
} }
GenerateToString(vector, builder);
GenerateSwizzle(vector, builder); GenerateSwizzle(vector, builder);
builder.Append('}'); builder.Append('}');
context.AddSource($"{vector.VectorName}.g.cs", builder.ToString()); context.AddSource($"{vector.VectorName}.g.cs", builder.ToString());
} }
} }
private void GenerateToString(VectorDefinition vector, StringBuilder builder)
{
builder.Append("public override string ToString() => $\"{{");
for (int i = 0; i < vector.ComponentCount; i++)
{
if (i > 0)
builder.Append(", ");
builder.Append($"{ComponentNames[i]}: {{{ComponentNames[i]}}}");
}
builder.Append("}}\";\n\n");
}
private void GenerateCastOperator(VectorCastSyntaxReceiver.VectorCast cast, VectorDefinition fromVector, VectorDefinition toVector, StringBuilder builder) private void GenerateCastOperator(VectorCastSyntaxReceiver.VectorCast cast, VectorDefinition fromVector, VectorDefinition toVector, StringBuilder builder)
{ {
if (fromVector.ComponentCount != toVector.ComponentCount) if (fromVector.ComponentCount != toVector.ComponentCount)
@@ -592,7 +612,10 @@ public class VectorGenerator : ISourceGenerator
//"""); //""");
// } // }
// DebuggerBrowsableState.Never to hide swizzle properties in debugger view
builder.Append($$""" builder.Append($$"""
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public {{vector.BaseName}}{{swizzleCount}} {{swizzleName}} public {{vector.BaseName}}{{swizzleCount}} {{swizzleName}}
{ {
get => new({{swizzleConstructor}}); get => new({{swizzleConstructor}});