From 60c8bad65d707f2618d597b239ac4a9e56e49efe Mon Sep 17 00:00:00 2001 From: weizhihong Date: Wed, 27 Sep 2023 14:11:53 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=AD=98=E5=82=A8=E4=BB=BF=E7=9C=9F?= =?UTF-8?q?=E7=9A=84=E5=AE=9E=E4=BD=93=E3=80=91=E3=80=90=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E9=81=93=E5=B2=94=E7=8A=B6=E6=80=81=E9=80=BB=E8=BE=91=E5=A4=84?= =?UTF-8?q?=E7=90=86=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entities/switch_entity.go | 22 +++++++--------------- simulation/simulation.go | 22 +++++++++++++++++++--- simulation/world/init.go | 21 +++++++++++++++++---- 3 files changed, 43 insertions(+), 22 deletions(-) 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)