53 lines
1.5 KiB
Go
53 lines
1.5 KiB
Go
package ecs
|
||
|
||
import (
|
||
"fmt"
|
||
|
||
"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.
|
||
// 使用Each方法时必须在World线程中执行,即调用World.Execute执行
|
||
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.
|
||
// 使用First方法时必须在World线程中执行,即调用World.Execute执行
|
||
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.
|
||
// 使用MustFirst方法时必须在World线程中执行,即调用World.Execute执行
|
||
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)
|
||
}
|