42 lines
703 B
Go
42 lines
703 B
Go
package components
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
)
|
|
|
|
type Timer struct {
|
|
currentTicks int
|
|
targetTicks int
|
|
}
|
|
|
|
func NewTimer(d time.Duration) *Timer {
|
|
return &Timer{}
|
|
}
|
|
|
|
func (timer *Timer) Start(d time.Duration) {
|
|
min := int(1000 / ebiten.TPS()) // 16.66ms == 1 tick
|
|
useD := d
|
|
|
|
if int(d.Milliseconds()) < min {
|
|
useD = time.Duration(1000/ebiten.TPS()) * time.Millisecond
|
|
}
|
|
|
|
timer.targetTicks = int(useD.Milliseconds()) / min // id d=50, then 50 / 16.6 =
|
|
}
|
|
|
|
func (t *Timer) Update() {
|
|
if t.currentTicks < t.targetTicks {
|
|
t.currentTicks++
|
|
}
|
|
}
|
|
|
|
func (t *Timer) IsReady() bool {
|
|
return t.currentTicks >= t.targetTicks
|
|
}
|
|
|
|
func (t *Timer) Reset() {
|
|
t.currentTicks = 0
|
|
}
|