• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / arche-model / 4916614333

08 May 2023 03:06PM CUT coverage: 97.426%. Remained the same
4916614333

push

github

GitHub
Upgrade to Arche dev version, fix examples for breaking changes (#44)

530 of 544 relevant lines covered (97.43%)

107.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.86
/model/systems.go
1
package model
2

3
import (
4
        "fmt"
5
        "time"
6

7
        "github.com/mlange-42/arche-model/resource"
8
        "github.com/mlange-42/arche/ecs"
9
        "github.com/mlange-42/arche/generic"
10
)
11

12
// System is the interface for ECS systems.
13
//
14
// See also [UISystem] for systems with an independent graphics step.
15
type System interface {
16
        Initialize(w *ecs.World) // Initialize the system.
17
        Update(w *ecs.World)     // Update the system.
18
        Finalize(w *ecs.World)   // Finalize the system.
19
}
20

21
// UISystem is the interface for ECS systems that display UI in an independent graphics step.
22
//
23
// See also [System] for normal systems.
24
type UISystem interface {
25
        InitializeUI(w *ecs.World) // InitializeUI the system.
26
        UpdateUI(w *ecs.World)     // UpdateUI/update the system.
27
        PostUpdateUI(w *ecs.World) // PostUpdateUI does the final part of updating, e.g. update the GL window.
28
        FinalizeUI(w *ecs.World)   // FinalizeUI the system.
29
}
30

31
// Systems manages and schedules ECS [System] and [UISystem] instances.
32
//
33
// [System] instances are updated with a frequency given by TPS (ticks per second).
34
// [UISystem] instances are updated independently of normal systems, with a frequency given by FPS (frames per second).
35
//
36
// [Systems] is an embed in [Model] and it's methods are usually only used through a [Model] instance.
37
// By also being a resource of each [Model], however, systems can access it and e.g. remove themselves from a model.
38
type Systems struct {
39
        // Ticks per second for normal systems.
40
        // Values <= 0 (the default) mean as fast as possible.
41
        TPS float64
42
        // Frames per second for UI systems.
43
        // A zero/unset value defaults to 30 FPS. Values < 0 sync FPS with TPS.
44
        // With fast movement, a value of 60 may be required for fluent graphics.
45
        FPS float64
46
        // Whether the simulation is currently paused.
47
        // When paused, only UI updates but no normal updates are performed.
48
        Paused bool
49

50
        world      *ecs.World
51
        systems    []System
52
        uiSystems  []UISystem
53
        toRemove   []System
54
        uiToRemove []UISystem
55

56
        nextDraw   time.Time
57
        nextUpdate time.Time
58

59
        initialized bool
60
        locked      bool
61

62
        tickRes generic.Resource[resource.Tick]
63
        termRes generic.Resource[resource.Termination]
64
}
65

66
// AddSystem adds a [System] to the model.
67
//
68
// Panics if the system is also a [UISystem].
69
// To add systems that implement both [System] and [UISystem], use [Systems.AddUISystem]
70
func (s *Systems) AddSystem(sys System) {
35✔
71
        if s.initialized {
38✔
72
                panic("adding systems after model initialization is not implemented yet")
3✔
73
        }
74
        if sys, ok := sys.(UISystem); ok {
35✔
75
                panic(fmt.Sprintf("System %T is also an UI system. Must be added via AddSystem.", sys))
3✔
76
        }
77
        s.systems = append(s.systems, sys)
29✔
78
}
79

80
// AddUISystem adds an [UISystem] to the model.
81
//
82
// Adds the [UISystem] also as a normal [System] if it implements the interface.
83
func (s *Systems) AddUISystem(sys UISystem) {
13✔
84
        if s.initialized {
16✔
85
                panic("adding systems after model initialization is not implemented yet")
3✔
86
        }
87
        s.uiSystems = append(s.uiSystems, sys)
10✔
88
        if sys, ok := sys.(System); ok {
13✔
89
                s.systems = append(s.systems, sys)
3✔
90
        }
3✔
91
}
92

93
// RemoveSystem removes a system from the model.
94
//
95
// Systems can also be removed during a model run.
96
// However, this will take effect only after the end of the full model step.
97
func (s *Systems) RemoveSystem(sys System) {
10✔
98
        if sys, ok := sys.(UISystem); ok {
13✔
99
                panic(fmt.Sprintf("System %T is also an UI system. Must be removed via RemoveUISystem.", sys))
3✔
100
        }
101
        s.toRemove = append(s.toRemove, sys)
7✔
102
        if !s.locked {
11✔
103
                s.removeSystems()
4✔
104
        }
4✔
105
}
106

107
// RemoveUISystem removes an UI system from the model.
108
//
109
// Systems can also be removed during a model run.
110
// However, this will take effect only after the end of the full model step.
111
func (s *Systems) RemoveUISystem(sys UISystem) {
12✔
112
        s.uiToRemove = append(s.uiToRemove, sys)
12✔
113
        if !s.locked {
21✔
114
                s.removeSystems()
9✔
115
        }
9✔
116
}
117

118
// Removes systems that were removed during the model step.
119
func (s *Systems) removeSystems() {
1,358✔
120
        rem := s.toRemove
1,358✔
121
        remUI := s.uiToRemove
1,358✔
122

1,358✔
123
        s.toRemove = s.toRemove[:0]
1,358✔
124
        s.uiToRemove = s.uiToRemove[:0]
1,358✔
125

1,358✔
126
        for _, sys := range rem {
1,365✔
127
                s.removeSystem(sys)
7✔
128
        }
7✔
129
        for _, sys := range remUI {
1,367✔
130
                if sys, ok := sys.(System); ok {
18✔
131
                        s.removeSystem(sys)
6✔
132
                }
6✔
133
                s.removeUISystem(sys)
9✔
134
        }
135
}
136

137
func (s *Systems) removeSystem(sys System) {
16✔
138
        if s.locked {
19✔
139
                panic("can't remove a system in locked state")
3✔
140
        }
141
        idx := -1
13✔
142
        for i := 0; i < len(s.systems); i++ {
35✔
143
                if sys == s.systems[i] {
29✔
144
                        idx = i
7✔
145
                        break
7✔
146
                }
147
        }
148
        if idx < 0 {
19✔
149
                panic(fmt.Sprintf("can't remove system %T: not in the model", sys))
6✔
150
        }
151
        s.systems[idx].Finalize(s.world)
7✔
152
        s.systems = append(s.systems[:idx], s.systems[idx+1:]...)
7✔
153
}
154

155
func (s *Systems) removeUISystem(sys UISystem) {
12✔
156
        if s.locked {
15✔
157
                panic("can't remove a system in locked state")
3✔
158
        }
159
        idx := -1
9✔
160
        for i := 0; i < len(s.uiSystems); i++ {
15✔
161
                if sys == s.uiSystems[i] {
12✔
162
                        idx = i
6✔
163
                        break
6✔
164
                }
165
        }
166
        if idx < 0 {
12✔
167
                panic(fmt.Sprintf("can't remove UI system %T: not in the model", sys))
3✔
168
        }
169
        s.uiSystems[idx].FinalizeUI(s.world)
6✔
170
        s.uiSystems = append(s.uiSystems[:idx], s.uiSystems[idx+1:]...)
6✔
171
}
172

173
// Initialize all systems.
174
func (s *Systems) initialize() {
25✔
175
        if s.initialized {
28✔
176
                panic("model is already initialized")
3✔
177
        }
178

179
        if s.FPS == 0 {
23✔
180
                s.FPS = 30
1✔
181
        }
1✔
182

183
        s.tickRes = generic.NewResource[resource.Tick](s.world)
22✔
184
        s.termRes = generic.NewResource[resource.Termination](s.world)
22✔
185

22✔
186
        s.locked = true
22✔
187
        for _, sys := range s.systems {
53✔
188
                sys.Initialize(s.world)
31✔
189
        }
31✔
190
        for _, sys := range s.uiSystems {
32✔
191
                sys.InitializeUI(s.world)
10✔
192
        }
10✔
193
        s.locked = false
22✔
194
        s.removeSystems()
22✔
195
        s.initialized = true
22✔
196

22✔
197
        s.nextDraw = time.Time{}
22✔
198
        s.nextUpdate = time.Time{}
22✔
199
}
200

201
// Update all systems.
202
func (s *Systems) update() {
1,301✔
203
        s.locked = true
1,301✔
204
        update := s.updateSystems()
1,301✔
205
        s.updateUISystems(update)
1,301✔
206
        s.locked = false
1,301✔
207

1,301✔
208
        s.removeSystems()
1,301✔
209

1,301✔
210
        if update {
2,581✔
211
                time := s.tickRes.Get()
1,280✔
212
                time.Tick++
1,280✔
213
        } else {
1,301✔
214
                s.wait()
21✔
215
        }
21✔
216
}
217

218
// Calculates and waits the time until the next update of UI update.
219
func (s *Systems) wait() {
21✔
220
        nextUpdate := s.nextUpdate
21✔
221

21✔
222
        if (s.Paused || s.FPS > 0) && s.nextDraw.Before(nextUpdate) {
33✔
223
                nextUpdate = s.nextDraw
12✔
224
        }
12✔
225

226
        t := time.Now()
21✔
227
        wait := nextUpdate.Sub(t)
21✔
228

21✔
229
        if wait > 0 {
42✔
230
                time.Sleep(wait)
21✔
231
        }
21✔
232
}
233

234
// Update normal systems.
235
func (s *Systems) updateSystems() bool {
1,301✔
236
        if s.Paused {
1,301✔
237
                return false
×
238
        }
×
239
        update := false
1,301✔
240
        if s.TPS <= 0 {
2,566✔
241
                update = true
1,265✔
242
                for _, sys := range s.systems {
2,602✔
243
                        sys.Update(s.world)
1,337✔
244
                }
1,337✔
245
        } else {
36✔
246
                update = !time.Now().Before(s.nextUpdate)
36✔
247
                if update {
51✔
248
                        s.nextUpdate = nextTime(s.nextUpdate, s.TPS)
15✔
249
                        for _, sys := range s.systems {
30✔
250
                                sys.Update(s.world)
15✔
251
                        }
15✔
252
                }
253
        }
254
        return update
1,301✔
255
}
256

257
// Update UI systems.
258
func (s *Systems) updateUISystems(updated bool) {
1,301✔
259
        if len(s.uiSystems) > 0 {
1,372✔
260
                if !s.Paused && s.FPS <= 0 {
79✔
261
                        if updated {
13✔
262
                                for _, sys := range s.uiSystems {
10✔
263
                                        sys.UpdateUI(s.world)
5✔
264
                                }
5✔
265
                                for _, sys := range s.uiSystems {
10✔
266
                                        sys.PostUpdateUI(s.world)
5✔
267
                                }
5✔
268
                        }
269
                } else {
63✔
270
                        if !time.Now().Before(s.nextDraw) {
93✔
271
                                fps := s.FPS
30✔
272
                                if s.Paused {
30✔
273
                                        fps = 30
×
274
                                }
×
275
                                s.nextDraw = nextTime(s.nextDraw, fps)
30✔
276
                                for _, sys := range s.uiSystems {
66✔
277
                                        sys.UpdateUI(s.world)
36✔
278
                                }
36✔
279
                                for _, sys := range s.uiSystems {
66✔
280
                                        sys.PostUpdateUI(s.world)
36✔
281
                                }
36✔
282
                        }
283
                }
284
        }
285
}
286

287
// Finalize all systems.
288
func (s *Systems) finalize() {
22✔
289
        s.locked = true
22✔
290
        for _, sys := range s.systems {
50✔
291
                sys.Finalize(s.world)
28✔
292
        }
28✔
293
        for _, sys := range s.uiSystems {
29✔
294
                sys.FinalizeUI(s.world)
7✔
295
        }
7✔
296
        s.locked = false
22✔
297
        s.removeSystems()
22✔
298
}
299

300
// Run the model.
301
func (s *Systems) run() {
22✔
302
        if !s.initialized {
44✔
303
                s.initialize()
22✔
304
        }
22✔
305

306
        time := s.tickRes.Get()
22✔
307
        time.Tick = 0
22✔
308
        terminate := s.termRes.Get()
22✔
309

22✔
310
        for !terminate.Terminate {
1,323✔
311
                s.update()
1,301✔
312
        }
1,301✔
313

314
        s.finalize()
22✔
315
}
316

317
// Removes all systems.
318
func (s *Systems) reset() {
16✔
319
        s.systems = []System{}
16✔
320
        s.uiSystems = []UISystem{}
16✔
321
        s.toRemove = s.toRemove[:0]
16✔
322
        s.uiToRemove = s.uiToRemove[:0]
16✔
323

16✔
324
        s.nextDraw = time.Time{}
16✔
325
        s.nextUpdate = time.Time{}
16✔
326

16✔
327
        s.initialized = false
16✔
328
        s.tickRes = generic.Resource[resource.Tick]{}
16✔
329
}
16✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc