tienes opciones, implementar una clase que herede ese código o crear una clase para administrar los cuerpos.
intenta creando una nueva clase:
public class BodyManager
{
private final static float PX_TO_METERS = 80.f;
public static BodyDef.BodyType DYNAMIC_BODY = BodyDef.BodyType.DynamicBody;
public static BodyDef.BodyType STATIC_BODY = BodyDef.BodyType.StaticBody;
public Body createBody(World world, BodyProperties properties)
{
BodyDef cuerpo = new BodyDef();
cuerpo.type = BodyDef.BodyType.DynamicBody;
cuerpo.position.set(properties.getPosition().x, properties.getPosition().y);
cuerpo.fixedRotation = true;
Body body = world.createBody(cuerpo);
PolygonShape forma = new PolygonShape();
forma.setAsBox(
properties.getSize().y / 2 / PX_TO_METERS,
properties.getSize().x / 2 / PX_TO_METERS);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = forma;
fixtureDef.density = 1f;
body.createFixture(fixtureDef);
forma.dispose();
return body;
}
public static class BodyProperties{
private BodyDef.BodyType type;
private Vector2 position;
private Vector2 size;
public BodyProperties BodyProperties(Sprite sprite, BodyDef.BodyType type){
setPosition(new Vector2(sprite.getX(), sprite.getY()));
setSize(new Vector2(sprite.getWidth(), sprite.getHeight()));
setType(type);
return this;
}
public BodyProperties BodyProperties()
{
position = new Vector2(0, 0);
size = new Vector2(0, 0);
type = STATIC_BODY;
return this;
}
public BodyProperties setType(BodyDef.BodyType type) { this.type = type; return this; }
public BodyProperties setPosition(Vector2 position) { this.position = position; return this; }
public BodyProperties setSize(Vector2 size) { this.size = size; return this; }
public BodyDef.BodyType getType(){ return type; }
public Vector2 getPosition(){ return position; }
public Vector2 getSize(){ return size; }
}
}
Con esta clase puedes crear nuevos cuerpos agregando sus propiedades.
// creamos el sprite
Sprite jugador = new Sprite(new Texture(Gdx.files.internal("jugador.png")));
jugador.setPosition(50, 50);
// creamos la instancia de BodyManager
BodyManager bodymanager = new BodyManager();
// creamos el cuerpo estatico en el mundo
Body cuerpoJugador = bodymanager.createBody(
mundo,
new BodyManager.BodyProperties()
.BodyProperties(jugador, BodyManager.STATIC_BODY)
);