物品容器实现
This commit is contained in:
parent
c428ce6e56
commit
40b90ddd20
|
@ -0,0 +1,73 @@
|
||||||
|
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)
|
||||||
|
}
|
|
@ -1,5 +0,0 @@
|
||||||
package game
|
|
||||||
|
|
||||||
// Container 容器
|
|
||||||
type Container[T any] interface {
|
|
||||||
}
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
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)
|
||||||
|
}
|
Loading…
Reference in New Issue