timer

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package timer provides countdowns as Timer components. NewSystem advances them on fixed ticks, records completions in Timer.CompletedOn, and emits one Completed signal per completion tick. It does not remove timers.

Index

Constants

View Source
const SystemName = "engine.timer"

SystemName is the name used to register the timer system in the fixed schedule.

Variables

This section is empty.

Functions

func NewSystem

func NewSystem() core.System

NewSystem returns the fixed-tick system that advances running Timer components, records completions in Timer.CompletedOn, and emits one Completed signal per completion.

The game constructor registers this system under SystemName when `castrum.WithTimer` is enabled. Otherwise, register it yourself.

Types

type Completed added in v0.3.0

type Completed struct {
	// Entity is the entity whose Timer completed.
	Entity core.EntityID
	// Tick is the fixed tick of the completion.
	Tick uint64
}

Completed is the signal the timer system emits when a Timer completes an interval. The system emits one signal per completion tick: a repeating timer that finishes several intervals in one slow tick records one completion and emits one signal. Tick equals Timer.CompletedOn.

type Groups added in v0.2.0

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

Groups manages the pause and scale state for named timer groups. Groups is stored as a resource in the world and membership lives on Timer.Group. Timers without a group are ungrouped and not affected by any group's pause or scale settings.

Groups are safe under the single-threaded context of the game loop.

func NewGroups added in v0.2.0

func NewGroups() *Groups

NewGroups creates and returns a new Groups instance with no initial groups.

func (*Groups) Pause added in v0.2.0

func (g *Groups) Pause(name string)

Pause stops the named group's timers from advancing. Pausing an unknown group creates it paused.

func (*Groups) Paused added in v0.2.0

func (g *Groups) Paused(name string) bool

Paused reports whether the named timer group is currently paused. Unknown groups are considered unpaused.

func (*Groups) Resume added in v0.2.0

func (g *Groups) Resume(name string)

Resume allows the named group's timers to advance. Resuming an unknown group creates it unpaused.

func (*Groups) Scale added in v0.2.0

func (g *Groups) Scale(name string) float64

Scale returns the current time scale for the named timer group. Unknown groups are considered to have a scale of 1.0.

func (*Groups) SetScale added in v0.2.0

func (g *Groups) SetScale(name string, scale float64) error

SetScale sets the named group's time scale, the multiplier applied to the tick time its timers accumulate. It returns an error for an empty name or a scale that is not a positive finite number; use Pause to stop a group.

type Timer

type Timer struct {
	// Duration is the length of each interval and must be positive.
	Duration time.Duration
	// Elapsed is the time accumulated in the current interval.
	Elapsed time.Duration
	// Repeating starts a new interval after each completion and carries over
	// excess elapsed time. Otherwise, the timer stops at Duration.
	Repeating bool
	// Running controls whether elapsed time accumulates. Setting it false
	// pauses an unfinished timer; restart a completed one-shot with
	// [Timer.Restart].
	Running bool
	// CompletedOn is the fixed tick of the most recent completion. Zero means
	// no completion has been recorded because game ticks start at one.
	// [Timer.Restart] clears it.
	CompletedOn uint64
	// Group adds the timer to a named group, which can be paused or scaled
	// independently of other groups.
	Group string
}

Timer tracks a countdown on an entity. NewSystem advances it on fixed ticks.

An entity can hold only one Timer. Give each timer its own entity to track multiple timers for one actor.

func NewTimer

func NewTimer(duration time.Duration, repeating bool) Timer

NewTimer returns a running timer with the given duration and repeat setting. Timer.Validate rejects a non-positive duration when the timer enters storage.

func (Timer) HasCompleted

func (t Timer) HasCompleted() bool

HasCompleted reports whether a one-shot timer has completed. It remains true until Timer.Restart. It always returns false for repeating timers; use Timer.JustCompleted to observe each completion.

func (Timer) JustCompleted

func (t Timer) JustCompleted(tick uint64) bool

JustCompleted reports whether the timer's most recent completion occurred on tick. Pass core.Context.Tick to detect a completion during the current fixed update.

func (*Timer) Restart

func (t *Timer) Restart()

Restart clears Elapsed and CompletedOn, then marks the timer as Running. To restart a timer stored on an entity, call it inside core.Entity.Update.

func (Timer) Validate

func (t Timer) Validate() error

Validate reports an error if Duration is non-positive or Elapsed is negative.

Jump to

Keyboard shortcuts

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