Skip to content

Commit

Permalink
refactor: PrimitiveCreationOption abstract class added
Browse files Browse the repository at this point in the history
  • Loading branch information
VaclavElias committed Mar 2, 2024
1 parent 36c2078 commit 5c3fd21
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 131 deletions.
11 changes: 6 additions & 5 deletions src/Stride.CommunityToolkit/Engine/GameExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ public static Entity Create3DPrimitive(this IGame game, PrimitiveModelType type,

if (!options.IncludeCollider || options.PhysicsComponent is null) return entity;

var colliderShape = Get3DColliderShape(type, options.Size, options.Is2D);
var colliderShape = Get3DColliderShape(type, options.Size);

if (colliderShape is null) return entity;

Expand Down Expand Up @@ -829,21 +829,22 @@ public static void SetMaxFPS(this IGame game, int targetFPS)
Primitive2DModelType.Rectangle => size is null ? new BoxColliderShapeDesc() { Is2D = true } : new() { Size = new(size.Value.X, size.Value.Y, 0), Is2D = true },
Primitive2DModelType.Square => size is null ? new BoxColliderShapeDesc() { Is2D = true } : new() { Size = new(size.Value.X, size.Value.Y, 0), Is2D = true },
Primitive2DModelType.Circle => size is null ? new SphereColliderShapeDesc() : new() { Radius = size.Value.X, Is2D = true },
Primitive2DModelType.Capsule => size is null ? new CapsuleColliderShapeDesc() : new() { Radius = size.Value.X, Is2D = true },
_ => throw new InvalidOperationException(),
};

private static IInlineColliderShapeDesc? Get3DColliderShape(PrimitiveModelType type, Vector3? size = null, bool is2D = false)
private static IInlineColliderShapeDesc? Get3DColliderShape(PrimitiveModelType type, Vector3? size = null)
=> type switch
{
PrimitiveModelType.Plane => size is null ? new BoxColliderShapeDesc() : new() { Size = new Vector3(size.Value.X, 0, size.Value.Y) },
PrimitiveModelType.InfinitePlane => new StaticPlaneColliderShapeDesc(),
PrimitiveModelType.Sphere => size is null ? new SphereColliderShapeDesc() : new() { Radius = size.Value.X, Is2D = is2D },
PrimitiveModelType.Cube => size is null ? new BoxColliderShapeDesc() : new() { Size = size ?? Vector3.One, Is2D = is2D },
PrimitiveModelType.Sphere => size is null ? new SphereColliderShapeDesc() : new() { Radius = size.Value.X },
PrimitiveModelType.Cube => size is null ? new BoxColliderShapeDesc() : new() { Size = size ?? Vector3.One },
PrimitiveModelType.Cylinder => size is null ? new CylinderColliderShapeDesc() : new() { Radius = size.Value.X, Height = size.Value.Y },
PrimitiveModelType.Torus => null,
PrimitiveModelType.Teapot => null,
PrimitiveModelType.Cone => size is null ? new ConeColliderShapeDesc() : new() { Radius = size.Value.X, Height = size.Value.Y },
PrimitiveModelType.Capsule => size is null ? new CapsuleColliderShapeDesc() { Radius = 0.35f } : new() { Radius = size.Value.X, Length = size.Value.Y, Is2D = is2D },
PrimitiveModelType.Capsule => size is null ? new CapsuleColliderShapeDesc() { Radius = 0.35f } : new() { Radius = size.Value.X, Length = size.Value.Y },
_ => throw new InvalidOperationException(),
};
}
27 changes: 3 additions & 24 deletions src/Stride.CommunityToolkit/Engine/Primitive2DCreationOptions.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,19 @@
using Stride.Engine;
using Stride.Engine;
using Stride.Physics;
using Stride.Rendering;

namespace Stride.CommunityToolkit.Engine;

public class Primitive2DCreationOptions
public class Primitive2DCreationOptions : PrimitiveCreationOptions
{
/// <summary>
/// Gets or sets the name of the entity.
/// </summary>
public string? EntityName { get; set; }

/// <summary>
/// Gets or sets the material to be applied to the primitive model.
/// </summary>
public Material? Material { get; set; }

/// <summary>
/// Determines whether to include a collider component in the entity. Defaults to true.
/// </summary>
public bool IncludeCollider { get; set; } = true;

/// <summary>
/// Gets or sets the size of the primitive model. If null, default dimensions are used.
/// </summary>
public Vector2? Size { get; set; }

/// <summary>
/// Gets or sets the render group for the entity. Defaults to RenderGroup.Group0.
/// </summary>
public RenderGroup RenderGroup { get; set; } = RenderGroup.Group0;
public float Depth { get; set; } = 0.04f;

/// <summary>
/// Gets or sets the physics component to be added to the entity. Defaults to a new instance of RigidbodyComponent.
/// </summary>
public PhysicsComponent? PhysicsComponent { get; set; } = new RigidbodyComponent();

public float Depth { get; set; } = 0.04f;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Stride.BepuPhysics;
using Stride.BepuPhysics.Definitions.Colliders;
using Stride.CommunityToolkit.Bepu;

namespace Stride.CommunityToolkit.Engine;

public class Primitive2DCreationOptionsWithBepu : PrimitiveCreationOptions
{
/// <summary>
/// Gets or sets the size of the primitive model. If null, default dimensions are used.
/// </summary>
public Vector2? Size { get; set; }

public float Depth { get; set; } = 1;

/// <summary>
/// Gets or sets the physics component to be added to the entity.
/// </summary>
public ContainerComponent Component { get; set; } = new Body2DComponent() { Collider = new CompoundCollider() };
}
103 changes: 1 addition & 102 deletions src/Stride.CommunityToolkit/Engine/Primitive3DCreationOptions.cs
Original file line number Diff line number Diff line change
@@ -1,121 +1,20 @@
using Stride.BepuPhysics;
using Stride.BepuPhysics.Definitions.Colliders;
using Stride.Engine;
using Stride.Physics;
using Stride.Rendering;

namespace Stride.CommunityToolkit.Engine;

/// <summary>
/// Provides options for creating a primitive entity in a 3D scene.
/// </summary>
public class Primitive3DCreationOptions
public class Primitive3DCreationOptions : PrimitiveCreationOptions
{
/// <summary>
/// Gets or sets the name of the entity.
/// </summary>
public string? EntityName { get; set; }

/// <summary>
/// Gets or sets the material to be applied to the primitive model.
/// </summary>
public Material? Material { get; set; }

/// <summary>
/// Determines whether to include a collider component in the entity. Defaults to true.
/// </summary>
public bool IncludeCollider { get; set; } = true;

/// <summary>
/// Gets or sets the size of the primitive model. If null, default dimensions are used.
/// </summary>
public Vector3? Size { get; set; }

/// <summary>
/// Gets or sets the render group for the entity. Defaults to RenderGroup.Group0.
/// </summary>
public RenderGroup RenderGroup { get; set; } = RenderGroup.Group0;

/// <summary>
/// Indicates whether the primitive is 2D. Defaults to false.
/// </summary>
public bool Is2D { get; set; }

/// <summary>
/// Gets or sets the physics component to be added to the entity. Defaults to a new instance of RigidbodyComponent.
/// </summary>
public PhysicsComponent? PhysicsComponent { get; set; } = new RigidbodyComponent();
}

/// <summary>
/// Provides options for creating a primitive entity in a 3D scene.
/// </summary>
public class Primitive3DCreationOptionsWithBepu
{
/// <summary>
/// Gets or sets the name of the entity.
/// </summary>
public string? EntityName { get; set; }

/// <summary>
/// Gets or sets the material to be applied to the primitive model.
/// </summary>
public Material? Material { get; set; }

/// <summary>
/// Determines whether to include a collider component in the entity. Defaults to true.
/// </summary>
public bool IncludeCollider { get; set; } = true;

/// <summary>
/// Gets or sets the size of the primitive model. If null, default dimensions are used.
/// </summary>
public Vector3? Size { get; set; }

/// <summary>
/// Gets or sets the render group for the entity. Defaults to RenderGroup.Group0.
/// </summary>
public RenderGroup RenderGroup { get; set; } = RenderGroup.Group0;

/// <summary>
/// Gets or sets the physics component to be added to the entity.
/// </summary>
public ContainerComponent Component { get; set; } = new BodyComponent() { Collider = new CompoundCollider() };

public float Depth { get; set; } = 0.04f;
}

public class Primitive2DCreationOptionsWithBepu
{
/// <summary>
/// Gets or sets the name of the entity.
/// </summary>
public string? EntityName { get; set; }

/// <summary>
/// Gets or sets the material to be applied to the primitive model.
/// </summary>
public Material? Material { get; set; }

/// <summary>
/// Determines whether to include a collider component in the entity. Defaults to true.
/// </summary>
public bool IncludeCollider { get; set; } = true;

/// <summary>
/// Gets or sets the size of the primitive model. If null, default dimensions are used.
/// </summary>
public Vector2? Size { get; set; }

/// <summary>
/// Gets or sets the render group for the entity. Defaults to RenderGroup.Group0.
/// </summary>
public RenderGroup RenderGroup { get; set; } = RenderGroup.Group0;

/// <summary>
/// Gets or sets the physics component to be added to the entity.
/// </summary>
public ContainerComponent Component { get; set; } = new Body2DComponent() { Collider = new CompoundCollider() };

public float Depth { get; set; } = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Stride.BepuPhysics;
using Stride.BepuPhysics.Definitions.Colliders;

namespace Stride.CommunityToolkit.Engine;

/// <summary>
/// Provides options for creating a primitive entity in a 3D scene.
/// </summary>
public class Primitive3DCreationOptionsWithBepu : PrimitiveCreationOptions
{
/// <summary>
/// Gets or sets the size of the primitive model. If null, default dimensions are used.
/// </summary>
public Vector3? Size { get; set; }

/// <summary>
/// Gets or sets the physics component to be added to the entity.
/// </summary>
public ContainerComponent Component { get; set; } = new BodyComponent() { Collider = new CompoundCollider() };
}
26 changes: 26 additions & 0 deletions src/Stride.CommunityToolkit/Engine/PrimitiveCreationOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Stride.Rendering;

namespace Stride.CommunityToolkit.Engine;

public abstract class PrimitiveCreationOptions
{
/// <summary>
/// Gets or sets the name of the entity.
/// </summary>
public string? EntityName { get; set; }

/// <summary>
/// Gets or sets the material to be applied to the primitive model.
/// </summary>
public Material? Material { get; set; }

/// <summary>
/// Determines whether to include a collider component in the entity. Defaults to true.
/// </summary>
public bool IncludeCollider { get; set; } = true;

/// <summary>
/// Gets or sets the render group for the entity. Defaults to RenderGroup.Group0.
/// </summary>
public RenderGroup RenderGroup { get; set; } = RenderGroup.Group0;
}

0 comments on commit 5c3fd21

Please sign in to comment.