rts-sim-module/system/face_system.go

103 lines
2.1 KiB
Go
Raw Normal View History

2023-08-15 16:50:24 +08:00
package system
import (
2023-08-22 14:37:30 +08:00
"fmt"
2023-08-15 16:50:24 +08:00
"sync"
"time"
2023-08-22 14:37:30 +08:00
"github.com/yohamta/donburi/filter"
2023-08-15 16:50:24 +08:00
"joylink.club/ecs"
2023-08-22 14:37:30 +08:00
"joylink.club/rtsssimulation/components"
2023-08-15 16:50:24 +08:00
)
// 外界与world交互请求定义
type FaceRequest func(w ecs.World) any
type faceOutcome struct {
outcome any
ok bool
}
// 外界与world的交互
type FaceSystem struct {
req chan FaceRequest
rsp chan *faceOutcome
world ecs.World
//保证与world的交互串行
locker *sync.Mutex
}
func NewFaceSystem(w ecs.World) *FaceSystem {
return &FaceSystem{req: make(chan FaceRequest), rsp: make(chan *faceOutcome), locker: &sync.Mutex{}, world: w}
}
// world 执行
func (me *FaceSystem) Update(world ecs.World) {
select {
case reqFunc := <-me.req:
{
response := reqFunc(world)
if nil != response {
me.rsp <- &faceOutcome{outcome: response, ok: true}
} else {
me.rsp <- &faceOutcome{outcome: nil, ok: false}
}
}
default:
}
}
// 外界调用此方法与world交互
func (me *FaceSystem) Call(request FaceRequest) (any, bool) {
if !me.world.Running() {
return nil, false
}
//
me.locker.Lock()
defer me.locker.Unlock()
//
me.req <- request
//
result := <-me.rsp
return result.outcome, result.ok
}
2023-08-22 14:37:30 +08:00
// 根据id获取实体
func (me *FaceSystem) FindEntity(id string) *ecs.Entry {
2023-08-23 16:01:01 +08:00
return FindEntityById(me.world, id)
2023-08-22 14:37:30 +08:00
}
2023-08-15 16:50:24 +08:00
// 获取world当前时间
func (me *FaceSystem) WorldTime() *time.Time {
now, ok := me.Call(func(w ecs.World) any {
return GetWorldNow(w)
})
if ok {
return now.(*time.Time)
} else {
return nil
}
}
2023-08-23 16:01:01 +08:00
func FindEntityById(world ecs.World, id string) *ecs.Entry {
2023-08-22 15:04:46 +08:00
query := ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent))
2023-08-23 16:01:01 +08:00
return QueryEntityById(world, query, id)
2023-08-22 15:04:46 +08:00
}
2023-08-23 16:01:01 +08:00
func QueryEntityById(world ecs.World, q *ecs.Query, id string) *ecs.Entry {
2023-08-22 15:04:46 +08:00
var entry *ecs.Entry = nil
func() {
2023-08-23 16:08:21 +08:00
defer simpleRecover()
2023-08-22 15:04:46 +08:00
q.Each(world, func(e *ecs.Entry) {
if id == components.DeviceIdentityComponent.Get(e).Id {
entry = e
panic(fmt.Sprintf("找到实体[%s],结束查找", id))
}
})
}()
//
return entry
}
2023-08-23 16:08:21 +08:00
// 捕获panic并恢复执行
func simpleRecover() {
recover()
}