2023-08-04 11:02:08 +08:00
|
|
|
|
package ecs
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2023-09-21 09:41:49 +08:00
|
|
|
|
"math"
|
2023-08-04 11:02:08 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/yohamta/donburi"
|
|
|
|
|
"github.com/yohamta/donburi/features/events"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type WorldState int
|
|
|
|
|
|
2023-10-09 15:03:26 +08:00
|
|
|
|
type WorldId = donburi.WorldId
|
2023-08-04 11:02:08 +08:00
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
Init WorldState = iota
|
|
|
|
|
Running
|
|
|
|
|
Pause
|
|
|
|
|
Error
|
2023-10-09 17:36:04 +08:00
|
|
|
|
Close
|
2023-08-04 11:02:08 +08:00
|
|
|
|
Closed
|
|
|
|
|
)
|
|
|
|
|
|
2023-09-21 09:41:49 +08:00
|
|
|
|
type (
|
|
|
|
|
World interface {
|
2023-10-09 14:21:24 +08:00
|
|
|
|
donburi.World
|
2023-09-21 09:41:49 +08:00
|
|
|
|
|
|
|
|
|
StartUp()
|
|
|
|
|
Pause()
|
|
|
|
|
Resume()
|
|
|
|
|
SetSpeed(speed float64) error
|
|
|
|
|
AddSystem(sys ...ISystem)
|
2023-10-09 17:36:04 +08:00
|
|
|
|
// 在世界中执行处理逻辑(在世界运行线程中)
|
|
|
|
|
Execute(fn HandleFunc)
|
2023-09-21 09:41:49 +08:00
|
|
|
|
Close()
|
2023-10-09 17:36:04 +08:00
|
|
|
|
// 世界运行间隔
|
2023-09-21 09:41:49 +08:00
|
|
|
|
Tick() int
|
|
|
|
|
Running() bool
|
|
|
|
|
}
|
2023-10-09 11:12:05 +08:00
|
|
|
|
|
2023-10-09 17:36:04 +08:00
|
|
|
|
// 处理函数
|
|
|
|
|
HandleFunc func()
|
2023-09-21 09:41:49 +08:00
|
|
|
|
)
|
2023-08-04 11:02:08 +08:00
|
|
|
|
|
|
|
|
|
type world struct {
|
2023-10-09 14:21:24 +08:00
|
|
|
|
donburi.World
|
2023-09-21 09:41:49 +08:00
|
|
|
|
systems []ISystem
|
|
|
|
|
state WorldState
|
|
|
|
|
tick int
|
|
|
|
|
ticker *time.Ticker
|
2023-10-09 17:36:04 +08:00
|
|
|
|
// 世界运行倍速
|
|
|
|
|
speed float64
|
|
|
|
|
// 下一帧系统需要执行的次数
|
2023-09-21 09:41:49 +08:00
|
|
|
|
times float64
|
2023-08-04 11:02:08 +08:00
|
|
|
|
|
2023-09-21 09:41:49 +08:00
|
|
|
|
// 待执行函数
|
2023-10-09 17:36:04 +08:00
|
|
|
|
toBeExecuteds chan HandleFunc
|
2023-08-04 11:02:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-21 17:37:35 +08:00
|
|
|
|
// 新建一个组件类型
|
2023-08-04 11:02:08 +08:00
|
|
|
|
func NewComponentType[T any](opts ...interface{}) *ComponentType[T] {
|
|
|
|
|
ct := donburi.NewComponentType[T](opts...)
|
|
|
|
|
return &ComponentType[T]{ct}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-09 17:36:04 +08:00
|
|
|
|
// 新建一个标签
|
2023-09-21 17:37:35 +08:00
|
|
|
|
func NewTag() *ComponentType[struct{}] {
|
|
|
|
|
return NewComponentType[struct{}]()
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-09 14:21:24 +08:00
|
|
|
|
// 将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
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 15:30:51 +08:00
|
|
|
|
// 初始化一个新World
|
2023-09-21 17:37:35 +08:00
|
|
|
|
// tick 单位为ms,且必须大于0,(小于15ms的值在Windows系统中会达不到,Windows系统中系统中断好像默认是15.6ms,也就是一秒最多64次)
|
2023-08-04 11:02:08 +08:00
|
|
|
|
func NewWorld(tick int) World {
|
2023-10-09 17:36:04 +08:00
|
|
|
|
if tick <= 0 {
|
|
|
|
|
panic("tick必须大于0")
|
|
|
|
|
}
|
2023-08-04 11:02:08 +08:00
|
|
|
|
return &world{
|
2023-10-09 14:21:24 +08:00
|
|
|
|
World: donburi.NewWorld(),
|
2023-09-21 09:41:49 +08:00
|
|
|
|
systems: make([]ISystem, 0),
|
|
|
|
|
state: Init,
|
|
|
|
|
tick: tick,
|
|
|
|
|
ticker: time.NewTicker(time.Duration(tick) * time.Millisecond),
|
|
|
|
|
speed: 1,
|
|
|
|
|
times: 1,
|
2023-10-09 17:36:04 +08:00
|
|
|
|
toBeExecuteds: make(chan HandleFunc, 32),
|
2023-08-04 11:02:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-15 16:48:55 +08:00
|
|
|
|
func (w *world) Running() bool {
|
|
|
|
|
return w.state == Running
|
|
|
|
|
}
|
2023-08-15 09:17:22 +08:00
|
|
|
|
func (w *world) Tick() int {
|
|
|
|
|
return w.tick
|
|
|
|
|
}
|
2023-08-04 11:02:08 +08:00
|
|
|
|
|
|
|
|
|
// 添加系统
|
|
|
|
|
func (w *world) AddSystem(sys ...ISystem) {
|
|
|
|
|
w.systems = append(w.systems, sys...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 执行所有事件处理
|
|
|
|
|
func (w *world) ProcessAllEvents() {
|
2023-10-09 14:21:24 +08:00
|
|
|
|
events.ProcessAllEvents(w.World)
|
2023-08-04 11:02:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 暂停世界
|
|
|
|
|
func (w *world) Pause() {
|
|
|
|
|
if w.state == Running {
|
|
|
|
|
w.state = Pause
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 恢复世界运行
|
|
|
|
|
func (w *world) Resume() {
|
|
|
|
|
if w.state == Pause {
|
|
|
|
|
w.state = Running
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
SpeedMin = 0.1
|
|
|
|
|
SpeedMax = 10
|
|
|
|
|
)
|
|
|
|
|
|
2023-10-09 17:36:04 +08:00
|
|
|
|
// 设置世界运行倍速
|
2023-08-04 11:02:08 +08:00
|
|
|
|
func (w *world) SetSpeed(speed float64) error {
|
|
|
|
|
if speed < SpeedMin || speed > SpeedMax {
|
|
|
|
|
return fmt.Errorf("速度必须在[%f, %d]之间", SpeedMin, SpeedMax)
|
|
|
|
|
}
|
|
|
|
|
w.speed = speed
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 启动世界,世界逻辑开始执行且世界为运行状态
|
|
|
|
|
func (w *world) StartUp() {
|
|
|
|
|
if w.state == Init { // 避免重复运行
|
|
|
|
|
w.state = Running
|
|
|
|
|
go w.run()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-21 09:41:49 +08:00
|
|
|
|
// 在世界线程执行逻辑
|
2023-10-09 17:36:04 +08:00
|
|
|
|
func (w *world) Execute(fn HandleFunc) {
|
2023-09-21 09:41:49 +08:00
|
|
|
|
w.toBeExecuteds <- fn
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-04 11:02:08 +08:00
|
|
|
|
// 关闭世界
|
|
|
|
|
func (w *world) Close() {
|
2023-10-09 17:36:04 +08:00
|
|
|
|
w.state = Close
|
2023-08-04 11:02:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-21 17:37:35 +08:00
|
|
|
|
// 执行待处理方法
|
|
|
|
|
func (w *world) executeTodos() {
|
2023-10-09 17:36:04 +08:00
|
|
|
|
funcs := w.toBeExecuteds
|
2023-08-31 16:19:41 +08:00
|
|
|
|
for {
|
|
|
|
|
select {
|
2023-10-09 17:36:04 +08:00
|
|
|
|
case fn := <-funcs:
|
2023-08-31 16:19:41 +08:00
|
|
|
|
{
|
2023-10-09 17:36:04 +08:00
|
|
|
|
fn()
|
2023-08-31 16:19:41 +08:00
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-04 11:02:08 +08:00
|
|
|
|
func (w *world) run() {
|
|
|
|
|
for {
|
|
|
|
|
if w.state == Error {
|
2023-09-20 15:30:51 +08:00
|
|
|
|
// 世界错误,关闭世界
|
2023-08-04 11:02:08 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2023-10-09 17:36:04 +08:00
|
|
|
|
if w.state == Close {
|
2023-09-20 15:30:51 +08:00
|
|
|
|
// 世界正常关闭
|
2023-10-09 17:36:04 +08:00
|
|
|
|
w.state = Closed
|
2023-08-04 11:02:08 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2023-09-20 15:30:51 +08:00
|
|
|
|
<-w.ticker.C
|
2023-10-09 15:03:26 +08:00
|
|
|
|
// start := time.Now()
|
2023-08-04 11:02:08 +08:00
|
|
|
|
if w.state == Pause { // 暂停不更新
|
|
|
|
|
continue
|
|
|
|
|
}
|
2023-09-21 09:41:49 +08:00
|
|
|
|
if w.times > 1 {
|
|
|
|
|
times := int(math.Floor(w.times))
|
|
|
|
|
for i := 0; i < times; i++ {
|
|
|
|
|
for _, sys := range w.systems {
|
|
|
|
|
sys.Update(w)
|
|
|
|
|
}
|
|
|
|
|
// 处理事件管理相关
|
2023-09-21 17:37:35 +08:00
|
|
|
|
w.executeTodos()
|
2023-09-21 09:41:49 +08:00
|
|
|
|
// 处理所有事件
|
|
|
|
|
processAllEvents(w)
|
|
|
|
|
}
|
|
|
|
|
w.times = w.times - float64(times) + w.speed
|
|
|
|
|
} else {
|
|
|
|
|
w.times += w.speed
|
2023-08-04 11:02:08 +08:00
|
|
|
|
}
|
2023-10-09 15:03:26 +08:00
|
|
|
|
// dt := time.Since(start)
|
|
|
|
|
// fmt.Println("仿真系统执行耗时:", dt.Milliseconds(), "ms")
|
2023-08-04 11:02:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|