rts-sim-module/fi/train.go

77 lines
2.0 KiB
Go
Raw Normal View History

2023-11-09 14:59:18 +08:00
package fi
import (
"fmt"
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/entity"
)
// AddTrainToWorld 添加列车
func AddTrainToWorld(w ecs.World, trainId string) error {
result := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(w)
_, find := wd.EntityMap[trainId]
if !find {
entity.NewTrainEntity(w, trainId)
}
return ecs.NewOkEmptyResult()
})
return result.Err
}
// RemoveTrainFromWorld 移除列车
func RemoveTrainFromWorld(w ecs.World, trainId string) error {
result := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(w)
te, find := wd.EntityMap[trainId]
if find {
te.Remove()
delete(wd.EntityMap, trainId)
}
return ecs.NewOkEmptyResult()
})
return result.Err
}
// UpdateTrainFromDynamics 更新列车所在的物理区段
func UpdateTrainFromDynamics(w ecs.World, trainId string, sectionIds []string) error {
result := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(w)
te, find := wd.EntityMap[trainId]
if find {
tp := component.TrainPositionInfoType.Get(te)
tp.SectionIds = sectionIds
return ecs.NewOkEmptyResult()
} else {
return ecs.NewErrResult(fmt.Errorf("列车[%s]实体不存在", trainId))
}
})
return result.Err
}
2023-11-10 10:39:41 +08:00
// UpdateTrainPositionFromDynamics 更新列车所在的物理区段
func UpdateTrainPositionFromDynamics(w ecs.World, tpi TrainPositionInfo) error {
result := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
return ecs.NewOkEmptyResult()
})
return result.Err
}
type TrainPositionInfo struct {
//列车id
TrainId string
//列车头当前运行方向true偏移量增大/false减小方向
Up bool
//列车长度 mm
Len uint32
//列车所在轨道link
HeadLink uint8
//列车所在link偏移量mm
HeadLinkOffset uint32
//列车所在轨道link
TailLink uint8
//列车所在link偏移量mm
TailLinkOffset uint32
}