Documentation
¶
Index ¶
- type FinishMode
- type TimeUnit
- type Timer
- func (timer *Timer) Cancel()
- func (timer *Timer) Pause()
- func (timer *Timer) Removed() bool
- func (timer *Timer) Restart() error
- func (timer *Timer) Resume()
- func (timer *Timer) SetDuration(duration TimeUnit)
- func (timer *Timer) SetDurationRandom(min, max TimeUnit)
- func (timer *Timer) State() TimerState
- func (timer *Timer) TimeLeft() TimeUnit
- func (timer *Timer) TimeLeftInPercentage() float32
- func (timer *Timer) TimeLeftInRealTime() TimeUnit
- type TimerFunc
- type TimerState
- type TimerSystem
- func (ts *TimerSystem) After(tickCount int, onElapsed TimerFunc) *Timer
- func (ts *TimerSystem) AfterDuration(duration time.Duration, onElapsed TimerFunc) *Timer
- func (ts *TimerSystem) Clear()
- func (ts *TimerSystem) CurrentTime() TimeUnit
- func (ts *TimerSystem) SetTPS(targetTPS int)
- func (ts *TimerSystem) TPS() int
- func (ts *TimerSystem) Timers() []*Timer
- func (ts *TimerSystem) ToTimeUnit(ticks int) TimeUnit
- func (ts *TimerSystem) ToTimeUnitSeconds(duration time.Duration) TimeUnit
- func (ts *TimerSystem) Update()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type TimeUnit ¶
type TimeUnit struct {
// contains filtered or unexported fields
}
TimeUnit represents a game tick in a 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. By default, whenever the TimerSystem is updated, one TimeUnit has passed.
func (TimeUnit) ToDuration ¶
ToDuration converts the timestamp to a generic time.Duration.
type Timer ¶
type Timer struct {
// 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.
func (*Timer) Resume ¶
func (timer *Timer) Resume()
Resume resumes a paused Timer. This does nothing on a Timer if it isn't paused.
func (*Timer) SetDuration ¶
SetDuration sets the duration of the Timer.
func (*Timer) SetDurationRandom ¶
SetDurationRandom sets the Timer's duration to be a random value between the minimum and maximum time unit values.
func (*Timer) State ¶
func (timer *Timer) State() TimerState
func (*Timer) TimeLeftInPercentage ¶
TimeLeftInPercentage returns the amount of time left on the timer in percentage of the total duration.
func (*Timer) TimeLeftInRealTime ¶
TimeLeftInRealTime returns a TimeUnit indicating how much -absolute- time is left on the Timer. This value is multiplied by the owning system's current speed value.
type TimerFunc ¶
type TimerFunc func(timer *Timer, loopCount int) FinishMode
type TimerState ¶
type TimerState int
const ( StateRunning TimerState = iota StateCanceled StatePaused StateFinished )
The various possible states for a Timer.
type TimerSystem ¶
type TimerSystem struct {
// contains filtered or unexported fields
}
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(tickCount int, onElapsed TimerFunc) *Timer
After 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) AfterDuration ¶
func (ts *TimerSystem) AfterDuration(duration time.Duration, onElapsed TimerFunc) *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) 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) CurrentTime ¶
func (ts *TimerSystem) CurrentTime() TimeUnit
Returns the current time of the TimerSyStem.
func (*TimerSystem) SetTPS ¶
func (ts *TimerSystem) SetTPS(targetTPS int)
Sets the target TPS (ticks per second) for the timer system.
func (*TimerSystem) TPS ¶
func (ts *TimerSystem) TPS() int
Returns the target TPS (ticks per ssecond) for the timer system.
func (*TimerSystem) Timers ¶
func (ts *TimerSystem) Timers() []*Timer
Returns all timers currently active on the TimerSystem.
func (*TimerSystem) ToTimeUnit ¶
func (ts *TimerSystem) ToTimeUnit(ticks int) TimeUnit
ToTimeUnit converts the given number of seconds to a TimeUnit using the TimerSystem's targeted TPS value.
func (*TimerSystem) ToTimeUnitSeconds ¶
func (ts *TimerSystem) ToTimeUnitSeconds(duration time.Duration) TimeUnit
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.