I'm working on a game with Slick2D and am just working through the Entity
class. I'm making every variable protected
and then accessing them with getters and setters. Yet it feels like I'm over-complicating things and it is more efficient to simply let some of these properties be public
.
public abstract class Entity {
protected Point location;
protected Vector2f vector;
protected boolean visible;
protected Image image;
public Entity() {
location = new Point();
vector = new Vector2f();
}
public void setLocation(int x, int y) {
location.setX(x);
location.setY(y);
}
public void setVelocity(int vx, int vy) {
vector.x = vx;
vector.y = vy;
}
public void update(){
location.setX(Math.round(location.getX() + vector.x));
location.setY(Math.round(location.getY() + vector.y));
}
public void setImage(String path) {
try {
image = new Image("res/" + path + ".png");
} catch (Exception e) {
e.printStackTrace();
}
}
public int getX(){
return location.getX();
}
public int getY(){
return location.getY();
}
public Image getImage(){
return image;
}
Then in the main Game
class:
public void render(GameContainer gc, Graphics g) throws SlickException {
g.drawImage(player.getImage(), player.getX(), player.getY());
...
}
It just feels like additional work using so many methods as opposed to directly accessing the field.
Additionally, is there any particular reason Vector2f()
's x
and y
fields are public
while Point
's x
and y
must be accessed through getters/setters?