CardBreaker is a small component that shatters a card object into pieces. On Init it sets the rotation and uniform scale, unparents every child, swaps each child's ModelRenderer material and tint, then throws each piece with Rigidbody impulses and random angular velocity before destroying the card GameObject.
using Sandbox;
public sealed class CardBreaker : Component
{
protected override void OnStart()
{
}
public void Init(Material material, Rotation rot, float scale)
{
LocalRotation = rot;
LocalScale = new Vector3( scale );
for ( int i = GameObject.Children.Count - 1; i >= 0; i-- )
{
var child = GameObject.Children[i];
child.SetParent( null );
var modelRenderer = child.Components.Get<ModelRenderer>();
modelRenderer.Tint = Color.White;
modelRenderer.MaterialOverride = material;
var rigidBody = child.Components.Get<Rigidbody>();
rigidBody.ApplyImpulse( (child.WorldPosition - WorldPosition).Normal * Game.Random.Float( 10f, 100f ) * rigidBody.PhysicsBody.Mass );
rigidBody.ApplyImpulse( new Vector3( 0f, 0f, 1f ) * Game.Random.Float( 400f, 800f ) * rigidBody.PhysicsBody.Mass );
rigidBody.AngularVelocity = Vector3.Random * Game.Random.Float( -10f, 10f );
}
GameObject.Destroy();
}
}