Files

41 lines
1.1 KiB
Go

package westworldwithmessaging
import (
"sync"
"time"
)
// clockSingleton is our own stand-in for whatever timer/clock utility
// class the book uses elsewhere (it only shows up here as
// "Clock->GetCurrentTime()" - the book hasn't shown us that class's
// actual source, likely a separate timer file). All DispatchMessage
// and DispatchDelayedMessages actually need is a steadily increasing
// number of seconds, so this just measures time elapsed since the
// clock was first used.
type clockSingleton struct {
start time.Time
}
var (
clockInstance *clockSingleton
clockOnce sync.Once
)
func newClock() *clockSingleton {
clockOnce.Do(func() {
clockInstance = &clockSingleton{start: time.Now()}
})
return clockInstance
}
// Clock is a shorthand for the clock singleton, same "#define"-style
// convenience pattern as EntityMgr and Dispatch.
var Clock = newClock()
// GetCurrentTime returns the number of seconds elapsed since the
// clock started, matching the book's
// "double CurrentTime = Clock->GetCurrentTime();".
func (c *clockSingleton) GetCurrentTime() float64 {
return time.Since(c.start).Seconds()
}