Documentation ¶
Index ¶
Constants ¶
const ( StateRunning = iota StateCanceled StatePaused StateFinished )
The various possible states for a Timer.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type TimeUnit ¶
type TimeUnit float32
TimeUnit represents a game tick in an ebitengine game. For simplicity, a TimeUnit can be used as either a timestamp (think time.Time{}, time.Now()), or a duration of time (time.Duration{}, time.Since()) depending on the context with which the value is used. It is a float so that a TimerSystem can run at faster or slower speeds.
func ToTimeUnit ¶
ToTimeUnit converts the given number of seconds to a TimeUnit using Ebiten's current TPS value.
func (TimeUnit) ToDuration ¶
ToDuration converts the timestamp to a generic time.Duration.
type Timer ¶
type Timer struct { StartTick TimeUnit // On what tick of the TimerSystem the Timer was initially started. OnExecute func() // What the timer does once it elapses. Loop bool // If the Timer should loop after elapsing. Defaults to off. State int // What state the Timer is in. // contains filtered or unexported fields }
Timer represents a timer that elapses after a given amount of time.
func (*Timer) Cancel ¶
func (timer *Timer) Cancel()
Cancel cancels a Timer, removing it from the TimerSystem the next time TimerSystem.Update() is called. This does nothing on a finished Timer.
func (*Timer) Pause ¶
func (timer *Timer) Pause()
Pause pauses the Timer. While paused, a Timer is not incrementing time. This does nothing on a Timer if it isn't running, specifically.
func (*Timer) Resume ¶
func (timer *Timer) Resume()
Resume resumes a paused Timer. This does nothing on a Timer if it isn't paused, specifically.
func (*Timer) SetDuration ¶
type TimerSystem ¶
type TimerSystem struct { Timers []*Timer // The Timers presently existing in the System. CurrentTime TimeUnit // The current TimeUnit (tick) of the TimerSystem. TimerSystem.Update() increments this by TimerSystem.Speed each game tick. Speed float64 // Overall update speed of the system; changing this changes how fast the TimerSystem runs. Defaults to 1. }
TimerSystem represents a system that updates and triggers timers added to the System.
func NewTimerSystem ¶
func NewTimerSystem() *TimerSystem
NewTimerSystem creates a new TimerSystem instance.
func (*TimerSystem) After ¶
func (ts *TimerSystem) After(duration time.Duration, onElapsed func()) *Timer
After creates a new Timer that will elapse after the given duration, running the onElapsed() function when it does so. Note that the granularity for conversion from time.Duration is whole ticks, so fractions will be rounded down. For example, if your game runs at 60 FPS / TPS, then a tick is 16.67 milliseconds. If you pass a duration of 20 milliseconds, the timer will trigger after one tick. If you pass a duration of 16 milliseconds, the timer will trigger immediately. This will happen on whatever thread TimerSystem.Update() is called on (most probably the main thread).
func (*TimerSystem) AfterTicks ¶
func (ts *TimerSystem) AfterTicks(tickCount TimeUnit, onElapsed func()) *Timer
AfterTicks creates a new Timer that will elapse after tickCount ticks, running the onElapsed() function when it does so. This will happen on whatever thread TimerSystem.Update() is called on (most probably the main thread).
func (*TimerSystem) Clear ¶
func (ts *TimerSystem) Clear()
Clear cancels all Timers that belong to the TimerSystem and removes them from the TimerSystem. This is safe to call from a Timer's elapsing function.
func (*TimerSystem) Update ¶
func (ts *TimerSystem) Update()
Update updates the TimerSystem and triggers any Timers that have elapsed. This should be called once per frame in your game's update loop. Note that timers will no longer be accurate if Ebitengine's TPS is changed while they are running.