Browse Source

fixed error with updating time from current time

master
Pixdigit 4 years ago
parent
commit
8bb83808af
  1. 7
      Timer.go
  2. 6
      timer_test.go

7
Timer.go

@ -3,14 +3,14 @@ package simpleTimer
type Timer struct {
Duration float64
TimePassed float64
LastStartOffset float64
LastUpdate float64
}
func NewTimer(duration float64) *Timer {
timer := Timer{
Duration: duration,
TimePassed: 0,
LastStartOffset: 0,
LastUpdate: 0,
}
return &timer
}
@ -28,7 +28,8 @@ func (t *Timer) Tick(timePassed float64) {
}
func (t *Timer) Update(currentTime float64) {
t.TimePassed = currentTime - t.LastStartOffset
t.TimePassed += currentTime - t.LastUpdate
t.LastUpdate = currentTime
}
func (t *Timer) Ended() bool {

6
timer_test.go

@ -38,11 +38,11 @@ func TestOffset(t *testing.T) {
timer := NewTimer(5)
timer.TimePassed = 3
timer.LastStartOffset = 2
timer.LastUpdate = 2
timer.Update(6)
tools.Test(timer.TimePassed== 4, "did not set correct time with offset", t)
tools.Test(timer.TimePassed== 7, "did not set correct time with offset", t)
timer.Tick(1)
tools.Test(timer.TimePassed == 5, "did not tick correct time with offset", t)
tools.Test(timer.TimePassed == 8, "did not tick correct time with offset", t)
}

Loading…
Cancel
Save