From d3d843312ca81b74b03cc2d4b8e4517b306068d9 Mon Sep 17 00:00:00 2001 From: Light_Quanta Date: Mon, 24 Feb 2025 04:18:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96DamageModifier?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entity/vehicle/damage/DamageModifier.java | 47 ++++++++++++------- .../entity/vehicle/damage/DamageModify.java | 19 +++++--- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/damage/DamageModifier.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/damage/DamageModifier.java index 5e854b661..01dd61ed6 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/damage/DamageModifier.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/damage/DamageModifier.java @@ -11,13 +11,15 @@ import java.util.function.Function; public class DamageModifier { - private final List list = new ArrayList<>(); + private final List immuneList = new ArrayList<>(); + private final List reduceList = new ArrayList<>(); + private final List multiplyList = new ArrayList<>(); /** * 免疫所有伤害 */ public DamageModifier immuneTo() { - list.add(new DamageModify(DamageModify.ReduceType.IMMUNITY, 0, (TagKey) null)); + immuneList.add(new DamageModify(DamageModify.ModifyType.IMMUNITY, 0)); return this; } @@ -27,7 +29,7 @@ public class DamageModifier { * @param sourceTagKey 伤害类型 */ public DamageModifier immuneTo(TagKey sourceTagKey) { - list.add(new DamageModify(DamageModify.ReduceType.IMMUNITY, 0, sourceTagKey)); + immuneList.add(new DamageModify(DamageModify.ModifyType.IMMUNITY, 0, sourceTagKey)); return this; } @@ -37,7 +39,7 @@ public class DamageModifier { * @param sourceKey 伤害类型 */ public DamageModifier immuneTo(ResourceKey sourceKey) { - list.add(new DamageModify(DamageModify.ReduceType.IMMUNITY, 0, sourceKey)); + immuneList.add(new DamageModify(DamageModify.ModifyType.IMMUNITY, 0, sourceKey)); return this; } @@ -47,7 +49,7 @@ public class DamageModifier { * @param condition 伤害来源判定条件 */ public DamageModifier immuneTo(Function condition) { - list.add(new DamageModify(DamageModify.ReduceType.IMMUNITY, 0, condition)); + immuneList.add(new DamageModify(DamageModify.ModifyType.IMMUNITY, 0, condition)); return this; } @@ -57,7 +59,7 @@ public class DamageModifier { * @param value 要减少的数值 */ public DamageModifier reduce(float value) { - list.add(new DamageModify(DamageModify.ReduceType.REDUCE, value, (TagKey) null)); + reduceList.add(new DamageModify(DamageModify.ModifyType.REDUCE, value)); return this; } @@ -68,7 +70,7 @@ public class DamageModifier { * @param sourceTagKey 伤害类型 */ public DamageModifier reduce(float value, TagKey sourceTagKey) { - list.add(new DamageModify(DamageModify.ReduceType.REDUCE, value, sourceTagKey)); + reduceList.add(new DamageModify(DamageModify.ModifyType.REDUCE, value, sourceTagKey)); return this; } @@ -79,7 +81,7 @@ public class DamageModifier { * @param sourceKey 伤害类型 */ public DamageModifier reduce(float value, ResourceKey sourceKey) { - list.add(new DamageModify(DamageModify.ReduceType.REDUCE, value, sourceKey)); + reduceList.add(new DamageModify(DamageModify.ModifyType.REDUCE, value, sourceKey)); return this; } @@ -90,53 +92,55 @@ public class DamageModifier { * @param condition 伤害来源判定条件 */ public DamageModifier reduce(float value, Function condition) { - list.add(new DamageModify(DamageModify.ReduceType.REDUCE, value, condition)); + reduceList.add(new DamageModify(DamageModify.ModifyType.REDUCE, value, condition)); return this; } /** - * 将所有类型的伤害乘以指定数值 + * 将所有类型的伤害值乘以指定数值 * * @param value 要乘以的数值 */ public DamageModifier multiply(float value) { - list.add(new DamageModify(DamageModify.ReduceType.MULTIPLY, value, (TagKey) null)); + multiplyList.add(new DamageModify(DamageModify.ModifyType.MULTIPLY, value)); return this; } /** - * 将指定类型的伤害乘以指定数值 + * 将指定类型的伤害值乘以指定数值 * * @param value 要乘以的数值 * @param sourceTagKey 伤害类型 */ public DamageModifier multiply(float value, TagKey sourceTagKey) { - list.add(new DamageModify(DamageModify.ReduceType.MULTIPLY, value, sourceTagKey)); + multiplyList.add(new DamageModify(DamageModify.ModifyType.MULTIPLY, value, sourceTagKey)); return this; } /** - * 将指定类型的伤害乘以指定数值 + * 将指定类型的伤害值乘以指定数值 * * @param value 要乘以的数值 * @param sourceKey 伤害类型 */ public DamageModifier multiply(float value, ResourceKey sourceKey) { - list.add(new DamageModify(DamageModify.ReduceType.MULTIPLY, value, sourceKey)); + multiplyList.add(new DamageModify(DamageModify.ModifyType.MULTIPLY, value, sourceKey)); return this; } /** - * 将指定类型的伤害乘以指定数值 + * 将指定类型的伤害值乘以指定数值 * * @param value 要乘以的数值 * @param condition 伤害来源判定条件 */ public DamageModifier multiply(float value, Function condition) { - list.add(new DamageModify(DamageModify.ReduceType.MULTIPLY, value, condition)); + multiplyList.add(new DamageModify(DamageModify.ModifyType.MULTIPLY, value, condition)); return this; } + private final List combinedList = new ArrayList<>(); + /** * 计算减伤后的伤害值 * @@ -145,7 +149,14 @@ public class DamageModifier { * @return 减伤后的伤害值 */ public float compute(DamageSource source, float damage) { - for (DamageModify damageModify : list) { + if (combinedList.isEmpty()) { + // 计算优先级 免疫 > 固定减伤 > 乘 + combinedList.addAll(immuneList); + combinedList.addAll(reduceList); + combinedList.addAll(multiplyList); + } + + for (DamageModify damageModify : combinedList) { if (damageModify.match(source)) { damage = damageModify.compute(damage); diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/damage/DamageModify.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/damage/DamageModify.java index fc0d38f30..76538d051 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/damage/DamageModify.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/damage/DamageModify.java @@ -8,32 +8,38 @@ import net.minecraft.world.damagesource.DamageType; import java.util.function.Function; public class DamageModify { - public enum ReduceType { + public enum ModifyType { IMMUNITY, // 完全免疫 REDUCE, // 固定数值减伤 MULTIPLY, // 乘以指定倍数 } private final float value; - private final ReduceType type; + private final ModifyType type; private TagKey sourceTagKey = null; private ResourceKey sourceKey = null; private Function condition = null; - public DamageModify(ReduceType type, float value, TagKey sourceTagKey) { + public DamageModify(ModifyType type, float value) { + this.type = type; + this.value = value; + } + + + public DamageModify(ModifyType type, float value, TagKey sourceTagKey) { this.type = type; this.value = value; this.sourceTagKey = sourceTagKey; } - public DamageModify(ReduceType type, float value, ResourceKey sourceKey) { + public DamageModify(ModifyType type, float value, ResourceKey sourceKey) { this.type = type; this.value = value; this.sourceKey = sourceKey; } - public DamageModify(ReduceType type, float value, Function condition) { + public DamageModify(ModifyType type, float value, Function condition) { this.type = type; this.value = value; this.condition = condition; @@ -52,9 +58,8 @@ public class DamageModify { return source.is(sourceTagKey); } else if (sourceKey != null) { return source.is(sourceKey); - } else { - return true; } + return true; } /**