ebitick

package module
v0.0.0-...-2390450 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: MIT Imports: 3 Imported by: 5 modules, 4 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.

README

ebitick⏱️

Go Reference

ebitick is a timer system for Ebitengine games.

Why?

Because timing stuff is important and can be done in an easy to use, set-it-and-forget-it kinda way. You can just use time.After(), but that works on an additional goroutine, which can introduce race conditions into the mix. Ebitick, in comparison, works on the same goroutine as the rest of your game.

❗ : Note that ebitick keeps time by counting ticks against the target tickrate (TPS), so it won't work properly if you change the tickrate while timers are running.

How?

go get github.com/solarlune/ebitick


package main

import (
    "fmt"
    "time"
    
    "github.com/hajimehoshi/ebiten/v2"
    "github.com/solarlune/ebitick"
)

type Game struct {
    TimerSystem *ebitick.TimerSystem
}

func NewGame() *Game {

    game := &Game{
        TimerSystem: ebitick.NewTimerSystem(),
    }

    // Below, we specify that after a second has passed,
    // we run the given function literal.
    game.TimerSystem.After(time.Second, func() {
        fmt.Println("A second has passed.")
    })

    // There's also a TimerSystem.AfterTicks() function if you want to use
    // ticks exactly without any human-readable time.Duration conversions.
    
    // The After functions return the created Timer, allowing you to pause it
    // later as necessary or set it to loop, for example. Once the timer elapses,
    // it is removed from the TimerSystem and its state is set to StateFinished,
    // so you can check for that if necessary as well.

    return game

}

func (game *Game) Update() error {

    // We have the TimerSystem update once per game tick, and that's it.
    // If you want to adjust the speed with which timers run, change TimerSystem.Speed.
    game.TimerSystem.Update()

    return nil

}

func (game *Game) Draw(screen *ebiten.Image) {}

func (game *Game) Layout(w, h int) (int, int) { return 320, 240 }

func main() {

    ebiten.SetWindowTitle("Ebitick Minimal Example")
    ebiten.RunGame(NewGame())

}

Shout-outs

Ebitengine's Discord server~

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FinishMode

type FinishMode int
const (
	FinishEnd FinishMode = iota
	FinishLoop
)

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

func (ts TimeUnit) ToDuration() time.Duration

ToDuration converts the timestamp to a generic time.Duration.

func (TimeUnit) ToTicks

func (ts TimeUnit) ToTicks() int

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) Removed

func (timer *Timer) Removed() bool

func (*Timer) Restart

func (timer *Timer) Restart() error

Restart restarts the Timer.

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

func (timer *Timer) SetDuration(duration TimeUnit)

SetDuration sets the duration of the Timer.

func (*Timer) SetDurationRandom

func (timer *Timer) SetDurationRandom(min, max TimeUnit)

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) TimeLeft

func (timer *Timer) TimeLeft() TimeUnit

TimeLeft returns a TimeUnit indicating how much time is left on the Timer.

func (*Timer) TimeLeftInPercentage

func (timer *Timer) TimeLeftInPercentage() float32

TimeLeftInPercentage returns the amount of time left on the timer in percentage of the total duration.

func (*Timer) TimeLeftInRealTime

func (timer *Timer) TimeLeftInRealTime() TimeUnit

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址