76 lines
1.4 KiB
Go
76 lines
1.4 KiB
Go
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))
|
|
|
|
// 系统时钟操作
|
|
type TimerSystem struct {
|
|
}
|
|
|
|
func NewTimerSystem() *TimerSystem {
|
|
return &TimerSystem{}
|
|
}
|
|
|
|
// world 执行
|
|
func (me *TimerSystem) Update(w ecs.World) {
|
|
if e, ok := timerQuery.First(w); ok {
|
|
timer := SystemTimerComponent.Get(e)
|
|
timer.Tick(w.Tick())
|
|
}
|
|
}
|
|
|
|
// 重置world时间
|
|
func ResetWorldTimer(w ecs.World, time time.Time) {
|
|
if e, ok := timerQuery.First(w); ok {
|
|
timer := SystemTimerComponent.Get(e)
|
|
timer.ResetTime(time)
|
|
}
|
|
}
|
|
|
|
// 获取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
|
|
}
|
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
// 系统时钟,单例
|
|
type SystemTimer struct {
|
|
timer *time.Time
|
|
}
|
|
|
|
// 以指定时间构建
|
|
func NewSystemTimer(time *time.Time) *SystemTimer {
|
|
return &SystemTimer{
|
|
timer: time,
|
|
}
|
|
}
|
|
|
|
// 重置时间
|
|
func (me *SystemTimer) ResetTime(time time.Time) {
|
|
*me.timer = time
|
|
}
|
|
|
|
// 获取当前时间的副本
|
|
func (me *SystemTimer) Now() time.Time {
|
|
return *me.timer
|
|
}
|
|
|
|
// tick系统时钟,单位ms
|
|
func (me *SystemTimer) Tick(tick int) {
|
|
*me.timer = me.timer.Add(time.Duration(tick) * time.Millisecond)
|
|
}
|