moving

package
v0.5.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 24, 2024 License: MIT Imports: 5 Imported by: 0

README

Moving

Go doc

暂无介绍...

目录导航

列出了该 package 下所有的函数及类型定义,可通过目录导航进行快捷跳转 ❤️

展开 / 折叠目录导航

包级函数定义

函数名称 描述
NewTwoDimensional 创建一个用于2D对象移动的实例(TwoDimensional)
WithTwoDimensionalTimeUnit 通过特定时间单位创建
WithTwoDimensionalIdleWaitTime 通过特定的空闲等待时间创建
WithTwoDimensionalInterval 通过特定的移动间隔时间创建
NewEntity 暂无描述...

类型定义

类型 名称 描述
STRUCT TwoDimensional 用于2D对象移动的数据结构
INTERFACE TwoDimensionalEntity 2D移动对象接口定义
STRUCT Position2DChangeEventHandle 暂无描述...
STRUCT TwoDimensionalOption 暂无描述...

详情信息

func NewTwoDimensional[EID generic.Basic, PosType generic.SignedNumber](options ...TwoDimensionalOption[EID, PosType]) *TwoDimensional[EID, PosType]

创建一个用于2D对象移动的实例(TwoDimensional)

示例代码:


func ExampleNewTwoDimensional() {
	m := moving2.NewTwoDimensional[int64, float64]()
	defer func() {
		m.Release()
	}()
	fmt.Println(m != nil)
}

查看 / 收起单元测试

func TestNewTwoDimensional(t *testing.T) {
	m := moving2.NewTwoDimensional[int64, float64]()
	defer func() {
		m.Release()
	}()
}


func WithTwoDimensionalTimeUnit[EID generic.Basic, PosType generic.SignedNumber](duration time.Duration) TwoDimensionalOption[EID, PosType]

通过特定时间单位创建

  • 默认单位为1毫秒,最小单位也为1毫秒

func WithTwoDimensionalIdleWaitTime[EID generic.Basic, PosType generic.SignedNumber](duration time.Duration) TwoDimensionalOption[EID, PosType]

通过特定的空闲等待时间创建

  • 默认情况下在没有新的移动计划时将限制 100毫秒 + 移动间隔事件(默认100毫秒)

func WithTwoDimensionalInterval[EID generic.Basic, PosType generic.SignedNumber](duration time.Duration) TwoDimensionalOption[EID, PosType]

通过特定的移动间隔时间创建


func NewEntity(guid int64, speed float64) *MoveEntity


TwoDimensional STRUCT

用于2D对象移动的数据结构

  • 通过对象调用 MoveTo 方法后将开始执行该对象的移动
  • 移动将在根据设置的每次移动间隔时间(WithTwoDimensionalInterval)进行移动,当无对象移动需要移动时将会进入短暂的休眠
  • 当对象移动速度永久为0时,将会导致永久无法完成的移动
type TwoDimensional[EID generic.Basic, PosType generic.SignedNumber] struct {
	rw                                sync.RWMutex
	entities                          map[EID]*moving2DTarget[EID, PosType]
	timeUnit                          float64
	idle                              time.Duration
	interval                          time.Duration
	close                             bool
	position2DChangeEventHandles      []Position2DChangeEventHandle[EID, PosType]
	position2DDestinationEventHandles []Position2DDestinationEventHandle[EID, PosType]
	position2DStopMoveEventHandles    []Position2DStopMoveEventHandle[EID, PosType]
}

func (*TwoDimensional) MoveTo(entity TwoDimensionalEntity[EID, PosType], x PosType, y PosType)

设置对象移动到特定位置

示例代码:


func ExampleTwoDimensional_MoveTo() {
	m := moving2.NewTwoDimensional(moving2.WithTwoDimensionalTimeUnit[int64, float64](time.Second))
	defer func() {
		m.Release()
	}()
	var wait sync.WaitGroup
	m.RegPosition2DDestinationEvent(func(moving *moving2.TwoDimensional[int64, float64], entity moving2.TwoDimensionalEntity[int64, float64]) {
		fmt.Println("done")
		wait.Done()
	})
	wait.Add(1)
	entity := NewEntity(1, 100)
	m.MoveTo(entity, 50, 30)
	wait.Wait()
}


func (*TwoDimensional) StopMove(id EID)

停止特定对象的移动

示例代码:


func ExampleTwoDimensional_StopMove() {
	m := moving2.NewTwoDimensional(moving2.WithTwoDimensionalTimeUnit[int64, float64](time.Second))
	defer func() {
		m.Release()
	}()
	var wait sync.WaitGroup
	m.RegPosition2DChangeEvent(func(moving *moving2.TwoDimensional[int64, float64], entity moving2.TwoDimensionalEntity[int64, float64], oldX, oldY float64) {
		fmt.Println("move")
	})
	m.RegPosition2DStopMoveEvent(func(moving *moving2.TwoDimensional[int64, float64], entity moving2.TwoDimensionalEntity[int64, float64]) {
		fmt.Println("stop")
		wait.Done()
	})
	m.RegPosition2DDestinationEvent(func(moving *moving2.TwoDimensional[int64, float64], entity moving2.TwoDimensionalEntity[int64, float64]) {
		fmt.Println("done")
		wait.Done()
	})
	wait.Add(1)
	entity := NewEntity(1, 100)
	m.MoveTo(entity, 50, 300)
	m.StopMove(1)
	wait.Wait()
}

查看 / 收起单元测试

func TestTwoDimensional_StopMove(t *testing.T) {
	var wait sync.WaitGroup
	m := moving2.NewTwoDimensional(moving2.WithTwoDimensionalTimeUnit[int64, float64](time.Second))
	defer func() {
		m.Release()
	}()
	m.RegPosition2DChangeEvent(func(moving *moving2.TwoDimensional[int64, float64], entity moving2.TwoDimensionalEntity[int64, float64], oldX, oldY float64) {
		x, y := entity.GetPosition().GetXY()
		fmt.Println(fmt.Sprintf("%d : %d | %f, %f > %f, %f", entity.GetTwoDimensionalEntityID(), time.Now().UnixMilli(), oldX, oldY, x, y))
	})
	m.RegPosition2DDestinationEvent(func(moving *moving2.TwoDimensional[int64, float64], entity moving2.TwoDimensionalEntity[int64, float64]) {
		fmt.Println(fmt.Sprintf("%d : %d | destination", entity.GetTwoDimensionalEntityID(), time.Now().UnixMilli()))
		wait.Done()
	})
	m.RegPosition2DStopMoveEvent(func(moving *moving2.TwoDimensional[int64, float64], entity moving2.TwoDimensionalEntity[int64, float64]) {
		fmt.Println(fmt.Sprintf("%d : %d | stop", entity.GetTwoDimensionalEntityID(), time.Now().UnixMilli()))
		wait.Done()
	})
	for i := 0; i < 10; i++ {
		wait.Add(1)
		entity := NewEntity(int64(i)+1, float64(10+i))
		m.MoveTo(entity, 50, 30)
	}
	time.Sleep(time.Second * 1)
	for i := 0; i < 10; i++ {
		m.StopMove(int64(i) + 1)
	}
	wait.Wait()
}


func (*TwoDimensional) RegPosition2DChangeEvent(handle Position2DChangeEventHandle[EID, PosType])

在对象位置改变时将执行注册的事件处理函数


func (*TwoDimensional) OnPosition2DChangeEvent(entity TwoDimensionalEntity[EID, PosType], oldX PosType, oldY PosType)

func (*TwoDimensional) RegPosition2DDestinationEvent(handle Position2DDestinationEventHandle[EID, PosType])

在对象到达终点时将执行被注册的事件处理函数


func (*TwoDimensional) OnPosition2DDestinationEvent(entity TwoDimensionalEntity[EID, PosType])

func (*TwoDimensional) RegPosition2DStopMoveEvent(handle Position2DStopMoveEventHandle[EID, PosType])

在对象停止移动时将执行被注册的事件处理函数


func (*TwoDimensional) OnPosition2DStopMoveEvent(entity TwoDimensionalEntity[EID, PosType])

func (*TwoDimensional) Release()

释放对象移动对象所占用的资源


TwoDimensionalEntity INTERFACE

2D移动对象接口定义

type TwoDimensionalEntity[EID generic.Basic, PosType generic.SignedNumber] interface {
	GetTwoDimensionalEntityID() EID
	GetSpeed() float64
	GetPosition() geometry.Point[PosType]
	SetPosition(geometry.Point[PosType])
}

Position2DChangeEventHandle STRUCT
type Position2DChangeEventHandle[EID generic.Basic, PosType generic.SignedNumber] func(moving *TwoDimensional[EID, PosType], entity TwoDimensionalEntity[EID, PosType], oldX PosType)

TwoDimensionalOption STRUCT
type TwoDimensionalOption[EID generic.Basic, PosType generic.SignedNumber] func(moving *TwoDimensional[EID, PosType])

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Position2DChangeEventHandle

type Position2DChangeEventHandle[EID generic.Basic, PosType generic.SignedNumber] func(moving *TwoDimensional[EID, PosType], entity TwoDimensionalEntity[EID, PosType], oldX, oldY PosType)

type Position2DDestinationEventHandle

type Position2DDestinationEventHandle[EID generic.Basic, PosType generic.SignedNumber] func(moving *TwoDimensional[EID, PosType], entity TwoDimensionalEntity[EID, PosType])

type Position2DStopMoveEventHandle

type Position2DStopMoveEventHandle[EID generic.Basic, PosType generic.SignedNumber] func(moving *TwoDimensional[EID, PosType], entity TwoDimensionalEntity[EID, PosType])

type TwoDimensional

type TwoDimensional[EID generic.Basic, PosType generic.SignedNumber] struct {
	// contains filtered or unexported fields
}

TwoDimensional 用于2D对象移动的数据结构

  • 通过对象调用 MoveTo 方法后将开始执行该对象的移动
  • 移动将在根据设置的每次移动间隔时间(WithTwoDimensionalInterval)进行移动,当无对象移动需要移动时将会进入短暂的休眠
  • 当对象移动速度永久为0时,将会导致永久无法完成的移动

func NewTwoDimensional

func NewTwoDimensional[EID generic.Basic, PosType generic.SignedNumber](options ...TwoDimensionalOption[EID, PosType]) *TwoDimensional[EID, PosType]

NewTwoDimensional 创建一个用于2D对象移动的实例(TwoDimensional)

Example
package main

import (
	"fmt"
	moving2 "github.com/kercylan98/minotaur/utils/moving"
)

func main() {
	m := moving2.NewTwoDimensional[int64, float64]()
	defer func() {
		m.Release()
	}()
	fmt.Println(m != nil)

}
Output:

true

func (*TwoDimensional[EID, PosType]) MoveTo

func (slf *TwoDimensional[EID, PosType]) MoveTo(entity TwoDimensionalEntity[EID, PosType], x, y PosType)

MoveTo 设置对象移动到特定位置

Example
m := moving2.NewTwoDimensional(moving2.WithTwoDimensionalTimeUnit[int64, float64](time.Second))
defer func() {
	m.Release()
}()

var wait sync.WaitGroup
m.RegPosition2DDestinationEvent(func(moving *moving2.TwoDimensional[int64, float64], entity moving2.TwoDimensionalEntity[int64, float64]) {
	fmt.Println("done")
	wait.Done()
})

wait.Add(1)
entity := NewEntity(1, 100)
m.MoveTo(entity, 50, 30)

wait.Wait()
Output:

done

func (*TwoDimensional[EID, PosType]) OnPosition2DChangeEvent

func (slf *TwoDimensional[EID, PosType]) OnPosition2DChangeEvent(entity TwoDimensionalEntity[EID, PosType], oldX, oldY PosType)

func (*TwoDimensional[EID, PosType]) OnPosition2DDestinationEvent

func (slf *TwoDimensional[EID, PosType]) OnPosition2DDestinationEvent(entity TwoDimensionalEntity[EID, PosType])

func (*TwoDimensional[EID, PosType]) OnPosition2DStopMoveEvent

func (slf *TwoDimensional[EID, PosType]) OnPosition2DStopMoveEvent(entity TwoDimensionalEntity[EID, PosType])

func (*TwoDimensional[EID, PosType]) RegPosition2DChangeEvent

func (slf *TwoDimensional[EID, PosType]) RegPosition2DChangeEvent(handle Position2DChangeEventHandle[EID, PosType])

RegPosition2DChangeEvent 在对象位置改变时将执行注册的事件处理函数

func (*TwoDimensional[EID, PosType]) RegPosition2DDestinationEvent

func (slf *TwoDimensional[EID, PosType]) RegPosition2DDestinationEvent(handle Position2DDestinationEventHandle[EID, PosType])

RegPosition2DDestinationEvent 在对象到达终点时将执行被注册的事件处理函数

func (*TwoDimensional[EID, PosType]) RegPosition2DStopMoveEvent

func (slf *TwoDimensional[EID, PosType]) RegPosition2DStopMoveEvent(handle Position2DStopMoveEventHandle[EID, PosType])

RegPosition2DStopMoveEvent 在对象停止移动时将执行被注册的事件处理函数

func (*TwoDimensional[EID, PosType]) Release

func (slf *TwoDimensional[EID, PosType]) Release()

Release 释放对象移动对象所占用的资源

func (*TwoDimensional[EID, PosType]) StopMove

func (slf *TwoDimensional[EID, PosType]) StopMove(id EID)

StopMove 停止特定对象的移动

Example
m := moving2.NewTwoDimensional(moving2.WithTwoDimensionalTimeUnit[int64, float64](time.Second))
defer func() {
	m.Release()
}()

var wait sync.WaitGroup
m.RegPosition2DChangeEvent(func(moving *moving2.TwoDimensional[int64, float64], entity moving2.TwoDimensionalEntity[int64, float64], oldX, oldY float64) {
	fmt.Println("move")
})
m.RegPosition2DStopMoveEvent(func(moving *moving2.TwoDimensional[int64, float64], entity moving2.TwoDimensionalEntity[int64, float64]) {
	fmt.Println("stop")
	wait.Done()
})
m.RegPosition2DDestinationEvent(func(moving *moving2.TwoDimensional[int64, float64], entity moving2.TwoDimensionalEntity[int64, float64]) {
	fmt.Println("done")
	wait.Done()
})

wait.Add(1)
entity := NewEntity(1, 100)
m.MoveTo(entity, 50, 300)
m.StopMove(1)

wait.Wait()
Output:

stop

type TwoDimensionalEntity

type TwoDimensionalEntity[EID generic.Basic, PosType generic.SignedNumber] interface {
	// GetTwoDimensionalEntityID 获取 Moving 对象 ID
	GetTwoDimensionalEntityID() EID
	// GetSpeed 获取移动速度
	GetSpeed() float64
	// GetPosition 获取位置
	GetPosition() geometry.Point[PosType]
	// SetPosition 设置位置
	SetPosition(geometry.Point[PosType])
}

TwoDimensionalEntity 2D移动对象接口定义

type TwoDimensionalOption

type TwoDimensionalOption[EID generic.Basic, PosType generic.SignedNumber] func(moving *TwoDimensional[EID, PosType])

func WithTwoDimensionalIdleWaitTime

func WithTwoDimensionalIdleWaitTime[EID generic.Basic, PosType generic.SignedNumber](duration time.Duration) TwoDimensionalOption[EID, PosType]

WithTwoDimensionalIdleWaitTime 通过特定的空闲等待时间创建

  • 默认情况下在没有新的移动计划时将限制 100毫秒 + 移动间隔事件(默认100毫秒)

func WithTwoDimensionalInterval

func WithTwoDimensionalInterval[EID generic.Basic, PosType generic.SignedNumber](duration time.Duration) TwoDimensionalOption[EID, PosType]

WithTwoDimensionalInterval 通过特定的移动间隔时间创建

func WithTwoDimensionalTimeUnit

func WithTwoDimensionalTimeUnit[EID generic.Basic, PosType generic.SignedNumber](duration time.Duration) TwoDimensionalOption[EID, PosType]

WithTwoDimensionalTimeUnit 通过特定时间单位创建

  • 默认单位为1毫秒,最小单位也为1毫秒

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL