ConsumeItemBattleMutationInterceptor.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.mutation.BattleMutation
import io.github.lishangbu.avalon.game.battle.engine.core.mutation.ConsumeItemMutation
import io.github.lishangbu.avalon.game.battle.engine.core.runtime.support.MutationTargetSelectorResolver
/**
* 道具消耗 mutation 的生命周期拦截器。
*
* 设计意图:
* - 在 `ConsumeItemMutation` 真正把 `itemId` 清空前,为目标派发 `on_consume_item`;
* - 让目标身上的 ability / status / condition 等效果有机会阻止这次消耗;
* - 只有当前目标确实持有道具时,才认为这次生命周期成立。
*/
class ConsumeItemBattleMutationInterceptor : BattleMutationInterceptor {
override val order: Int = 525
override fun supports(mutation: BattleMutation): Boolean = mutation is ConsumeItemMutation
override fun intercept(
context: BattleMutationInterceptionContext,
attachedEffectProcessor: BattleAttachedEffectProcessor,
): BattleMutationInterceptionResult {
val mutation =
context.mutation as? ConsumeItemMutation
?: return BattleMutationInterceptionResult(context.snapshot, true, context.mutation)
val targetUnitIds =
MutationTargetSelectorResolver.resolve(
mutation.target,
BattleMutationInterceptorSupport.mutationApplicationContext(context),
)
var currentSnapshot = context.snapshot
var blocked = false
targetUnitIds.forEach { affectedTargetId ->
val affectedUnit = currentSnapshot.units[affectedTargetId] ?: return@forEach
val itemId = affectedUnit.itemId ?: return@forEach
val result =
attachedEffectProcessor.process(
snapshot = currentSnapshot,
unitId = affectedTargetId,
hookName = StandardHookNames.ON_CONSUME_ITEM.value,
targetId = affectedTargetId,
sourceId = context.sourceId,
relay = true,
attributes =
buildMap {
put("itemId", itemId)
BattleMutationInterceptorSupport
.resolveTargetRelation(currentSnapshot, context.sourceId, affectedTargetId)
?.let { relation -> put(BattleAttributeKeys.TARGET_RELATION, relation) }
},
)
currentSnapshot = result.snapshot
if (result.cancelled || result.relay == false) {
blocked = true
}
}
return BattleMutationInterceptionResult(
snapshot = currentSnapshot,
allowed = !blocked,
mutation = mutation,
)
}
}