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

mlange-42 / ark-tools / 13609263776

01 Mar 2025 10:08PM CUT coverage: 100.0%. First build
13609263776

Pull #1

github

web-flow
Merge 737eee7f4 into 3e01f444a
Pull Request #1: Add App and required resources

343 of 343 new or added lines in 7 files covered. (100.0%)

343 of 343 relevant lines covered (100.0%)

219.19 hits per line

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

100.0
/app/app.go
1
package app
2

3
import (
4
        "math/rand/v2"
5
        "time"
6

7
        "github.com/mlange-42/ark-tools/resource"
8
        "github.com/mlange-42/ark/ecs"
9
)
10

11
// App is the top-level ECS entrypoint.
12
//
13
// App 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 app's [resource.Tick], [resource.Termination]
20
// and a central [resource.Rand] PRNG source can be accessed by systems as resources.
21
type App 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 app.
30
//
31
// See [ecs.NewWorld] for the arguments.
32
func New(initialCapacity uint32) *App {
12✔
33
        var app = App{
12✔
34
                World: ecs.NewWorld(initialCapacity),
12✔
35
        }
12✔
36
        app.FPS = 30
12✔
37
        app.TPS = 0
12✔
38
        app.Systems.world = &app.World
12✔
39

12✔
40
        app.rand = resource.Rand{
12✔
41
                Source: rand.NewPCG(0, uint64(time.Now().UnixNano())),
12✔
42
        }
12✔
43
        ecs.AddResource(&app.World, &app.rand)
12✔
44
        app.time = resource.Tick{}
12✔
45
        ecs.AddResource(&app.World, &app.time)
12✔
46
        app.terminate = resource.Termination{}
12✔
47
        ecs.AddResource(&app.World, &app.terminate)
12✔
48

12✔
49
        ecs.AddResource(&app.World, &app.Systems)
12✔
50

12✔
51
        return &app
12✔
52
}
12✔
53

54
// Seed sets the random seed of the app's [resource.Rand].
55
// Call without an argument to seed from the current time.
56
//
57
// Systems should always use the Rand resource for PRNGs.
58
func (app *App) Seed(seed ...uint64) *App {
28✔
59
        switch len(seed) {
28✔
60
        case 0:
4✔
61
                app.rand.Source = rand.NewPCG(0, uint64(time.Now().UnixNano()))
4✔
62
        case 1:
23✔
63
                app.rand.Source = rand.NewPCG(0, seed[0])
23✔
64
        default:
1✔
65
                panic("can only use a single random seed")
1✔
66
        }
67
        return app
27✔
68
}
69

70
// Run the app, updating systems and ui systems according to App.TPS and App.FPS, respectively.
71
// Initializes the app if it is not already initialized.
72
// Finalizes the app after the run.
73
//
74
// Runs until Terminate in the resource resource.Termination is set to true
75
// (see [resource.Termination]).
76
//
77
// To perform updates manually, see [App.Update] and [App.UpdateUI],
78
// as well as [App.Initialize] and [App.Finalize].
79
func (app *App) Run() {
22✔
80
        app.Systems.run()
22✔
81
}
22✔
82

83
// Initialize the app.
84
func (app *App) Initialize() {
4✔
85
        app.Systems.initialize()
4✔
86
}
4✔
87

88
// Update the app's systems.
89
// Return whether the run should continue.
90
//
91
// Ignores App.TPS.
92
//
93
// Panics if [App.Initialize] was not called.
94
func (app *App) Update() bool {
136✔
95
        return app.Systems.updateSystems()
136✔
96
}
136✔
97

98
// UpdateUI the app's UI systems.
99
//
100
// Ignores App.FPS.
101
//
102
// Panics if [App.Initialize] was not called.
103
func (app *App) UpdateUI() {
129✔
104
        app.Systems.updateUISystems()
129✔
105
}
129✔
106

107
// Finalize the app.
108
func (app *App) Finalize() {
4✔
109
        app.Systems.finalize()
4✔
110
}
4✔
111

112
// Reset resets the world and removes all systems.
113
//
114
// Can be used to run systematic simulations without the need to re-allocate memory for each run.
115
// Accelerates re-populating the world by a factor of 2-3.
116
func (app *App) Reset() {
19✔
117
        app.World.Reset()
19✔
118
        app.Systems.reset()
19✔
119

19✔
120
        app.rand = resource.Rand{
19✔
121
                Source: rand.NewPCG(0, uint64(time.Now().UnixNano())),
19✔
122
        }
19✔
123
        ecs.AddResource(&app.World, &app.rand)
19✔
124
        app.time = resource.Tick{}
19✔
125
        ecs.AddResource(&app.World, &app.time)
19✔
126
        app.terminate = resource.Termination{}
19✔
127
        ecs.AddResource(&app.World, &app.terminate)
19✔
128

19✔
129
        ecs.AddResource(&app.World, &app.Systems)
19✔
130
}
19✔
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