ScriptCore: Added more properties to components

- Added CircleRenderer
- Transform: Added Rotation, RotationEuler, RotationAxisAngle and Scale
- RigidBody: Added BodyType and GravityScale
- Added ColorRGBA
This commit is contained in:
Simon Lübeß
2024-01-27 19:46:37 +01:00
parent bc535ddc26
commit 8ff35d3967
12 changed files with 566 additions and 22 deletions
@@ -349,11 +349,31 @@ namespace GlitchyEngine.World
struct Rigidbody2DComponent : IDisposableComponent
{
public enum BodyType { Static = 0, Dynamic = 1, Kinematic = 2 }
public enum BodyType
{
case Static = 0; case Dynamic = 1; case Kinematic = 2;
public BodyType BodyType = .Static;
public b2BodyType GetBox2DBodyType()
{
switch (this)
{
case .Static:
return .b2_staticBody;
case .Dynamic:
return .b2_dynamicBody;
case .Kinematic:
return .b2_kinematicBody;
default:
Log.EngineLogger.AssertDebug(false, "Unknown body type");
return .b2_staticBody;
}
}
}
public bool _fixedRotation = false;
private BodyType _bodyType = .Static;
private bool _fixedRotation = false;
private float _gravityScale = 1.0f;
private int _runtimeBody = 0;
@@ -367,6 +387,20 @@ namespace GlitchyEngine.World
[Inline]
set mut => _runtimeBody = (int)(void*)value;
}
public BodyType BodyType
{
get => _bodyType;
set mut
{
_bodyType = value;
if (RuntimeBody != null)
{
Box2D.Body.SetBodyType(RuntimeBody, _bodyType.GetBox2DBodyType());
}
}
}
public bool FixedRotation
{
@@ -382,6 +416,20 @@ namespace GlitchyEngine.World
}
}
public float GravityScale
{
get => _gravityScale;
set mut
{
_gravityScale = value;
if (RuntimeBody != null)
{
Box2D.Body.SetGravityScale(RuntimeBody, _gravityScale);
}
}
}
/*public float LinearDamping
{
get => _linearDamping;
+3 -17
View File
@@ -204,22 +204,6 @@ namespace GlitchyEngine.World
}
}
static b2BodyType GetBox2DBodyType(Rigidbody2DComponent.BodyType bodyType)
{
switch (bodyType)
{
case .Static:
return .b2_staticBody;
case .Dynamic:
return .b2_dynamicBody;
case .Kinematic:
return .b2_kinematicBody;
default:
Log.EngineLogger.AssertDebug(false, "Unknown body type");
return .b2_staticBody;
}
}
/**
* Starts the scene.
* @param startRuntime If true, the script runtime will be started.
@@ -477,7 +461,7 @@ namespace GlitchyEngine.World
var transform = entity.Transform;
b2BodyDef def = .();
def.type = GetBox2DBodyType(rigidBody.BodyType);
def.type = rigidBody.BodyType.GetBox2DBodyType();
def.userData = (void*)(uint)entity.Handle;
// TODO: check how stable this is
@@ -488,7 +472,9 @@ namespace GlitchyEngine.World
def.angle = worldRotationEuler.Z;
b2Body* body = Box2D.World.CreateBody(_physicsWorld2D, &def);
// BodyType set above
Box2D.Body.SetFixedRotation(body, rigidBody.FixedRotation);
Box2D.Body.SetGravityScale(body, rigidBody.GravityScale);
rigidBody.RuntimeBody = body;
}
+11 -1
View File
@@ -186,6 +186,8 @@ class SceneSerializer
Serialize.Value(writer, "BodyType", component.BodyType);
Serialize.Value(writer, "FixedRotation", component.FixedRotation);
Serialize.Value(writer, "GravityScale", component.GravityScale);
});
SerializeComponent<BoxCollider2DComponent>(writer, entity, "BoxCollider2D", scope (component) =>
@@ -584,13 +586,21 @@ class SceneSerializer
case "Rigidbody2D":
Try!(DeserializeComponent<Rigidbody2DComponent>(reader, entity, scope (component) =>
{
Deserialize.Value(reader, "BodyType", out component.BodyType);
Rigidbody2DComponent.BodyType bodyType = .Static;
Deserialize.Value(reader, "BodyType", out bodyType);
component.BodyType = bodyType;
reader.EntryEnd();
bool fixedRotation = false;
Deserialize.Value(reader, "FixedRotation", out fixedRotation);
component.FixedRotation = fixedRotation;
reader.EntryEnd();
Deserialize.Value<float>(reader, "GravityScale", let gravityScale);
component.GravityScale = gravityScale;
return .Ok;
}));
case "BoxCollider2D":