motion

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package motion executes paths over time: spline interpolation, speed/easing curves, per-entity motion stacks (current + queued + idle fallback), pause/resume/abort.

Distinct from kit/pathfinding which produces a path (waypoint sequence); motion is the per-tick execution of that path through space, with a built-in motion-type registry (Point, Path, Idle).

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyStack = errors.New("motion: stack is empty")
)

Errors surfaced by the Stack.

Functions

This section is empty.

Types

type IdleMotion

type IdleMotion struct{}

IdleMotion holds the entity in place. Never reports done. Useful as the Stack.idle fallback.

func (IdleMotion) Kind

func (IdleMotion) Kind() string

Kind implements Motion.

func (IdleMotion) OnAbort

func (IdleMotion) OnAbort()

func (IdleMotion) OnPause

func (IdleMotion) OnPause()

OnPause / OnResume / OnAbort default no-ops.

func (IdleMotion) OnResume

func (IdleMotion) OnResume()

func (IdleMotion) Tick

func (IdleMotion) Tick(_ time.Duration, current Transform) (Transform, bool)

Tick implements Motion. The transform is unchanged and the motion never completes — the stack remains parked in Idle until something else is pushed.

type Motion

type Motion interface {
	// Kind is a stable identifier for logs and factory wiring.
	Kind() string
	// Tick advances the motion by dt. `current` is the entity's
	// transform at the start of the tick — implementations may use it
	// as the "from" for interpolation (Point) or ignore it (Path).
	Tick(dt time.Duration, current Transform) (next Transform, done bool)
	// OnPause / OnResume / OnAbort fire when the stack transitions
	// states. Default implementations are no-ops; override when state
	// needs invalidation (e.g. abort cancels a pending callback).
	OnPause()
	OnResume()
	OnAbort()
}

Motion is the unit of executable movement. Tick is called every frame; it advances internal state by `dt` and reports the next Transform plus whether the motion has completed.

Implementations should be cheap: a Motion typically lives for many ticks and Tick runs at the simulation frequency.

type PathMotion

type PathMotion struct {
	Waypoints []geometry.Vec3
	Speed     float32
	Arrival   float32
	// contains filtered or unexported fields
}

PathMotion walks through a list of waypoints in order at a constant speed. Done when the last waypoint is reached.

func NewPath

func NewPath(points []geometry.Vec3, speed float32) *PathMotion

NewPath constructs a PathMotion with a 0.05-unit default arrival.

func (PathMotion) Kind

func (PathMotion) Kind() string

Kind implements Motion.

func (PathMotion) OnAbort

func (PathMotion) OnAbort()

func (PathMotion) OnPause

func (PathMotion) OnPause()

OnPause / OnResume / OnAbort default no-ops.

func (PathMotion) OnResume

func (PathMotion) OnResume()

func (*PathMotion) Tick

func (p *PathMotion) Tick(dt time.Duration, current Transform) (Transform, bool)

Tick implements Motion.

type PointMotion

type PointMotion struct {
	Target           geometry.Vec3
	Speed            float32 // world-units per second
	ArrivalThreshold float32 // distance below which the motion completes
}

PointMotion drives the entity toward a fixed point at a constant speed. Done when within a configurable arrival threshold.

func NewPoint

func NewPoint(target geometry.Vec3, speed float32) *PointMotion

NewPoint constructs a PointMotion with a 0.05-unit default arrival.

func (PointMotion) Kind

func (PointMotion) Kind() string

Kind implements Motion.

func (PointMotion) OnAbort

func (PointMotion) OnAbort()

func (PointMotion) OnPause

func (PointMotion) OnPause()

OnPause / OnResume / OnAbort default no-ops.

func (PointMotion) OnResume

func (PointMotion) OnResume()

func (*PointMotion) Tick

func (p *PointMotion) Tick(dt time.Duration, current Transform) (Transform, bool)

Tick implements Motion.

type Stack

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

Stack is a per-entity motion stack: a current Motion, a queue of follow-ups, and an optional idle Motion that runs when the queue drains. Goroutine-safe; the typical owner is the entity actor.

func NewStack

func NewStack() *Stack

NewStack constructs an empty Stack.

func (*Stack) Current

func (s *Stack) Current() Motion

Current returns the currently-executing Motion, or nil.

func (*Stack) Depth

func (s *Stack) Depth() int

Depth is the queue depth (excludes the currently running motion).

func (*Stack) Pause

func (s *Stack) Pause()

Pause stops Tick from advancing the current motion.

func (*Stack) Push

func (s *Stack) Push(m Motion)

Push adds m to the queue. If nothing is currently active, m is promoted immediately to current.

func (*Stack) Replace

func (s *Stack) Replace(m Motion)

Replace aborts the current motion (and clears the queue) and installs m as the new current. Useful for "drop everything and do X" signals (player issued a new order).

func (*Stack) Resume

func (s *Stack) Resume()

Resume re-enables Tick.

func (*Stack) SetIdle

func (s *Stack) SetIdle(m Motion)

SetIdle installs the fallback motion that runs when the queue drains. nil clears it.

func (*Stack) Tick

func (s *Stack) Tick(dt time.Duration, cur Transform) Transform

Tick advances the stack one frame and returns the resulting Transform. When the current motion reports done, the next queued motion is promoted; if the queue drains, the idle motion runs.

type Transform

type Transform struct {
	Position geometry.Vec3
	Rotation geometry.Quat
}

Transform is the position + rotation an entity is at.

Jump to

Keyboard shortcuts

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