jl-ecs/component.go
2023-08-04 11:02:08 +08:00

48 lines
1.3 KiB
Go

package ecs
import (
"github.com/yohamta/donburi"
)
type ComponentType[T any] struct {
*donburi.ComponentType[T]
}
// Get returns component data from the entry.
func (c *ComponentType[T]) Get(entry *Entry) *T {
return c.ComponentType.Get(entry.Entry)
}
// Set sets component data to the entry.
func (c *ComponentType[T]) Set(entry *Entry, component *T) {
c.ComponentType.Set(entry.Entry, component)
}
// // Each iterates over the entities that have the component.
// func (c *ComponentType[T]) Each(w World, callback func(*Entry)) {
// c.ComponentType.Each(w.(*world).world, func(entry *donburi.Entry) {
// callback(&Entry{Entry: entry})
// })
// }
// // First returns the first entity that has the component.
// func (c *ComponentType[T]) First(w World) (*Entry, bool) {
// entry, ok := c.ComponentType.First(w.(*world).world)
// return &Entry{entry}, ok
// }
// // MustFirst returns the first entity that has the component or panics.
// func (c *ComponentType[T]) MustFirst(w World) *Entry {
// e, ok := c.First(w)
// if !ok {
// panic(fmt.Sprintf("no entity has the component %s", c.ComponentType.Name()))
// }
// return e
// }
// SetValue sets the value of the component.
func (c *ComponentType[T]) SetValue(entry *Entry, value T) {
c.ComponentType.SetValue(entry.Entry, value)
}