refactor(fight): 重构暴击判断逻辑并优化伤害计算,添加泛型Max函数到utils包

This commit is contained in:
1
2025-09-10 08:05:45 +00:00
parent f09b43fabd
commit a0441700e5
4 changed files with 46 additions and 23 deletions

View File

@@ -20,3 +20,18 @@ func ToSliceMap[T any, K comparable](slice []T, keyFunc func(T) K) map[K][]T {
}
return m
}
// 定义泛型约束:只允许数值类型(整数、浮点数等)
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
}