卡牌游戏开发中,使用效果时有两种模式,一种是选择对象发动能力,一种是输入序号发动抉择效果。
可能两种都有,可能两种都没有。如果【使用效果】对象是用BiConsumer来处理效果,那么如果两种都没有时,就要传一堆垃圾参数,看上去就很难受。
不多说,我是这样写的:
public record Play(Supplier<List<GameObj>> targets, int targetNum,
int choiceNum, BiConsumer<Integer,List<GameObj>> effect){
/**
* 仅抉择
*/
public Play(int choiceNum,Consumer<Integer> effect) {
this(ArrayList::new,0,choiceNum,((integer, gameObjs) -> effect.accept(integer)));
}
/**
* 仅选择目标
*/
public Play(Supplier<List<GameObj>> targets, int targetNum,Consumer<List<GameObj>> effect) {
this(targets,targetNum,0,((integer, gameObjs) -> effect.accept(gameObjs)));
}
/**
* 不需要抉择/选择目标
*/
public Play(FunctionN effect) {
this(ArrayList::new,0,0,((integer, gameObjs) -> effect.apply()));
}
}
其中FunctionN 是自己写的一个函数类,代表无入参无出参,补齐了jdk中函数类的一个痛点。
@FunctionalInterface
public interface FunctionN {
void apply();
}