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

mlange-42 / arche-model / 4731229459

18 Apr 2023 10:20AM CUT coverage: 96.368% (+0.2%) from 96.144%
4731229459

push

github

GitHub
Single grid coords & MatrixToGrid observer (#38)

24 of 24 new or added lines in 1 file covered. (100.0%)

398 of 413 relevant lines covered (96.37%)

127.65 hits per line

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

97.27
/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
        for _, sys := range s.toRemove {
1,368✔
121
                s.removeSystem(sys)
10✔
122
        }
10✔
123
        for _, sys := range s.uiToRemove {
1,361✔
124
                if sys, ok := sys.(System); ok {
15✔
125
                        s.removeSystem(sys)
6✔
126
                }
6✔
127
                s.removeUISystem(sys)
6✔
128
        }
129
        s.toRemove = s.toRemove[:0]
1,349✔
130
        s.uiToRemove = s.uiToRemove[:0]
1,349✔
131
}
132

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

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

169
// Initialize all systems.
170
func (s *Systems) initialize() {
25✔
171
        if s.initialized {
28✔
172
                panic("model is already initialized")
3✔
173
        }
174

175
        if s.FPS == 0 {
23✔
176
                s.FPS = 30
1✔
177
        }
1✔
178

179
        s.tickRes = generic.NewResource[resource.Tick](s.world)
22✔
180
        s.termRes = generic.NewResource[resource.Termination](s.world)
22✔
181

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

22✔
193
        s.nextDraw = time.Time{}
22✔
194
        s.nextUpdate = time.Time{}
22✔
195
}
196

197
// Update all systems.
198
func (s *Systems) update() {
1,301✔
199
        s.locked = true
1,301✔
200
        update := s.updateSystems()
1,301✔
201
        s.updateUISystems(update)
1,301✔
202
        s.locked = false
1,301✔
203

1,301✔
204
        s.removeSystems()
1,301✔
205

1,301✔
206
        if update {
2,581✔
207
                time := s.tickRes.Get()
1,280✔
208
                time.Tick++
1,280✔
209
        } else {
1,301✔
210
                s.wait()
21✔
211
        }
21✔
212
}
213

214
// Calculates and waits the time until the next update of UI update.
215
func (s *Systems) wait() {
21✔
216
        nextUpdate := s.nextUpdate
21✔
217

21✔
218
        if (s.Paused || s.FPS > 0) && s.nextDraw.Before(nextUpdate) {
33✔
219
                nextUpdate = s.nextDraw
12✔
220
        }
12✔
221

222
        t := time.Now()
21✔
223
        wait := nextUpdate.Sub(t)
21✔
224

21✔
225
        if wait > 0 {
42✔
226
                time.Sleep(wait)
21✔
227
        }
21✔
228
}
229

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

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

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

296
// Run the model.
297
func (s *Systems) run() {
22✔
298
        if !s.initialized {
44✔
299
                s.initialize()
22✔
300
        }
22✔
301

302
        time := s.tickRes.Get()
22✔
303
        time.Tick = 0
22✔
304
        terminate := s.termRes.Get()
22✔
305

22✔
306
        for !terminate.Terminate {
1,323✔
307
                s.update()
1,301✔
308
        }
1,301✔
309

310
        s.finalize()
22✔
311
}
312

313
// Removes all systems.
314
func (s *Systems) reset() {
16✔
315
        s.systems = []System{}
16✔
316
        s.uiSystems = []UISystem{}
16✔
317
        s.toRemove = []System{}
16✔
318
        s.uiToRemove = []UISystem{}
16✔
319

16✔
320
        s.nextDraw = time.Time{}
16✔
321
        s.nextUpdate = time.Time{}
16✔
322

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