diff --git a/entities/switch_entity.go b/entities/switch_entity.go index e3dec48..dd9c5b7 100644 --- a/entities/switch_entity.go +++ b/entities/switch_entity.go @@ -3,7 +3,6 @@ package entities import ( "time" - "github.com/yohamta/donburi/filter" "joylink.club/ecs" "joylink.club/rtsssimulation/repository" "joylink.club/rtsssimulation/simulation" @@ -13,6 +12,7 @@ import ( type Position int type TurnoutState struct { + Id string Normal bool Reverse bool Turning bool @@ -78,18 +78,10 @@ func TurnToReverse(worldId ecs.WorldId, turnoutId string) { func GetState(worldId ecs.WorldId, turnoutId string) *TurnoutState { sim := simulation.FindSimulation(worldId) - query := ecs.NewQuery(filter.Contains(system.EntityIdentityComponent, system.Switch2jZdj9StateComponent)) - var turnoutState *TurnoutState - // 查找道岔位置 - query.Each(sim.World(), func(e *ecs.Entry) { - if turnoutId == system.EntityIdentityComponent.Get(e).Id { - state := system.Switch2jZdj9StateComponent.Get(e) - turnoutState = &TurnoutState{ - Normal: state.IsNormal(), - Reverse: state.IsReverse(), - Turning: state.IsTurning(), - } - } - }) - return turnoutState + entry := sim.GetEntry(turnoutId) + if entry == nil { + return nil + } + state := system.Switch2jZdj9StateComponent.Get(entry) + return &TurnoutState{Id: turnoutId, Normal: state.IsNormal(), Reverse: state.IsReverse(), Turning: state.IsTurning()} } diff --git a/simulation/simulation.go b/simulation/simulation.go index d093d0d..77a1d48 100644 --- a/simulation/simulation.go +++ b/simulation/simulation.go @@ -1,6 +1,8 @@ package simulation import ( + "sync" + "joylink.club/ecs" "joylink.club/rtsssimulation/repository" ) @@ -10,8 +12,9 @@ var ( ) type Simulation struct { - world ecs.World - repo *repository.Repository + world ecs.World + repo *repository.Repository + entityMap sync.Map } func (s *Simulation) World() ecs.World { @@ -22,12 +25,25 @@ func (s *Simulation) Repo() *repository.Repository { return s.repo } -func CreateSimulation(repo *repository.Repository, world ecs.World) { +func (s *Simulation) AddEntry(id string, entity *ecs.Entry) { + s.entityMap.Store(id, entity) +} + +func (s *Simulation) GetEntry(id string) *ecs.Entry { + e, ok := s.entityMap.Load(id) + if ok { + return e.(*ecs.Entry) + } + return nil +} + +func CreateSimulation(repo *repository.Repository, world ecs.World) *Simulation { sim := &Simulation{ world: world, repo: repo, } simulationManager[world.Id()] = sim + return sim } func DestroySimulation(id ecs.WorldId) { diff --git a/simulation/world/init.go b/simulation/world/init.go index bee6476..f50e447 100644 --- a/simulation/world/init.go +++ b/simulation/world/init.go @@ -13,16 +13,15 @@ import ( func CreateSimulation(repo *repository.Repository) ecs.WorldId { wc := &WorldConfig{ - Systems: []ecs.ISystem{system.NewTimerSystem(), system.NewPercentageMovableSystem(), system.NewSwitch2jZdj9System(), system.NewRelaySystem()}, + Systems: []ecs.ISystem{system.NewSwitch2jZdj9System(), system.NewRelaySystem()}, Tick: 200, InitTime: time.Now(), } w := InitializeWorld(wc) - simulation.CreateSimulation(repo, w) + sim := simulation.CreateSimulation(repo, w) //添加实体 entities.CreateSystemTimerEntity(w, wc.InitTime) - entities.CreateTurnoutEntries(w, repo.TurnoutList()) - entities.CreateRelayEntries(w, repo.RelayList()) + initDeviceEntries(w, sim, repo) //初始化组件 InitComponent(w, repo) //添加监听器 @@ -33,6 +32,20 @@ func CreateSimulation(repo *repository.Repository) ecs.WorldId { return w.Id() } +func initDeviceEntries(w ecs.World, sim *simulation.Simulation, repo *repository.Repository) { + // 初始化道岔 + for _, turnout := range repo.TurnoutList() { + if len(turnout.RelayGroups()) == 0 { + continue + } + sim.AddEntry(turnout.Id(), entities.CreateSwitch2jzdj9Entity(w, turnout.Id())) + } + // 初始化继电器 + for _, relay := range repo.RelayList() { + sim.AddEntry(relay.Id(), entities.CreateRelayEntity(w, relay.Id())) + } +} + // InitializeWorld 初始化仿真world func InitializeWorld(config *WorldConfig) ecs.World { world := ecs.NewWorld(config.Tick)