BattleMoveBeforeDamagePhaseStep.kt

package io.github.lishangbu.avalon.game.battle.engine.core.runtime.flow

import io.github.lishangbu.avalon.game.battle.engine.core.constant.BattleAttributeKeys
import io.github.lishangbu.avalon.game.battle.engine.core.event.StandardHookNames
import io.github.lishangbu.avalon.game.battle.engine.core.runtime.support.BattleMoveDataReader

/**
 * 命中后、伤害结算前的 hook phase step。
 *
 * 设计意图:
 * - 为 Spectral Thief 这类“命中成立后、伤害计算前先改写状态”的规则提供稳定插槽;
 * - 避免把这类逻辑错误塞进 `on_try_hit`,导致招式 miss 时也提前生效;
 * - 让 effect 自身与挂载效果都能在正式算伤前观察到当前命中结果。
 */
class BattleMoveBeforeDamagePhaseStep(
    private val phaseProcessor: BattleFlowPhaseProcessor,
) : BattleMoveResolutionStep {
    override val order: Int = 275

    override fun execute(context: BattleMoveResolutionContext) {
        if (!context.hitSuccessful || context.cancelled) {
            return
        }
        val result =
            phaseProcessor.processPhase(
                snapshot = context.snapshot,
                hookName = StandardHookNames.ON_BEFORE_DAMAGE.value,
                moveEffect = context.moveEffect,
                selfId = context.attackerId,
                targetId = context.targetId,
                sourceId = context.sourceId,
                relay = null,
                attributes =
                    context.attributes +
                        mapOf(
                            BattleAttributeKeys.CRITICAL_HIT to context.criticalHit,
                            BattleAttributeKeys.MOVE_TYPE to BattleMoveDataReader.readType(context.moveEffect.data),
                            BattleAttributeKeys.DAMAGE_CLASS to BattleMoveDataReader.readDamageClass(context.moveEffect.data),
                            BattleAttributeKeys.BASE_POWER to context.basePower,
                        ),
            )
        context.snapshot = result.snapshot
        if (result.cancelled) {
            context.markCancelled(result.snapshot)
        }
    }
}