Documentation
¶
Overview ¶
Package timer provides countdowns as Timer components. NewSystem advances them on fixed ticks and records completions in Timer.CompletedOn; it emits no events and does not remove timers.
Index ¶
Constants ¶
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 ¶
NewSystem returns the fixed-tick system that advances running Timer components and records completions in Timer.CompletedOn.
The game constructor registers this system under SystemName when `castrum.WithTimer` is enabled. Otherwise, register it yourself.
Types ¶
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
Pause stops the named group's timers from advancing. Pausing an unknown group creates it paused.
func (*Groups) Paused ¶ added in v0.2.0
Paused reports whether the named timer group is currently paused. Unknown groups are considered unpaused.
func (*Groups) Resume ¶ added in v0.2.0
Resume allows the named group's timers to advance. Resuming an unknown group creates it unpaused.
func (*Groups) Scale ¶ added in v0.2.0
Scale returns the current time scale for the named timer group. Unknown groups are considered to have a scale of 1.0.
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 ¶
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 ¶
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 ¶
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.