物品实现

This commit is contained in:
kercylan 2023-04-29 20:45:05 +08:00
parent 27c4925b25
commit c428ce6e56
3 changed files with 63 additions and 2 deletions

48
game/builtin/item.go Normal file
View File

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

View File

@ -0,0 +1,5 @@
package builtin
var (
ErrItemCountException = "the number of items cannot be empty and must be a positive integer"
)

View File

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