feat: components.Moving2D 支持停止移动事件注册

This commit is contained in:
kercylan98 2023-07-01 09:30:39 +08:00
parent 67616b2963
commit f67a66d2d0
3 changed files with 99 additions and 8 deletions

View File

@ -32,11 +32,11 @@ type Moving2D struct {
timeUnit float64 timeUnit float64
idle time.Duration idle time.Duration
interval time.Duration interval time.Duration
event chan func()
close bool close bool
position2DChangeEventHandles []component.Position2DChangeEventHandle position2DChangeEventHandles []component.Position2DChangeEventHandle
position2DDestinationEventHandles []component.Position2DDestinationEventHandle position2DDestinationEventHandles []component.Position2DDestinationEventHandle
position2DStopMoveEventHandles []component.Position2DStopMoveEventHandle
} }
// MoveTo 设置对象移动到特定位置 // MoveTo 设置对象移动到特定位置
@ -67,8 +67,12 @@ func (slf *Moving2D) MoveTo(entity component.Moving2DEntity, x float64, y float6
// StopMove 停止特定对象的移动 // StopMove 停止特定对象的移动
func (slf *Moving2D) StopMove(guid int64) { func (slf *Moving2D) StopMove(guid int64) {
slf.rw.Lock() slf.rw.Lock()
delete(slf.entities, guid) defer slf.rw.Unlock()
slf.rw.Unlock() entity, exist := slf.entities[guid]
if exist {
slf.OnPosition2DStopMoveEvent(entity)
delete(slf.entities, guid)
}
} }
// RegPosition2DChangeEvent 在对象位置改变时将执行注册的事件处理函数 // RegPosition2DChangeEvent 在对象位置改变时将执行注册的事件处理函数
@ -93,6 +97,17 @@ func (slf *Moving2D) OnPosition2DDestinationEvent(entity component.Moving2DEntit
} }
} }
// RegPosition2DStopMoveEvent 在对象停止移动时将执行被注册的事件处理函数
func (slf *Moving2D) RegPosition2DStopMoveEvent(handle component.Position2DStopMoveEventHandle) {
slf.position2DStopMoveEventHandles = append(slf.position2DStopMoveEventHandles, handle)
}
func (slf *Moving2D) OnPosition2DStopMoveEvent(entity component.Moving2DEntity) {
for _, handle := range slf.position2DStopMoveEventHandles {
handle(slf, entity)
}
}
type moving2DTarget struct { type moving2DTarget struct {
component.Moving2DEntity component.Moving2DEntity
x, y float64 x, y float64
@ -104,7 +119,6 @@ func (slf *Moving2D) Release() {
slf.rw.Lock() slf.rw.Lock()
defer slf.rw.Unlock() defer slf.rw.Unlock()
slf.close = true slf.close = true
close(slf.event)
} }
func (slf *Moving2D) handle() { func (slf *Moving2D) handle() {

View File

@ -19,4 +19,5 @@ type Moving2D interface {
type ( type (
Position2DChangeEventHandle func(moving Moving2D, entity Moving2DEntity, oldX, oldY float64) Position2DChangeEventHandle func(moving Moving2D, entity Moving2DEntity, oldX, oldY float64)
Position2DDestinationEventHandle func(moving Moving2D, entity Moving2DEntity) Position2DDestinationEventHandle func(moving Moving2D, entity Moving2DEntity)
Position2DStopMoveEventHandle func(moving Moving2D, entity Moving2DEntity)
) )

View File

@ -34,6 +34,81 @@ type Rule struct {
restraint map[string]map[string]struct{} restraint map[string]map[string]struct{}
} }
// GetCardCountWithPointMaximumNumber 获取指定点数的牌的数量
func (slf *Rule) GetCardCountWithPointMaximumNumber(cards []Card, point Point, maximumNumber int) int {
count := 0
for _, card := range cards {
if card.GetPoint() == point {
count++
if count >= maximumNumber {
return count
}
}
}
return count
}
// GetCardCountWithColorMaximumNumber 获取指定花色的牌的数量
func (slf *Rule) GetCardCountWithColorMaximumNumber(cards []Card, color Color, maximumNumber int) int {
count := 0
for _, card := range cards {
if card.GetColor() == color {
count++
if count >= maximumNumber {
return count
}
}
}
return count
}
// GetCardCountWithMaximumNumber 获取指定牌的数量
func (slf *Rule) GetCardCountWithMaximumNumber(cards []Card, card Card, maximumNumber int) int {
count := 0
for _, c := range cards {
if c.Equal(card) {
count++
if count >= maximumNumber {
return count
}
}
}
return count
}
// GetCardCountWithPoint 获取指定点数的牌的数量
func (slf *Rule) GetCardCountWithPoint(cards []Card, point Point) int {
count := 0
for _, card := range cards {
if card.GetPoint() == point {
count++
}
}
return count
}
// GetCardCountWithColor 获取指定花色的牌的数量
func (slf *Rule) GetCardCountWithColor(cards []Card, color Color) int {
count := 0
for _, card := range cards {
if card.GetColor() == color {
count++
}
}
return count
}
// GetCardCount 获取指定牌的数量
func (slf *Rule) GetCardCount(cards []Card, card Card) int {
count := 0
for _, c := range cards {
if c.Equal(card) {
count++
}
}
return count
}
// PokerHandIsMatch 检查两组扑克牌牌型是否匹配 // PokerHandIsMatch 检查两组扑克牌牌型是否匹配
func (slf *Rule) PokerHandIsMatch(cardsA, cardsB []Card) bool { func (slf *Rule) PokerHandIsMatch(cardsA, cardsB []Card) bool {
handA, exist := slf.PokerHand(cardsA...) handA, exist := slf.PokerHand(cardsA...)
@ -148,16 +223,17 @@ func (slf *Rule) SortByColorAsc(cards []Card) []Card {
} }
// GetValueWithPokerHand 获取扑克牌的牌型牌值 // GetValueWithPokerHand 获取扑克牌的牌型牌值
func (slf *Rule) GetValueWithPokerHand(hand string) int { func (slf *Rule) GetValueWithPokerHand(hand string, cards ...Card) int {
return slf.pokerHandValue[hand] hv := slf.pokerHandValue[hand]
return hv * slf.GetValueWithCards(cards...)
} }
// GetValueWithCards 获取扑克牌的牌值 // GetValueWithCards 获取扑克牌的牌值
func (slf *Rule) GetValueWithCards(cards ...Card) int { func (slf *Rule) GetValueWithCards(cards ...Card) int {
var value int var value int
for _, card := range cards { for _, card := range cards {
value += slf.pointValue[card.GetPoint()] value += slf.GetValueWithPoint(card.GetPoint())
value += slf.colorValue[card.GetColor()] value += slf.GetValueWithColor(card.GetColor())
} }
return value return value
} }