Package nl.saxion.gameapp.screens
Class CameraControlledGameScreen
java.lang.Object
nl.saxion.gameapp.screens.GameScreen
nl.saxion.gameapp.screens.GameScreenWithHUD
nl.saxion.gameapp.screens.CameraControlledGameScreen
- All Implemented Interfaces:
com.badlogic.gdx.InputProcessor,com.badlogic.gdx.Screen,com.badlogic.gdx.utils.Disposable,HUD,WorldSize
A CameraControlledGameScreen is a GameScreen with a camera that follows a dynamic target.
Features:
- Camera follows a target with optional smooth movement (lerp)
- Zoom in/out with additive or multiplicative (factor) adjustments
- Zoom clamped so viewport never shows outside the world
- Automatic viewport and projection handling
- Utility methods for converting mouse coordinates to world coordinates
- HUD support: fixed coordinate drawing independent of world zoom/movement
-
Constructor Summary
ConstructorsConstructorDescriptionCameraControlledGameScreen(int viewportWidth, int viewportHeight, float worldWidth, float worldHeight) -
Method Summary
Modifier and TypeMethodDescriptioncom.badlogic.gdx.graphics.OrthographicCamerafloatcom.badlogic.gdx.math.Vector2Convert current mouse position to world coordinates.floatGet the current mouse x position in the worldfloatGet the current mouse y position in the worldcom.badlogic.gdx.utils.viewport.FitViewportfloatReturns the Y-coordinate of the bottom edge of the viewport in world units.com.badlogic.gdx.math.RectangleReturns the current visible bounds of the viewport as a rectangle in world coordinates.floatReturns the height of the viewport in world units.floatReturns the X-coordinate of the left edge of the viewport in world units.floatReturns the X-coordinate of the right edge of the viewport in world units.floatReturns the Y-coordinate of the top edge of the viewport in world units.floatReturns the width of the viewport in world units.com.badlogic.gdx.graphics.CameraReturns the combined matrix of the world camera.floatReturns total world heightfloatReturns total world widthfloatgetZoom()voidrender(float delta) Called once per frame to render the screen.voidresize(int width, int height) Updates the HUD viewport when the window is resized.com.badlogic.gdx.math.Vector2screenToWorld(float screenX, float screenY) Converts screen coordinates to world coordinates.voidsetCameraLerp(float lerp) Set the camera lerp factor for smooth following.voidsetCameraTarget(float x, float y) Set the position the camera should follow.voidsetCameraTargetInstantly(float x, float y) Immediately moves the camera to the specified target position (no smooth transition).voidsetZoom(float zoom) Set the camera zoom, clamped so the viewport never shows outside the world.voidzoomBy(float amount) Adjust the camera zoom by a delta, clamped to min/max automatically.voidzoomByFactor(float factor) Zoom the camera by a factor.Methods inherited from class nl.saxion.gameapp.screens.GameScreenWithHUD
enableHUD, getHUDCamera, getHUDHeight, getHUDViewport, getHUDWidth, getMouseHUDPosition, getMouseHUDX, getMouseHUDY, getUIStage, hudToWorld, hudXtoWorldX, hudYtoWorldY, isHUDEnabled, onUIInteraction, renderUI, switchToHudRendering, switchToWorldRendering, worldToHUD, worldXtoHUDX, worldYtoHUDYMethods inherited from class nl.saxion.gameapp.screens.GameScreen
dispose, keyDown, keyTyped, keyUp, mouseMoved, pause, resume, scrolled, touchCancelled, touchDown, touchDragged, touchUpMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.badlogic.gdx.Screen
hide, show
-
Constructor Details
-
CameraControlledGameScreen
public CameraControlledGameScreen(int viewportWidth, int viewportHeight, float worldWidth, float worldHeight) - Parameters:
viewportWidth- Width of the viewport (visible area)viewportHeight- Height of the viewport (visible area)worldWidth- Total width of the level/worldworldHeight- Total height of the level/world
-
-
Method Details
-
setCameraTarget
public void setCameraTarget(float x, float y) Set the position the camera should follow.- Parameters:
x- X-coordinate in world unitsy- Y-coordinate in world units
-
setCameraTargetInstantly
public void setCameraTargetInstantly(float x, float y) Immediately moves the camera to the specified target position (no smooth transition).- Parameters:
x- X-coordinate in world unitsy- Y-coordinate in world units
-
render
public void render(float delta) Description copied from class:GameScreenCalled once per frame to render the screen.This is the only abstract method — subclasses must implement it to define what happens every frame.
- Specified by:
renderin interfacecom.badlogic.gdx.Screen- Specified by:
renderin classGameScreen- Parameters:
delta- the time in seconds since the last frame was rendered
-
setZoom
public void setZoom(float zoom) Set the camera zoom, clamped so the viewport never shows outside the world. -
zoomBy
public void zoomBy(float amount) Adjust the camera zoom by a delta, clamped to min/max automatically. -
zoomByFactor
public void zoomByFactor(float factor) Zoom the camera by a factor.- Parameters:
factor- >1 zooms in, <1 zooms out
-
screenToWorld
public com.badlogic.gdx.math.Vector2 screenToWorld(float screenX, float screenY) Converts screen coordinates to world coordinates.- Parameters:
screenX- screen X positionscreenY- screen Y position- Returns:
- world coordinates as a Vector2
-
resize
public void resize(int width, int height) Description copied from class:GameScreenWithHUDUpdates the HUD viewport when the window is resized.- Specified by:
resizein interfacecom.badlogic.gdx.Screen- Overrides:
resizein classGameScreenWithHUD- Parameters:
width- the new window width in pixelsheight- the new window height in pixels
-
setCameraLerp
public void setCameraLerp(float lerp) Set the camera lerp factor for smooth following.- Parameters:
lerp- cameraLerp in [0,1], 1 = no movement, 0 = instant
-
getCamera
public com.badlogic.gdx.graphics.OrthographicCamera getCamera()- Returns:
- the OrthographicCamera used by this screen
-
getViewport
public com.badlogic.gdx.utils.viewport.FitViewport getViewport()- Returns:
- the FitViewport used by this screen
-
getZoom
public float getZoom()- Returns:
- current zoom level
-
getMouseWorldPosition
public com.badlogic.gdx.math.Vector2 getMouseWorldPosition()Convert current mouse position to world coordinates.- Returns:
- a
Vector2representing the mouse position in world space
-
getMouseX
public float getMouseX()Get the current mouse x position in the world- Returns:
- x coordinate in world coordinates
-
getMouseY
public float getMouseY()Get the current mouse y position in the world- Returns:
- y coordinate in world coordinates
-
getWorldWidth
public float getWorldWidth()Returns total world width- Returns:
- the world width
-
getWorldHeight
public float getWorldHeight()Returns total world height- Returns:
- the world height
-
getCameraLerp
public float getCameraLerp()- Returns:
- current camera lerp factor
-
getViewportBounds
public com.badlogic.gdx.math.Rectangle getViewportBounds()Returns the current visible bounds of the viewport as a rectangle in world coordinates.The rectangle's x and y correspond to the bottom-left corner of the visible area.
- Returns:
- a Rectangle representing the visible area in world coordinates
-
getViewportLeft
public float getViewportLeft()Returns the X-coordinate of the left edge of the viewport in world units.- Returns:
- left edge of the visible area (world coordinates)
-
getViewportRight
public float getViewportRight()Returns the X-coordinate of the right edge of the viewport in world units.- Returns:
- right edge of the visible area (world coordinates)
-
getViewportBottom
public float getViewportBottom()Returns the Y-coordinate of the bottom edge of the viewport in world units.- Returns:
- bottom edge of the visible area (world coordinates)
-
getViewportTop
public float getViewportTop()Returns the Y-coordinate of the top edge of the viewport in world units.- Returns:
- top edge of the visible area (world coordinates)
-
getViewportWidth
public float getViewportWidth()Returns the width of the viewport in world units.- Returns:
- visible width of the viewport (world coordinates)
-
getViewportHeight
public float getViewportHeight()Returns the height of the viewport in world units.- Returns:
- visible height of the viewport (world coordinates)
-
getWorldCamera
public com.badlogic.gdx.graphics.Camera getWorldCamera()Description copied from interface:WorldSizeReturns the combined matrix of the world camera.This is used by the HUD to switch back to world rendering.
- Returns:
- the world camera's combined matrix
-