added stop timer to add a delay after level end
This commit is contained in:
@@ -1,41 +1,48 @@
|
||||
package components
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
type Timer struct {
|
||||
currentTicks int
|
||||
targetTicks int
|
||||
CurrentTicks int
|
||||
TargetTicks int
|
||||
Running bool
|
||||
Id int
|
||||
}
|
||||
|
||||
func NewTimer(d time.Duration) *Timer {
|
||||
func NewTimer() *Timer {
|
||||
return &Timer{}
|
||||
}
|
||||
|
||||
func (timer *Timer) Start(d time.Duration) {
|
||||
func (timer *Timer) Start(duration 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
|
||||
dur := duration
|
||||
if int(duration.Milliseconds()) < min {
|
||||
dur = time.Duration(1000/ebiten.TPS()) * time.Millisecond
|
||||
}
|
||||
|
||||
timer.targetTicks = int(useD.Milliseconds()) / min // id d=50, then 50 / 16.6 =
|
||||
timer.TargetTicks = int(dur.Milliseconds()) / min // id d=50, then 50 / 16.6 =
|
||||
timer.Running = true
|
||||
timer.Id = rand.Int()
|
||||
}
|
||||
|
||||
func (t *Timer) Update() {
|
||||
if t.currentTicks < t.targetTicks {
|
||||
t.currentTicks++
|
||||
func (timer *Timer) Update() {
|
||||
if (timer.CurrentTicks < timer.TargetTicks) && timer.Running {
|
||||
timer.CurrentTicks++
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Timer) IsReady() bool {
|
||||
return t.currentTicks >= t.targetTicks
|
||||
func (timer *Timer) IsReady() bool {
|
||||
return (timer.CurrentTicks >= timer.TargetTicks) && timer.Running
|
||||
}
|
||||
|
||||
func (t *Timer) Reset() {
|
||||
t.currentTicks = 0
|
||||
func (timer *Timer) Reset() {
|
||||
timer.CurrentTicks = 0
|
||||
timer.TargetTicks = 0
|
||||
timer.Running = false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user