/// <summary>
        /// Assemble the body for the given entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="args">The assembler args.</param>
        public void AssembleBody(Entity entity, HumanoidAssemblerArgs args)
        {
            // Randomise the sprite variations
            int clothesVariation = this.world.Resources.GetRandomSpriteVariation("body", "torso", args.SpriteFamily);
            int headVariation = this.world.Resources.GetRandomSpriteVariation("body", "head", args.SpriteFamily);
            int beardVariation = this.world.Resources.GetRandomSpriteVariation("body", "beard", args.SpriteFamily);

            // Create the body parts
            BodyPartInfo torso = this.AssembleBodyPart(
                entity,
                BodyPart.Torso,
                "torso",
                args.SpriteFamily,
                args.BodyPosition + args.TorsoPosition,
                args.CollisionGroup,
                clothesVariation);
            BodyPartInfo head = this.AssembleBodyPart(
                entity,
                BodyPart.Head,
                "head",
                args.SpriteFamily,
                args.BodyPosition + args.HeadPosition,
                args.CollisionGroup,
                clothesVariation);
            BodyPartInfo leftArm = this.AssembleBodyPart(
                entity,
                BodyPart.Arm,
                "arm",
                args.SpriteFamily,
                args.BodyPosition + args.ArmPosition,
                args.CollisionGroup,
                clothesVariation);
            BodyPartInfo rightArm = this.AssembleBodyPart(
                entity,
                BodyPart.Arm,
                "arm",
                args.SpriteFamily,
                args.BodyPosition + args.ArmPosition + args.LeftToRightOffset,
                args.CollisionGroup,
                clothesVariation);
            BodyPartInfo leftLeg = this.AssembleBodyPart(
                entity,
                BodyPart.Leg,
                "leg",
                args.SpriteFamily,
                args.BodyPosition + args.LegPosition,
                args.CollisionGroup);
            BodyPartInfo rightLeg = this.AssembleBodyPart(
                entity,
                BodyPart.Leg,
                "leg",
                args.SpriteFamily,
                args.BodyPosition + args.LegPosition + args.LeftToRightOffset,
                args.CollisionGroup);
            BodyPartInfo beard = this.AssembleBodyPart(
                entity,
                BodyPart.Beard,
                "beard",
                args.SpriteFamily,
                args.BodyPosition + args.BeardPosition,
                args.CollisionGroup,
                beardVariation,
                false);

            // Create the joints
            this.CreateFixedJoint(head, beard);
            this.CreateRotationalJoint(
                torso,
                head,
                args.NeckJointPosition.Position - args.HeadPosition,
                args.NeckJointPosition.EnableLimit,
                args.NeckJointPosition.UpperLimit,
                args.NeckJointPosition.LowerLimit,
                args.NeckJointPosition.EnableMotor,
                args.NeckJointPosition.MaxMotorTorque);
            this.CreateRotationalJoint(
                torso,
                leftArm,
                args.ShoulderJointPosition.Position - args.ArmPosition,
                args.NeckJointPosition.EnableLimit,
                args.NeckJointPosition.UpperLimit,
                args.NeckJointPosition.LowerLimit,
                args.NeckJointPosition.EnableMotor,
                args.NeckJointPosition.MaxMotorTorque);
            this.CreateRotationalJoint(
                torso,
                rightArm,
                args.ShoulderJointPosition.Position - args.ArmPosition,
                args.NeckJointPosition.EnableLimit,
                args.NeckJointPosition.UpperLimit,
                args.NeckJointPosition.LowerLimit,
                args.NeckJointPosition.EnableMotor,
                args.NeckJointPosition.MaxMotorTorque);
            this.CreateRotationalJoint(
                torso,
                leftLeg,
                args.HipJointPosition.Position - args.LegPosition,
                args.NeckJointPosition.EnableLimit,
                args.NeckJointPosition.UpperLimit,
                args.NeckJointPosition.LowerLimit,
                args.NeckJointPosition.EnableMotor,
                args.NeckJointPosition.MaxMotorTorque);
            this.CreateRotationalJoint(
                torso,
                rightLeg,
                args.HipJointPosition.Position - args.LegPosition,
                args.NeckJointPosition.EnableLimit,
                args.NeckJointPosition.UpperLimit,
                args.NeckJointPosition.LowerLimit,
                args.NeckJointPosition.EnableMotor,
                args.NeckJointPosition.MaxMotorTorque);
        }
        /// <summary>
        /// Create the body part entity.
        /// </summary>
        /// <param name="bodyEntity">The body entity that the torso belongs to.</param>
        /// <param name="bodyPart">The type of body part.</param>
        /// <param name="spriteType">The sprite type.</param>
        /// <param name="spriteFamily">The sprite family.</param>
        /// <param name="position">The world position of the body part.</param>
        /// <param name="collisionGroup">The collision group of the body part.</param>
        /// <param name="spriteVariation">The sprite variation.</param>
        /// <param name="isPhysical">Indicates whether the body part can collide with physics objects.</param>
        /// <returns>The body part entity and the component.</returns>
        private BodyPartInfo AssembleBodyPart(
            Entity bodyEntity,
            BodyPart bodyPart,
            string spriteType,
            string spriteFamily,
            Vector2 position,
            short collisionGroup,
            int spriteVariation = -1,
            bool isPhysical = true)
        {
            Entity entity = this.world.EntityManager.CreateEntity();

            // Add the body part
            var bodyPartComponent = new BodyPartComponent(bodyEntity, bodyPart);
            this.world.EntityManager.AddComponent(entity, bodyPartComponent);

            // Add the sprite
            string spriteName =
                this.world.Resources.GetSpriteName("body", spriteType, spriteFamily, spriteVariation);
            this.world.EntityManager.AddComponent(entity, new SpriteComponent(spriteName));

            Body body;
            if (isPhysical)
            {
                // Get the texture data for the sprite
                Rectangle rectangle = this.world.Resources.GetSpriteRectangle(spriteName);
                uint[] spriteData = new uint[rectangle.Width * rectangle.Height];
                this.world.Resources.SpriteSheet.GetData(0, rectangle, spriteData, 0, spriteData.Length);

                // Create a polygon for the sprite texture
                Vertices vertices = PolygonTools.CreatePolygon(spriteData, rectangle.Width, true);

                // Scale the vertices from pixels to physics-world units
                var scale = new Vector2(Const.PixelsToMeters, -Const.PixelsToMeters);
                vertices.Scale(ref scale);

                // Partition into smaller polygons to split concave segments
                List<Vertices> convexVertices = BayazitDecomposer.ConvexPartition(vertices);

                // Create the body
                body = BodyFactory.CreateCompoundPolygon(this.world.Physics, convexVertices, 1.0f);
            }
            else
            {
                // Create a tiny rectangle since this isn't a physical body. A body is just created so that the body
                // part can be welded onto other parts
                body = BodyFactory.CreateRectangle(this.world.Physics, 0.001f, 0.001f, 1.0f);
            }

            body.IsStatic = false;
            body.CollisionGroup = collisionGroup;
            body.IsSensor = !isPhysical;

            // Add the physics component
            var physicsComponent = new PhysicsComponent(body);
            this.world.EntityManager.AddComponent(entity, physicsComponent);

            // Add the position component
            this.world.EntityManager.AddComponent(entity, new PositionComponent(body, position));

            // Add the scale component
            this.world.EntityManager.AddComponent(entity, new ScaleComponent(Const.PixelsToMeters));

            return new BodyPartInfo(entity, bodyPartComponent, physicsComponent);
        }
 /// <summary>
 /// Initializes a new instance of the BodyPartInfo class.
 /// </summary>
 /// <param name="entity">The body part entity.</param>
 /// <param name="bodyPartComponent">The body part component.</param>
 /// <param name="physicsComponent">The physics component.</param>
 public BodyPartInfo(Entity entity, BodyPartComponent bodyPartComponent, PhysicsComponent physicsComponent)
 {
     this.Entity = entity;
     this.BodyPartComponent = bodyPartComponent;
     this.PhysicsComponent = physicsComponent;
 }
 /// <summary>
 /// Initializes a new instance of the BodyPartComponent class.
 /// </summary>
 /// <param name="parentEntity">The parent entity.</param>
 /// <param name="bodyPart">The body part type.</param>
 public BodyPartComponent(Entity parentEntity, BodyPart bodyPart)
 {
     this.ParentEntity = parentEntity;
     this.BodyPart = bodyPart;
     this.Joints = new List<Joint>();
 }