From e5fec2c22c68443d2ae9c8e8e221041a53d0ad25 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Tue, 22 Jan 2019 05:46:26 +0900 Subject: [PATCH] implementing the iconic 'lag behind' camera the right way --- src/net/torvald/terrarum/modulebasegame/Ingame.kt | 5 +++-- .../torvald/terrarum/worlddrawer/WorldCamera.kt | 15 ++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/net/torvald/terrarum/modulebasegame/Ingame.kt b/src/net/torvald/terrarum/modulebasegame/Ingame.kt index 778ae95c0..e45caf8e1 100644 --- a/src/net/torvald/terrarum/modulebasegame/Ingame.kt +++ b/src/net/torvald/terrarum/modulebasegame/Ingame.kt @@ -505,8 +505,6 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) { // camera-related updates // //////////////////////////// FeaturesDrawer.update(delta) - WorldCamera.update(gameworld, actorNowPlaying) - /////////////////////////// @@ -529,6 +527,9 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) { visibleActorsRenderMidTop = actorsRenderMidTop.filter { it.inScreen() } visibleActorsRenderFront = actorsRenderFront.filter { it.inScreen() } visibleActorsRenderOverlay=actorsRenderOverlay.filter { it.inScreen() } + + + WorldCamera.update(gameworld, actorNowPlaying) } diff --git a/src/net/torvald/terrarum/worlddrawer/WorldCamera.kt b/src/net/torvald/terrarum/worlddrawer/WorldCamera.kt index e7677d539..1339aff29 100644 --- a/src/net/torvald/terrarum/worlddrawer/WorldCamera.kt +++ b/src/net/torvald/terrarum/worlddrawer/WorldCamera.kt @@ -2,9 +2,10 @@ package net.torvald.terrarum.worlddrawer import com.jme3.math.FastMath import net.torvald.terrarum.Terrarum -import net.torvald.terrarum.gameworld.GameWorld -import net.torvald.terrarum.gameactors.ActorWBMovable import net.torvald.terrarum.floorInt +import net.torvald.terrarum.gameactors.ActorWBMovable +import net.torvald.terrarum.gameworld.GameWorld +import org.dyn4j.geometry.Vector2 /** * Created by minjaesong on 2016-12-30. @@ -29,6 +30,8 @@ object WorldCamera { inline val yCentre: Int get() = y + height.ushr(1) + private val nullVec = Vector2(0.0, 0.0) + fun update(world: GameWorld, player: ActorWBMovable?) { if (player == null) return @@ -38,12 +41,14 @@ object WorldCamera { // TOP-LEFT position of camera border // some hacky equation to position player at the dead centre - // NOT tested for WorldDrawer sampling negative coord for its drawing (which causes some fucking artefacts) - x = ((player.hitbox.centeredX).toFloat() - (width / 2)).floorInt() // X only: ROUNDWORLD implementation + // implementing the "lag behind" camera the right way + val pVecSum = player.externalV + (player.controllerV ?: nullVec) + + x = ((player.hitbox.centeredX - pVecSum.x).toFloat() - (width / 2)).floorInt() // X only: ROUNDWORLD implementation y = (FastMath.clamp( - player.hitbox.centeredY.toFloat() - height / 2, + (player.hitbox.centeredY - pVecSum.y).toFloat() - height / 2, TILE_SIZE.toFloat(), world.height * TILE_SIZE - height - TILE_SIZE.toFloat() )).floorInt().clampCameraY(world)