refactor(fightc): 重构战斗回合逻辑,提取公共方法并优化代码结构
This commit is contained in:
@@ -398,8 +398,18 @@ func (f *FightC) parseskill(id *info.SelectSkillAction) {
|
||||
|
||||
}
|
||||
}
|
||||
func (f *FightC) enterturn(fattack, sattack info.BattleActionI) {
|
||||
|
||||
// 创建BPET实例的辅助函数
|
||||
func (f *FightC) newBPET(input *Input) *BPET {
|
||||
return &BPET{
|
||||
FightC: f,
|
||||
Input: input,
|
||||
BattlePetEntity: input.CurrentPet,
|
||||
AttackValue: info.NewAttackValue(input.Player.ID()),
|
||||
Damage: 0,
|
||||
}
|
||||
}
|
||||
func (f *FightC) initAttackers(fattack, sattack info.BattleActionI) {
|
||||
// 伤害值
|
||||
|
||||
// 根据攻击方归属设置当前战斗的主/次攻击方属性
|
||||
@@ -413,102 +423,78 @@ func (f *FightC) enterturn(fattack, sattack info.BattleActionI) {
|
||||
}
|
||||
|
||||
// 统一赋值,减少重复代码
|
||||
f.First = &BPET{
|
||||
FightC: f,
|
||||
Input: first,
|
||||
BattlePetEntity: first.CurrentPet,
|
||||
AttackValue: info.NewAttackValue(first.Player.ID()),
|
||||
Damage: 0}
|
||||
f.Second = &BPET{
|
||||
FightC: f,
|
||||
Input: second,
|
||||
BattlePetEntity: second.CurrentPet,
|
||||
AttackValue: info.NewAttackValue(second.Player.ID()),
|
||||
Damage: 0}
|
||||
f.First = f.newBPET(first)
|
||||
f.Second = f.newBPET(second)
|
||||
|
||||
fmt.Println("先手", f.First.CurrentPet.Info.CatchTime, "后手", f.Second.CurrentPet.Info.CatchTime)
|
||||
|
||||
// TODO: 在这里调用技能结算逻辑
|
||||
}
|
||||
|
||||
// 处理技能攻击逻辑
|
||||
func (f *FightC) processSkillAttack(attacker, defender *BPET, skill *info.SelectSkillAction) {
|
||||
f.parseskill(skill)
|
||||
spower := skill.Skill.CalculatePower(defender.Type().ID)
|
||||
attacker.Damage = spower
|
||||
|
||||
// 记录技能信息
|
||||
attacker.AttackValue.SkillID = uint32(skill.Skill.ID)
|
||||
attacker.AttackValue.AttackTime = skill.Skill.AttackTime()
|
||||
// 判断是否暴击
|
||||
if _, ok := skill.Skill.IsCritical(); ok {
|
||||
attacker.AttackValue.IsCritical = 1
|
||||
}
|
||||
|
||||
// 扣减防御方血量
|
||||
if attacker.Damage > defender.CurrentPet.Info.Hp {
|
||||
defender.CurrentPet.Info.Hp = 0
|
||||
} else {
|
||||
defender.CurrentPet.Info.Hp -= attacker.Damage
|
||||
}
|
||||
}
|
||||
|
||||
// 检查战斗是否结束
|
||||
func (f *FightC) winner(winner *BPET) func() {
|
||||
|
||||
return func() {
|
||||
f.Broadcast(func(ff *Input) {
|
||||
ff.Player.SendFightEndInfo(info.FightOverInfo{
|
||||
WinnerId: winner.Player.ID(),
|
||||
})
|
||||
})
|
||||
close(f.actionChan)
|
||||
}
|
||||
}
|
||||
func (f *FightC) enterturn(fattack, sattack info.BattleActionI) {
|
||||
f.initAttackers(fattack, sattack) //初始化先后手
|
||||
skill, _ := fattack.(*info.SelectSkillAction)
|
||||
|
||||
f.parseskill(skill)
|
||||
spower := skill.Skill.CalculatePower(f.Second.Type().ID)
|
||||
|
||||
//开始计算技能效果battleLoop
|
||||
_, ok := skill.Skill.IsCritical()
|
||||
|
||||
//CalculatePower()
|
||||
//记录伤害,到后手方出手时计算伤害
|
||||
f.First.Damage = (spower)
|
||||
|
||||
fmt.Println("先手技能伤害", spower, "先手剩余血量", f.First.CurrentPet.Info.Hp)
|
||||
|
||||
f.First.AttackValue.SkillID = uint32(skill.Skill.ID)
|
||||
f.First.AttackValue.AttackTime = skill.Skill.AttackTime()
|
||||
if ok { //判断暴击
|
||||
f.First.AttackValue.IsCritical = 1
|
||||
}
|
||||
fmt.Println(f.First.CurrentPet.Info.CatchTime, "先手出招结束")
|
||||
|
||||
if f.First.Damage > f.Second.CurrentPet.Info.Hp {
|
||||
f.Second.CurrentPet.Info.Hp = 0
|
||||
} else {
|
||||
f.Second.CurrentPet.Info.Hp = f.Second.CurrentPet.Info.Hp - f.First.Damage //扣减后手方血量
|
||||
}
|
||||
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.parseskill(skill)
|
||||
spower := skill.Skill.CalculatePower(f.First.Type().ID)
|
||||
|
||||
//开始计算技能效果battleLoop
|
||||
_, ok := skill.Skill.IsCritical()
|
||||
|
||||
//CalculatePower()
|
||||
//记录伤害,到后手方出手时计算伤害
|
||||
f.Second.Damage = (spower)
|
||||
|
||||
fmt.Println("后手技能伤害", spower, "后手剩余血量", f.Second.CurrentPet.Info.Hp)
|
||||
|
||||
f.Second.AttackValue.SkillID = uint32(skill.Skill.ID)
|
||||
f.Second.AttackValue.AttackTime = skill.Skill.AttackTime()
|
||||
if ok { //判断暴击
|
||||
f.Second.AttackValue.IsCritical = 1
|
||||
}
|
||||
fmt.Println(f.Second.CurrentPet.Info.CatchTime, "后手出招结束")
|
||||
if f.Second.Damage > f.First.CurrentPet.Info.Hp {
|
||||
f.First.CurrentPet.Info.Hp = 0
|
||||
} else {
|
||||
f.First.CurrentPet.Info.Hp = f.First.CurrentPet.Info.Hp - f.Second.Damage //扣减先手方血量
|
||||
}
|
||||
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.UserID,
|
||||
}) //广播逃跑原因
|
||||
})
|
||||
defer close(f.actionChan) //关闭战斗通道
|
||||
defer f.winner(f.Second) //广播逃跑原因
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
f.Second.AttackValue = info.NewAttackValue(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.UserID,
|
||||
}) //广播逃跑原因
|
||||
})
|
||||
defer close(f.actionChan) //关闭战斗通道
|
||||
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.winner(f.First) //广播逃跑原因
|
||||
|
||||
}
|
||||
}
|
||||
@@ -525,5 +511,5 @@ func (f *FightC) enterturn(fattack, sattack info.BattleActionI) {
|
||||
ret.SAttack.LostHp = uint32(f.Second.Damage) //后手方造成血量
|
||||
ff.Player.SendAttackValue(ret)
|
||||
})
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user