yohamta / donburi

Just another ECS library for Go/Ebitengine
https://pkg.go.dev/github.com/yohamta/donburi
Other
232 stars 21 forks source link

Add archetype utility #97

Closed yohamta closed 1 year ago

yohamta commented 1 year ago

It helps to reduce repetitive code that was required for spawning entities with a particular set of components. Example:

package archetypes
// ...
-func NewPlayer(ecs *ecs.ECS) *donburi.Entry {
-       w := ecs.World
-
-       entry := w.Entry(ecs.Create(
-               layers.Default,
-               tags.Player,
-               components.Player,
-               components.Object,
-       ))
-
-       return entry
-}
+ var Player = ecs.NewArchetype(
+       tags.Player,
+       components.Player,
+       components.Object,
+   )

An entity can be spawned as below:

player := archetypes.Player.Spawn(ecs)

On spawning timing, additional components can be specified. Example:

player := archetypes.Player.Spawn(ecs, tags.Enemy)