From e6b36401811ee2ba3f44e2372f4ae908cfd2f8d6 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Mon, 24 Apr 2023 09:35:47 +0300 Subject: [PATCH] chore: Battle animation readability --- .../worldscreen/bottombar/BattleTable.kt | 4 +- .../bottombar/BattleTableHelpers.kt | 81 +++++++++---------- 2 files changed, 38 insertions(+), 47 deletions(-) diff --git a/core/src/com/unciv/ui/screens/worldscreen/bottombar/BattleTable.kt b/core/src/com/unciv/ui/screens/worldscreen/bottombar/BattleTable.kt index 2426ec0b1a..7ffec9c4fc 100644 --- a/core/src/com/unciv/ui/screens/worldscreen/bottombar/BattleTable.kt +++ b/core/src/com/unciv/ui/screens/worldscreen/bottombar/BattleTable.kt @@ -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) } diff --git a/core/src/com/unciv/ui/screens/worldscreen/bottombar/BattleTableHelpers.kt b/core/src/com/unciv/ui/screens/worldscreen/bottombar/BattleTableHelpers.kt index 795ecb8d17..90ef0845a4 100644 --- a/core/src/com/unciv/ui/screens/worldscreen/bottombar/BattleTableHelpers.kt +++ b/core/src/com/unciv/ui/screens/worldscreen/bottombar/BattleTableHelpers.kt @@ -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) : 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, 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 = 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) ) ) ))