物品实现
This commit is contained in:
parent
de339215bd
commit
fbcea9fba5
|
@ -0,0 +1,51 @@
|
|||
package builtin
|
||||
|
||||
import "github.com/kercylan98/minotaur/utils/huge"
|
||||
|
||||
func NewItem[ID comparable](id ID, options ...ItemOption[ID]) *Item[ID] {
|
||||
item := &Item[ID]{
|
||||
id: id,
|
||||
}
|
||||
for _, option := range options {
|
||||
option(item)
|
||||
}
|
||||
return item
|
||||
}
|
||||
|
||||
type Item[ID comparable] struct {
|
||||
id ID
|
||||
stackLimit *huge.Int
|
||||
count *huge.Int
|
||||
}
|
||||
|
||||
func (slf *Item[ID]) GetID() ID {
|
||||
return slf.id
|
||||
}
|
||||
|
||||
func (slf *Item[ID]) GetCount() *huge.Int {
|
||||
return slf.count
|
||||
}
|
||||
|
||||
func (slf *Item[ID]) GetStackLimit() *huge.Int {
|
||||
return slf.stackLimit
|
||||
}
|
||||
|
||||
func (slf *Item[ID]) SetCount(count *huge.Int) {
|
||||
if count.LessThan(huge.IntZero) {
|
||||
slf.count = huge.IntZero.Copy()
|
||||
return
|
||||
}
|
||||
slf.count = count.Copy()
|
||||
}
|
||||
|
||||
func (slf *Item[ID]) ChangeCount(count *huge.Int) error {
|
||||
newCount := slf.count.Copy().Add(count)
|
||||
if newCount.LessThan(huge.IntZero) {
|
||||
return ErrItemInsufficientQuantityDeduction
|
||||
}
|
||||
if newCount.GreaterThan(slf.stackLimit) {
|
||||
return ErrItemStackLimit
|
||||
}
|
||||
slf.count = newCount
|
||||
return nil
|
||||
}
|
|
@ -1,5 +1,8 @@
|
|||
package builtin
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrItemCountException = "the number of items cannot be empty and must be a positive integer"
|
||||
ErrItemInsufficientQuantityDeduction = errors.New("insufficient quantity deduction")
|
||||
ErrItemStackLimit = errors.New("the number of items reaches the stacking limit")
|
||||
)
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package builtin
|
||||
|
||||
import "github.com/kercylan98/minotaur/utils/huge"
|
||||
|
||||
type ItemOption[ID comparable] func(item *Item[ID])
|
||||
|
||||
// WithItemStackLimit 通过特定堆叠上限创建物品
|
||||
// - 默认无限制
|
||||
func WithItemStackLimit[ID comparable](stackLimit *huge.Int) ItemOption[ID] {
|
||||
return func(item *Item[ID]) {
|
||||
if stackLimit == nil || stackLimit.LessThanOrEqualTo(huge.IntZero) {
|
||||
return
|
||||
}
|
||||
item.stackLimit = stackLimit
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package game
|
||||
|
||||
import "github.com/kercylan98/minotaur/utils/huge"
|
||||
|
||||
// Item 物品
|
||||
type Item[ID comparable] interface {
|
||||
// GetID 获取物品id
|
||||
GetID() ID
|
||||
// GetCount 获取物品数量
|
||||
GetCount() *huge.Int
|
||||
// GetStackLimit 获取堆叠上限
|
||||
GetStackLimit() *huge.Int
|
||||
// SetCount 设置物品数量
|
||||
SetCount(count *huge.Int)
|
||||
// ChangeCount 改变物品数量
|
||||
ChangeCount(count *huge.Int) error
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package server
|
||||
|
||||
// Cross 跨服功能接口实现
|
||||
type Cross interface {
|
||||
// PushPacket 推送数据包
|
||||
PushPacket(serverId int64, packet []byte) error
|
||||
|
||||
//
|
||||
}
|
Loading…
Reference in New Issue