refactor(fightc): 重构战斗逻辑并优化代码结构

- 为 BPET 结构体添加 BattleActionI 接口字段
- 优化 initAttackers 方法,使用更简洁的赋值方式
- 重构 enterturn 方法,提高代码可读性和可维护性
- 改进 winner 方法,使用
This commit is contained in:
2025-09-10 01:35:08 +08:00
parent a09bc7884c
commit 1ea714efe8

View File

@@ -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,41 +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)
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 {
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)
} //还有系统选择放弃出手的
if f.First.CurrentPet.Info.Hp == 0 {
f.First.CanChange = true //被打死就可以切精灵了
if f.Second.IsWin(f.First.CurrentPet.Info.CatchTime) { //然后检查是否战斗结束
defer f.Broadcast(func(ff *Input) {
ff.Player.SendFightEndInfo(info.FightOverInfo{
WinnerId: f.Second.Player.ID(),
})
})
defer close(f.actionChan)
}
}
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
} else {
f.Second.AttackValue = info.NewAttackValue(f.Second.Player.ID()) //已经被打死了,直接将后手方输出归0
f.Second.CanChange = true //被打死就可以切精灵了
if f.First.IsWin(f.Second.CurrentPet.Info.CatchTime) { //然后检查是否战斗结束
attacker, defender = f.Second, f.First
}
skill, ok := attacker.BattleActionI.(*info.SelectSkillAction)
if !ok || attacker.CurrentPet.Info.Hp <= 0 {
continue
} //还有系统选择放弃出手的
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.First.Player.ID(),
WinnerId: WinnerId,
})
})
defer close(f.actionChan)
@@ -516,6 +516,8 @@ func (f *FightC) enterturn(fattack, sattack info.BattleActionI) {
}
}
}
f.Broadcast(func(ff *Input) {
ret := info.AttackValueS{
FAttack: *f.First.AttackValue,