修改World、Entity等接口,直接导出donburi对应定义,不再自己包装

This commit is contained in:
walker 2023-10-09 14:21:24 +08:00
parent 01707edd32
commit 0d24ef1dc2
8 changed files with 44 additions and 95 deletions

View File

@ -13,15 +13,15 @@ type ComponentType[T any] struct {
// Get returns component data from the entry. // Get returns component data from the entry.
func (c *ComponentType[T]) Get(entry *Entry) *T { 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. // Set sets component data to the entry.
func (c *ComponentType[T]) Set(entry *Entry, component *T) { 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. // 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) {
c.ComponentType.SetValue(entry.Entry, value) c.ComponentType.SetValue(entry, value)
} }

10
debug/debug.go Normal file
View File

@ -0,0 +1,10 @@
package debug
import (
"github.com/yohamta/donburi/features/debug"
"joylink.club/ecs"
)
func PrintEntityCounts(w ecs.World) {
debug.PrintEntityCounts(w)
}

View File

@ -2,8 +2,4 @@ package ecs
import "github.com/yohamta/donburi" import "github.com/yohamta/donburi"
type ( type Entity = donburi.Entity
Entity struct {
donburi.Entity
}
)

View File

@ -2,6 +2,4 @@ package ecs
import "github.com/yohamta/donburi" import "github.com/yohamta/donburi"
type Entry struct { type Entry = donburi.Entry
*donburi.Entry
}

View File

@ -40,14 +40,14 @@ func NewEventType[T any]() *EventType[T] {
// 迭代处理所有事件 // 迭代处理所有事件
// 在world协程中执行 // 在world协程中执行
func processAllEvents(w World) { func processAllEvents(w World) {
events.ProcessAllEvents(w.(*world).world) events.ProcessAllEvents(w)
} }
// 发布该类型的事件 // 发布该类型的事件
// 在world协程外执行 // 在world协程外执行
func (me *EventType[T]) Publish(wd World, event *T) { func (me *EventType[T]) Publish(wd World, event *T) {
wd.Execute(func() { 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 me.subscriberMap[subscriberKey] = fn
wd.Execute(func() { 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) subscriberKey := subscriberKey[T](wd, subscriber)
if sub, ok := me.subscriberMap[subscriberKey]; ok { if sub, ok := me.subscriberMap[subscriberKey]; ok {
wd.Execute(func() { wd.Execute(func() {
me.et.Unsubscribe(wd.(*world).world, sub) me.et.Unsubscribe(wd, sub)
delete(me.subscriberMap, subscriberKey) delete(me.subscriberMap, subscriberKey)
}) })
} }

View File

@ -85,7 +85,7 @@ func initSwitchState(world ecs.World) {
simMemory := simulation.ModelsMemory simMemory := simulation.ModelsMemory
for _, switchModel := range simMemory.Switchs { for _, switchModel := range simMemory.Switchs {
dc1 := &state.SwitchState{Normal: true, Reverse: false} 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.ComSwitchState.Set(dc1Entry, dc1)
simulation.ComId.Set(dc1Entry, &iwayside.IdData{Id: switchModel.GetId()}) simulation.ComId.Set(dc1Entry, &iwayside.IdData{Id: switchModel.GetId()})
} }
@ -96,7 +96,7 @@ func initLinkState(world ecs.World) {
simMemory := simulation.ModelsMemory simMemory := simulation.ModelsMemory
for _, linkModel := range simMemory.Links { for _, linkModel := range simMemory.Links {
linkState := &state.LinkState{Id: linkModel.GetId(), Occupied: false} 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) simulation.ComLinkState.Set(linkEntry, linkState)
} }
} }
@ -106,7 +106,7 @@ func initPhsicalSectionState(world ecs.World) {
simMemory := simulation.ModelsMemory simMemory := simulation.ModelsMemory
for _, phSecModel := range simMemory.PhysicalSections { for _, phSecModel := range simMemory.PhysicalSections {
sectionState := &state.PhysicalSectionState{Occupied: false} 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.ComPhsicalSectionState.Set(sectionEntry, sectionState)
simulation.ComId.Set(sectionEntry, &iwayside.IdData{Id: phSecModel.GetId()}) 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) { func initTrainState(world ecs.World, trainId string, linkLocation iwayside.LinkLocation, up bool) {
train := &state.TrainState{LinkId: linkLocation.LinkId, LinkOffset: linkLocation.LinkOffset, Up: up} 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.ComTrainState.Set(trainEntry, train)
simulation.ComId.Set(trainEntry, &iwayside.IdData{Id: trainId}) simulation.ComId.Set(trainEntry, &iwayside.IdData{Id: trainId})
} }

View File

@ -5,29 +5,9 @@ import (
"joylink.club/ecs/filter" "joylink.club/ecs/filter"
) )
type Query struct { type Query = donburi.Query
*donburi.Query
}
// NewQuery creates a new query. // 新建一个查询对象
// It receives arbitrary filters that are used to filter entities.
func NewQuery(filter filter.LayoutFilter) *Query { func NewQuery(filter filter.LayoutFilter) *Query {
return &Query{ return donburi.NewQuery(filter)
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
} }

View File

@ -25,21 +25,7 @@ const (
type ( type (
World interface { World interface {
donburi.World
// 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
StartUp() StartUp()
Pause() Pause()
@ -64,7 +50,7 @@ type (
) )
type world struct { type world struct {
world donburi.World donburi.World
systems []ISystem systems []ISystem
state WorldState state WorldState
tick int tick int
@ -90,11 +76,20 @@ func NewTag() *ComponentType[struct{}] {
return NewComponentType[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 // 初始化一个新World
// tick 单位为ms且必须大于0,(小于15ms的值在Windows系统中会达不到Windows系统中系统中断好像默认是15.6ms也就是一秒最多64次) // 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(),
systems: make([]ISystem, 0), systems: make([]ISystem, 0),
state: Init, state: Init,
tick: tick, tick: tick,
@ -102,7 +97,7 @@ func NewWorld(tick int) World {
speed: 1, speed: 1,
times: 1, times: 1,
quit: make(chan struct{}), quit: make(chan struct{}),
toBeExecuteds: make(chan ExecuteFunc, 1024), toBeExecuteds: make(chan ExecuteFunc, 256),
} }
} }
func (w *world) Running() bool { func (w *world) Running() bool {
@ -111,39 +106,6 @@ func (w *world) Running() bool {
func (w *world) Tick() int { func (w *world) Tick() int {
return w.tick 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) { func (w *world) AddSystem(sys ...ISystem) {
@ -152,7 +114,7 @@ func (w *world) AddSystem(sys ...ISystem) {
// 执行所有事件处理 // 执行所有事件处理
func (w *world) ProcessAllEvents() { func (w *world) ProcessAllEvents() {
events.ProcessAllEvents(w.world) events.ProcessAllEvents(w.World)
} }
// 暂停世界 // 暂停世界
@ -217,10 +179,11 @@ func (w *world) executeTodos() {
} }
func (w *world) run() { func (w *world) run() {
for { for {
start := time.Now()
select { select {
case <-w.quit: // 退出信号 case <-w.quit: // 退出信号
// 仿真退出,更新状态 // 仿真退出,更新状态
log.Println("仿真退出,id:", w.world.Id()) log.Println("仿真退出,id:", w.World.Id())
w.state = Closed w.state = Closed
default: default:
} }
@ -251,5 +214,7 @@ func (w *world) run() {
} else { } else {
w.times += w.speed w.times += w.speed
} }
dt := time.Since(start)
fmt.Println("仿真执行耗时:", dt.Milliseconds(), "ms")
} }
} }