108 lines
3.1 KiB
Go
108 lines
3.1 KiB
Go
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
|
||
}
|
||
|
||
// UpdateTrainPositionFromDynamics 更新列车所在的物理区段
|
||
func UpdateTrainPositionFromDynamics(w ecs.World, tpi TrainPositionInfo) error {
|
||
result := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
|
||
wd := entity.GetWorldData(w)
|
||
te, find := wd.EntityMap[tpi.TrainId]
|
||
if find {
|
||
train := component.TrainPositionInfoType.Get(te)
|
||
//
|
||
//lastTrainHeadPos := &device_sys.TrainHeadPosition{Up: train.Up, HeadLink: train.HeadLink, HeadLinkOffset: train.HeadLinkOffset}
|
||
//
|
||
train.Up = tpi.Up
|
||
train.Len = int64(tpi.Len)
|
||
train.HeadLink = tpi.HeadLink
|
||
train.HeadLinkOffset = int64(tpi.HeadLinkOffset)
|
||
train.TailLink = tpi.TailLink
|
||
train.TailLinkOffset = int64(tpi.TailLinkOffset)
|
||
/*
|
||
//列车车头移动范围
|
||
curTrainHeadPos := &device_sys.TrainHeadPosition{Up: train.Up, HeadLink: train.HeadLink, HeadLinkOffset: train.HeadLinkOffset}
|
||
//根据列车位置探测应答器
|
||
device_sys.NewBaliseDetection().DetectBalise(w, te, lastTrainHeadPos, curTrainHeadPos)
|
||
*/
|
||
//
|
||
return ecs.NewOkEmptyResult()
|
||
} else {
|
||
return ecs.NewErrResult(fmt.Errorf("列车[%s]实体不存在", tpi.TrainId))
|
||
}
|
||
|
||
})
|
||
return result.Err
|
||
}
|
||
|
||
// /////////////////////////////////////////////////////////////////////////////////////////
|
||
|
||
type TrainPositionInfo struct {
|
||
//列车id
|
||
TrainId string
|
||
//列车头当前运行方向(true偏移量增大/false减小方向)
|
||
Up bool
|
||
//列车长度 mm
|
||
Len uint32
|
||
//列车所在轨道link
|
||
HeadLink string
|
||
//列车所在link偏移量(mm)
|
||
HeadLinkOffset uint32
|
||
//列车所在轨道link
|
||
TailLink string
|
||
//列车所在link偏移量(mm)
|
||
TailLinkOffset uint32
|
||
}
|
||
|
||
// TrainHeadPositionInfo 列车车头运行位置信息
|
||
type TrainHeadPositionInfo struct {
|
||
//列车id
|
||
TrainId string
|
||
//列车头当前运行方向(true偏移量增大/false减小方向)
|
||
Up bool
|
||
//列车所在轨道link
|
||
Link string
|
||
//列车所在link偏移量(mm)
|
||
LinkOffset int64
|
||
//列车运行速度(m/s)
|
||
Speed float32
|
||
//加速度(m/s^2)
|
||
Acceleration float32
|
||
}
|
||
|
||
func (t *TrainHeadPositionInfo) String() string {
|
||
return fmt.Sprintf("TrainHeadPositionInfo :: TrainId=%s Up=%t Link=%s LinkOffset=%d Speed=%f Ac=%f",
|
||
t.TrainId, t.Up, t.Link, t.LinkOffset, t.Speed, t.Acceleration)
|
||
}
|