I have an BallEntity class that is part of a game that uses a Entity Component System architecture.
This class have 'components' that are like the attributes of that class.
I create and initialize this components in getComponents method. To build this components I need others objects, like a Sprite (that is the image represents that ball) for SpriteComponent, a Body for BodyComponent, a camera for CameraFollowerComponent.
Those objets come to the class through arguments to the constructor.
I instantiate the BallEntity this way:
BallEntity ballEntity = new BallEntity(new Sprite(atlas.findRegion("ball")),rubeSceneHelper.getBody("ball"), camera, rubeSceneHelper);
My question is Whether these objects (or which) must be initialized in the constructor of BallEntity or Within the class that instantiates BallEntity?
public class BallEntity extends UserEntity {
public static final float SCALE = 0.78f;
private final RubeSceneHelper rubeSceneHelper;
private ScaledSprite ballSprite;
private Body ballBody;
private Camera camera;
public BallEntity(Sprite sprite, Body ballBody, Camera camera, RubeSceneHelper rubeSceneHelper) {
this.ballSprite = ScaledSprite.create(sprite, SCALE / sprite.getHeight());
this.ballBody = ballBody;
this.camera = camera;
this.rubeSceneHelper = rubeSceneHelper;
}
@Override
public Array<Component> getComponents() {
Array<Component> components = new Array<Component>();
components.add(PositionComponent.newInstance());
components.add(CameraFollowerComponent.newInstance(camera));
components.add(SpriteComponent.newInstance(ballSprite.getSprite()));
components.add(BodyComponent.newInstance(ballBody));
components.add(BallContextComponent.newInstance());
return components;
}
@Override
public void init(Entity entity) {
BodyComponent bodyComponent = entity.getComponent(BodyComponent.class);
bodyComponent.setPosition(Vector2.Zero);
Fixture ballFixture = rubeSceneHelper.getFixture(bodyComponent.getBody(), "ball");
ballFixture.setUserData(new FixtureUserData(FixtureType.BALL, entity));
}
}