diff --git a/component.go b/component.go index fe302a4..da121d1 100644 --- a/component.go +++ b/component.go @@ -1,6 +1,8 @@ package ecs import ( + "fmt" + "github.com/yohamta/donburi" ) @@ -18,28 +20,28 @@ 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}) -// }) -// } +// 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 -// } +// 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())) -// } +// 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 -// } + return e +} // SetValue sets the value of the component. func (c *ComponentType[T]) SetValue(entry *Entry, value T) { diff --git a/examples/rtss-event/main.go b/examples/rtss-event/main.go index 507c890..db621c5 100644 --- a/examples/rtss-event/main.go +++ b/examples/rtss-event/main.go @@ -23,6 +23,7 @@ func (me *SwitchSystem) WhenFireSwitchDcEvent(w ecs.World, event FireSwitchDcEve func (me *SwitchSystem) Update(w ecs.World) { me.count += 1 fmt.Println("time: ", time.Now().Format(time.StampMilli), "====>>>world-", w.Id(), " SwitchSystem update , count: ", me.count) + fireSwitchDcEventType.Publish(w, &FireSwitchDcEvent{Dc: true}) } // var switchSystem = &SwitchSystem{} @@ -34,8 +35,8 @@ func NewSwitchSystem() *SwitchSystem { } var ( - wd1 = ecs.NewWorld(500) - wd2 = ecs.NewWorld(500) + wd1 = ecs.NewWorld(20) + wd2 = ecs.NewWorld(20) ) // //////////////////////////////////////// @@ -43,7 +44,6 @@ func main() { go world1() go world2() time.Sleep(4 * time.Second) - fireSwitchDcEventType.Publish(wd1, &FireSwitchDcEvent{Dc: true}) fireSwitchDcEventType.Publish(wd2, &FireSwitchDcEvent{Dc: false}) } func world1() { @@ -57,7 +57,7 @@ func world1() { // time.Sleep(3 * time.Second) // - fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: true}) + fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: false}) time.Sleep(10 * time.Second) } @@ -71,7 +71,6 @@ func world2() { // time.Sleep(3 * time.Second) // - fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: true}) fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: false}) time.Sleep(10 * time.Second) } diff --git a/world.go b/world.go index f7b841a..8900437 100644 --- a/world.go +++ b/world.go @@ -72,13 +72,19 @@ type world struct { toBeExecuteds chan ExecuteFunc } +// 新建一个组件类型 func NewComponentType[T any](opts ...interface{}) *ComponentType[T] { ct := donburi.NewComponentType[T](opts...) return &ComponentType[T]{ct} } +// 新建一个标签 +func NewTag() *ComponentType[struct{}] { + return NewComponentType[struct{}]() +} + // 初始化一个新World -// tick 单位为ms,且必须大于0 +// tick 单位为ms,且必须大于0,(小于15ms的值在Windows系统中会达不到,Windows系统中系统中断好像默认是15.6ms,也就是一秒最多64次) func NewWorld(tick int) World { return &world{ world: donburi.NewWorld(), @@ -188,8 +194,8 @@ func (w *world) Close() { w.quit <- struct{}{} } -// 事件管理相关处理 -func (w *world) processManageEventFuncs() { +// 执行待处理方法 +func (w *world) executeTodos() { manageEventChan := w.toBeExecuteds for { select { @@ -200,9 +206,7 @@ func (w *world) processManageEventFuncs() { default: return } - } - } func (w *world) run() { for { @@ -232,7 +236,7 @@ func (w *world) run() { sys.Update(w) } // 处理事件管理相关 - w.processManageEventFuncs() + w.executeTodos() // 处理所有事件 processAllEvents(w) }