diff --git a/component.go b/component.go index 4bd36c7..e7507c9 100644 --- a/component.go +++ b/component.go @@ -13,15 +13,15 @@ type ComponentType[T any] struct { // Get returns component data from the entry. func (c *ComponentType[T]) Get(entry *Entry) *T { - return c.ComponentType.Get(entry.Entry) + return c.ComponentType.Get(entry) } // Set sets component data to the entry. func (c *ComponentType[T]) Set(entry *Entry, component *T) { - c.ComponentType.Set(entry.Entry, component) + c.ComponentType.Set(entry, component) } // SetValue sets the value of the component. func (c *ComponentType[T]) SetValue(entry *Entry, value T) { - c.ComponentType.SetValue(entry.Entry, value) + c.ComponentType.SetValue(entry, value) } diff --git a/debug/debug.go b/debug/debug.go new file mode 100644 index 0000000..2b2c7b1 --- /dev/null +++ b/debug/debug.go @@ -0,0 +1,10 @@ +package debug + +import ( + "github.com/yohamta/donburi/features/debug" + "joylink.club/ecs" +) + +func PrintEntityCounts(w ecs.World) { + debug.PrintEntityCounts(w) +} diff --git a/entity.go b/entity.go index 352feca..4aa715c 100644 --- a/entity.go +++ b/entity.go @@ -2,8 +2,4 @@ package ecs import "github.com/yohamta/donburi" -type ( - Entity struct { - donburi.Entity - } -) +type Entity = donburi.Entity diff --git a/entry.go b/entry.go index d66c848..7720075 100644 --- a/entry.go +++ b/entry.go @@ -2,6 +2,4 @@ package ecs import "github.com/yohamta/donburi" -type Entry struct { - *donburi.Entry -} +type Entry = donburi.Entry diff --git a/events.go b/events.go index 1539e68..77a9e84 100644 --- a/events.go +++ b/events.go @@ -40,14 +40,14 @@ func NewEventType[T any]() *EventType[T] { // 迭代处理所有事件 // 在world协程中执行 func processAllEvents(w World) { - events.ProcessAllEvents(w.(*world).world) + events.ProcessAllEvents(w) } // 发布该类型的事件 // 在world协程外执行 func (me *EventType[T]) Publish(wd World, event *T) { wd.Execute(func() { - me.et.Publish(wd.(*world).world, *event) + me.et.Publish(wd, *event) }) } @@ -64,7 +64,7 @@ func (me *EventType[T]) Subscribe(wd World, subscriber Subscriber[T]) { } me.subscriberMap[subscriberKey] = fn wd.Execute(func() { - me.et.Subscribe(wd.(*world).world, fn) + me.et.Subscribe(wd, fn) }) } } @@ -76,7 +76,7 @@ func (me *EventType[T]) Unsubscribe(wd World, subscriber Subscriber[T]) { subscriberKey := subscriberKey[T](wd, subscriber) if sub, ok := me.subscriberMap[subscriberKey]; ok { wd.Execute(func() { - me.et.Unsubscribe(wd.(*world).world, sub) + me.et.Unsubscribe(wd, sub) delete(me.subscriberMap, subscriberKey) }) } diff --git a/examples/rtss-cg/main.go b/examples/rtss-cg/main.go index 77c7cc4..43cce4d 100644 --- a/examples/rtss-cg/main.go +++ b/examples/rtss-cg/main.go @@ -85,7 +85,7 @@ func initSwitchState(world ecs.World) { simMemory := simulation.ModelsMemory for _, switchModel := range simMemory.Switchs { dc1 := &state.SwitchState{Normal: true, Reverse: false} - dc1Entry := world.Create(simulation.ComId, simulation.ComSwitchState, simulation.ComSwitchOperating) + dc1Entry := world.Entry(world.Create(simulation.ComId, simulation.ComSwitchState, simulation.ComSwitchOperating)) simulation.ComSwitchState.Set(dc1Entry, dc1) simulation.ComId.Set(dc1Entry, &iwayside.IdData{Id: switchModel.GetId()}) } @@ -96,7 +96,7 @@ func initLinkState(world ecs.World) { simMemory := simulation.ModelsMemory for _, linkModel := range simMemory.Links { linkState := &state.LinkState{Id: linkModel.GetId(), Occupied: false} - linkEntry := world.Create(simulation.ComLinkState) + linkEntry := world.Entry(world.Create(simulation.ComLinkState)) simulation.ComLinkState.Set(linkEntry, linkState) } } @@ -106,7 +106,7 @@ func initPhsicalSectionState(world ecs.World) { simMemory := simulation.ModelsMemory for _, phSecModel := range simMemory.PhysicalSections { sectionState := &state.PhysicalSectionState{Occupied: false} - sectionEntry := world.Create(simulation.ComId, simulation.ComPhsicalSectionState) + sectionEntry := world.Entry(world.Create(simulation.ComId, simulation.ComPhsicalSectionState)) simulation.ComPhsicalSectionState.Set(sectionEntry, sectionState) simulation.ComId.Set(sectionEntry, &iwayside.IdData{Id: phSecModel.GetId()}) } @@ -115,7 +115,7 @@ func initPhsicalSectionState(world ecs.World) { // 添加列车并初始化 func initTrainState(world ecs.World, trainId string, linkLocation iwayside.LinkLocation, up bool) { train := &state.TrainState{LinkId: linkLocation.LinkId, LinkOffset: linkLocation.LinkOffset, Up: up} - trainEntry := world.Create(simulation.ComId, simulation.ComTrainState) + trainEntry := world.Entry(world.Create(simulation.ComId, simulation.ComTrainState)) simulation.ComTrainState.Set(trainEntry, train) simulation.ComId.Set(trainEntry, &iwayside.IdData{Id: trainId}) } diff --git a/query.go b/query.go index 60d68ac..526d3e5 100644 --- a/query.go +++ b/query.go @@ -5,29 +5,9 @@ import ( "joylink.club/ecs/filter" ) -type Query struct { - *donburi.Query -} +type Query = donburi.Query -// NewQuery creates a new query. -// It receives arbitrary filters that are used to filter entities. +// 新建一个查询对象 func NewQuery(filter filter.LayoutFilter) *Query { - return &Query{ - donburi.NewQuery(filter), - } -} - -func (q *Query) Each(w World, callback func(*Entry)) { - q.Query.Each(w.(*world).world, func(entry *donburi.Entry) { - callback(&Entry{entry}) - }) -} - -func (q *Query) Count(w World) int { - return q.Query.Count(w.(*world).world) -} - -func (q *Query) First(w World) (entry *Entry, ok bool) { - e, ok := q.Query.First(w.(*world).world) - return &Entry{e}, ok + return donburi.NewQuery(filter) } diff --git a/world.go b/world.go index 8af71e4..7df1c3d 100644 --- a/world.go +++ b/world.go @@ -25,21 +25,7 @@ const ( type ( World interface { - - // Id returns the unique identifier for the world. - Id() WorldId - // Create creates a new entity with the specified components. - Create(components ...donburi.IComponentType) *Entry - // CreateMany creates a new entity with the specified components. - CreateMany(n int, components ...donburi.IComponentType) []*Entry - // Entry returns an entry for the specified entity. - Entry(entity Entity) *Entry - // Remove removes the specified entity. - Remove(entity Entity) - // Valid returns true if the specified entity is valid. - Valid(e Entity) bool - // Len returns the number of entities in the world. - Len() int + donburi.World StartUp() Pause() @@ -64,7 +50,7 @@ type ( ) type world struct { - world donburi.World + donburi.World systems []ISystem state WorldState tick int @@ -90,11 +76,20 @@ func NewTag() *ComponentType[struct{}] { return NewComponentType[struct{}]() } +// 将entity列表转换为entry列表 +func Entries(w World, entities []donburi.Entity) []*Entry { + entries := make([]*Entry, len(entities)) + for i, entity := range entities { + entries[i] = w.Entry(entity) + } + return entries +} + // 初始化一个新World // tick 单位为ms,且必须大于0,(小于15ms的值在Windows系统中会达不到,Windows系统中系统中断好像默认是15.6ms,也就是一秒最多64次) func NewWorld(tick int) World { return &world{ - world: donburi.NewWorld(), + World: donburi.NewWorld(), systems: make([]ISystem, 0), state: Init, tick: tick, @@ -102,7 +97,7 @@ func NewWorld(tick int) World { speed: 1, times: 1, quit: make(chan struct{}), - toBeExecuteds: make(chan ExecuteFunc, 1024), + toBeExecuteds: make(chan ExecuteFunc, 256), } } func (w *world) Running() bool { @@ -111,39 +106,6 @@ func (w *world) Running() bool { func (w *world) Tick() int { return w.tick } -func (w *world) Id() WorldId { - return WorldId(w.world.Id()) -} - -func (w *world) Create(components ...donburi.IComponentType) *Entry { - entity := w.world.Create(components...) - return &Entry{w.world.Entry(entity)} -} - -func (w *world) CreateMany(n int, components ...donburi.IComponentType) []*Entry { - entitys := w.world.CreateMany(n, components...) - ets := make([]*Entry, len(entitys)) - for i, e := range entitys { - ets[i] = &Entry{w.world.Entry(e)} - } - return ets -} - -func (w *world) Entry(entity Entity) *Entry { - return &Entry{w.world.Entry(entity.Entity)} -} - -func (w *world) Remove(entity Entity) { - w.world.Remove(entity.Entity) -} - -func (w *world) Valid(e Entity) bool { - return w.world.Valid(e.Entity) -} - -func (w *world) Len() int { - return w.world.Len() -} // 添加系统 func (w *world) AddSystem(sys ...ISystem) { @@ -152,7 +114,7 @@ func (w *world) AddSystem(sys ...ISystem) { // 执行所有事件处理 func (w *world) ProcessAllEvents() { - events.ProcessAllEvents(w.world) + events.ProcessAllEvents(w.World) } // 暂停世界 @@ -217,10 +179,11 @@ func (w *world) executeTodos() { } func (w *world) run() { for { + start := time.Now() select { case <-w.quit: // 退出信号 // 仿真退出,更新状态 - log.Println("仿真退出,id:", w.world.Id()) + log.Println("仿真退出,id:", w.World.Id()) w.state = Closed default: } @@ -251,5 +214,7 @@ func (w *world) run() { } else { w.times += w.speed } + dt := time.Since(start) + fmt.Println("仿真执行耗时:", dt.Milliseconds(), "ms") } }