timer

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: Apache-2.0 Imports: 3 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 and records completions in Timer.CompletedOn; it emits no events and 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 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 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
}

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