diff --git a/game/builtin/item.go b/game/builtin/item.go new file mode 100644 index 0000000..2ee78b9 --- /dev/null +++ b/game/builtin/item.go @@ -0,0 +1,48 @@ +package builtin + +import ( + "minotaur/utils/huge" +) + +func NewItem[ID comparable](id ID, count *huge.Int) *Item[ID] { + if count == nil || count.LessThan(huge.IntZero) { + panic(ErrItemCountException) + } + return &Item[ID]{ + id: id, + count: count, + } +} + +type Item[ID comparable] struct { + id ID + guid int64 + count *huge.Int +} + +func (slf *Item[ID]) GetID() ID { + return slf.id +} + +func (slf *Item[ID]) GetGuid() int64 { + return slf.guid +} + +func (slf *Item[ID]) SetGuid(guid int64) { + slf.guid = guid +} + +func (slf *Item[ID]) ChangeStackCount(count *huge.Int) *huge.Int { + if count.IsZero() { + return slf.count.Copy() + } + newCount := slf.count.Add(count) + if newCount.LessThan(huge.IntZero) { + slf.count = newCount.Set(huge.IntZero) + } + return slf.count +} + +func (slf *Item[ID]) GetStackCount() *huge.Int { + return slf.count.Copy() +} diff --git a/game/builtin/item_errors.go b/game/builtin/item_errors.go new file mode 100644 index 0000000..e036a3d --- /dev/null +++ b/game/builtin/item_errors.go @@ -0,0 +1,5 @@ +package builtin + +var ( + ErrItemCountException = "the number of items cannot be empty and must be a positive integer" +) diff --git a/game/item.go b/game/item.go index 9e9dfe6..a1ff591 100644 --- a/game/item.go +++ b/game/item.go @@ -1,9 +1,17 @@ package game +import "minotaur/utils/huge" + // Item 物品接口定义 type Item[ID comparable] interface { // GetID 获取物品id GetID() ID - // GetCount 获取物品数量 - GetCount() int + // GetGuid 获取物品guid + GetGuid() int64 + // SetGuid 设置物品guid + SetGuid(guid int64) + // ChangeStackCount 改变物品堆叠数量,返回新数量 + ChangeStackCount(count *huge.Int) *huge.Int + // GetStackCount 获取物品堆叠数量 + GetStackCount() *huge.Int }