Skip to content

Commit

Permalink
Document manual model update (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlange-42 authored Jan 30, 2024
1 parent cda3d50 commit 5940120
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Changelog

## [[unpublished]](https://github.com/mlange-42/arche-model/compare/v0.6.0...main)
## [[v0.7.0]](https://github.com/mlange-42/arche-model/compare/v0.6.0...v0.7.0)

### Features

Expand Down
14 changes: 11 additions & 3 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,15 @@ func (m *Model) Seed(seed ...uint64) *Model {
return m
}

// Run the model. Initializes the model if it is not already initialized.
// Run the model, updating systems and ui systems according to Model.TPS and Model.FPS, respectively.
// Initializes the model if it is not already initialized.
// Finalizes the model after the run.
//
// Runs until Terminate in the resource resource.Termination is set to true
// (see [resource.Termination]).
//
// To perform updates manually, see [Model.Update] and [Model.UpdateUI],
// as well as [Model.Initialize] and [Model.Finalize].
func (m *Model) Run() {
m.Systems.run()
}
Expand All @@ -82,16 +86,20 @@ func (m *Model) Initialize() {
// Update the model's systems.
// Return whether the run should continue.
//
// Ignores Model.TPS.
//
// Panics if [Model.Initialize] was not called.
func (m *Model) Update() bool {
return m.Systems.UpdateSystems()
return m.Systems.updateSystems()
}

// UpdateUI the model's UI systems.
//
// Ignores Model.FPS.
//
// Panics if [Model.Initialize] was not called.
func (m *Model) UpdateUI() {
m.Systems.UpdateUISystems()
m.Systems.updateUISystems()
}

// Finalize the model.
Expand Down
18 changes: 18 additions & 0 deletions model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ func ExampleModel() {
// Output:
}

func ExampleModel_manualUpdate() {
// Create a new, seeded model.
m := model.New().Seed(123)

// Add systems.
m.AddSystem(&system.FixedTermination{
Steps: 100,
})

// Run the simulation manually.
m.Initialize()
for m.Update() {
m.UpdateUI()
}
m.Finalize()
// Output:
}

func ExampleModel_Reset() {
// Create a new model.
m := model.New()
Expand Down
32 changes: 16 additions & 16 deletions model/systems.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ func (s *Systems) initialize() {
// Update all systems.
func (s *Systems) update() bool {
s.locked = true
update := s.updateSystems()
s.updateUISystems(update)
update := s.updateSystemsTimed()
s.updateUISystemsTimed(update)
s.locked = false

s.removeSystems()
Expand All @@ -229,8 +229,8 @@ func (s *Systems) update() bool {
return !s.termRes.Get().Terminate
}

// UpdateSystems updates all normal systems
func (s *Systems) UpdateSystems() bool {
// updateSystems updates all normal systems
func (s *Systems) updateSystems() bool {
if !s.initialized {
panic("the model is not initialized")
}
Expand All @@ -251,8 +251,8 @@ func (s *Systems) UpdateSystems() bool {
return !s.termRes.Get().Terminate
}

// UpdateUISystems updates all UI systems
func (s *Systems) UpdateUISystems() {
// updateUISystems updates all UI systems
func (s *Systems) updateUISystems() {
if !s.initialized {
panic("the model is not initialized")
}
Expand Down Expand Up @@ -280,7 +280,15 @@ func (s *Systems) wait() {
}

// Update normal systems.
func (s *Systems) updateSystems() bool {
func (s *Systems) updateSystemsSimple() bool {
for _, sys := range s.systems {
sys.Update(s.world)
}
return true
}

// Update normal systems.
func (s *Systems) updateSystemsTimed() bool {
update := false
if s.Paused {
update = !time.Now().Before(s.nextUpdate)
Expand All @@ -303,14 +311,6 @@ func (s *Systems) updateSystems() bool {
return update
}

// Update normal systems.
func (s *Systems) updateSystemsSimple() bool {
for _, sys := range s.systems {
sys.Update(s.world)
}
return true
}

// Update ui systems.
func (s *Systems) updateUISystemsSimple() {
for _, sys := range s.uiSystems {
Expand All @@ -322,7 +322,7 @@ func (s *Systems) updateUISystemsSimple() {
}

// Update UI systems.
func (s *Systems) updateUISystems(updated bool) {
func (s *Systems) updateUISystemsTimed(updated bool) {
if !s.Paused && s.FPS <= 0 {
if updated {
s.updateUISystemsSimple()
Expand Down

0 comments on commit 5940120

Please sign in to comment.