Files
bl/logic/service/fight/input/id.go

125 lines
4.7 KiB
Go
Raw Normal View History

package input
import "fmt"
// -------------------------- 位运算常量(统一管理) --------------------------
const (
// 位移偏移量64位ID结构 = [高16位:效果类型][中32位:CatchTime][低16位:扩展标识]
prefixOffset = 48 // 高16位uint16效果类型
catchOffset = 16 // 中32位uint32CatchTime核心调整点
suffixOffset = 0 // 低16位uint16扩展标识/子ID
// 掩码(提取对应位段)
suffixMask = 0xFFFF // 低16位掩码
catchMask = 0xFFFFFFFF // 中32位掩码CatchTime专属
prefixMask = 0xFFFF // 高16位掩码
maxPrefix = prefixMask // 效果类型最大值uint16范围
maxCatch = catchMask // CatchTime最大值uint32范围
maxSuffix = suffixMask // 扩展标识最大值uint16范围
)
// -------------------------- 64位拼接工具无状态工具类 --------------------------
// EffectIDCombiner 64位效果ID拼接器[效果类型(uint16)+CatchTime(uint32)+扩展标识(uint16)] → int64
// 无状态设计,可全局复用
type EffectIDCombiner struct {
Base int64
}
// Combine 拼接三个字段为64位效果ID
// 参数:
// prefix: 高16位效果类型如EffectType.Skill
// catchTime: 中32位核心字段32位CatchTime
// suffix: 低16位扩展标识/子ID
// 返回拼接后的int64参数超出范围返回错误
func (c *EffectIDCombiner) Combine(prefix EnumEffectType, catchTime uint32, suffix uint16) {
// // 范围校验(避免溢出)
// if prefix > maxPrefix {
// return 0, fmt.Errorf("效果类型值%d超出uint16范围最大%d", prefix, maxPrefix)
// }
// if catchTime > maxCatch {
// return 0, fmt.Errorf("CatchTime值%d超出uint32范围最大%d", catchTime, maxCatch)
// }
// if suffix > maxSuffix {
// return 0, fmt.Errorf("扩展标识值%d超出uint16范围最大%d", suffix, maxSuffix)
// }
c.Base = (int64(prefix) << prefixOffset) |
(int64(catchTime) << catchOffset) |
int64(suffix)
}
// EffectID 获取完整的64位效果ID
func (e *EffectIDCombiner) EffectID() int64 {
return e.Base
}
// SetEffectType 设置效果类型(修复掩码偏移问题)
// 参数t必须传入有效值否则panic也可加len(t)==0判断
func (e *EffectIDCombiner) SetEffectType(t EnumEffectType) {
// 修正掩码左移到高16位仅清空效果类型区域
clearMask := uint64(prefixMask) << prefixOffset
e.Base = (e.Base & ^int64(clearMask)) | (int64(t) << prefixOffset)
}
// GetEffectType 读取效果类型
func (e EffectIDCombiner) GetEffectType() EnumEffectType {
return EnumEffectType((e.Base >> prefixOffset) & int64(prefixMask))
}
// SetCatchTime 设置CatchTime修复掩码偏移问题避免清空Suffix
func (e *EffectIDCombiner) SetCatchTime(t uint32) {
// 修正掩码左移到中32位仅清空CatchTime区域
clearMask := uint64(catchMask) << catchOffset
e.Base = (e.Base & ^int64(clearMask)) | (int64(t) << catchOffset)
}
func (e EffectIDCombiner) GetCatchTime() uint32 {
return uint32((e.Base >> catchOffset) & catchMask)
}
// Suffix 读取/替换扩展标识读写一体和CatchTime写法一致
// 参数t可选传入则替换当前扩展标识不传则仅读取
// 返回:当前的扩展标识值
func (e EffectIDCombiner) Suffix() uint16 {
// if len(t) > 0 {
// // 替换逻辑先清空低16位再设置新值
// e.Base = (e.Base & ^suffixMask) | int64(t[0])
// }
// 读取并返回当前扩展标识
return uint16(e.Base & suffixMask)
}
// -------------------------- 自由取值方法(核心需求:按需取不同类型) --------------------------
func Test() {
combiner := &EffectIDCombiner{}
// 1. 初始化拼接ID
combiner.Combine(EffectType.Skill, 123456, 789)
fmt.Println("=== 初始值 ===")
fmt.Printf("完整ID%d\n", combiner.EffectID())
fmt.Printf("效果类型:%d\n", combiner.GetEffectType()) // 仅读取
fmt.Printf("CatchTime%d\n", combiner.GetCatchTime()) // 仅读取
fmt.Printf("扩展标识:%d\n", combiner.Suffix()) // 仅读取
// 2. 替换字段(传参即替换)
combiner.SetEffectType(EffectType.NewSel) // 替换效果类型为Buff
combiner.SetCatchTime(987654) // 替换CatchTime
// 替换扩展标识
fmt.Println("\n=== 替换后 ===")
fmt.Printf("完整ID%d\n", combiner.EffectID())
fmt.Printf("效果类型:%d\n", combiner.GetEffectType())
fmt.Printf("CatchTime%d\n", combiner.GetCatchTime())
fmt.Printf("扩展标识:%d\n", combiner.Suffix())
// 3. 链式替换+读取(一行完成替换并获取新值)
combiner.SetCatchTime(111222) // 替换并返回新值
fmt.Printf("\n替换CatchTime并读取%d\n", combiner.GetCatchTime())
}