Camera Movement - UQdeco2800/2022-studio-3 GitHub Wiki

Introduction

In order to move the camera around the map in the typical RTS style (moving the mouse near a corner of the screen adjusts the camera in that direction) a Component is necessary to determine user input, and adjust the game camera accordingly. CameraInputComponent has been constructed to do this, by extending InputComponent and listening for a mouseMoved and scrolled event.

If the mouse has moved, the CameraInputComponent will check to see if the user's mouse is in an appropriate corner of the screen, and update the direction to move the camera if it is. Each update() call to CameraInputComponent will adjust the camera in the set direction.

If the mouse has scrolled, the camera's zoom will be adjusted at a rate proportional to the scroll amount, up to a limit defined in constant "maxZoom".

Usage

Creating a CameraInputComponent

To use the CameraInputComponent, it must simply be added to the same Entity as the game Camera. Currently, the camera is created in RenderFactory, by the function createCamera().

  public static Entity createCamera() {
    Entity camera = new Entity();
    camera.addComponent(new CameraComponent());
    //Add a CameraInputComponent to manage camera movement around map
    camera.addComponent(ServiceLocator.getInputService().getInputFactory().createForCamera());
    return camera;
  }

  public static Renderer createRenderer() {
    Entity camera = createCamera();
    ServiceLocator.getEntityService().register(camera);
    CameraComponent camComponent = camera.getComponent(CameraComponent.class);

    return new Renderer(camComponent);
  }

This will create the Camera entity, equipped with this component as required to process input.

Updating game max zoom

If it is required to update the max zoom distance, the constant MAX_ZOOM can simply be set:

    /**
     * The maximum distance the player may zoom out of the game
     */
    private final float maxZoom = 10;

For example will set the maximum zoom amount to 10 scroll "clicks"

Updating the distance required to trigger camera movement

If you wish to trigger camera movement by hovering closer - or further- from the screen extremities, adjusting this constant in CameraInputComponent will do so

    /**
     * Distance in pixels between the edge of the screen and a camera movement trigger
     */
    private final float buffer = 80;

Where a smaller number will trigger closer to an edge and vice versa

Updating camera movement speed

If the camera is scrolling too fast, or too slow, the speed is also adjustable by the following constants

    /**
     * Default scrolling speed of camera
     */
    private final float defaultSpeed = 0.3f;
    /**
     * Scrolling speed of camera diagonally
     */
    private final float fastSpeed = 0.4f;