• 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

100.0
/model/model.go
1
package model
2

3
import (
4
        "time"
5

6
        "github.com/mlange-42/arche-model/resource"
7
        "github.com/mlange-42/arche/ecs"
8
        "golang.org/x/exp/rand"
9
)
10

11
// Model is the top-level ECS entrypoint.
12
//
13
// Model provides access to the ECS world, and manages the scheduling
14
// of [System] and [UISystem] instances via [Systems].
15
// [System] instances are updated with a frequency given by TPS.
16
// [UISystem] instances are updated independently of normal systems,
17
// with a frequency given by FPS.
18
//
19
// The [Systems] scheduler, the model's [resource.Tick], [resource.Termination]
20
// and a central [resource.Rand] PRNG source can be accessed by systems as resources.
21
type Model struct {
22
        Systems             // Systems manager and scheduler
23
        World     ecs.World // The ECS world
24
        rand      resource.Rand
25
        time      resource.Tick
26
        terminate resource.Termination
27
}
28

29
// New creates a new model.
30
func New(config ...ecs.Config) *Model {
11✔
31
        var mod = Model{
11✔
32
                World: ecs.NewWorld(config...),
11✔
33
        }
11✔
34
        mod.FPS = 30
11✔
35
        mod.TPS = 0
11✔
36
        mod.Systems.world = &mod.World
11✔
37

11✔
38
        mod.rand = resource.Rand{
11✔
39
                Source: rand.NewSource(uint64(time.Now().UnixNano())),
11✔
40
        }
11✔
41
        ecs.AddResource(&mod.World, &mod.rand)
11✔
42
        mod.time = resource.Tick{}
11✔
43
        ecs.AddResource(&mod.World, &mod.time)
11✔
44
        mod.terminate = resource.Termination{}
11✔
45
        ecs.AddResource(&mod.World, &mod.terminate)
11✔
46

11✔
47
        ecs.AddResource(&mod.World, &mod.Systems)
11✔
48

11✔
49
        return &mod
11✔
50
}
11✔
51

52
// Seed sets the random seed of the model's [resource.Rand].
53
// Call without an argument to seed from the current time.
54
//
55
// Systems should always use the Rand resource for PRNGs.
56
func (m *Model) Seed(seed ...uint64) *Model {
25✔
57
        switch len(seed) {
25✔
58
        case 0:
4✔
59
                m.rand.Seed(uint64(time.Now().UnixNano()))
4✔
60
        case 1:
20✔
61
                m.rand.Seed(seed[0])
20✔
62
        default:
1✔
63
                panic("can only use a single random seed")
1✔
64
        }
65
        return m
24✔
66
}
67

68
// Run runs the model.
69
//
70
// Runs until Terminate in the resource resource.Termination is set to true
71
// (see [resource.Termination]).
72
func (m *Model) Run() {
22✔
73
        m.Systems.run()
22✔
74
}
22✔
75

76
// Reset resets the world and removes all systems.
77
//
78
// Can be used to run systematic simulations without the need to re-allocate memory for each run.
79
// Accelerates re-populating the world by a factor of 2-3.
80
func (m *Model) Reset() {
16✔
81
        m.World.Reset()
16✔
82
        m.Systems.reset()
16✔
83

16✔
84
        m.rand = resource.Rand{
16✔
85
                Source: rand.NewSource(uint64(time.Now().UnixNano())),
16✔
86
        }
16✔
87
        ecs.AddResource(&m.World, &m.rand)
16✔
88
        m.time = resource.Tick{}
16✔
89
        ecs.AddResource(&m.World, &m.time)
16✔
90
        m.terminate = resource.Termination{}
16✔
91
        ecs.AddResource(&m.World, &m.terminate)
16✔
92

16✔
93
        ecs.AddResource(&m.World, &m.Systems)
16✔
94
}
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