删除物品和物品容器,设计不合理

This commit is contained in:
kercylan 2023-04-30 00:51:40 +08:00
parent 40b90ddd20
commit c8b75b5a74
4 changed files with 0 additions and 159 deletions

View File

@ -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()
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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)
}