chore: Battle animation readability

This commit is contained in:
Yair Morgenstern
2023-04-24 09:35:47 +03:00
parent 19920ffbae
commit e6b3640181
2 changed files with 38 additions and 47 deletions

View File

@ -28,7 +28,7 @@ import com.unciv.ui.components.extensions.toTextButton
import com.unciv.ui.images.ImageGetter
import com.unciv.ui.screens.basescreen.BaseScreen
import com.unciv.ui.screens.worldscreen.WorldScreen
import com.unciv.ui.screens.worldscreen.bottombar.BattleTableHelpers.flashWoundedCombatants
import com.unciv.ui.screens.worldscreen.bottombar.BattleTableHelpers.battleAnimation
import com.unciv.ui.screens.worldscreen.bottombar.BattleTableHelpers.getHealthBar
import com.unciv.utils.DebugUtils
import kotlin.math.max
@ -311,7 +311,7 @@ class BattleTable(val worldScreen: WorldScreen): Table() {
SoundPlayer.play(attacker.getAttackSound())
Battle.attackOrNuke(attacker, attackableTile)
worldScreen.flashWoundedCombatants(attacker, damageToAttacker, defender, damageToDefender)
worldScreen.battleAnimation(attacker, damageToAttacker, defender, damageToDefender)
}

View File

@ -2,6 +2,7 @@ package com.unciv.ui.screens.worldscreen.bottombar
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.math.Interpolation
import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.scenes.scene2d.Actor
import com.badlogic.gdx.scenes.scene2d.Group
import com.badlogic.gdx.scenes.scene2d.actions.Actions
@ -18,23 +19,42 @@ import com.unciv.ui.screens.worldscreen.WorldScreen
object BattleTableHelpers {
fun WorldScreen.flashWoundedCombatants(
class FlashRedAction(start:Float, end:Float, private val actorsToOriginalColors:Map<Actor, Color>) : FloatAction(start, end, 0.2f, Interpolation.sine){
private fun updateRedPercent(percent: Float) {
for ((actor, color) in actorsToOriginalColors)
actor.color = color.cpy().lerp(Color.RED, start+percent*(end-start))
}
override fun update(percent: Float) = updateRedPercent(percent)
}
class MoveActorsAction(private val actorsToMove:List<Actor>, private val movementVector: Vector2) : RelativeTemporalAction(){
init {
duration = 0.3f
interpolation = Interpolation.sine
}
override fun updateRelative(percentDelta: Float) {
for (actor in actorsToMove){
actor.moveBy(movementVector.x * percentDelta, movementVector.y * percentDelta)
}
}
}
fun WorldScreen.battleAnimation(
attacker: ICombatant, damageToAttacker: Int,
defender: ICombatant, damageToDefender: Int
) {
fun getMapActorsForCombatant(combatant: ICombatant):Sequence<Actor> =
sequence {
val tileGroup = mapHolder.tileGroups[combatant.getTile()]!!
when {
combatant.isCity() -> {
val icon = tileGroup.layerMisc.improvementIcon
if (icon != null)
yield(icon)
}
else -> {
val slot = if (combatant.isCivilian()) 0 else 1
yieldAll((tileGroup.layerUnitArt.getChild(slot) as Group).children)
}
if (combatant.isCity()) {
val icon = tileGroup.layerMisc.improvementIcon
if (icon != null) yield(icon)
}
else {
val slot = if (combatant.isCivilian()) 0 else 1
yieldAll((tileGroup.layerUnitArt.getChild(slot) as Group).children)
}
}
@ -42,12 +62,7 @@ object BattleTableHelpers {
sequence {
if (damageToDefender != 0) yieldAll(getMapActorsForCombatant(defender))
if (damageToAttacker != 0) yieldAll(getMapActorsForCombatant(attacker))
}.mapTo(arrayListOf()) { it to it.color.cpy() }
fun updateRedPercent(percent: Float) {
for ((actor, color) in actorsToFlashRed)
actor.color = color.cpy().lerp(Color.RED, percent)
}
}.mapTo(arrayListOf()) { it to it.color.cpy() }.toMap()
val actorsToMove = getMapActorsForCombatant(attacker).toList()
@ -58,36 +73,12 @@ object BattleTableHelpers {
stage.addAction(
Actions.sequence(
object : RelativeTemporalAction(){
init {
duration = 0.3f
interpolation = Interpolation.sine
}
override fun updateRelative(percentDelta: Float) {
for (actor in actorsToMove){
actor.moveBy(attackVectorWorldCoords.x * percentDelta, attackVectorWorldCoords.y * percentDelta)
}
}
},
MoveActorsAction(actorsToMove, attackVectorWorldCoords),
Actions.parallel( // While the unit is moving back to its normal position, we flash the damages on both units
object : RelativeTemporalAction(){
init {
duration = 0.3f
interpolation = Interpolation.sine
}
override fun updateRelative(percentDelta: Float) {
for (actor in actorsToMove){
actor.moveBy(attackVectorWorldCoords.x * -percentDelta, attackVectorWorldCoords.y * -percentDelta)
}
}
},
MoveActorsAction(actorsToMove, attackVectorWorldCoords.cpy().scl(-1f)),
Actions.sequence(
object : FloatAction(0f, 1f, 0.2f, Interpolation.sine) {
override fun update(percent: Float) = updateRedPercent(percent)
},
object : FloatAction(0f, 1f, 0.2f, Interpolation.sine) {
override fun update(percent: Float) = updateRedPercent(1 - percent)
}
FlashRedAction(0f,1f, actorsToFlashRed),
FlashRedAction(1f,0f, actorsToFlashRed)
)
)
))