From 974ad2ec508df75d103a019c85cecb6804b88d97 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Sun, 21 May 2023 16:57:28 +0900 Subject: [PATCH] loading process will try to unstuck the player if the player appears to stuck in the terrain --- .../terrarum/gameactors/ActorWithBody.kt | 34 +++++++++++++++++++ .../terrarum/modulebasegame/TerrarumIngame.kt | 5 +++ .../modulebasegame/serialise/WriteActor.kt | 12 +++---- .../modulebasegame/serialise/WriteWorld.kt | 8 ++--- .../terrarum/serialise/ReadSimpleWorld.kt | 8 ++--- 5 files changed, 52 insertions(+), 15 deletions(-) diff --git a/src/net/torvald/terrarum/gameactors/ActorWithBody.kt b/src/net/torvald/terrarum/gameactors/ActorWithBody.kt index 5f65e488a..8a8cf36da 100644 --- a/src/net/torvald/terrarum/gameactors/ActorWithBody.kt +++ b/src/net/torvald/terrarum/gameactors/ActorWithBody.kt @@ -459,6 +459,40 @@ open class ActorWithBody : Actor { private val bounceDampenVelThreshold = 0.5 + /** + * Used on final loading phase, move the player to the opposite direction of the gravitation, until the player's + * not colliding + */ + open fun tryUnstuck() { + val newHitbox = hitbox.clone() + val translation = gravitation.setMagnitude(-1.0) + + while (isColliding(newHitbox)) { + newHitbox.translate(translation) + } + + hitbox.reassign(newHitbox) + + hIntTilewiseHitbox.setFromTwoPoints( + hitbox.startX.plus(PHYS_EPSILON_DIST).div(TILE_SIZE).floor() + 0.5, + hitbox.startY.plus(PHYS_EPSILON_DIST).div(TILE_SIZE).floor() + 0.5, + hitbox.endX.plus(PHYS_EPSILON_DIST).div(TILE_SIZE).floor() + 0.5, + hitbox.endY.plus(PHYS_EPSILON_DIST).div(TILE_SIZE).floor() + 0.5 + ) + intTilewiseHitbox.setFromTwoPoints( + hitbox.startX.div(TILE_SIZE).floor(), + hitbox.startY.div(TILE_SIZE).floor(), + hitbox.endX.minus(PHYS_EPSILON_DIST).div(TILE_SIZE).floor(), + hitbox.endY.minus(PHYS_EPSILON_DIST).div(TILE_SIZE).floor() + ) + + centrePosVector.set(hitbox.centeredX, hitbox.centeredY) + centrePosPoint.set(hitbox.centeredX, hitbox.centeredY) + feetPosVector.set(hitbox.centeredX, hitbox.endY) + feetPosPoint.set(hitbox.centeredX, hitbox.endY) + feetPosTile.set(hIntTilewiseHitbox.centeredX.floorInt(), hIntTilewiseHitbox.endY.floorInt()) + } + override fun update(delta: Float) { // re-scale hitbox diff --git a/src/net/torvald/terrarum/modulebasegame/TerrarumIngame.kt b/src/net/torvald/terrarum/modulebasegame/TerrarumIngame.kt index 704dc0a5a..b193fbc3c 100644 --- a/src/net/torvald/terrarum/modulebasegame/TerrarumIngame.kt +++ b/src/net/torvald/terrarum/modulebasegame/TerrarumIngame.kt @@ -372,6 +372,11 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) { } + // try to unstuck the repositioned player + codices.player.tryUnstuck() + + + // by doing this, whatever the "possession" the player had will be broken by the game load actorNowPlaying = codices.player actorGamer = codices.player diff --git a/src/net/torvald/terrarum/modulebasegame/serialise/WriteActor.kt b/src/net/torvald/terrarum/modulebasegame/serialise/WriteActor.kt index 5e9442cc3..13a49b37b 100644 --- a/src/net/torvald/terrarum/modulebasegame/serialise/WriteActor.kt +++ b/src/net/torvald/terrarum/modulebasegame/serialise/WriteActor.kt @@ -137,8 +137,7 @@ object WritePlayer { object ReadPlayer { operator fun invoke(disk: SimpleFileSystem, dataStream: Reader): IngamePlayer = - ReadActor(disk, dataStream) as IngamePlayer - + ReadActor(disk, dataStream) as IngamePlayer } /** @@ -152,9 +151,11 @@ object ReadPlayer { object ReadActor { operator fun invoke(disk: SimpleFileSystem, dataStream: Reader): Actor = - fillInDetails(disk, Common.jsoner.fromJson(null, dataStream)) + Common.jsoner.fromJson(null, dataStream).also { + fillInDetails(disk, it) + } - private fun fillInDetails(disk: SimpleFileSystem, actor: Actor): Actor { + private fun fillInDetails(disk: SimpleFileSystem, actor: Actor) { actor.reload() @@ -197,9 +198,6 @@ object ReadActor { //actor.reassembleSprite(actor.sprite, actor.spriteGlow, null) } - - - return actor } } \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/serialise/WriteWorld.kt b/src/net/torvald/terrarum/modulebasegame/serialise/WriteWorld.kt index 03b2ee47e..2e5dc3058 100644 --- a/src/net/torvald/terrarum/modulebasegame/serialise/WriteWorld.kt +++ b/src/net/torvald/terrarum/modulebasegame/serialise/WriteWorld.kt @@ -99,16 +99,16 @@ object WriteWorld { object ReadWorld { operator fun invoke(worldDataStream: Reader, origin: File?): GameWorld = - fillInDetails(Common.jsoner.fromJson(GameWorld::class.java, worldDataStream), origin) + Common.jsoner.fromJson(GameWorld::class.java, worldDataStream).also { + fillInDetails(origin, it) + } - private fun fillInDetails(world: GameWorld, origin: File?): GameWorld { + private fun fillInDetails(origin: File?, world: GameWorld) { world.tileNumberToNameMap.forEach { l, s -> world.tileNameToNumberMap[s] = l.toInt() } ItemCodex.loadFromSave(origin, world.dynamicToStaticTable, world.dynamicItemInventory) - - return world } private val cw = LandUtil.CHUNK_W diff --git a/src/net/torvald/terrarum/serialise/ReadSimpleWorld.kt b/src/net/torvald/terrarum/serialise/ReadSimpleWorld.kt index bba69c549..9ed647279 100644 --- a/src/net/torvald/terrarum/serialise/ReadSimpleWorld.kt +++ b/src/net/torvald/terrarum/serialise/ReadSimpleWorld.kt @@ -15,16 +15,16 @@ import java.io.Reader object ReadSimpleWorld { operator fun invoke(worldDataStream: Reader, origin: File?): GameWorld = - fillInDetails(Common.jsoner.fromJson(SimpleGameWorld::class.java, worldDataStream), origin) + Common.jsoner.fromJson(SimpleGameWorld::class.java, worldDataStream).also { + fillInDetails(origin, it) + } - private fun fillInDetails(world: GameWorld, origin: File?): GameWorld { + private fun fillInDetails(origin: File?, world: GameWorld) { world.tileNumberToNameMap.forEach { l, s -> world.tileNameToNumberMap[s] = l.toInt() } ItemCodex.loadFromSave(origin, world.dynamicToStaticTable, world.dynamicItemInventory) - - return world } fun readWorldAndSetNewWorld(ingame: IngameInstance, worldDataStream: Reader, origin: File?): GameWorld {