refactor(fightc): 使用泛型实现 Max 函数并优化血量扣减逻辑

This commit is contained in:
1
2025-09-10 02:16:47 +00:00
parent 2f348a7732
commit 931de95144

View File

@@ -415,6 +415,19 @@ func (f *FightC) newBPET(input *Input) *BPET {
AttackValue: info.NewAttackValue(input.Player.ID()), AttackValue: info.NewAttackValue(input.Player.ID()),
Damage: 0, 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) { 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 { if _, ok := skill.Skill.IsCritical(); ok {
attacker.AttackValue.IsCritical = 1 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) skill, ok := attacker.BattleActionI.(*info.SelectSkillAction)
if !ok {//还有系统选择放弃出手的 if !ok { //还有系统选择放弃出手的
continue continue
} }
if attacker.CurrentPet.Info.Hp <= 0 {//攻击方死亡 if attacker.CurrentPet.Info.Hp <= 0 { //攻击方死亡
continue continue
} }
f.processSkillAttack(attacker, defender, skill) f.processSkillAttack(attacker, defender, skill)
fmt.Println(i, fmt.Println(i,
"玩家技能伤害:", attacker.Damage, "玩家技能伤害:", attacker.Damage,