From 65d9ae7ee1a4c569e5b1a8d9ac6ca36da4f6b3c5 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Tue, 11 Jan 2022 15:19:16 +0900 Subject: [PATCH] new walk anim delay WIP --- assets/mods/basegame/sprites/taimu.properties | 2 +- .../basegame/sprites/taimu_glow.properties | 2 +- .../spriteanimation/HasAssembledSprite.kt | 1 + .../spriteanimation/SpriteAnimation.kt | 2 +- .../gameactors/ActorHumanoid.kt | 21 +++++++++++++++---- .../modulebasegame/gameactors/IngamePlayer.kt | 17 ++++++++------- .../terrarum/spriteassembler/ADProperties.kt | 12 ++++++++++- 7 files changed, 41 insertions(+), 16 deletions(-) diff --git a/assets/mods/basegame/sprites/taimu.properties b/assets/mods/basegame/sprites/taimu.properties index cd3581c22..15131f416 100644 --- a/assets/mods/basegame/sprites/taimu.properties +++ b/assets/mods/basegame/sprites/taimu.properties @@ -37,7 +37,7 @@ SKELETON_STAND=HEADGEAR 0,78;\ ! When you move the arms/hands, make sure you move the HELD_ITEM as well # RUNNING might need its own skeleton... -ANIM_RUN=DELAY 0.3;ROW 2;SKELETON SKELETON_STAND +ANIM_RUN=DELAY 0.16;ROW 2;SKELETON SKELETON_STAND ANIM_RUN_1=LEG_REST_RIGHT 2,2;LEG_REST_LEFT -2,0;TAIL_0 1,0;TORSO_1 0,-999 ANIM_RUN_2=ALL 0,2;LEG_REST_RIGHT 0,-2;LEG_REST_LEFT 0,2;TAIL_0 -1,0;TORSO_1 0,-999 ANIM_RUN_3=LEG_REST_RIGHT -2,0;LEG_REST_LEFT 2,2;TAIL_0 -1,0;TORSO_1 0,-999 diff --git a/assets/mods/basegame/sprites/taimu_glow.properties b/assets/mods/basegame/sprites/taimu_glow.properties index a67d48b7f..2911119a5 100644 --- a/assets/mods/basegame/sprites/taimu_glow.properties +++ b/assets/mods/basegame/sprites/taimu_glow.properties @@ -37,7 +37,7 @@ SKELETON_STAND=HEADGEAR 0,78;\ ! When you move the arms/hands, make sure you move the HELD_ITEM as well # RUNNING might need its own skeleton... -ANIM_RUN=DELAY 0.3;ROW 2;SKELETON SKELETON_STAND +ANIM_RUN=DELAY 0.16;ROW 2;SKELETON SKELETON_STAND ANIM_RUN_1=LEG_REST_RIGHT 2,2;LEG_REST_LEFT -2,0;TAIL_0 1,0;TORSO_1 0,-999 ANIM_RUN_2=ALL 0,2;LEG_REST_RIGHT 0,-2;LEG_REST_LEFT 0,2;TAIL_0 -1,0;TORSO_1 0,-999 ANIM_RUN_3=LEG_REST_RIGHT -2,0;LEG_REST_LEFT 2,2;TAIL_0 -1,0;TORSO_1 0,-999 diff --git a/src/net/torvald/spriteanimation/HasAssembledSprite.kt b/src/net/torvald/spriteanimation/HasAssembledSprite.kt index 3faa4b349..9c0e53202 100644 --- a/src/net/torvald/spriteanimation/HasAssembledSprite.kt +++ b/src/net/torvald/spriteanimation/HasAssembledSprite.kt @@ -14,6 +14,7 @@ interface HasAssembledSprite { /** ADL for main sprite. Necessary. */ var animDesc: ADProperties? + /** ADL for glow sprite. Optional. */ var animDescGlow: ADProperties? diff --git a/src/net/torvald/spriteanimation/SpriteAnimation.kt b/src/net/torvald/spriteanimation/SpriteAnimation.kt index 0ac8c3fdc..37b0dffed 100644 --- a/src/net/torvald/spriteanimation/SpriteAnimation.kt +++ b/src/net/torvald/spriteanimation/SpriteAnimation.kt @@ -28,7 +28,7 @@ class SpriteAnimation(@Transient val parentActor: ActorWithBody) : Disposable { internal set private val currentDelay: Second - get() = delays[currentRow] + get() = delays[currentRow].coerceAtLeast(1f / 16f) // animation delay cannot be too short /** * Sets delays for each rows. Array size must be the same as the rows of the sheet diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/ActorHumanoid.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/ActorHumanoid.kt index 96d6785ca..cab257dd7 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/ActorHumanoid.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/ActorHumanoid.kt @@ -696,7 +696,7 @@ open class ActorHumanoid : ActorWithBody, Controllable, Pocketed, Factionable, L fun Float.abs() = FastMath.abs(this) - private fun updateSprite(delta: Float) { + open fun updateSprite(delta: Float) { sprite?.update(delta) spriteGlow?.update(delta) @@ -706,11 +706,24 @@ open class ActorHumanoid : ActorWithBody, Controllable, Pocketed, Factionable, L spriteGlow?.switchRow(SPRITE_ROW_WALK) // set anim frame delay - // 4f of the divider is a magic number, empirically decided if (this is HasAssembledSprite) { - sprite?.delays?.set(SPRITE_ROW_WALK, scale.sqrt().toFloat() / (4f * (controllerV?.x ?: 0.0001).abs().toFloat())) // FIXME empirical value - spriteGlow?.delays?.set(SPRITE_ROW_WALK, scale.sqrt().toFloat() / (4f * (controllerV?.x ?: 0.0001).abs().toFloat())) // FIXME empirical value + try { + val baseDelay = animDesc!!.getAnimByFrameName("ANIM_RUN").delay + val moveSpeedMult = (controllerV?.x ?: 0.0).abs().coerceAtLeast(PHYS_EPSILON_VELO).toFloat() / 4f // FIXME empirical value + val stride = scale.toFloat() + val maxMoveSpeed = scale.sqrt().toFloat() // ActorWithBody uses scale.sqrt() for determining walk acceleration + val scaleCompensation = stride / maxMoveSpeed + val finalDelay = baseDelay * (scaleCompensation / moveSpeedMult) + + sprite?.delays?.set(SPRITE_ROW_WALK, finalDelay) + spriteGlow?.delays?.set(SPRITE_ROW_WALK, finalDelay) + } + catch (e: NullPointerException) { + println(animDesc!!.animations.keys.joinToString()) + + throw e + } } // flipping the sprite diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/IngamePlayer.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/IngamePlayer.kt index 1b898c966..8235bb169 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/IngamePlayer.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/IngamePlayer.kt @@ -3,6 +3,7 @@ package net.torvald.terrarum.modulebasegame.gameactors import com.badlogic.gdx.Gdx import com.badlogic.gdx.graphics.Texture import com.badlogic.gdx.graphics.g2d.TextureRegion +import net.torvald.spriteanimation.HasAssembledSprite import net.torvald.spriteanimation.SpriteAnimation import net.torvald.terrarum.spriteassembler.ADProperties import net.torvald.terrarum.spriteassembler.AssembleSheetPixmap @@ -22,7 +23,7 @@ import java.util.* * Created by minjaesong on 2015-12-31. */ -class IngamePlayer : ActorHumanoid { +class IngamePlayer : ActorHumanoid, HasAssembledSprite { val creationTime = App.getTIME_T() var lastPlayTime = App.getTIME_T() // cumulative value for the savegame @@ -31,13 +32,13 @@ class IngamePlayer : ActorHumanoid { val uuid = UUID.randomUUID() var worldCurrentlyPlaying: UUID = UUID(0L,0L) // only filled up on save and load; DO NOT USE THIS - var spriteHeadTexture: TextureRegion? = null + @Transient override var spriteHeadTexture: TextureRegion? = null /** ADL for main sprite. Necessary. */ - @Transient var animDesc: ADProperties? = null + @Transient override var animDesc: ADProperties? = null /** ADL for glow sprite. Optional. */ - @Transient var animDescGlow: ADProperties? = null + @Transient override var animDescGlow: ADProperties? = null private constructor() @@ -75,8 +76,8 @@ class IngamePlayer : ActorHumanoid { /** To be used later by the game to rebuild the sprite. * Which `_rebuild` function to use is determined at the load time. */ - private lateinit var rebuildfun: (item: GameItem?) -> Unit - private lateinit var rebuildfunGlow: (item: GameItem?) -> Unit + @Transient private lateinit var rebuildfun: (item: GameItem?) -> Unit + @Transient private lateinit var rebuildfunGlow: (item: GameItem?) -> Unit /** @@ -89,7 +90,7 @@ class IngamePlayer : ActorHumanoid { * reassembleSprite(this.sprite, this.spriteGlow) * ``` */ - fun reassembleSprite(sprite: SpriteAnimation?, spriteGlow: SpriteAnimation?, heldItem: GameItem?) { + override fun reassembleSprite(sprite: SpriteAnimation?, spriteGlow: SpriteAnimation?, heldItem: GameItem?) { if (animDesc != null && sprite != null) { rebuildfun = { item: GameItem? -> _rebuild(animDesc!!, sprite, item) }; rebuildfun(heldItem) spriteHeadTexture = AssembleSheetPixmap.getMugshotFromAssetsDir(animDesc!!) @@ -164,7 +165,7 @@ class IngamePlayer : ActorHumanoid { return spriteHeadTexture } - private var unequipNoSpriteUpdate = false + @Transient private var unequipNoSpriteUpdate = false override fun equipItem(item: GameItem) { val oldItemID = inventory.itemEquipped[item.equipPosition] diff --git a/src/net/torvald/terrarum/spriteassembler/ADProperties.kt b/src/net/torvald/terrarum/spriteassembler/ADProperties.kt index ceabf536f..84f5c3d16 100644 --- a/src/net/torvald/terrarum/spriteassembler/ADProperties.kt +++ b/src/net/torvald/terrarum/spriteassembler/ADProperties.kt @@ -1,6 +1,7 @@ package net.torvald.terrarum.spriteassembler import com.badlogic.gdx.files.FileHandle +import net.torvald.terrarum.App.printdbg import net.torvald.terrarum.linearSearchBy import net.torvald.terrarum.serialise.Common import java.io.InputStream @@ -238,7 +239,16 @@ class ADProperties { internal fun getSkeleton(name: String) = skeletons[name]!! internal fun getTransform(name: String) = transforms[name]!! - private fun getAnimNameFromFrame(s: String) = s.substring(0 until s.lastIndexOf('_')) + /** + * Removes number suffix from the animation name + */ + private fun getAnimNameFromFrame(s: String): String { + val ret = if (s.matches(reAnimWithNumber)) s.substringBeforeLast('_') else s +// printdbg(this, "getAnimNameFromFrame $s -> $ret") + return ret + } + + private val reAnimWithNumber = Regex("""ANIM_[A-Z]+_[0-9]+""") private fun List.toJoints() = List(this.size) { Joint(this[it].name.toUpperCase(), this[it].input!! as ADPropertyObject.Vector2i)