gametime

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 11 Imported by: ? 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 gametime is the home of the engine's time work: the fixed loop's state as the Simulation resource and countdowns as Timer components.

NewTimerSystem advances timers on fixed ticks, records completions in CompletedOn, and emits one Completed signal per completion tick. It does not remove timers. See simulation.go for the simulation time resource.

Index

Constants

View Source
const (
	// SimulationSaveKey identifies [Simulation] in save data.
	SimulationSaveKey = "gametime.Simulation"
	// RealSaveKey identifies [Real] in save data.
	RealSaveKey = "gametime.Real"
	// GroupsSaveKey identifies [Groups] in save data.
	GroupsSaveKey = "gametime.Groups"
)

Save keys are stable wire identities; renaming one is a breaking change.

View Source
const SchedulerSystemName = "engine.scheduler"

SchedulerSystemName is the name used to register the scheduler system in the pre-fixed schedule.

View Source
const TimerSystemName = "engine.timer"

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

Variables

This section is empty.

Functions

func NewTimerSystem

func NewTimerSystem() core.SystemFunc

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

The game constructor registers this system under TimerSystemName for every game: timers are declared by component presence, so the system is always on and costs an empty query when no timers exist.

func RegisterBlueprints

func RegisterBlueprints(reg *serialization.Registry) error

RegisterBlueprints registers the package's persistent state with reg: the Simulation, Real, and Groups resources.

Types

type Completed

type Completed struct {
	// Entity is the entity whose Timer completed.
	Entity core.Entity
	// 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 CompletedOn in Timer.

type Groups

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

func NewGroups() *Groups

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

func (*Groups) MarshalJSON

func (g *Groups) MarshalJSON() ([]byte, error)

MarshalJSON projects the groups as a list ordered by name, so the wire form is deterministic.

func (*Groups) Pause

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

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

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

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

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.

func (*Groups) UnmarshalJSON

func (g *Groups) UnmarshalJSON(data []byte) error

UnmarshalJSON replaces the groups from their wire form. It validates names and scales before applying anything.

type Real

type Real struct {
	// Frame counts advances, including those made while paused.
	Frame uint64 `json:"frame"`
	// Elapsed is the sum of elapsed times supplied to advances.
	Elapsed time.Duration `json:"elapsed"`
	// Delta is the elapsed time supplied to the most recent advance.
	Delta time.Duration `json:"delta"`
	// MaxFrameTime limits elapsed time used for frame updates and, before
	// scaling, the fixed-loop accumulator.
	MaxFrameTime time.Duration `json:"maxFrameTime"`
}

Real tracks the elapsed time supplied to the engine on each advance.

The engine updates it even while paused, without applying time scaling or the frame clamp. Use Simulation for fixed-step simulation time.

func NewReal

func NewReal(maxFrameTime time.Duration) *Real

NewReal returns a real-time resource with the given frame clamp.

type Scheduler

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

Scheduler manages timed execution of systems within a world. It supports one-time and recurring entries, and allows querying and cancellation of scheduled entries.

Entries are reconciled in the core.PhasePreFixed phase of the world's update cycle.

Like the world, the Scheduler is not concurrency-safe and should be accessed from a single goroutine only.

func NewScheduler

func NewScheduler(world *core.World) *Scheduler

NewScheduler creates a new Scheduler associated with the given world. The world's tick will be used as the reference for scheduling entries.

func (*Scheduler) AtTick

func (s *Scheduler) AtTick(name string, tick uint64, sys core.SystemFunc) error

AtTick schedules a one-time execution of the given system at the specified tick.

Returns an error if the name is empty, the system is nil, or the tick is in the past.

func (*Scheduler) Cancel

func (s *Scheduler) Cancel(name string) error

Cancel removes the entry with the given name from the scheduler.

func (*Scheduler) Every

func (s *Scheduler) Every(name string, interval uint64, sys core.SystemFunc) error

Every schedules recurring execution of the given system at the specified interval. The interval is measured in ticks relative to the world's current tick.

Returns an error if the name is empty, the system is nil, the interval is zero, or the interval is too large.

func (*Scheduler) NextDue

func (s *Scheduler) NextDue(name string) (uint64, bool)

NextDue returns the due tick and existence status of the entry with the given name.

func (*Scheduler) Once

func (s *Scheduler) Once(name string, delay uint64, sys core.SystemFunc) error

Once schedules a one-time execution of the given system after the specified delay. The delay is measured in ticks relative to the world's current tick.

Returns an error if the name is empty, the system is nil, or the delay is too large.

func (*Scheduler) System

func (s *Scheduler) System() core.SystemFunc

System returns the system that executes due entries from the scheduler. Register it in core.PhasePreFixed as SchedulerSystemName so scheduled entries run before the fixed phase's systems.

An entry whose action returns an error stays registered and retries on the next run; the rest of the batch still executes. All errors are joined and reported after the batch completes.

type Simulation

type Simulation struct {
	// Tick counts fixed updates since the game started.
	Tick uint64 `json:"tick"`
	// Delta is the fixed time interval per tick, independent of Scale.
	Delta time.Duration `json:"delta"`
	// Paused stops fixed updates and preserves the accumulator; frame updates
	// continue.
	Paused bool `json:"paused"`
	// Scale multiplies elapsed time accumulated toward fixed ticks. It must
	// be finite and positive; use [Simulation.Pause] to stop.
	Scale float64 `json:"scale"`
}

Simulation holds the engine's fixed-step time state.

The engine updates Tick as fixed updates run. The fixed-loop accumulator is not part of this resource, so copying it does not preserve the accumulator's position.

func NewSimulation

func NewSimulation(delta time.Duration) *Simulation

NewSimulation returns a running simulation with the given fixed tick interval, at scale 1, starting at tick 0.

func (*Simulation) Elapsed

func (s *Simulation) Elapsed() time.Duration

Elapsed returns Tick multiplied by the current Delta.

func (*Simulation) Pause

func (s *Simulation) Pause()

Pause stops fixed updates on the next advance.

func (*Simulation) Resume

func (s *Simulation) Resume()

Resume allows fixed updates to continue on the next advance.

func (*Simulation) SetScale

func (s *Simulation) SetScale(scale float64) error

SetScale sets Simulation.Scale, returning an error without changing it if scale is invalid.

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