I’m currently working on a game using the Component Pattern, and have always wondered how this is done.
I have an entity which is practically just a bag of components. Each components extends the Component class, which just has some basic functionality.
Extending the component class, new components are created, for handeling input, graphics etc. Now comes the problem; When I’m trying to get a specific component from the entity, it always returns the basic Component class, which prevents me from using the specific component functions.
public class GameEntity
{
private ArrayList<Component> components;
public GameEntity()
{
components = new ArrayList<Component>();
}
public void addComponent(Component component)
{
components.add(component);
}
public void update()
{
}
public Component getComponent(Class type)
{
for (Component component : components)
{
if(component.getClass() == type)
{
//return component as Class;
}
}
return null;
}
public void draw(Canvas canvas)
{
for (Component component : components)
{
component.update();
component.draw(canvas);
}
}
}
Some example components:
public class GraphicsComponent extends Component {
public Bitmap bitmap;
public Rect currentFrameRect;
private ArrayList spriteAnimations;
public SpriteAnimation currentAnimation; public int x = 0; public int y = 50; public
GraphicsComponent() {
spriteAnimations = new ArrayList(); }/** * Adds image [converts to spriteanimation] * @param image */ public void addImage(Bitmap image, String label) { Rect[] tmpRects = {new Rect(0, 0, image.getWidth(), image.getHeight())} ; addAnimation(new SpriteAnimation( image, tmpRects, label )); } public void addAnimation(SpriteAnimation spriteAnimation) { spriteAnimations.add(spriteAnimation); if(currentAnimation == null) { currentAnimation = spriteAnimation; } } @Override public void update() { currentFrameRect = currentAnimation.frames[currentAnimation.currentFrame]; }@Override public void draw(Canvas canvas) {
if(currentAnimation != null) { currentAnimation.draw(x, y, canvas); } }
public int getWidth()
{
return currentAnimation.frames[currentAnimation.currentFrame].width();
}
public int getHeight()
{
return currentAnimation.frames[currentAnimation.currentFrame].height();
}
}
public class InteractiveComponent extends Component
{
public GraphicsComponent graphics;
public InteractiveComponent(GraphicsComponent graphics)
{
this.graphics = graphics;
}
public boolean isOver(int tapX, int tapY)
{
//left top right bottom
if(tapX > graphics.x && tapX < graphics.x + graphics.getWidth() &&
tapY > graphics.y && tapY < graphics.y + graphics.getHeight()
)
{
return true;
}
return false;
}
}
There seem to be some problems with the code formatting, but it should be clear.
I can’t access any specific functions like getHeight() in the graphicComponent or isOver() from the interactiveComponent because I just get a basic Component returned.
I would like to return a GraphicsComponent or InteractiveComponent based on the Class that I pass into getComponent().
What you’re trying to do seems architecturally suspect to me, but nonetheless it can be done in a “clean” way using generics, and you’re already passing in the class object as a type token:
You can
==orequals()instead of theisAssignableFrom()check depending on the exact behaviour you want.