rts-sim-module/simulation/system/switch_system.go
2023-08-15 17:29:11 +08:00

68 lines
1.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package system
import (
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
"joylink.club/rtsssimulation/components"
"joylink.club/rtsssimulation/state"
)
// 道岔系统操作
type SwitchSystem struct {
//正常转动道岔系统查询
turningQuery *ecs.Query
}
func NewSwitchSystem() *SwitchSystem {
return &SwitchSystem{
turningQuery: ecs.NewQuery(filter.Contains(components.ComSwitchState, components.ComSwitchTurnOperating)),
}
}
// API:触发道岔正常转动
// w: 当前世界
// switchId: 道岔id
// turnNormal: true-道岔转动到定位false-道岔转动到反位
// turnTime: 道岔转动耗时ms
func FireSwitchTurn(w ecs.World, switchId string, turnNormal bool, turnTime int) bool {
fireQuery := ecs.NewQuery(filter.Contains(components.ComDeviceIdentity, components.ComSwitchTurnOperating))
//
var turnOperating *state.SwitchTurnOperating
fireQuery.Each(w, func(e *ecs.Entry) {
if id := components.ComDeviceIdentity.Get(e).Id; id == switchId {
turnOperating = components.ComSwitchTurnOperating.Get(e)
}
})
//
if nil == turnOperating {
return false
}
//
turnOperating.TurnNormal = turnNormal
turnOperating.TurningTime = int64(turnTime)
turnOperating.StartTurn = true
return true
}
// world 执行
func (me *SwitchSystem) Update(w ecs.World) {
//道岔转动操作
me.turningQuery.Each(w, func(e *ecs.Entry) {
if turnOperting := components.ComSwitchTurnOperating.Get(e); turnOperting.StartTurn {
if turnOperting.TurningTime > 0 { //正在转动
turnOperting.TurningTime -= int64(w.Tick())
} else { //转动完成
turnOperting.StartTurn = false
state := components.ComSwitchState.Get(e)
if turnOperting.TurnNormal {
state.Normal = true
state.Reverse = false
} else {
state.Normal = false
state.Reverse = true
}
}
}
})
}