forked from x-hgg-x/goecsengine
/
loader.go
42 lines (34 loc) · 1.13 KB
/
loader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"github.com/ghtalpo/goecsengine/loader"
"github.com/ghtalpo/goecsengine/utils"
w "github.com/ghtalpo/goecsengine/world"
"github.com/pelletier/go-toml"
ecs "github.com/x-hgg-x/goecs/v2"
)
type gameComponentList struct {
Gopher *Gopher
Sticky *Sticky
}
type entity struct {
Components gameComponentList
}
type entityGameMetadata struct {
Entities []entity `toml:"entity"`
}
func loadGameComponents(entityMetadataPath string, world w.World) []interface{} {
var entityGameMetadata entityGameMetadata
tree, err := toml.LoadFile(entityMetadataPath)
utils.LogError(err)
utils.LogError(tree.Unmarshal(&entityGameMetadata))
gameComponentList := make([]interface{}, len(entityGameMetadata.Entities))
for iEntity, entity := range entityGameMetadata.Entities {
gameComponentList[iEntity] = entity.Components
}
return gameComponentList
}
// LoadEntities creates entities with components from a TOML file
func LoadEntities(entityMetadataPath string, world w.World) []ecs.Entity {
gameComponentList := loadGameComponents(entityMetadataPath, world)
return loader.LoadEntities(entityMetadataPath, world, gameComponentList)
}