package components import ( "math/rand" "time" "github.com/hajimehoshi/ebiten/v2" ) type Timer struct { CurrentTicks int TargetTicks int Running bool Id int } func NewTimer() *Timer { return &Timer{} } func (timer *Timer) Start(duration time.Duration) { min := int(1000 / ebiten.TPS()) // 16.66ms == 1 tick dur := duration if int(duration.Milliseconds()) < min { dur = time.Duration(1000/ebiten.TPS()) * time.Millisecond } timer.TargetTicks = int(dur.Milliseconds()) / min // id d=50, then 50 / 16.6 = timer.Running = true timer.Id = rand.Int() } func (timer *Timer) Update() { if (timer.CurrentTicks < timer.TargetTicks) && timer.Running { timer.CurrentTicks++ } } func (timer *Timer) IsReady() bool { return (timer.CurrentTicks >= timer.TargetTicks) && timer.Running } func (timer *Timer) Reset() { timer.CurrentTicks = 0 timer.TargetTicks = 0 timer.Running = false }