1
\$\begingroup\$

I am following an eBook called "Learning Libgdx Game Development"

In it, the author implements a CameraHelper class that implements Zoom, target-following behaviour, and probably some other things along the way.

Can I do this just by extending the camera class, or using composition?

Approaching it like this seems like a clearer code to me:

public class Camera extends OrthographicCamera {
    GameObject target;
    boolean followTarget;

    Camera(float viewWidth, float viewHeight){
        super(viewWidth, viewHeight);
    }

    public void setTarget(GameObject target) {
        this.target = target;
    }

    public GameObject getTarget() {
        return target;
    }

    @Override
    public void update(){
        if(followTarget && target != null){
            this.position.x = target.position.x;
            this.position.y = target.position.y;
        }
        update(true);
    }
}

Any problems with this that I can expect?

\$\endgroup\$
  • \$\begingroup\$ You don't need to ask us for permission to write code that you personally find clearer. If it looks clearer to you, go for it. If you run into a problem along the way, ask about that concrete problem and we can help you find concrete solutions. \$\endgroup\$ – DMGregory Jul 28 '19 at 19:13
0
\$\begingroup\$

Yes, you can. I don't see any extra problems. It is OOP way.

  • Camera extends OrthographicCamera -> Camera IS OrthographicCamera
  • CameraHelper isn't Camera it Helps to work with camera. This is like lib with commonly used stuff. For example if camera doesn't follow player (no need), you don't use/call method from CameraHelper class.
|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.