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()),
|
||||
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) {
|
||||
// 伤害值
|
||||
@@ -451,13 +464,11 @@ func (f *FightC) processSkillAttack(attacker, defender *BPET, skill *info.Select
|
||||
if _, ok := skill.Skill.IsCritical(); ok {
|
||||
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)
|
||||
if !ok {//还有系统选择放弃出手的
|
||||
if !ok { //还有系统选择放弃出手的
|
||||
continue
|
||||
|
||||
}
|
||||
if attacker.CurrentPet.Info.Hp <= 0 {//攻击方死亡
|
||||
}
|
||||
if attacker.CurrentPet.Info.Hp <= 0 { //攻击方死亡
|
||||
continue
|
||||
|
||||
}
|
||||
}
|
||||
f.processSkillAttack(attacker, defender, skill)
|
||||
fmt.Println(i,
|
||||
"玩家技能伤害:", attacker.Damage,
|
||||
|
||||
Reference in New Issue
Block a user