我正在开发基于Java
的游戏,我需要对一些基本形状进行建模。我目前的实现在相同尺寸的形状上有很多重复的属性。
例如,我有一个Interactive
Rectangle
与Box2d
世界有关,而我有一个Background
Rectangle
与Box2d
世界无关。但是这两个类都需要定义width
和height
。
有谁知道更好的建模数据的方法吗?
当前执行中..
模拟新实现的想法,但是Interactive
和Background
只能从一个父级继承。
之所以拥有Interactive
和Background
类,是因为我可以遍历下面的对象。
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));
}
同样,
Interactive
类定义了许多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;
}
最佳答案
第二个示例要好得多,因为Rectangle
确实是一个形状,但是具有其他数据(宽度和高度)
我认为Iteractive
和Background
不是必需的,除非它们添加了任何其他操作(从图片中看,它们没有)。