moving

package
v0.0.25 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Position2DChangeEventHandle

type Position2DChangeEventHandle func(moving *TwoDimensional, entity TwoDimensionalEntity, oldX, oldY float64)

type Position2DDestinationEventHandle

type Position2DDestinationEventHandle func(moving *TwoDimensional, entity TwoDimensionalEntity)

type Position2DStopMoveEventHandle

type Position2DStopMoveEventHandle func(moving *TwoDimensional, entity TwoDimensionalEntity)

type TwoDimensional

type TwoDimensional struct {
	// contains filtered or unexported fields
}

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

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

func NewTwoDimensional

func NewTwoDimensional(options ...TwoDimensionalOption) *TwoDimensional

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

Example
package main

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

func main() {
	m := moving.NewTwoDimensional()
	defer func() {
		m.Release()
	}()
	fmt.Println(m != nil)

}
Output:

true

func (*TwoDimensional) MoveTo

func (slf *TwoDimensional) MoveTo(entity TwoDimensionalEntity, x float64, y float64)

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

Example
m := moving.NewTwoDimensional(moving.WithTwoDimensionalTimeUnit(time.Second))
defer func() {
	m.Release()
}()

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

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

wait.Wait()
Output:

done

func (*TwoDimensional) OnPosition2DChangeEvent

func (slf *TwoDimensional) OnPosition2DChangeEvent(entity TwoDimensionalEntity, oldX, oldY float64)

func (*TwoDimensional) OnPosition2DDestinationEvent

func (slf *TwoDimensional) OnPosition2DDestinationEvent(entity TwoDimensionalEntity)

func (*TwoDimensional) OnPosition2DStopMoveEvent

func (slf *TwoDimensional) OnPosition2DStopMoveEvent(entity TwoDimensionalEntity)

func (*TwoDimensional) RegPosition2DChangeEvent

func (slf *TwoDimensional) RegPosition2DChangeEvent(handle Position2DChangeEventHandle)

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

func (*TwoDimensional) RegPosition2DDestinationEvent

func (slf *TwoDimensional) RegPosition2DDestinationEvent(handle Position2DDestinationEventHandle)

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

func (*TwoDimensional) RegPosition2DStopMoveEvent

func (slf *TwoDimensional) RegPosition2DStopMoveEvent(handle Position2DStopMoveEventHandle)

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

func (*TwoDimensional) Release

func (slf *TwoDimensional) Release()

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

func (*TwoDimensional) StopMove

func (slf *TwoDimensional) StopMove(guid int64)

StopMove 停止特定对象的移动

Example
m := moving.NewTwoDimensional(moving.WithTwoDimensionalTimeUnit(time.Second))
defer func() {
	m.Release()
}()

var wait sync.WaitGroup
m.RegPosition2DChangeEvent(func(moving *moving.TwoDimensional, entity moving.TwoDimensionalEntity, oldX, oldY float64) {
	fmt.Println("move")
})
m.RegPosition2DStopMoveEvent(func(moving *moving.TwoDimensional, entity moving.TwoDimensionalEntity) {
	fmt.Println("stop")
	wait.Done()
})
m.RegPosition2DDestinationEvent(func(moving *moving.TwoDimensional, entity moving.TwoDimensionalEntity) {
	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 interface {
	game.Actor
	game.Position2D
	game.Position2DSet

	// GetSpeed 获取移动速度
	GetSpeed() float64
}

TwoDimensionalEntity 2D移动对象接口定义

type TwoDimensionalOption

type TwoDimensionalOption func(moving *TwoDimensional)

func WithTwoDimensionalIdleWaitTime

func WithTwoDimensionalIdleWaitTime(duration time.Duration) TwoDimensionalOption

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

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

func WithTwoDimensionalInterval

func WithTwoDimensionalInterval(duration time.Duration) TwoDimensionalOption

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

func WithTwoDimensionalTimeUnit

func WithTwoDimensionalTimeUnit(duration time.Duration) TwoDimensionalOption

WithTwoDimensionalTimeUnit 通过特定时间单位创建

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

Jump to

Keyboard shortcuts

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