From 5855b1387306d9b697e27f779c73ab893fd89481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Tue, 30 May 2023 16:50:22 +0200 Subject: [PATCH] =?UTF-8?q?Wer=20wei=C3=9F,=20was=20ich=20alles=20so=20gem?= =?UTF-8?q?acht=20hatte...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fixed crash caused by early ScriptEngine shutdown --- .../src/EditWindows/ComponentEditWindow.bf | 8 ++ GlitchyEngine/src/Application.bf | 3 +- GlitchyEngine/src/Scripting/ScriptClass.bf | 38 ++++++++ GlitchyEngine/src/Scripting/ScriptEngine.bf | 41 +++++++- GlitchyEngine/src/Scripting/ScriptGlue.bf | 34 ++++++- GlitchyEngine/src/World/Scene.bf | 93 +++++++++++-------- GlitchyEngineHelper/src/Mono/Mono.bf | 20 +++- ScriptCore/Math/Vector2.cs | 64 +++++++++++++ ScriptCore/MyTestEntity.cs | 56 +++++------ ScriptCore/Physics2D.cs | 22 +++++ ScriptCore/ScriptCore.csproj | 1 + ScriptCore/ScriptGlue.cs | 10 ++ 12 files changed, 310 insertions(+), 80 deletions(-) create mode 100644 ScriptCore/Physics2D.cs diff --git a/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf b/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf index 630275b..e1c054c 100644 --- a/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf +++ b/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf @@ -459,6 +459,14 @@ namespace GlitchyEditor.EditWindows ImGui.EndCombo(); } + + if (scriptComponent.Instance?.ScriptClass != null) + { + for (let (fieldName, scriptField) in scriptComponent.Instance?.ScriptClass.Fields) + { + ImGui.TextUnformatted(fieldName); + } + } } private static void LabelColumn(StringView label) diff --git a/GlitchyEngine/src/Application.bf b/GlitchyEngine/src/Application.bf index 6d1bc5e..9507705 100644 --- a/GlitchyEngine/src/Application.bf +++ b/GlitchyEngine/src/Application.bf @@ -93,7 +93,6 @@ namespace GlitchyEngine SamplerStateManager.Uninit(); - ScriptEngine.Shutdown(); Renderer.Deinit(); delete _contentManager; @@ -103,6 +102,8 @@ namespace GlitchyEngine delete _gameTime; delete _layerStack; + + ScriptEngine.Shutdown(); } public void OnEvent(Event e) diff --git a/GlitchyEngine/src/Scripting/ScriptClass.bf b/GlitchyEngine/src/Scripting/ScriptClass.bf index 3b0f09e..ab57a15 100644 --- a/GlitchyEngine/src/Scripting/ScriptClass.bf +++ b/GlitchyEngine/src/Scripting/ScriptClass.bf @@ -1,9 +1,24 @@ using Mono; using System; using GlitchyEngine.Core; +using System.Collections; namespace GlitchyEngine.Scripting; +using internal GlitchyEngine.Scripting; + +public struct ScriptField +{ + public StringView Name; + internal MonoClassField* _monoField; + + internal this(StringView name, MonoClassField* monoField) + { + Name = name; + _monoField = monoField; + } +} + class ScriptClass : RefCounter { private String _namespace ~ delete _; @@ -12,6 +27,9 @@ class ScriptClass : RefCounter private MonoClass* _monoClass; + //private List _monoFields ~ delete _; + private Dictionary _monoFields ~ delete _; + //typealias ConstructorMethod = function void(MonoObject* instance, UUID uuid, MonoException** exception); typealias OnCreateMethod = function MonoObject*(MonoObject* instance, MonoException** exception); typealias OnUpdateMethod = function MonoObject*(MonoObject* instance, float deltaTime, MonoException** exception); @@ -27,6 +45,8 @@ class ScriptClass : RefCounter public StringView ClassName => _className; public StringView FullName => _fullName; + public Dictionary Fields => _monoFields; + [AllowAppend] public this(StringView classNamespace, StringView className) { @@ -41,6 +61,24 @@ class ScriptClass : RefCounter _onCreate = (OnCreateMethod)GetMethodThunk("OnCreate"); _onUpdate = (OnUpdateMethod)GetMethodThunk("OnUpdate", 1); _onDestroy = (OnDestroyMethod)GetMethodThunk("OnDestroy"); + + ExtractFields(); + } + + private void ExtractFields() + { + //_monoFields = new List();ยด + _monoFields = new Dictionary(); + + void* iterator = null; + MonoClassField* currentField = null; + while ((currentField = Mono.mono_class_get_fields(_monoClass, &iterator)) != null) + { + // TODO: does the pointer from mono really never move? + StringView name = StringView(Mono.mono_field_get_name(currentField)); + + _monoFields[name] = .(name, currentField); + } } public void OnCreate(MonoObject* instance) diff --git a/GlitchyEngine/src/Scripting/ScriptEngine.bf b/GlitchyEngine/src/Scripting/ScriptEngine.bf index f83a331..eb7894f 100644 --- a/GlitchyEngine/src/Scripting/ScriptEngine.bf +++ b/GlitchyEngine/src/Scripting/ScriptEngine.bf @@ -129,19 +129,58 @@ static class ScriptEngine MonoTableInfo* typeDefinitionsTable = Mono.mono_image_get_table_info(image, .MONO_TABLE_TYPEDEF); int32 numTypes = Mono.mono_table_info_get_rows(typeDefinitionsTable); + /*MonoTableInfo* fieldsTable = Mono.mono_image_get_table_info(image, .MONO_TABLE_FIELD); + int32 numFields = Mono.mono_table_info_get_rows(fieldsTable);*/ + s_EngineObject = new ScriptClass("GlitchyEngine.Core", "EngineObject"); s_EntityRoot = new ScriptClass("GlitchyEngine", "Entity"); Log.EngineLogger.Assert(s_EntityRoot != null); + /*for (int32 fieldIndex < numFields) + { + uint32[(.)FIELD_TABLE_FLAGS.MONO_FIELD_SIZE] fieldCols = .(); + Mono.mono_metadata_decode_row(fieldsTable, fieldIndex, (.)&fieldCols, (.)FIELD_TABLE_FLAGS.MONO_FIELD_SIZE); + + char8* fieldName = Mono.mono_metadata_string_heap(image, (.)fieldCols[(.)FIELD_TABLE_FLAGS.MONO_FIELD_NAME]); + Log.EngineLogger.Info($"{StringView(fieldName)}"); + }*/ + for (int32 i = 0; i < numTypes; i++) { int32[(.)SOME_RANDOM_ENUM.MONO_TYPEDEF_SIZE] cols = .(); Mono.mono_metadata_decode_row(typeDefinitionsTable, i, (.)&cols, (.)SOME_RANDOM_ENUM.MONO_TYPEDEF_SIZE); char8* nameSpace = Mono.mono_metadata_string_heap(image, (.)cols[(.)SOME_RANDOM_ENUM.MONO_TYPEDEF_NAMESPACE]); - char8* name = Mono.mono_metadata_string_heap(image, (.)cols[(.)SOME_RANDOM_ENUM.MONO_TYPEDEF_NAME]); + char8* name = Mono.mono_metadata_string_heap(image, (.)cols[(.)SOME_RANDOM_ENUM.MONO_TYPEDEF_NAME]); + + /*int32 firstFieldIndex = cols[(.)SOME_RANDOM_ENUM.MONO_TYPEDEF_FIELD_LIST] - 1; + if (firstFieldIndex >= 0) + { + int32 lastFieldIndex; + if ((i + 1) < numTypes) + { + int32[(.)SOME_RANDOM_ENUM.MONO_TYPEDEF_SIZE] nextCols = .(); + Mono.mono_metadata_decode_row(typeDefinitionsTable, i + 1, (.)&nextCols, (.)SOME_RANDOM_ENUM.MONO_TYPEDEF_SIZE); + lastFieldIndex = nextCols[(.)SOME_RANDOM_ENUM.MONO_TYPEDEF_FIELD_LIST] - 1; + } + else + { + lastFieldIndex = numFields; + } + + for (int32 fieldIndex = firstFieldIndex; fieldIndex < lastFieldIndex; fieldIndex++) + { + uint32[(.)FIELD_TABLE_FLAGS.MONO_FIELD_SIZE] fieldCols = .(); + Mono.mono_metadata_decode_row(fieldsTable, fieldIndex, (.)&fieldCols, (.)FIELD_TABLE_FLAGS.MONO_FIELD_SIZE); + + char8* fieldName = Mono.mono_metadata_string_heap(image, (.)fieldCols[(.)FIELD_TABLE_FLAGS.MONO_FIELD_NAME]); + Log.EngineLogger.Info($"{StringView(nameSpace)}.{StringView(name)}::{StringView(fieldName)}"); + //char8* fieldSignature = Mono.mono_metadata_blob_heap(image, (.)fieldCols[(.)FIELD_TABLE_FLAGS.MONO_FIELD_SIGNATURE]); + } + }*/ + MonoClass* monoClass = Mono.mono_class_from_name(image, nameSpace, name); if (monoClass != null && Mono.mono_class_is_subclass_of(monoClass, s_EntityRoot.[Friend]_monoClass, false)) diff --git a/GlitchyEngine/src/Scripting/ScriptGlue.bf b/GlitchyEngine/src/Scripting/ScriptGlue.bf index c49fbaa..9f4d7d5 100644 --- a/GlitchyEngine/src/Scripting/ScriptGlue.bf +++ b/GlitchyEngine/src/Scripting/ScriptGlue.bf @@ -7,6 +7,7 @@ using GlitchyEngine.Math; using GlitchyEngine.World; using GlitchyEngine.Core; using System.Collections; +using Box2D; namespace GlitchyEngine.Scripting; @@ -94,7 +95,6 @@ static class ScriptGlue } } - [RegisterCall("Log::LogMessage_Impl")] static void Log(int32 logLevel, MonoString* message) { @@ -147,8 +147,8 @@ static class ScriptGlue 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."); + else + Log.EngineLogger.AssertDebug(false, "No managed component with the given type registered."); } [RegisterCall("ScriptGlue::Entity_HasComponent")] @@ -173,8 +173,8 @@ static class ScriptGlue 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."); + else + Log.EngineLogger.AssertDebug(false, "No managed component with the given type registered."); } /*[RegisterCall("Input::IsMouseButtonReleasing")] static void Destroy() @@ -230,6 +230,30 @@ static class ScriptGlue #endregion RigidBody2D +#region Physics2D + + // TODO: We need a wrapper class! + + [RegisterCall("ScriptGlue::Physics2D_GetGravity")] + static void Physics2D_GetGravity(ref Vector2 gravity) + { + Scene scene = ScriptEngine.Context; + + var box2DGravity = Box2D.World.GetGravity(scene.[Friend]_physicsWorld2D); + gravity = *(Vector2*)&box2DGravity; + } + + [RegisterCall("ScriptGlue::Physics2D_SetGravity")] + static void Physics2D_SetGravity(ref Vector2 gravity) + { + Scene scene = ScriptEngine.Context; + +#unwarn + Box2D.World.SetGravity(scene.[Friend]_physicsWorld2D, ref *(b2Vec2*)&gravity); + } + +#endregion Physics2D + private static void RegisterCall(String name, T method) where T : var { Mono.mono_add_internal_call(scope $"GlitchyEngine.{name}", (void*)method); diff --git a/GlitchyEngine/src/World/Scene.bf b/GlitchyEngine/src/World/Scene.bf index 1e76ebb..6908f65 100644 --- a/GlitchyEngine/src/World/Scene.bf +++ b/GlitchyEngine/src/World/Scene.bf @@ -46,6 +46,15 @@ namespace GlitchyEngine.World cameraComponent.Camera.SetViewportSize(e.Scene._viewportWidth, e.Scene._viewportHeight); }); + + /*_onComponentAddedHandlers.Add(typeof(Rigidbody2DComponent), (e, t, c) => { + if () + + + Rigidbody2DComponent* rigidBodyComponent = (.)c; + + //cameraComponent.Camera.SetViewportSize(e.Scene._viewportWidth, e.Scene._viewportHeight); + });*/ } public ~this() @@ -155,58 +164,62 @@ namespace GlitchyEngine.World { _physicsWorld2D = Box2D.World.Create(ref _gravity2D); - for (var entry in _ecsWorld.Enumerate()) + for (let (ecsHandle, rigidBody) in _ecsWorld.Enumerate()) { - Entity entity = .(entry.Entity, this); + Entity entity = .(ecsHandle, this); - var transform = entity.Transform; - var rigidBody = entry.Component; + InitializeRigidbody2D(entity, rigidBody); + } + } - b2BodyDef def = .(); - def.type = GetBox2DBodyType(rigidBody.BodyType); + private void InitializeRigidbody2D(Entity entity, Rigidbody2DComponent* rigidBody) + { + var transform = entity.Transform; - // TODO: breaks with hierarchy - def.position = b2Vec2(transform.Position.X, transform.Position.Y); - def.angle = transform.RotationEuler.Z; + b2BodyDef def = .(); + def.type = GetBox2DBodyType(rigidBody.BodyType); - b2Body* body = Box2D.World.CreateBody(_physicsWorld2D, &def); - Box2D.Body.SetFixedRotation(body, rigidBody.FixedRotation); + // TODO: breaks with hierarchy + def.position = b2Vec2(transform.Position.X, transform.Position.Y); + def.angle = transform.RotationEuler.Z; - rigidBody.RuntimeBody = body; + b2Body* body = Box2D.World.CreateBody(_physicsWorld2D, &def); + Box2D.Body.SetFixedRotation(body, rigidBody.FixedRotation); - if (entity.TryGetComponent(let boxCollider)) - { - b2Shape* boxShape = Box2D.Shape.CreatePolygon(); - Box2D.Shape.PolygonSetAsBox(boxShape, boxCollider.Size.X * transform.Scale.X, boxCollider.Size.Y * transform.Scale.Y); - Box2D.Shape.PolygonSetAsBoxWithCenterAngle(boxShape, boxCollider.Size.X * transform.Scale.X, boxCollider.Size.Y * transform.Scale.Y, ref boxCollider.b2Offset, 0.0f); + rigidBody.RuntimeBody = body; - b2FixtureDef fixtureDef = .(); - fixtureDef.shape = boxShape; - fixtureDef.density = boxCollider.Density; - fixtureDef.friction = boxCollider.Friction; - fixtureDef.restitution = boxCollider.Restitution; - fixtureDef.restitutionThreshold = boxCollider.RestitutionThreshold; + if (entity.TryGetComponent(let boxCollider)) + { + b2Shape* boxShape = Box2D.Shape.CreatePolygon(); + Box2D.Shape.PolygonSetAsBox(boxShape, boxCollider.Size.X * transform.Scale.X, boxCollider.Size.Y * transform.Scale.Y); + Box2D.Shape.PolygonSetAsBoxWithCenterAngle(boxShape, boxCollider.Size.X * transform.Scale.X, boxCollider.Size.Y * transform.Scale.Y, ref boxCollider.b2Offset, 0.0f); - b2Fixture* fixture = Box2D.Body.CreateFixture(body, &fixtureDef); - boxCollider.RuntimeFixture = fixture; - } + b2FixtureDef fixtureDef = .(); + fixtureDef.shape = boxShape; + fixtureDef.density = boxCollider.Density; + fixtureDef.friction = boxCollider.Friction; + fixtureDef.restitution = boxCollider.Restitution; + fixtureDef.restitutionThreshold = boxCollider.RestitutionThreshold; - if (entity.TryGetComponent(let circleCollider)) - { - b2Shape* circleShape = Box2D.Shape.CreateCircle(); - Box2D.Shape.CircleSetPosition(circleShape, ref circleCollider.b2Offset); - Box2D.Shape.SetRadius(circleShape, circleCollider.Radius); + b2Fixture* fixture = Box2D.Body.CreateFixture(body, &fixtureDef); + boxCollider.RuntimeFixture = fixture; + } - b2FixtureDef fixtureDef = .(); - fixtureDef.shape = circleShape; - fixtureDef.density = circleCollider.Density; - fixtureDef.friction = circleCollider.Friction; - fixtureDef.restitution = circleCollider.Restitution; - fixtureDef.restitutionThreshold = circleCollider.RestitutionThreshold; + if (entity.TryGetComponent(let circleCollider)) + { + b2Shape* circleShape = Box2D.Shape.CreateCircle(); + Box2D.Shape.CircleSetPosition(circleShape, ref circleCollider.b2Offset); + Box2D.Shape.SetRadius(circleShape, circleCollider.Radius); - b2Fixture* fixture = Box2D.Body.CreateFixture(body, &fixtureDef); - circleCollider.RuntimeFixture = fixture; - } + b2FixtureDef fixtureDef = .(); + fixtureDef.shape = circleShape; + fixtureDef.density = circleCollider.Density; + fixtureDef.friction = circleCollider.Friction; + fixtureDef.restitution = circleCollider.Restitution; + fixtureDef.restitutionThreshold = circleCollider.RestitutionThreshold; + + b2Fixture* fixture = Box2D.Body.CreateFixture(body, &fixtureDef); + circleCollider.RuntimeFixture = fixture; } } diff --git a/GlitchyEngineHelper/src/Mono/Mono.bf b/GlitchyEngineHelper/src/Mono/Mono.bf index 2c92518..d2f004f 100644 --- a/GlitchyEngineHelper/src/Mono/Mono.bf +++ b/GlitchyEngineHelper/src/Mono/Mono.bf @@ -58,7 +58,11 @@ static class Mono int32 res_size); [LinkName(.C)] - public static extern char8* mono_metadata_string_heap(MonoImage *meta, uint32 table_index); + public static extern char8* mono_metadata_string_heap(MonoImage* meta, uint32 table_index); + [LinkName(.C)] + public static extern uint8* mono_metadata_blob_heap(MonoImage* meta, uint32 index); + [LinkName(.C)] + public static extern uint32 mono_metadata_decode_blob_size(uint8* xptr, out uint8* newPosition); typealias gconstpointer = void*; typealias gpointer = void*; @@ -129,6 +133,12 @@ static class Mono [LinkName(.C)] public static extern MonoType* mono_reflection_type_get_type(MonoReflectionType* reflectionType); + + [LinkName(.C)] + public static extern MonoClassField* mono_class_get_fields(MonoClass* klass, gpointer* iter); + + [LinkName(.C)] + public static extern char8* mono_field_get_name(MonoClassField* field); } struct MonoDomain; @@ -173,6 +183,14 @@ enum SOME_RANDOM_ENUM{ MONO_TYPEDEF_SIZE } +enum FIELD_TABLE_FLAGS : uint32 +{ + MONO_FIELD_FLAGS, + MONO_FIELD_NAME, + MONO_FIELD_SIGNATURE, + MONO_FIELD_SIZE +} + enum MonoMetaTableEnum : int32 { MONO_TABLE_MODULE, diff --git a/ScriptCore/Math/Vector2.cs b/ScriptCore/Math/Vector2.cs index 049a399..397837f 100644 --- a/ScriptCore/Math/Vector2.cs +++ b/ScriptCore/Math/Vector2.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; namespace GlitchyEngine.Math; @@ -50,4 +51,67 @@ public struct Vector2 } } } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector2 operator +(in Vector2 a, in Vector2 b) => new(a.X + b.X, a.Y + b.Y); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector2 operator +(float a, in Vector2 b) => new(a + b.X, a + b.Y); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector2 operator +(in Vector2 a, float b) => new(a.X + b, a.Y + b); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector2 operator +(in Vector2 a) => a; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector2 operator -(in Vector2 a, in Vector2 b) => new(a.X - b.X, a.Y - b.Y); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector2 operator -(float a, in Vector2 b) => new(a - b.X, a - b.Y); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector2 operator -(in Vector2 a, float b) => new(a.X - b, a.Y - b); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector2 operator -(in Vector2 a) => new Vector2(-a.X, -a.Y); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector2 operator *(in Vector2 a, in Vector2 b) => new(a.X * b.X, a.Y * b.Y); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector2 operator *(float a, in Vector2 b) => new(a * b.X, a * b.Y); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector2 operator *(in Vector2 a, float b) => new(a.X * b, a.Y * b); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector2 operator /(in Vector2 a, in Vector2 b) => new(a.X / b.X, a.Y / b.Y); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector2 operator /(float a, in Vector2 b) => new(a / b.X, a / b.Y); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector2 operator /(in Vector2 a, float b) => new(a.X / b, a.Y / b); + + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator ==(Vector2 a, Vector2 b) + { + float diffX = a.X - b.X; + float diffY = a.Y - b.Y; + + return diffX * diffX + diffY * diffY < 0.00001f; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator !=(Vector2 a, Vector2 b) + { + return !(a == b); + } + + public override int GetHashCode() + { + unchecked + { + var hashCode = X.GetHashCode(); + hashCode = (hashCode * 397) ^ Y.GetHashCode(); + return hashCode; + } + } + + public override string ToString() + { + return $"X:{X}, Y:{Y}"; + } } \ No newline at end of file diff --git a/ScriptCore/MyTestEntity.cs b/ScriptCore/MyTestEntity.cs index 59124cf..74bda99 100644 --- a/ScriptCore/MyTestEntity.cs +++ b/ScriptCore/MyTestEntity.cs @@ -13,48 +13,40 @@ class MyTestEntity : Entity void OnCreate() { Log.Info($"Create! {UUID}"); - - //_rigidBody = GetComponent(); - RemoveComponent(); + + _rigidBody ??= GetComponent() ?? AddComponent(); } - - ///// - ///// Called after the entity was created completely. - ///// - //void OnInstantiate() - //{ - // Log.Warning($"Instantiated!"); - //} - + /// /// Called every frame. /// /// void OnUpdate(float deltaTime) { - //Vector2 force = Vector2.Zero; + Vector2 force = Vector2.Zero; - //if (Input.IsKeyPressed(Key.A)) - //{ - // 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)) + if (Input.IsKeyPressed(Key.A)) { - Log.Info("Ouha!"); + 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.MiddleButton)) + //{ + // Log.Info("Ouha!"); + // Physics2D.Gravity *= new Vector2(1, -1); + //} } /// diff --git a/ScriptCore/Physics2D.cs b/ScriptCore/Physics2D.cs new file mode 100644 index 0000000..33b3348 --- /dev/null +++ b/ScriptCore/Physics2D.cs @@ -0,0 +1,22 @@ +using GlitchyEngine.Math; + +namespace GlitchyEngine; + +/// +/// Allows changing the properties of the 2D physics simulation in the current scene. +/// +public static class Physics2D +{ + /// + /// Gets or sets the gravity of the current scene. + /// + public static Vector2 Gravity + { + get + { + ScriptGlue.Physics2D_GetGravity(out Vector2 gravity); + return gravity; + } + set => ScriptGlue.Physics2D_SetGravity(in value); + } +} \ No newline at end of file diff --git a/ScriptCore/ScriptCore.csproj b/ScriptCore/ScriptCore.csproj index 5fd9386..d1e6d9f 100644 --- a/ScriptCore/ScriptCore.csproj +++ b/ScriptCore/ScriptCore.csproj @@ -57,6 +57,7 @@ + diff --git a/ScriptCore/ScriptGlue.cs b/ScriptCore/ScriptGlue.cs index f84310f..af175d2 100644 --- a/ScriptCore/ScriptGlue.cs +++ b/ScriptCore/ScriptGlue.cs @@ -43,4 +43,14 @@ internal static class ScriptGlue #endregion RigidBody2D +#region Physics2D + + [MethodImpl(MethodImplOptions.InternalCall)] + internal static extern void Physics2D_GetGravity(out Vector2 gravity); + + [MethodImpl(MethodImplOptions.InternalCall)] + internal static extern void Physics2D_SetGravity(in Vector2 gravity); + +#endregion Physics2D + }