From 931de95144ba1a47d4a9d743fb8b6f9f889bfda3 Mon Sep 17 00:00:00 2001 From: 1 <1@72wo.cn> Date: Wed, 10 Sep 2025 02:16:47 +0000 Subject: [PATCH] =?UTF-8?q?refactor(fightc):=20=E4=BD=BF=E7=94=A8=E6=B3=9B?= =?UTF-8?q?=E5=9E=8B=E5=AE=9E=E7=8E=B0=20Max=20=E5=87=BD=E6=95=B0=E5=B9=B6?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=A1=80=E9=87=8F=E6=89=A3=E5=87=8F=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- logic/service/fightc.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/logic/service/fightc.go b/logic/service/fightc.go index 99320e386..f7a36681b 100644 --- a/logic/service/fightc.go +++ b/logic/service/fightc.go @@ -415,6 +415,19 @@ func (f *FightC) newBPET(input *Input) *BPET { AttackValue: info.NewAttackValue(input.Player.ID()), Damage: 0, } +} // 定义泛型约束:只允许数值类型(整数、浮点数等) +type Number interface { + int | int8 | int16 | int32 | int64 | + uint | uint8 | uint16 | uint32 | uint64 | + float32 | float64 +} + +// Max 泛型函数:接收两个同类型的 Number 参数,返回最大值 +func Max[T Number](a, b T) T { + if a > b { + return a + } + return b } func (f *FightC) initAttackers(fattack, sattack info.BattleActionI) { // 伤害值 @@ -451,13 +464,11 @@ func (f *FightC) processSkillAttack(attacker, defender *BPET, skill *info.Select if _, ok := skill.Skill.IsCritical(); ok { attacker.AttackValue.IsCritical = 1 } + defender.CurrentPet.Info.Hp -= attacker.Damage + defender.CurrentPet.Info.Hp = Max((defender.CurrentPet.Info.Hp), 0) // 扣减防御方血量 - if attacker.Damage > defender.CurrentPet.Info.Hp { - defender.CurrentPet.Info.Hp = 0 - } else { - defender.CurrentPet.Info.Hp -= attacker.Damage - } + } // 检查战斗是否结束 @@ -487,14 +498,14 @@ func (f *FightC) enterturn(fattack, sattack info.BattleActionI) { } skill, ok := attacker.BattleActionI.(*info.SelectSkillAction) - if !ok {//还有系统选择放弃出手的 + if !ok { //还有系统选择放弃出手的 continue - } - if attacker.CurrentPet.Info.Hp <= 0 {//攻击方死亡 + } + if attacker.CurrentPet.Info.Hp <= 0 { //攻击方死亡 continue - } + } f.processSkillAttack(attacker, defender, skill) fmt.Println(i, "玩家技能伤害:", attacker.Damage,