rts-sim-module/deprecated/system/timer_system.go
walker 0bba8f0934 删除donburi包引用
修改filter导入为从ecs项目导入
整理包结构,将弃用的包放入deprecated文件中
2023-10-09 11:17:25 +08:00

76 lines
1.5 KiB
Go

package system
import (
"time"
"joylink.club/ecs"
"joylink.club/ecs/filter"
)
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)
}