Skip to content

Instantly share code, notes, and snippets.

@Riizade
Created November 15, 2020 22:47
Show Gist options
  • Select an option

  • Save Riizade/b80479c0fcbbe061df1ddd408c960bef to your computer and use it in GitHub Desktop.

Select an option

Save Riizade/b80479c0fcbbe061df1ddd408c960bef to your computer and use it in GitHub Desktop.
def moveCameraToPlayer(state: GameState): GameState = {
// move camera with player
val boundRatio = 0.75f
val playerCameraBounds = PixelVector2(
Core.INTERNAL_RESOLUTION.x / 2 * boundRatio,
Core.INTERNAL_RESOLUTION.y / 2 * boundRatio
)
val screenRelativePlayerPosition =
state.explorationState.playerPosition.toPixelVector2 - state.explorationState.cameraPosition
var newCameraPosition = state.explorationState.cameraPosition
// handle being outside positive bounds
newCameraPosition = newCameraPosition + PixelVector2(
Math.max(screenRelativePlayerPosition.x - playerCameraBounds.x, 0),
Math.max(screenRelativePlayerPosition.y - playerCameraBounds.y, 0)
)
// handle being outside negative bounds
newCameraPosition = newCameraPosition + PixelVector2(
Math.min(screenRelativePlayerPosition.x + playerCameraBounds.x, 0),
Math.min(screenRelativePlayerPosition.y + playerCameraBounds.y, 0)
)
val newExplorationState =
state.explorationState.copy(cameraPosition = newCameraPosition)
state.copy(explorationState = newExplorationState)
}
def boundCameraByZone(state: GameState): GameState = {
val zoneDimensions =
Resources
.zones(state.explorationState.currentZone)
.dimensions
.toPixelVector2
val zonePositiveBounds =
zoneDimensions - PixelVector2(
Core.INTERNAL_RESOLUTION.x / 2 + Core.INTERNAL_TILE_SIZE.x / 2,
Core.INTERNAL_RESOLUTION.y / 2 + Core.INTERNAL_TILE_SIZE.y / 2
)
val zoneNegativeBounds = PixelVector2(
Core.INTERNAL_RESOLUTION.x / 2 - Core.INTERNAL_TILE_SIZE.x / 2,
Core.INTERNAL_RESOLUTION.y / 2 - Core.INTERNAL_TILE_SIZE.y / 2
)
var newCameraPosition = state.explorationState.cameraPosition
// prevent the camera from seeing out-of-bounds of the zone
// handle positive edges
newCameraPosition = newCameraPosition + PixelVector2(
Math.min(zonePositiveBounds.x - newCameraPosition.x, 0),
Math.min(zonePositiveBounds.y - newCameraPosition.y, 0)
)
// handle negative edges
newCameraPosition = newCameraPosition + PixelVector2(
Math.max(zoneNegativeBounds.x - newCameraPosition.x, 0),
Math.max(zoneNegativeBounds.y - newCameraPosition.y, 0)
)
// if the zone is smaller than the screen on a particular axis, simply center the camera on that axis
if (zoneDimensions.x <= Core.INTERNAL_RESOLUTION.x) {
newCameraPosition = PixelVector2(
Core.INTERNAL_RESOLUTION.x / 2.0f - Core.INTERNAL_TILE_SIZE.x / 2,
newCameraPosition.y
)
}
if (zoneDimensions.y <= Core.INTERNAL_RESOLUTION.y) {
newCameraPosition = PixelVector2(
newCameraPosition.x,
Core.INTERNAL_RESOLUTION.y / 2.0f - Core.INTERNAL_TILE_SIZE.y / 2
)
}
val newExplorationState =
state.explorationState.copy(cameraPosition = newCameraPosition)
state.copy(explorationState = newExplorationState)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment