1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2025-02-04 15:46:51 -05:00
OpenDiablo2/d2core/d2systems/timescale.go
gravestench caafe7592c more work on ecs impl
* added command line arg for launching ecs impl
* removed render system tests, was causing gl context issues in tests
* fixed all lint errors in d2systems
2020-12-07 12:44:11 -08:00

64 lines
1.3 KiB
Go

package d2systems
import (
"time"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
"github.com/gravestench/akara"
)
const (
defaultScale float64 = 1
)
const (
logPrefixTimeScaleSystem = "Time Scale"
)
// NewTimeScaleSystem creates a timescale system
func NewTimeScaleSystem() *TimeScaleSystem {
m := &TimeScaleSystem{
BaseSystem: &akara.BaseSystem{},
Logger: d2util.NewLogger(),
}
m.SetPrefix(logPrefixTimeScaleSystem)
return m
}
// static check that TimeScaleSystem implements the System interface
var _ akara.System = &TimeScaleSystem{}
// TimeScaleSystem should be the first system added to the world, and whose only job is to
// apply a scalar the world's TimeDelta between frames. It's useful for slowing down or speeding
// up the game time without affecting the render rate.
type TimeScaleSystem struct {
*akara.BaseSystem
*d2util.Logger
scale float64
lastScale float64
}
// Init will initialize the TimeScale system
func (t *TimeScaleSystem) Init(world *akara.World) {
t.World = world
t.Info("initializing ...")
t.scale = defaultScale
}
// Update scales the worlds time delta for this frame
func (t *TimeScaleSystem) Update() {
if !t.Active() || t.scale == t.lastScale {
return
}
t.Infof("setting time scale to %.1f", t.scale)
t.lastScale = t.scale
t.World.TimeDelta *= time.Duration(t.scale)
}