From c428ce6e56feb065aefeb55362ba6b1ec2f113a1 Mon Sep 17 00:00:00 2001 From: kercylan <61743331+kercylan98@users.noreply.github.com> Date: Sat, 29 Apr 2023 20:45:05 +0800 Subject: [PATCH] =?UTF-8?q?=E7=89=A9=E5=93=81=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game/builtin/item.go | 48 +++++++++++++++++++++++++++++++++++++ game/builtin/item_errors.go | 5 ++++ game/item.go | 12 ++++++++-- 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 game/builtin/item.go create mode 100644 game/builtin/item_errors.go 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 }