add box2D colliders simple impl, some little tweaks, fixed camera bug

This commit is contained in:
2025-12-13 22:42:03 +08:00
parent 4140a5b4be
commit c92c831d02
30 changed files with 964 additions and 310 deletions

View File

@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Prism
@ -10,6 +11,16 @@ namespace Prism
{
}
private List<Action<float>> m_Collision2DBeginCallbacks = new List<Action<float>>();
private List<Action<float>> m_Collision2DEndCallbacks = new List<Action<float>>();
protected Entity() { ID = 0; }
internal Entity(ulong id)
{
ID = id;
}
public T CreateComponent<T>() where T : Component, new()
{
CreateComponent_Native(ID, typeof(T));
@ -34,6 +45,12 @@ namespace Prism
return null;
}
public Entity FindEntityByTag(string tag)
{
ulong entityID = FindEntityByTag_Native(tag);
return new Entity(entityID);
}
public Mat4 GetTransform()
{
Mat4 mat4Instance;
@ -46,6 +63,28 @@ namespace Prism
SetTransform_Native(ID, ref transform);
}
public void AddCollision2DBeginCallback(Action<float> callback)
{
m_Collision2DBeginCallbacks.Add(callback);
}
public void AddCollision2DEndCallback(Action<float> callback)
{
m_Collision2DEndCallbacks.Add(callback);
}
private void OnCollision2DBegin(float data)
{
foreach (var callback in m_Collision2DBeginCallbacks)
callback.Invoke(data);
}
private void OnCollision2DEnd(float data)
{
foreach (var callback in m_Collision2DEndCallbacks)
callback.Invoke(data);
}
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void CreateComponent_Native(ulong entityID, Type type);
[MethodImpl(MethodImplOptions.InternalCall)]
@ -54,6 +93,8 @@ namespace Prism
private static extern void GetTransform_Native(ulong entityID, out Mat4 matrix);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void SetTransform_Native(ulong entityID, ref Mat4 matrix);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern ulong FindEntityByTag_Native(string tag);
}
}

View File

@ -18,5 +18,27 @@ namespace Prism
X = x;
Y = y;
}
public Vec2(Vec3 vec) {
X = vec.X;
Y = vec.Y;
}
public void Clamp(Vec2 min, Vec2 max) {
if (X < min.X)
X = min.X;
if (X > max.X)
X = max.X;
if (Y < min.Y)
Y = min.Y;
if (Y > max.Y)
Y = max.Y;
}
public static Vec2 operator -(Vec2 vector)
{
return new Vec2(-vector.X, -vector.Y);
}
}
}

View File

@ -2,18 +2,24 @@ using System.Runtime.InteropServices;
namespace Prism
{
[StructLayout(LayoutKind.Explicit)]
[StructLayout(LayoutKind.Sequential)]
public struct Vec3
{
[FieldOffset(0)] public float X;
[FieldOffset(4)] public float Y;
[FieldOffset(8)] public float Z;
public float X;
public float Y;
public float Z;
public Vec3(float scalar)
{
X = Y = Z = scalar;
}
public Vec3(Vec2 vec) {
X = vec.X;
Y = vec.Y;
Z = 0.0f;
}
public Vec3(float x, float y, float z)
{
X = x;
@ -21,6 +27,27 @@ namespace Prism
Z = z;
}
public Vec3(Vec4 vec) {
X = vec.X;
Y = vec.Y;
Z = vec.Z;
}
public Vec2 XY {
get { return new Vec2(X, Y); }
set { X = value.X; Y = value.Y; }
}
public Vec2 XZ
{
get { return new Vec2(X, Z); }
set { X = value.X; Z = value.Y; }
}
public Vec2 YZ
{
get { return new Vec2(Y, Z); }
set { Y = value.X; Z = value.Y; }
}
}
}

View File

@ -100,7 +100,27 @@ namespace Prism
ApplyLinearImpulse_Native(Entity.ID, ref impulse, ref offset, wake);
}
public Vec2 GetLinearVelocity()
{
GetLinearVelocity_Native(Entity.ID, out Vec2 velocity);
return velocity;
}
public void SetLinearVelocity(Vec2 velocity)
{
SetLinearVelocity_Native(Entity.ID, ref velocity);
}
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void ApplyLinearImpulse_Native(ulong entityID, ref Vec2 impulse, ref Vec2 offset, bool wake);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void GetLinearVelocity_Native(ulong entityID, out Vec2 velocity);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void SetLinearVelocity_Native(ulong entityID, ref Vec2 velocity);
}
public class BoxCollider2DComponent : Component
{
}
}