Skip to content

Commit

Permalink
Allow for gamespeed changes and direct setting of systems
Browse files Browse the repository at this point in the history
  • Loading branch information
unitoftime committed Jul 27, 2024
1 parent fbba72d commit 03fbb2d
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions system.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ type Scheduler struct {
sysLogBackFixed, sysLogFrontFixed []SystemLog
fixedTimeStep time.Duration
accumulator time.Duration
// gameSpeed int64
gameSpeed float64
quit atomic.Bool
pauseRender atomic.Bool
maxLoopCount int
Expand All @@ -156,16 +156,16 @@ func NewScheduler() *Scheduler {
sysLogBackFixed: make([]SystemLog, 0),
fixedTimeStep: 16 * time.Millisecond,
accumulator: 0,
// gameSpeed: 1,
gameSpeed: 1,
}
}

// TODO make SetGameSpeed and SetFixedTimeStep thread safe.

// Sets the rate at which time accumulates. Also, you want them to only change at the end of a frame, else you might get some inconsistencies. Just use a mutex and a single temporary variable
// func (s *Scheduler) SetGameSpeed(speed int64) {
// s.gameSpeed = speed
// }
func (s *Scheduler) SetGameSpeed(speed float64) {
s.gameSpeed = speed
}

// Tells the scheduler to exit. Scheduler will finish executing its remaining tick before closing.
func (s *Scheduler) SetQuit(value bool) {
Expand Down Expand Up @@ -203,6 +203,21 @@ func (s *Scheduler) AppendRender(systems ...System) {
s.render = append(s.render, systems...)
}

// Adds a system to the list of physics systems
func (s *Scheduler) SetInput(systems ...System) {
s.input = systems
}

// Adds a system to the list of physics systems
func (s *Scheduler) SetPhysics(systems ...System) {
s.physics = systems
}

// Adds a system to the list of render systems
func (s *Scheduler) SetRender(systems ...System) {
s.render = systems
}

// func (s *Scheduler) AppendCleanup(systems ...System) {
// s.cleanup = append(s.cleanup, systems...)
// }
Expand Down Expand Up @@ -339,10 +354,10 @@ func (s *Scheduler) Run() {
// dt = time.Since(frameStart)
// frameStart = time.Now()

s.accumulator += dt
// s.accumulator += dt

// scaledDt := dt.Nanoseconds() * s.gameSpeed
// s.accumulator += time.Duration(scaledDt)
scaledDt := float64(dt.Nanoseconds()) * s.gameSpeed
s.accumulator += time.Duration(scaledDt)

// s.accumulator += 16667 * time.Microsecond
// fmt.Println(dt, s.accumulator)
Expand Down

0 comments on commit 03fbb2d

Please sign in to comment.