From c8b75b5a749205d41cbde53a2fed02d267c8af74 Mon Sep 17 00:00:00 2001 From: kercylan <61743331+kercylan98@users.noreply.github.com> Date: Sun, 30 Apr 2023 00:51:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E7=89=A9=E5=93=81=E5=92=8C?= =?UTF-8?q?=E7=89=A9=E5=93=81=E5=AE=B9=E5=99=A8=EF=BC=8C=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E4=B8=8D=E5=90=88=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game/builtin/item.go | 48 ---------------------- game/builtin/item_container.go | 73 ---------------------------------- game/item.go | 17 -------- game/item_container.go | 21 ---------- 4 files changed, 159 deletions(-) delete mode 100644 game/builtin/item.go delete mode 100644 game/builtin/item_container.go delete mode 100644 game/item.go delete mode 100644 game/item_container.go diff --git a/game/builtin/item.go b/game/builtin/item.go deleted file mode 100644 index 2ee78b9..0000000 --- a/game/builtin/item.go +++ /dev/null @@ -1,48 +0,0 @@ -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_container.go b/game/builtin/item_container.go deleted file mode 100644 index 122a781..0000000 --- a/game/builtin/item_container.go +++ /dev/null @@ -1,73 +0,0 @@ -package builtin - -import ( - "minotaur/game" - "minotaur/utils/huge" - "minotaur/utils/synchronization" -) - -func NewItemContainer[ItemID comparable, I game.Item[ItemID]]() *ItemContainer[ItemID, I] { - return &ItemContainer[ItemID, I]{ - items: synchronization.NewMap[ItemID, *synchronization.Map[int64, I]](), - itemRef: synchronization.NewMap[int64, ItemID](), - } -} - -type ItemContainer[ItemID comparable, I game.Item[ItemID]] struct { - guid int64 - items *synchronization.Map[ItemID, *synchronization.Map[int64, I]] - itemRef *synchronization.Map[int64, ItemID] -} - -func (slf *ItemContainer[ItemID, I]) GetItem(guid int64) I { - id := slf.itemRef.Get(guid) - return slf.items.Get(id).Get(guid) -} - -func (slf *ItemContainer[ItemID, I]) GetItems() map[int64]I { - items := make(map[int64]I) - slf.items.Range(func(id ItemID, value *synchronization.Map[int64, I]) { - if value != nil { - value.Range(func(guid int64, value I) { - items[guid] = value - }) - } - }) - return items -} - -func (slf *ItemContainer[ItemID, I]) GetItemsWithId(id ItemID) map[int64]I { - return slf.items.Get(id).Map() -} - -func (slf *ItemContainer[ItemID, I]) AddItem(item I) error { - id := item.GetID() - items, exist := slf.items.GetExist(id) - if !exist { - items = synchronization.NewMap[int64, I]() - slf.items.Set(id, items) - } - slf.guid++ - items.Set(slf.guid, item) - slf.itemRef.Set(slf.guid, id) - return nil -} - -func (slf *ItemContainer[ItemID, I]) ChangeItemCount(guid int64, count *huge.Int) error { - item := slf.GetItem(guid) - item.ChangeStackCount(count) - return nil -} - -func (slf *ItemContainer[ItemID, I]) DeleteItem(guid int64) { - id := slf.GetItem(guid).GetID() - slf.items.Get(id).Delete(guid) - slf.itemRef.Delete(guid) -} - -func (slf *ItemContainer[ItemID, I]) DeleteItemsWithId(id ItemID) { - for guid := range slf.items.Get(id).Map() { - slf.itemRef.Delete(guid) - } - slf.items.Delete(id) -} diff --git a/game/item.go b/game/item.go deleted file mode 100644 index a1ff591..0000000 --- a/game/item.go +++ /dev/null @@ -1,17 +0,0 @@ -package game - -import "minotaur/utils/huge" - -// Item 物品接口定义 -type Item[ID comparable] interface { - // GetID 获取物品id - GetID() ID - // GetGuid 获取物品guid - GetGuid() int64 - // SetGuid 设置物品guid - SetGuid(guid int64) - // ChangeStackCount 改变物品堆叠数量,返回新数量 - ChangeStackCount(count *huge.Int) *huge.Int - // GetStackCount 获取物品堆叠数量 - GetStackCount() *huge.Int -} diff --git a/game/item_container.go b/game/item_container.go deleted file mode 100644 index 1039629..0000000 --- a/game/item_container.go +++ /dev/null @@ -1,21 +0,0 @@ -package game - -import "minotaur/utils/huge" - -// ItemContainer 物品容器 -type ItemContainer[ItemID comparable, I Item[ItemID]] interface { - // GetItem 根据guid获取物品 - GetItem(guid int64) I - // GetItems 获取容器中的所有物品 - GetItems() map[int64]I - // GetItemsWithId 根据id获取容器中所有物品 - GetItemsWithId(id ItemID) map[int64]I - // AddItem 添加物品到容器中 - AddItem(item I) error - // ChangeItemCount 改变容器中特定数量的物品(扣除时当数量不足时会尝试扣除相同ID的物品) - ChangeItemCount(guid int64, count *huge.Int) error - // DeleteItem 删除容器中的物品 - DeleteItem(guid int64) - // DeleteItemsWithId 删除容器中所有特定id的物品 - DeleteItemsWithId(id ItemID) -}