Reputation: 6745
I'm working on a Java
based game where I need to model some basic shapes. The implementation I have at the moment has a lot of duplicated attributes across shapes of the same dimensions.
Example, I have an Interactive
Rectangle
which is involved in Box2d
world and I have a Background
Rectangle
which is not involved in Box2d
world. But both classes need a width
and height
defined.
Does anyone know a better way to model this data?
Current implementation ..
Mock idea for new implementation, however Interactive
and Background
can only inherit from one parent.
The reason why I have Interactive
and Background
classes is so I can loop through my objects like below.
for (Background bgShape : getLevel.getBGShapes()){
bgShape.draw(
gl2,
new Vec3(
bgShape.getPosition().x,
bgShape.getPosition().y,
bgShape.getPosition().z));
}
for (Body body = world.getBodyList(); body != null; body = body.getNext()) {
Interactive iaShape = (Interactive) body.getUserData();
iaShape.draw(
gl2,
new Vec3(
body.getPosition().x,
body.getPosition().y,
0.0f));
}
Also Interactive
class defines a number of attributes which are not present in Background
,
public abstract class InteractiveShape extends Shape {
private String bodyType;
private float density;
private float friction;
private float restitution;
private boolean fixedRotation;
private Body body;
}
Upvotes: 0
Views: 358
Reputation: 9697
Rectangle
is really a shape, but has additional data (width and height)Iteractive
and Background
are necessary unless they add any additional operations (which from the picture they do not).Upvotes: 1