rts-sim-module/entity/balise.go

59 lines
1.9 KiB
Go

package entity
import (
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/component/component_data"
"joylink.club/rtsssimulation/repository"
"joylink.club/rtsssimulation/repository/model/proto"
)
var BaliseBaseComponentTypeArr = []ecs.IComponentType{component.UidType, component.BaliseFixedTelegramType,
component.LinkPositionType, component.KmType, component.BaliseWorkStateType}
// LoadBalises 加载应答器实体
func LoadBalises(w ecs.World) error {
data := GetWorldData(w)
balises := data.Repo.ResponderList()
for _, b := range balises {
be := newBaliseEntity(w, b, data)
switch b.TransponderType() {
case proto.Transponder_FB:
be.AddComponent(component.BaliseFB)
case proto.Transponder_WB:
be.AddComponent(component.BaliseWB)
case proto.Transponder_DB:
be.AddComponent(component.BaliseDB)
case proto.Transponder_VB:
be.AddComponent(component.BaliseVB)
case proto.Transponder_IB:
be.AddComponent(component.BaliseIB)
}
}
return nil
}
func newBaliseEntity(w ecs.World, td *repository.Transponder, worldData *component.WorldData) *ecs.Entry {
uid := td.Id()
entry, ok := worldData.EntityMap[uid]
if !ok {
entry = w.Entry(w.Create(BaliseBaseComponentTypeArr...))
component.UidType.SetValue(entry, component.Uid{Id: uid})
component.BaliseWorkStateType.SetValue(entry, component.BaliseWorkState{Work: true})
component.BaliseFixedTelegramType.Set(entry, &component.BaliseTelegram{
Telegram: td.FixedTelegram(),
UserTelegram: td.FixedUserTelegram(),
})
if proto.Transponder_IB == td.BaliseType() || proto.Transponder_VB == td.BaliseType() {
entry.AddComponent(component.BaliseVariableTelegramType)
}
component.LinkPositionType.SetValue(entry, component_data.LinkPosition{
LinkId: td.LinkPosition().Link().Id(),
Offset: td.LinkPosition().Offset(),
})
component.KmType.Set(entry, td.Km())
worldData.EntityMap[uid] = entry
}
return entry
}