Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions ecs/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,33 @@ package ecs

import "time"

const maxDaltaTime = time.Second / 30
const maxDefaultDeltaTime = time.Second / 30

// Time manages the time of the world.
type Time struct {
// timeScale is the scale of the time.
timeScale float64

// deltaTime is the time between the last update and the current update
deltaTime time.Duration

// maxDeltaTime is the *maximum* time between the last update and the current update.
// default is time.Second / 30 (33.333ms)
maxDeltaTime time.Duration

// sleep is the time to sleep
sleep time.Duration

prevTime time.Time
isPaused bool
}

// NewTIme creates a new Time.
// NewTime creates a new Time.
func NewTime() *Time {
return &Time{
prevTime: time.Now(),
timeScale: 1,
prevTime: time.Now(),
timeScale: 1,
maxDeltaTime: maxDefaultDeltaTime,
}
}

Expand All @@ -40,8 +47,8 @@ func (t *Time) Update() {
t.deltaTime = time.Duration(ms) * time.Millisecond
}

if t.deltaTime > maxDaltaTime {
t.deltaTime = maxDaltaTime
if t.deltaTime > t.maxDeltaTime {
t.deltaTime = t.maxDeltaTime
}

if t.sleep > 0 {
Expand Down Expand Up @@ -69,6 +76,11 @@ func (t *Time) SetTimeScale(scale float64) {
t.timeScale = scale
}

// SetMaxDeltaTime sets the max delta time.
func (t *Time) SetMaxDeltaTime(d time.Duration) {
t.maxDeltaTime = d
}

// TimeScale returns the time scale.
func (t *Time) TimeScale() float64 {
return t.timeScale
Expand Down
Loading