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

mlange-42 / arche-model / 5114593779

29 May 2023 06:56PM CUT coverage: 97.455%. Remained the same
5114593779

push

github

web-flow
Upgrade to Arche v0.8 (#46)

536 of 550 relevant lines covered (97.45%)

106.64 hits per line

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

97.93
/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
// Systems returns the normal/non-UI systems.
67
func (s *Systems) Systems() []System {
3✔
68
        return s.systems
3✔
69
}
3✔
70

71
// UISystems returns the UI systems.
72
func (s *Systems) UISystems() []UISystem {
3✔
73
        return s.uiSystems
3✔
74
}
3✔
75

76
// AddSystem adds a [System] to the model.
77
//
78
// Panics if the system is also a [UISystem].
79
// To add systems that implement both [System] and [UISystem], use [Systems.AddUISystem]
80
func (s *Systems) AddSystem(sys System) {
35✔
81
        if s.initialized {
38✔
82
                panic("adding systems after model initialization is not implemented yet")
3✔
83
        }
84
        if sys, ok := sys.(UISystem); ok {
35✔
85
                panic(fmt.Sprintf("System %T is also an UI system. Must be added via AddSystem.", sys))
3✔
86
        }
87
        s.systems = append(s.systems, sys)
29✔
88
}
89

90
// AddUISystem adds an [UISystem] to the model.
91
//
92
// Adds the [UISystem] also as a normal [System] if it implements the interface.
93
func (s *Systems) AddUISystem(sys UISystem) {
13✔
94
        if s.initialized {
16✔
95
                panic("adding systems after model initialization is not implemented yet")
3✔
96
        }
97
        s.uiSystems = append(s.uiSystems, sys)
10✔
98
        if sys, ok := sys.(System); ok {
13✔
99
                s.systems = append(s.systems, sys)
3✔
100
        }
3✔
101
}
102

103
// RemoveSystem removes a system from the model.
104
//
105
// Systems can also be removed during a model run.
106
// However, this will take effect only after the end of the full model step.
107
func (s *Systems) RemoveSystem(sys System) {
10✔
108
        if sys, ok := sys.(UISystem); ok {
13✔
109
                panic(fmt.Sprintf("System %T is also an UI system. Must be removed via RemoveUISystem.", sys))
3✔
110
        }
111
        s.toRemove = append(s.toRemove, sys)
7✔
112
        if !s.locked {
11✔
113
                s.removeSystems()
4✔
114
        }
4✔
115
}
116

117
// RemoveUISystem removes an UI system from the model.
118
//
119
// Systems can also be removed during a model run.
120
// However, this will take effect only after the end of the full model step.
121
func (s *Systems) RemoveUISystem(sys UISystem) {
12✔
122
        s.uiToRemove = append(s.uiToRemove, sys)
12✔
123
        if !s.locked {
21✔
124
                s.removeSystems()
9✔
125
        }
9✔
126
}
127

128
// Removes systems that were removed during the model step.
129
func (s *Systems) removeSystems() {
1,358✔
130
        rem := s.toRemove
1,358✔
131
        remUI := s.uiToRemove
1,358✔
132

1,358✔
133
        s.toRemove = s.toRemove[:0]
1,358✔
134
        s.uiToRemove = s.uiToRemove[:0]
1,358✔
135

1,358✔
136
        for _, sys := range rem {
1,365✔
137
                s.removeSystem(sys)
7✔
138
        }
7✔
139
        for _, sys := range remUI {
1,367✔
140
                if sys, ok := sys.(System); ok {
18✔
141
                        s.removeSystem(sys)
6✔
142
                }
6✔
143
                s.removeUISystem(sys)
9✔
144
        }
145
}
146

147
func (s *Systems) removeSystem(sys System) {
16✔
148
        if s.locked {
19✔
149
                panic("can't remove a system in locked state")
3✔
150
        }
151
        idx := -1
13✔
152
        for i := 0; i < len(s.systems); i++ {
35✔
153
                if sys == s.systems[i] {
29✔
154
                        idx = i
7✔
155
                        break
7✔
156
                }
157
        }
158
        if idx < 0 {
19✔
159
                panic(fmt.Sprintf("can't remove system %T: not in the model", sys))
6✔
160
        }
161
        s.systems[idx].Finalize(s.world)
7✔
162
        s.systems = append(s.systems[:idx], s.systems[idx+1:]...)
7✔
163
}
164

165
func (s *Systems) removeUISystem(sys UISystem) {
12✔
166
        if s.locked {
15✔
167
                panic("can't remove a system in locked state")
3✔
168
        }
169
        idx := -1
9✔
170
        for i := 0; i < len(s.uiSystems); i++ {
15✔
171
                if sys == s.uiSystems[i] {
12✔
172
                        idx = i
6✔
173
                        break
6✔
174
                }
175
        }
176
        if idx < 0 {
12✔
177
                panic(fmt.Sprintf("can't remove UI system %T: not in the model", sys))
3✔
178
        }
179
        s.uiSystems[idx].FinalizeUI(s.world)
6✔
180
        s.uiSystems = append(s.uiSystems[:idx], s.uiSystems[idx+1:]...)
6✔
181
}
182

183
// Initialize all systems.
184
func (s *Systems) initialize() {
25✔
185
        if s.initialized {
28✔
186
                panic("model is already initialized")
3✔
187
        }
188

189
        if s.FPS == 0 {
23✔
190
                s.FPS = 30
1✔
191
        }
1✔
192

193
        s.tickRes = generic.NewResource[resource.Tick](s.world)
22✔
194
        s.termRes = generic.NewResource[resource.Termination](s.world)
22✔
195

22✔
196
        s.locked = true
22✔
197
        for _, sys := range s.systems {
53✔
198
                sys.Initialize(s.world)
31✔
199
        }
31✔
200
        for _, sys := range s.uiSystems {
32✔
201
                sys.InitializeUI(s.world)
10✔
202
        }
10✔
203
        s.locked = false
22✔
204
        s.removeSystems()
22✔
205
        s.initialized = true
22✔
206

22✔
207
        s.nextDraw = time.Time{}
22✔
208
        s.nextUpdate = time.Time{}
22✔
209
}
210

211
// Update all systems.
212
func (s *Systems) update() {
1,301✔
213
        s.locked = true
1,301✔
214
        update := s.updateSystems()
1,301✔
215
        s.updateUISystems(update)
1,301✔
216
        s.locked = false
1,301✔
217

1,301✔
218
        s.removeSystems()
1,301✔
219

1,301✔
220
        if update {
2,581✔
221
                time := s.tickRes.Get()
1,280✔
222
                time.Tick++
1,280✔
223
        } else {
1,301✔
224
                s.wait()
21✔
225
        }
21✔
226
}
227

228
// Calculates and waits the time until the next update of UI update.
229
func (s *Systems) wait() {
21✔
230
        nextUpdate := s.nextUpdate
21✔
231

21✔
232
        if (s.Paused || s.FPS > 0) && s.nextDraw.Before(nextUpdate) {
33✔
233
                nextUpdate = s.nextDraw
12✔
234
        }
12✔
235

236
        t := time.Now()
21✔
237
        wait := nextUpdate.Sub(t)
21✔
238

21✔
239
        if wait > 0 {
42✔
240
                time.Sleep(wait)
21✔
241
        }
21✔
242
}
243

244
// Update normal systems.
245
func (s *Systems) updateSystems() bool {
1,301✔
246
        if s.Paused {
1,301✔
247
                return false
×
248
        }
×
249
        update := false
1,301✔
250
        if s.TPS <= 0 {
2,566✔
251
                update = true
1,265✔
252
                for _, sys := range s.systems {
2,602✔
253
                        sys.Update(s.world)
1,337✔
254
                }
1,337✔
255
        } else {
36✔
256
                update = !time.Now().Before(s.nextUpdate)
36✔
257
                if update {
51✔
258
                        s.nextUpdate = nextTime(s.nextUpdate, s.TPS)
15✔
259
                        for _, sys := range s.systems {
30✔
260
                                sys.Update(s.world)
15✔
261
                        }
15✔
262
                }
263
        }
264
        return update
1,301✔
265
}
266

267
// Update UI systems.
268
func (s *Systems) updateUISystems(updated bool) {
1,301✔
269
        if len(s.uiSystems) > 0 {
1,372✔
270
                if !s.Paused && s.FPS <= 0 {
79✔
271
                        if updated {
13✔
272
                                for _, sys := range s.uiSystems {
10✔
273
                                        sys.UpdateUI(s.world)
5✔
274
                                }
5✔
275
                                for _, sys := range s.uiSystems {
10✔
276
                                        sys.PostUpdateUI(s.world)
5✔
277
                                }
5✔
278
                        }
279
                } else {
63✔
280
                        if !time.Now().Before(s.nextDraw) {
93✔
281
                                fps := s.FPS
30✔
282
                                if s.Paused {
30✔
283
                                        fps = 30
×
284
                                }
×
285
                                s.nextDraw = nextTime(s.nextDraw, fps)
30✔
286
                                for _, sys := range s.uiSystems {
66✔
287
                                        sys.UpdateUI(s.world)
36✔
288
                                }
36✔
289
                                for _, sys := range s.uiSystems {
66✔
290
                                        sys.PostUpdateUI(s.world)
36✔
291
                                }
36✔
292
                        }
293
                }
294
        }
295
}
296

297
// Finalize all systems.
298
func (s *Systems) finalize() {
22✔
299
        s.locked = true
22✔
300
        for _, sys := range s.systems {
50✔
301
                sys.Finalize(s.world)
28✔
302
        }
28✔
303
        for _, sys := range s.uiSystems {
29✔
304
                sys.FinalizeUI(s.world)
7✔
305
        }
7✔
306
        s.locked = false
22✔
307
        s.removeSystems()
22✔
308
}
309

310
// Run the model.
311
func (s *Systems) run() {
22✔
312
        if !s.initialized {
44✔
313
                s.initialize()
22✔
314
        }
22✔
315

316
        time := s.tickRes.Get()
22✔
317
        time.Tick = 0
22✔
318
        terminate := s.termRes.Get()
22✔
319

22✔
320
        for !terminate.Terminate {
1,323✔
321
                s.update()
1,301✔
322
        }
1,301✔
323

324
        s.finalize()
22✔
325
}
326

327
// Removes all systems.
328
func (s *Systems) reset() {
16✔
329
        s.systems = []System{}
16✔
330
        s.uiSystems = []UISystem{}
16✔
331
        s.toRemove = s.toRemove[:0]
16✔
332
        s.uiToRemove = s.uiToRemove[:0]
16✔
333

16✔
334
        s.nextDraw = time.Time{}
16✔
335
        s.nextUpdate = time.Time{}
16✔
336

16✔
337
        s.initialized = false
16✔
338
        s.tickRes = generic.Resource[resource.Tick]{}
16✔
339
}
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