From 1ea714efe8eeaafd4d370a42ed662fa0f4c6caa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=94=E5=BF=B5?= <1@72wo.cn> Date: Wed, 10 Sep 2025 01:35:08 +0800 Subject: [PATCH] =?UTF-8?q?refactor(fightc):=20=E9=87=8D=E6=9E=84=E6=88=98?= =?UTF-8?q?=E6=96=97=E9=80=BB=E8=BE=91=E5=B9=B6=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 为 BPET 结构体添加 BattleActionI 接口字段 - 优化 initAttackers 方法,使用更简洁的赋值方式 - 重构 enterturn 方法,提高代码可读性和可维护性 - 改进 winner 方法,使用 --- logic/service/fightc.go | 64 +++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/logic/service/fightc.go b/logic/service/fightc.go index d4d3866ca..c834cc0df 100644 --- a/logic/service/fightc.go +++ b/logic/service/fightc.go @@ -348,6 +348,7 @@ type BPET struct { *info.BattlePetEntity //精灵实体 *info.AttackValue *FightC + info.BattleActionI Damage uint32 //造成伤害 } @@ -421,11 +422,11 @@ func (f *FightC) initAttackers(fattack, sattack info.BattleActionI) { // 根据攻击方归属设置当前战斗的主/次攻击方属性 var first, second *Input // 定义临时变量存储主/次攻击方 if fattack.GetPlayerID() == f.OwnerID { - first = f.Our // 攻击方为我方时,主攻击方是我方 - second = f.Opp // 次攻击方是对方 + first, second = f.Our, f.Opp // 攻击方为我方时,主攻击方是我方 + } else { - first = f.Opp // 攻击方为对方时,主攻击方是对方 - second = f.Our // 次攻击方是我方 + first, second = f.Opp, f.Our // 攻击方为对方时,主攻击方是对方 + } // 统一赋值,减少重复代码 @@ -474,27 +475,40 @@ func (f *FightC) winner(winner *BPET) func() { } func (f *FightC) enterturn(fattack, sattack info.BattleActionI) { f.initAttackers(fattack, sattack) //初始化先后手 - skill, _ := fattack.(*info.SelectSkillAction) + var attacker, defender *BPET + for i := 0; i < 2; i++ { + if i == 0 { // + attacker, defender = f.First, f.Second + attacker.BattleActionI, defender.BattleActionI = fattack, sattack - f.processSkillAttack(f.First, f.Second, skill) - fmt.Printf("先手技能伤害: %v, 先手剩余血量: %v\n", f.First.Damage, f.First.CurrentPet.Info.Hp) - fmt.Printf("%v 先手出招结束\n", f.First.CurrentPet.Info.CatchTime) - if f.Second.CurrentPet.Info.Hp > 0 { //如果后手方没被打死 - skill, ok := sattack.(*info.SelectSkillAction) - if ok { + } else { + attacker, defender = f.Second, f.First - f.processSkillAttack(f.Second, f.First, skill) - fmt.Printf("后手技能伤害: %v, 后手剩余血量: %v\n", f.Second.Damage, f.Second.CurrentPet.Info.Hp) - fmt.Printf("%v 后手出招结束\n", f.Second.CurrentPet.Info.CatchTime) + } + + skill, ok := attacker.BattleActionI.(*info.SelectSkillAction) + if !ok || attacker.CurrentPet.Info.Hp <= 0 { + continue } //还有系统选择放弃出手的 - - if f.First.CurrentPet.Info.Hp == 0 { - f.First.CanChange = true //被打死就可以切精灵了 - if f.Second.IsWin(f.First.CurrentPet.Info.CatchTime) { //然后检查是否战斗结束 + f.processSkillAttack(attacker, defender, skill) + fmt.Println(i, + "玩家技能伤害:", attacker.Damage, + "自身剩余血量:", attacker.CurrentPet.Info.Hp, + "对手剩余血量:", defender.CurrentPet.Info.Hp, + ) + if defender.CurrentPet.Info.Hp == 0 { + defender.CanChange = true //被打死就可以切精灵了 + if attacker.IsWin(defender.CurrentPet.Info.CatchTime) { //然后检查是否战斗结束 + var WinnerId uint32 + if i == 0 { + WinnerId = f.First.Player.ID() + } else { + WinnerId = f.Second.Player.ID() + } defer f.Broadcast(func(ff *Input) { ff.Player.SendFightEndInfo(info.FightOverInfo{ - WinnerId: f.Second.Player.ID(), + WinnerId: WinnerId, }) }) defer close(f.actionChan) @@ -502,18 +516,6 @@ func (f *FightC) enterturn(fattack, sattack info.BattleActionI) { } } - } else { - f.Second.AttackValue = info.NewAttackValue(f.Second.Player.ID()) //已经被打死了,直接将后手方输出归0 - f.Second.CanChange = true //被打死就可以切精灵了 - if f.First.IsWin(f.Second.CurrentPet.Info.CatchTime) { //然后检查是否战斗结束 - defer f.Broadcast(func(ff *Input) { - ff.Player.SendFightEndInfo(info.FightOverInfo{ - WinnerId: f.First.Player.ID(), - }) - }) - defer close(f.actionChan) - - } } f.Broadcast(func(ff *Input) {