From 66ae8ba87afbae833a69e1ed7d4ede6e8ec80afb Mon Sep 17 00:00:00 2001 From: Moritz Zwerger Date: Sun, 29 Oct 2023 17:40:45 +0100 Subject: [PATCH] 3rd person view: front and back --- .../gui/rendering/camera/view/ViewManager.kt | 18 ++++++++++-------- .../camera/view/person/ThirdPersonView.kt | 13 ++++++++----- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/ViewManager.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/ViewManager.kt index 299a020d6..2eb4e4478 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/ViewManager.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/ViewManager.kt @@ -26,13 +26,15 @@ import de.bixilon.minosoft.util.KUtil.toResourceLocation class ViewManager(private val camera: Camera) { private val debug = DebugView(camera) private val firstPerson = FirstPersonView(camera) - private val thirdPerson = ThirdPersonView(camera) + private val thirdPersonBack = ThirdPersonView(camera, true) + private val thirdPersonFront = ThirdPersonView(camera, false) + private val views = arrayOf(firstPerson, thirdPersonBack, thirdPersonFront) var view: CameraView by observed(firstPerson) private set private var isDebug = false - private var isThirdPerson = false + private var index = 0 fun init() { @@ -54,7 +56,8 @@ class ViewManager(private val camera: Camera) { KeyActions.STICKY to setOf(KeyCodes.KEY_F5), ) ) { - this.isThirdPerson = it + this.index++ + if (this.index >= views.size) this.index = 0 updateView() } @@ -62,8 +65,8 @@ class ViewManager(private val camera: Camera) { } - private fun updateView(debug: Boolean = isDebug, thirdPerson: Boolean = isThirdPerson) { - val next = getView(debug, thirdPerson) + private fun updateView(debug: Boolean = isDebug, index: Int = this.index) { + val next = getView(debug, index) if (next == this.view) { return } @@ -73,10 +76,9 @@ class ViewManager(private val camera: Camera) { this.view.onAttach(previous) } - private fun getView(debug: Boolean, thirdPerson: Boolean): CameraView { + private fun getView(debug: Boolean, index: Int): CameraView { if (debug) return this.debug - if (thirdPerson) return this.thirdPerson - return this.firstPerson + return views[index] } fun draw() { diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/person/ThirdPersonView.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/person/ThirdPersonView.kt index 767fdc8a6..ca063294b 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/person/ThirdPersonView.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/person/ThirdPersonView.kt @@ -27,7 +27,10 @@ import de.bixilon.minosoft.input.camera.MovementInputActions import de.bixilon.minosoft.input.camera.PlayerMovementInput // TODO: handle block changes -class ThirdPersonView(override val camera: Camera) : PersonView { +class ThirdPersonView( + override val camera: Camera, + val inverse: Boolean, +) : PersonView { override val context: RenderContext get() = camera.context override var eyePosition: Vec3d = Vec3d.EMPTY @@ -44,22 +47,22 @@ class ThirdPersonView(override val camera: Camera) : PersonView { override fun onMouse(delta: Vec2d) { val rotation = super.handleMouse(delta) ?: return this.rotation = rotation - this.front = rotation.front update(eyePosition, rotation.front) } private fun update() { val entity = camera.context.connection.camera.entity this.rotation = entity.physics.rotation - this.front = rotation.front - update(entity.renderInfo.eyePosition, front) + update(entity.renderInfo.eyePosition, rotation.front) } private fun update(position: Vec3d, front: Vec3) { - val target = camera.context.connection.camera.target.raycastBlock(position, (-front).toVec3d).first + val target = camera.context.connection.camera.target.raycastBlock(position, -front.toVec3d).first val distance = target?.distance?.let { minOf(it, MAX_DISTANCE) } ?: MAX_DISTANCE + val front = if (inverse) front else -front this.eyePosition = if (distance <= 0.0) position else position + (-front * (distance - MIN_MARGIN)) + this.front = front } override fun onAttach(previous: CameraView?) {