Documentation ¶
Overview ¶
Package components 通用组件的内置实现
- lockstep.go 帧同步组件
- lockstep_options.go 帧同步组件可选项
Index ¶
- type Lockstep
- func (slf *Lockstep[ClientID, Command]) AddCommand(command Command)
- func (slf *Lockstep[ClientID, Command]) GetClientCurrentFrame(clientId ClientID) int
- func (slf *Lockstep[ClientID, Command]) GetCurrentFrame() int
- func (slf *Lockstep[ClientID, Command]) GetFrameLimit() int
- func (slf *Lockstep[ClientID, Command]) GetFrames() [][]Command
- func (slf *Lockstep[ClientID, Command]) JoinClient(client component.LockstepClient[ClientID])
- func (slf *Lockstep[ClientID, Command]) JoinClientWithFrame(client component.LockstepClient[ClientID], frameIndex int)
- func (slf *Lockstep[ClientID, Command]) LeaveClient(clientId ClientID)
- func (slf *Lockstep[ClientID, Command]) OnLockstepStoppedEvent()
- func (slf *Lockstep[ClientID, Command]) RegLockstepStoppedEvent(handle component.LockstepStoppedEventHandle[ClientID, Command])
- func (slf *Lockstep[ClientID, Command]) StartBroadcast()
- func (slf *Lockstep[ClientID, Command]) StopBroadcast()
- type LockstepOption
- func WithLockstepFrameLimit[ClientID comparable, Command any](frameLimit int) LockstepOption[ClientID, Command]
- func WithLockstepFrameRate[ClientID comparable, Command any](frameRate int) LockstepOption[ClientID, Command]
- func WithLockstepSerialization[ClientID comparable, Command any](handle func(frame int, commands []Command) []byte) LockstepOption[ClientID, Command]
- type Moving2D
- func (slf *Moving2D) MoveTo(entity component.Moving2DEntity, x float64, y float64)
- func (slf *Moving2D) OnPosition2DChangeEvent(entity component.Moving2DEntity, oldX, oldY float64)
- func (slf *Moving2D) OnPosition2DDestinationEvent(entity component.Moving2DEntity)
- func (slf *Moving2D) OnPosition2DStopMoveEvent(entity component.Moving2DEntity)
- func (slf *Moving2D) RegPosition2DChangeEvent(handle component.Position2DChangeEventHandle)
- func (slf *Moving2D) RegPosition2DDestinationEvent(handle component.Position2DDestinationEventHandle)
- func (slf *Moving2D) RegPosition2DStopMoveEvent(handle component.Position2DStopMoveEventHandle)
- func (slf *Moving2D) Release()
- func (slf *Moving2D) StopMove(guid int64)
- type Moving2DOption
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Lockstep ¶
type Lockstep[ClientID comparable, Command any] struct { // contains filtered or unexported fields }
Lockstep 锁步(帧)同步默认实现
- 支持最大帧上限 WithLockstepFrameLimit
- 自定逻辑帧频率,默认为每秒15帧(帧/66ms) WithLockstepFrameRate
- 自定帧序列化方式 WithLockstepSerialization
- 从特定帧开始追帧
- 兼容各种基于TCP/UDP/Unix的网络类型,可通过客户端实现其他网络类型同步
可在 examples 目录下找到示例,示例项目:simple-server-lockstep
func NewLockstep ¶
func NewLockstep[ClientID comparable, Command any](options ...LockstepOption[ClientID, Command]) *Lockstep[ClientID, Command]
NewLockstep 创建一个锁步(帧)同步默认实现的组件(Lockstep)进行返回
func (*Lockstep[ClientID, Command]) AddCommand ¶
func (slf *Lockstep[ClientID, Command]) AddCommand(command Command)
AddCommand 添加命令到当前帧
func (*Lockstep[ClientID, Command]) GetClientCurrentFrame ¶
GetClientCurrentFrame 获取客户端当前帧
func (*Lockstep[ClientID, Command]) GetCurrentFrame ¶
GetCurrentFrame 获取当前帧
func (*Lockstep[ClientID, Command]) GetFrames ¶
func (slf *Lockstep[ClientID, Command]) GetFrames() [][]Command
GetFrames 获取所有帧数据
func (*Lockstep[ClientID, Command]) JoinClient ¶
func (slf *Lockstep[ClientID, Command]) JoinClient(client component.LockstepClient[ClientID])
JoinClient 加入客户端到广播队列中
func (*Lockstep[ClientID, Command]) JoinClientWithFrame ¶
func (slf *Lockstep[ClientID, Command]) JoinClientWithFrame(client component.LockstepClient[ClientID], frameIndex int)
JoinClientWithFrame 加入客户端到广播队列中,并从特定帧开始追帧
- 可用于重连及状态同步、帧同步混用的情况
- 混用:服务端记录指令时同时做一次状态计算,新客户端加入时直接同步当前状态,之后从特定帧开始广播
func (*Lockstep[ClientID, Command]) LeaveClient ¶
func (slf *Lockstep[ClientID, Command]) LeaveClient(clientId ClientID)
LeaveClient 将客户端从广播队列中移除
func (*Lockstep[ClientID, Command]) OnLockstepStoppedEvent ¶
func (slf *Lockstep[ClientID, Command]) OnLockstepStoppedEvent()
func (*Lockstep[ClientID, Command]) RegLockstepStoppedEvent ¶
func (slf *Lockstep[ClientID, Command]) RegLockstepStoppedEvent(handle component.LockstepStoppedEventHandle[ClientID, Command])
RegLockstepStoppedEvent 当广播停止时将触发被注册的事件处理函数
func (*Lockstep[ClientID, Command]) StartBroadcast ¶
func (slf *Lockstep[ClientID, Command]) StartBroadcast()
StartBroadcast 开始广播
- 在开始广播后将持续按照设定的帧率进行帧数推进,并在每一帧推进时向客户端进行同步,需提前将客户端加入广播队列 JoinClient
- 广播过程中使用 AddCommand 将该帧数据追加到当前帧中
func (*Lockstep[ClientID, Command]) StopBroadcast ¶
func (slf *Lockstep[ClientID, Command]) StopBroadcast()
StopBroadcast 停止广播
type LockstepOption ¶
type LockstepOption[ClientID comparable, Command any] func(lockstep *Lockstep[ClientID, Command])
func WithLockstepFrameLimit ¶
func WithLockstepFrameLimit[ClientID comparable, Command any](frameLimit int) LockstepOption[ClientID, Command]
WithLockstepFrameLimit 通过特定逻辑帧上限创建锁步(帧)同步组件
- 当达到上限时将停止广播
func WithLockstepFrameRate ¶
func WithLockstepFrameRate[ClientID comparable, Command any](frameRate int) LockstepOption[ClientID, Command]
WithLockstepFrameRate 通过特定逻辑帧率创建锁步(帧)同步组件
- 默认情况下为 15/s
func WithLockstepSerialization ¶
func WithLockstepSerialization[ClientID comparable, Command any](handle func(frame int, commands []Command) []byte) LockstepOption[ClientID, Command]
WithLockstepSerialization 通过特定的序列化方式将每一帧的数据进行序列化
默认情况下为将被序列化为以下结构体的JSON字符串
type Frame struct { Frame int `json:"frame"` Commands []Command `json:"commands"` }
type Moving2D ¶
type Moving2D struct {
// contains filtered or unexported fields
}
Moving2D 用于2D对象移动的数据结构
- 通过对象调用 MoveTo 方法后将开始执行该对象的移动
- 移动将在根据设置的每次移动间隔时间(WithMoving2DInterval)进行移动,当无对象移动需要移动时将会进入短暂的休眠
- 当对象移动速度永久为0时,将会导致永久无法完成的移动
func NewMoving2D ¶
func NewMoving2D(options ...Moving2DOption) *Moving2D
NewMoving2D 创建一个用于2D对象移动的实例(Moving2D)
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/component/components" ) func main() { moving := components.NewMoving2D() defer func() { moving.Release() }() fmt.Println(moving != nil) }
Output: true
func (*Moving2D) MoveTo ¶
func (slf *Moving2D) MoveTo(entity component.Moving2DEntity, x float64, y float64)
MoveTo 设置对象移动到特定位置
Example ¶
moving := components.NewMoving2D(components.WithMoving2DTimeUnit(time.Second)) defer func() { moving.Release() }() var wait sync.WaitGroup moving.RegPosition2DDestinationEvent(func(moving component.Moving2D, entity component.Moving2DEntity) { fmt.Println("done") wait.Done() }) wait.Add(1) entity := NewEntity(1, 100) moving.MoveTo(entity, 50, 30) wait.Wait()
Output: done
func (*Moving2D) OnPosition2DChangeEvent ¶
func (slf *Moving2D) OnPosition2DChangeEvent(entity component.Moving2DEntity, oldX, oldY float64)
func (*Moving2D) OnPosition2DDestinationEvent ¶
func (slf *Moving2D) OnPosition2DDestinationEvent(entity component.Moving2DEntity)
func (*Moving2D) OnPosition2DStopMoveEvent ¶ added in v0.0.4
func (slf *Moving2D) OnPosition2DStopMoveEvent(entity component.Moving2DEntity)
func (*Moving2D) RegPosition2DChangeEvent ¶
func (slf *Moving2D) RegPosition2DChangeEvent(handle component.Position2DChangeEventHandle)
RegPosition2DChangeEvent 在对象位置改变时将执行注册的事件处理函数
func (*Moving2D) RegPosition2DDestinationEvent ¶
func (slf *Moving2D) RegPosition2DDestinationEvent(handle component.Position2DDestinationEventHandle)
RegPosition2DDestinationEvent 在对象到达终点时将执行被注册的事件处理函数
func (*Moving2D) RegPosition2DStopMoveEvent ¶ added in v0.0.4
func (slf *Moving2D) RegPosition2DStopMoveEvent(handle component.Position2DStopMoveEventHandle)
RegPosition2DStopMoveEvent 在对象停止移动时将执行被注册的事件处理函数
func (*Moving2D) StopMove ¶
StopMove 停止特定对象的移动
Example ¶
moving := components.NewMoving2D(components.WithMoving2DTimeUnit(time.Second)) defer func() { moving.Release() }() var wait sync.WaitGroup moving.RegPosition2DChangeEvent(func(moving component.Moving2D, entity component.Moving2DEntity, oldX, oldY float64) { fmt.Println("move") }) moving.RegPosition2DStopMoveEvent(func(moving component.Moving2D, entity component.Moving2DEntity) { fmt.Println("stop") wait.Done() }) moving.RegPosition2DDestinationEvent(func(moving component.Moving2D, entity component.Moving2DEntity) { fmt.Println("done") wait.Done() }) wait.Add(1) entity := NewEntity(1, 100) moving.MoveTo(entity, 50, 300) moving.StopMove(1) wait.Wait()
Output: stop
type Moving2DOption ¶
type Moving2DOption func(moving *Moving2D)
func WithMoving2DIdleWaitTime ¶
func WithMoving2DIdleWaitTime(duration time.Duration) Moving2DOption
WithMoving2DIdleWaitTime 通过特定的空闲等待时间创建
- 默认情况下在没有新的移动计划时将限制 100毫秒 + 移动间隔事件(默认100毫秒)
func WithMoving2DInterval ¶
func WithMoving2DInterval(duration time.Duration) Moving2DOption
WithMoving2DInterval 通过特定的移动间隔时间创建
func WithMoving2DTimeUnit ¶
func WithMoving2DTimeUnit(duration time.Duration) Moving2DOption
WithMoving2DTimeUnit 通过特定时间单位创建
- 默认单位为1毫秒,最小单位也为1毫秒