From 33f1f5c317690dc87ab5cc6cb5124c6ca1592ef8 Mon Sep 17 00:00:00 2001 From: Martin Lange <44003176+mlange-42@users.noreply.github.com> Date: Thu, 16 May 2024 14:02:21 +0200 Subject: [PATCH] Add model initializer with custom systems (#53) --- model/default.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/model/default.go b/model/default.go index e677a14..a809ab4 100644 --- a/model/default.go +++ b/model/default.go @@ -66,3 +66,32 @@ func Default(p params.Params, m *model.Model) *model.Model { return m } + +// Default sets up a beecs model with the given systems instead of the default ones. +// +// If the argument m is nil, a new model instance is created. +// If it is non-nil, the model is reset and re-used, saving some time for initialization and memory allocation. +func WithSystems(p params.Params, sys []model.System, m *model.Model) *model.Model { + if m == nil { + m = model.New() + } else { + m.Reset() + } + + p.Apply(&m.World) + + factory := globals.NewForagerFactory(&m.World) + ecs.AddResource(&m.World, &factory) + + stats := globals.PopulationStats{} + ecs.AddResource(&m.World, &stats) + + consumptionStats := globals.ConsumptionStats{} + ecs.AddResource(&m.World, &consumptionStats) + + for _, s := range sys { + m.AddSystem(s) + } + + return m +}