package system import ( "time" "github.com/yohamta/donburi/filter" "joylink.club/ecs" ) var SystemTimerComponent = ecs.NewComponentType[SystemTimer]() var timerQuery *ecs.Query = ecs.NewQuery(filter.Contains(SystemTimerComponent)) // TimerSystem 系统时钟操作 type TimerSystem struct { } func NewTimerSystem() *TimerSystem { return &TimerSystem{} } // Update world 执行 func (me *TimerSystem) Update(w ecs.World) { if e, ok := timerQuery.First(w); ok { timer := SystemTimerComponent.Get(e) timer.Tick(w.Tick()) } } // ResetWorldTimer 重置world时间 func ResetWorldTimer(w ecs.World, time time.Time) { if e, ok := timerQuery.First(w); ok { timer := SystemTimerComponent.Get(e) timer.ResetTime(time) } } // GetWorldNow 获取world当前时间 func GetWorldNow(w ecs.World) *time.Time { if e, ok := timerQuery.First(w); ok { timer := SystemTimerComponent.Get(e) now := timer.Now() return &now } return nil } ///////////////////////////////////////////////////////////////// // SystemTimer 系统时钟,单例 type SystemTimer struct { timer *time.Time } // NewSystemTimer 以指定时间构建 func NewSystemTimer(time *time.Time) *SystemTimer { return &SystemTimer{ timer: time, } } // ResetTime 重置时间 func (me *SystemTimer) ResetTime(time time.Time) { *me.timer = time } // Now 获取当前时间的副本 func (me *SystemTimer) Now() time.Time { return *me.timer } // Tick tick系统时钟,单位ms func (me *SystemTimer) Tick(tick int) { *me.timer = me.timer.Add(time.Duration(tick) * time.Millisecond) }