aboutsummaryrefslogtreecommitdiff
path: root/internal/engine/tick.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/engine/tick.go')
-rw-r--r--internal/engine/tick.go25
1 files changed, 21 insertions, 4 deletions
diff --git a/internal/engine/tick.go b/internal/engine/tick.go
index d6d470a..419867d 100644
--- a/internal/engine/tick.go
+++ b/internal/engine/tick.go
@@ -1,12 +1,11 @@
package engine
import (
+ "math/rand"
"sync"
"time"
)
-const TickDuration = 600 * time.Millisecond
-
type Callback func() bool
type subscriber struct {
@@ -49,14 +48,17 @@ func (e *Engine) Unsubscribe(id uint64) {
delete(e.subscribers, id)
}
-func (e *Engine) Start() {
+func (e *Engine) Start(tickLengthMs int) {
e.mu.Lock()
defer e.mu.Unlock()
if e.running {
return
}
e.running = true
- e.ticker = time.NewTicker(TickDuration)
+ if tickLengthMs < 50 {
+ tickLengthMs = 50
+ }
+ e.ticker = time.NewTicker(time.Duration(tickLengthMs) * time.Millisecond)
e.stopCh = make(chan struct{})
go func() {
@@ -103,3 +105,18 @@ func (e *Engine) processTick() {
}
}
}
+
+func FractionalTicks(base, speed float64) int {
+ if speed <= 0 {
+ speed = 1
+ }
+ value := base / speed
+ if value < 1 {
+ value = 1
+ }
+ floor := int(value)
+ if rand.Float64() < value-float64(floor) {
+ return floor + 1
+ }
+ return floor
+}