3rd person view: front and back

This commit is contained in:
Moritz Zwerger 2023-10-29 17:40:45 +01:00
parent 96afa2e4c4
commit 66ae8ba87a
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 18 additions and 13 deletions

View File

@ -26,13 +26,15 @@ import de.bixilon.minosoft.util.KUtil.toResourceLocation
class ViewManager(private val camera: Camera) { class ViewManager(private val camera: Camera) {
private val debug = DebugView(camera) private val debug = DebugView(camera)
private val firstPerson = FirstPersonView(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) var view: CameraView by observed(firstPerson)
private set private set
private var isDebug = false private var isDebug = false
private var isThirdPerson = false private var index = 0
fun init() { fun init() {
@ -54,7 +56,8 @@ class ViewManager(private val camera: Camera) {
KeyActions.STICKY to setOf(KeyCodes.KEY_F5), KeyActions.STICKY to setOf(KeyCodes.KEY_F5),
) )
) { ) {
this.isThirdPerson = it this.index++
if (this.index >= views.size) this.index = 0
updateView() updateView()
} }
@ -62,8 +65,8 @@ class ViewManager(private val camera: Camera) {
} }
private fun updateView(debug: Boolean = isDebug, thirdPerson: Boolean = isThirdPerson) { private fun updateView(debug: Boolean = isDebug, index: Int = this.index) {
val next = getView(debug, thirdPerson) val next = getView(debug, index)
if (next == this.view) { if (next == this.view) {
return return
} }
@ -73,10 +76,9 @@ class ViewManager(private val camera: Camera) {
this.view.onAttach(previous) 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 (debug) return this.debug
if (thirdPerson) return this.thirdPerson return views[index]
return this.firstPerson
} }
fun draw() { fun draw() {

View File

@ -27,7 +27,10 @@ import de.bixilon.minosoft.input.camera.MovementInputActions
import de.bixilon.minosoft.input.camera.PlayerMovementInput import de.bixilon.minosoft.input.camera.PlayerMovementInput
// TODO: handle block changes // 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 val context: RenderContext get() = camera.context
override var eyePosition: Vec3d = Vec3d.EMPTY override var eyePosition: Vec3d = Vec3d.EMPTY
@ -44,22 +47,22 @@ class ThirdPersonView(override val camera: Camera) : PersonView {
override fun onMouse(delta: Vec2d) { override fun onMouse(delta: Vec2d) {
val rotation = super.handleMouse(delta) ?: return val rotation = super.handleMouse(delta) ?: return
this.rotation = rotation this.rotation = rotation
this.front = rotation.front
update(eyePosition, rotation.front) update(eyePosition, rotation.front)
} }
private fun update() { private fun update() {
val entity = camera.context.connection.camera.entity val entity = camera.context.connection.camera.entity
this.rotation = entity.physics.rotation this.rotation = entity.physics.rotation
this.front = rotation.front update(entity.renderInfo.eyePosition, rotation.front)
update(entity.renderInfo.eyePosition, front)
} }
private fun update(position: Vec3d, front: Vec3) { 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 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.eyePosition = if (distance <= 0.0) position else position + (-front * (distance - MIN_MARGIN))
this.front = front
} }
override fun onAttach(previous: CameraView?) { override fun onAttach(previous: CameraView?) {