取消组件Each,First等方法的注释
添加NewTag函数
This commit is contained in:
parent
03669cd1b3
commit
2949aa52e1
40
component.go
40
component.go
@ -1,6 +1,8 @@
|
|||||||
package ecs
|
package ecs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/yohamta/donburi"
|
"github.com/yohamta/donburi"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -18,28 +20,28 @@ func (c *ComponentType[T]) Set(entry *Entry, component *T) {
|
|||||||
c.ComponentType.Set(entry.Entry, component)
|
c.ComponentType.Set(entry.Entry, component)
|
||||||
}
|
}
|
||||||
|
|
||||||
// // Each iterates over the entities that have the component.
|
// Each iterates over the entities that have the component.
|
||||||
// func (c *ComponentType[T]) Each(w World, callback func(*Entry)) {
|
func (c *ComponentType[T]) Each(w World, callback func(*Entry)) {
|
||||||
// c.ComponentType.Each(w.(*world).world, func(entry *donburi.Entry) {
|
c.ComponentType.Each(w.(*world).world, func(entry *donburi.Entry) {
|
||||||
// callback(&Entry{Entry: entry})
|
callback(&Entry{Entry: entry})
|
||||||
// })
|
})
|
||||||
// }
|
}
|
||||||
|
|
||||||
// // First returns the first entity that has the component.
|
// First returns the first entity that has the component.
|
||||||
// func (c *ComponentType[T]) First(w World) (*Entry, bool) {
|
func (c *ComponentType[T]) First(w World) (*Entry, bool) {
|
||||||
// entry, ok := c.ComponentType.First(w.(*world).world)
|
entry, ok := c.ComponentType.First(w.(*world).world)
|
||||||
// return &Entry{entry}, ok
|
return &Entry{entry}, ok
|
||||||
// }
|
}
|
||||||
|
|
||||||
// // MustFirst returns the first entity that has the component or panics.
|
// MustFirst returns the first entity that has the component or panics.
|
||||||
// func (c *ComponentType[T]) MustFirst(w World) *Entry {
|
func (c *ComponentType[T]) MustFirst(w World) *Entry {
|
||||||
// e, ok := c.First(w)
|
e, ok := c.First(w)
|
||||||
// if !ok {
|
if !ok {
|
||||||
// panic(fmt.Sprintf("no entity has the component %s", c.ComponentType.Name()))
|
panic(fmt.Sprintf("no entity has the component %s", c.ComponentType.Name()))
|
||||||
// }
|
}
|
||||||
|
|
||||||
// return e
|
return e
|
||||||
// }
|
}
|
||||||
|
|
||||||
// SetValue sets the value of the component.
|
// SetValue sets the value of the component.
|
||||||
func (c *ComponentType[T]) SetValue(entry *Entry, value T) {
|
func (c *ComponentType[T]) SetValue(entry *Entry, value T) {
|
||||||
|
@ -23,6 +23,7 @@ func (me *SwitchSystem) WhenFireSwitchDcEvent(w ecs.World, event FireSwitchDcEve
|
|||||||
func (me *SwitchSystem) Update(w ecs.World) {
|
func (me *SwitchSystem) Update(w ecs.World) {
|
||||||
me.count += 1
|
me.count += 1
|
||||||
fmt.Println("time: ", time.Now().Format(time.StampMilli), "====>>>world-", w.Id(), " SwitchSystem update , count: ", me.count)
|
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{}
|
// var switchSystem = &SwitchSystem{}
|
||||||
@ -34,8 +35,8 @@ func NewSwitchSystem() *SwitchSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
wd1 = ecs.NewWorld(500)
|
wd1 = ecs.NewWorld(20)
|
||||||
wd2 = ecs.NewWorld(500)
|
wd2 = ecs.NewWorld(20)
|
||||||
)
|
)
|
||||||
|
|
||||||
// ////////////////////////////////////////
|
// ////////////////////////////////////////
|
||||||
@ -43,7 +44,6 @@ func main() {
|
|||||||
go world1()
|
go world1()
|
||||||
go world2()
|
go world2()
|
||||||
time.Sleep(4 * time.Second)
|
time.Sleep(4 * time.Second)
|
||||||
fireSwitchDcEventType.Publish(wd1, &FireSwitchDcEvent{Dc: true})
|
|
||||||
fireSwitchDcEventType.Publish(wd2, &FireSwitchDcEvent{Dc: false})
|
fireSwitchDcEventType.Publish(wd2, &FireSwitchDcEvent{Dc: false})
|
||||||
}
|
}
|
||||||
func world1() {
|
func world1() {
|
||||||
@ -57,7 +57,7 @@ func world1() {
|
|||||||
//
|
//
|
||||||
time.Sleep(3 * time.Second)
|
time.Sleep(3 * time.Second)
|
||||||
//
|
//
|
||||||
fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: true})
|
|
||||||
fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: false})
|
fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: false})
|
||||||
time.Sleep(10 * time.Second)
|
time.Sleep(10 * time.Second)
|
||||||
}
|
}
|
||||||
@ -71,7 +71,6 @@ func world2() {
|
|||||||
//
|
//
|
||||||
time.Sleep(3 * time.Second)
|
time.Sleep(3 * time.Second)
|
||||||
//
|
//
|
||||||
fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: true})
|
|
||||||
fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: false})
|
fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: false})
|
||||||
time.Sleep(10 * time.Second)
|
time.Sleep(10 * time.Second)
|
||||||
}
|
}
|
||||||
|
16
world.go
16
world.go
@ -72,13 +72,19 @@ type world struct {
|
|||||||
toBeExecuteds chan ExecuteFunc
|
toBeExecuteds chan ExecuteFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 新建一个组件类型
|
||||||
func NewComponentType[T any](opts ...interface{}) *ComponentType[T] {
|
func NewComponentType[T any](opts ...interface{}) *ComponentType[T] {
|
||||||
ct := donburi.NewComponentType[T](opts...)
|
ct := donburi.NewComponentType[T](opts...)
|
||||||
return &ComponentType[T]{ct}
|
return &ComponentType[T]{ct}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 新建一个标签
|
||||||
|
func NewTag() *ComponentType[struct{}] {
|
||||||
|
return NewComponentType[struct{}]()
|
||||||
|
}
|
||||||
|
|
||||||
// 初始化一个新World
|
// 初始化一个新World
|
||||||
// tick 单位为ms,且必须大于0
|
// tick 单位为ms,且必须大于0,(小于15ms的值在Windows系统中会达不到,Windows系统中系统中断好像默认是15.6ms,也就是一秒最多64次)
|
||||||
func NewWorld(tick int) World {
|
func NewWorld(tick int) World {
|
||||||
return &world{
|
return &world{
|
||||||
world: donburi.NewWorld(),
|
world: donburi.NewWorld(),
|
||||||
@ -188,8 +194,8 @@ func (w *world) Close() {
|
|||||||
w.quit <- struct{}{}
|
w.quit <- struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 事件管理相关处理
|
// 执行待处理方法
|
||||||
func (w *world) processManageEventFuncs() {
|
func (w *world) executeTodos() {
|
||||||
manageEventChan := w.toBeExecuteds
|
manageEventChan := w.toBeExecuteds
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@ -200,9 +206,7 @@ func (w *world) processManageEventFuncs() {
|
|||||||
default:
|
default:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
func (w *world) run() {
|
func (w *world) run() {
|
||||||
for {
|
for {
|
||||||
@ -232,7 +236,7 @@ func (w *world) run() {
|
|||||||
sys.Update(w)
|
sys.Update(w)
|
||||||
}
|
}
|
||||||
// 处理事件管理相关
|
// 处理事件管理相关
|
||||||
w.processManageEventFuncs()
|
w.executeTodos()
|
||||||
// 处理所有事件
|
// 处理所有事件
|
||||||
processAllEvents(w)
|
processAllEvents(w)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user