diff --git a/component/common.go b/component/common.go index 2d3c9fc..2626ad6 100644 --- a/component/common.go +++ b/component/common.go @@ -5,6 +5,7 @@ import ( "joylink.club/rtsssimulation/component/component_data" "joylink.club/rtsssimulation/component/component_proto" "joylink.club/rtsssimulation/consts" + "joylink.club/rtsssimulation/repository/model/proto" ) var ( @@ -65,3 +66,4 @@ type Counter struct { var CounterType = ecs.NewComponentType[Counter]() var LinkPositionType = ecs.NewComponentType[component_proto.LinkPosition]() +var KmType = ecs.NewComponentType[proto.Kilometer]() diff --git a/entity/balise.go b/entity/balise.go index 6cc61dc..e10e4ac 100644 --- a/entity/balise.go +++ b/entity/balise.go @@ -14,13 +14,6 @@ func LoadBalises(w ecs.World) error { balises := data.Repo.TransponderList() for _, b := range balises { be := newBaliseEntity(w, b, data) - //应答器位置 - be.AddComponent(component.LinkPositionType) - component.LinkPositionType.SetValue(be, component_proto.LinkPosition{ - LinkId: b.LinkPosition().Link().Id(), - Offset: b.LinkPosition().Offset(), - }) - //应答器类型 switch b.TransponderType() { case proto.Transponder_FB: be.AddComponent(component.BaliseFB) @@ -40,9 +33,14 @@ func newBaliseEntity(w ecs.World, td *repository.Transponder, worldData *compone uid := td.Id() entry, ok := worldData.EntityMap[uid] if !ok { - entry = w.Entry(w.Create(component.UidType, component.BaliseStateType)) + entry = w.Entry(w.Create(component.UidType, component.BaliseStateType, component.LinkPositionType, component.KmType)) component.UidType.SetValue(entry, component.Uid{Id: uid}) component.BaliseStateType.Set(entry, &component.BaliseState{}) + component.LinkPositionType.SetValue(entry, component_proto.LinkPosition{ + LinkId: td.LinkPosition().Link().Id(), + Offset: td.LinkPosition().Offset(), + }) + component.KmType.Set(entry, td.Km()) worldData.EntityMap[uid] = entry } return entry diff --git a/repository/calculate.go b/repository/calculate.go index c577c30..98ebea3 100644 --- a/repository/calculate.go +++ b/repository/calculate.go @@ -4,8 +4,8 @@ import ( "joylink.club/rtsssimulation/repository/model/proto" ) -// 转换公里标 -func convertKilometer(repo *Repository, km *proto.Kilometer, cs string) (*proto.Kilometer, error) { +// ConvertKilometer 转换公里标 +func ConvertKilometer(repo *Repository, km *proto.Kilometer, cs string) (*proto.Kilometer, error) { if km.CoordinateSystem == cs { return km, nil } diff --git a/repository/link.go b/repository/link.go index 0d4953b..988d1ad 100644 --- a/repository/link.go +++ b/repository/link.go @@ -70,6 +70,14 @@ func (l *Link) PhysicalSections() []*PhysicalSection { return l.physicalSections } +func (l *Link) AKm() *proto.Kilometer { + return l.aKm +} + +func (l *Link) BKm() *proto.Kilometer { + return l.bKm +} + func (l *Link) bindTurnoutPort(port proto.Port, turnoutPort *TurnoutPort) { switch port { case proto.Port_A: diff --git a/repository/repository_manager.go b/repository/repository_manager.go index dabd686..437b3ef 100644 --- a/repository/repository_manager.go +++ b/repository/repository_manager.go @@ -265,7 +265,7 @@ func completeTurnoutPortKm(repo *Repository) error { if tpKm == nil { dp := turnout.findDevicePortByPort(port) otherTurnout := repo.turnoutMap[dp.Device().Id()] - otherKm, err := convertKilometer(repo, otherTurnout.km, turnout.km.CoordinateSystem) + otherKm, err := ConvertKilometer(repo, otherTurnout.km, turnout.km.CoordinateSystem) if err != nil { return err } @@ -613,7 +613,7 @@ func buildLinks(repo *Repository) error { link.bindKm(endKm, proto.Port_B) } //计算Link长度 - convertedKm, err := convertKilometer(repo, endKm, linkZeroKm.CoordinateSystem) + convertedKm, err := ConvertKilometer(repo, endKm, linkZeroKm.CoordinateSystem) if err != nil { return err } @@ -688,8 +688,8 @@ func findEndTurnoutPortOrEndKm(repo *Repository, link *Link, startTp *TurnoutPor // 关联正常(非道岔)物理区段与Link func relatePhysicalSectionAndLink(repo *Repository, section *PhysicalSection, link *Link, baseKm *proto.Kilometer) { link.bindPhysicalSections(section) - aKm, _ := convertKilometer(repo, section.aKm, baseKm.CoordinateSystem) - bKm, _ := convertKilometer(repo, section.bKm, baseKm.CoordinateSystem) + aKm, _ := ConvertKilometer(repo, section.aKm, baseKm.CoordinateSystem) + bKm, _ := ConvertKilometer(repo, section.bKm, baseKm.CoordinateSystem) aOffset := number.Abs(aKm.Value - baseKm.Value) bOffset := number.Abs(bKm.Value - baseKm.Value) section.aLinkPosition = &LinkPosition{ @@ -734,7 +734,7 @@ func relateDevicesAndLink(repo *Repository, link *Link, startKm *proto.Kilometer if km == nil || km.CoordinateSystem == "" { continue } - convertedKm, err := convertKilometer(repo, km, startKm.CoordinateSystem) + convertedKm, err := ConvertKilometer(repo, km, startKm.CoordinateSystem) if err != nil { return err } @@ -761,7 +761,7 @@ func interrelateLinkAndTurnout(repo *Repository, linkZeroKm *proto.Kilometer, tp tp.turnout.bindLinkPort(tp.port, linkPort) linkPort.link.bindTurnoutPort(linkPort.port, tp) tpKm := tp.turnout.findBoundaryKmByPort(tp.port) - tpKm, _ = convertKilometer(repo, tpKm, linkZeroKm.CoordinateSystem) + tpKm, _ = ConvertKilometer(repo, tpKm, linkZeroKm.CoordinateSystem) offset := number.Abs(tpKm.Value - linkZeroKm.Value) tp.turnout.bindLinkPosition(tp.port, &LinkPosition{ link: linkPort.link, @@ -820,7 +820,7 @@ func slopeRelateLink(repo *Repository) error { func calculateLinkSegment(repo *Repository, startKm *proto.Kilometer, endKm *proto.Kilometer) (*LinkPosition, *LinkPosition, error) { - endKm, err := convertKilometer(repo, endKm, startKm.CoordinateSystem) + endKm, err := ConvertKilometer(repo, endKm, startKm.CoordinateSystem) if err != nil { return nil, nil, err } diff --git a/repository/transponder.go b/repository/transponder.go index 3c7941f..e660b6a 100644 --- a/repository/transponder.go +++ b/repository/transponder.go @@ -34,3 +34,7 @@ func (t *Transponder) bindLinkPosition(position *LinkPosition) { func (t *Transponder) LinkPosition() *LinkPosition { return t.linkPosition } + +func (t *Transponder) Km() *proto.Kilometer { + return t.km +} diff --git a/util/number/number.go b/util/number/number.go index e4f9e10..d2d80a4 100644 --- a/util/number/number.go +++ b/util/number/number.go @@ -1,7 +1,5 @@ package number -import "math" - type Number interface { ~int | ~int8 | ~int16 | ~int32 | int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | @@ -31,6 +29,8 @@ func Max[T Number](a T, b T) T { } func Abs[T Number](num T) T { - abs := math.Abs(float64(num)) - return T(abs) + if num < 0 { + return -num + } + return num }