refactor(fightc): 使用泛型实现 Max 函数并优化血量扣减逻辑
This commit is contained in:
@@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
// 扣减防御方血量
|
|
||||||
if attacker.Damage > defender.CurrentPet.Info.Hp {
|
|
||||||
defender.CurrentPet.Info.Hp = 0
|
|
||||||
} else {
|
|
||||||
defender.CurrentPet.Info.Hp -= attacker.Damage
|
defender.CurrentPet.Info.Hp -= attacker.Damage
|
||||||
}
|
|
||||||
|
defender.CurrentPet.Info.Hp = Max((defender.CurrentPet.Info.Hp), 0)
|
||||||
|
// 扣减防御方血量
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查战斗是否结束
|
// 检查战斗是否结束
|
||||||
|
|||||||
Reference in New Issue
Block a user