【存储仿真的实体】【获取道岔状态逻辑处理】

This commit is contained in:
weizhihong 2023-09-27 14:11:53 +08:00
parent 0e0c8d4093
commit 60c8bad65d
3 changed files with 43 additions and 22 deletions

View File

@ -3,7 +3,6 @@ package entities
import ( import (
"time" "time"
"github.com/yohamta/donburi/filter"
"joylink.club/ecs" "joylink.club/ecs"
"joylink.club/rtsssimulation/repository" "joylink.club/rtsssimulation/repository"
"joylink.club/rtsssimulation/simulation" "joylink.club/rtsssimulation/simulation"
@ -13,6 +12,7 @@ import (
type Position int type Position int
type TurnoutState struct { type TurnoutState struct {
Id string
Normal bool Normal bool
Reverse bool Reverse bool
Turning bool Turning bool
@ -78,18 +78,10 @@ func TurnToReverse(worldId ecs.WorldId, turnoutId string) {
func GetState(worldId ecs.WorldId, turnoutId string) *TurnoutState { func GetState(worldId ecs.WorldId, turnoutId string) *TurnoutState {
sim := simulation.FindSimulation(worldId) sim := simulation.FindSimulation(worldId)
query := ecs.NewQuery(filter.Contains(system.EntityIdentityComponent, system.Switch2jZdj9StateComponent)) entry := sim.GetEntry(turnoutId)
var turnoutState *TurnoutState if entry == nil {
// 查找道岔位置 return nil
query.Each(sim.World(), func(e *ecs.Entry) { }
if turnoutId == system.EntityIdentityComponent.Get(e).Id { state := system.Switch2jZdj9StateComponent.Get(entry)
state := system.Switch2jZdj9StateComponent.Get(e) return &TurnoutState{Id: turnoutId, Normal: state.IsNormal(), Reverse: state.IsReverse(), Turning: state.IsTurning()}
turnoutState = &TurnoutState{
Normal: state.IsNormal(),
Reverse: state.IsReverse(),
Turning: state.IsTurning(),
}
}
})
return turnoutState
} }

View File

@ -1,6 +1,8 @@
package simulation package simulation
import ( import (
"sync"
"joylink.club/ecs" "joylink.club/ecs"
"joylink.club/rtsssimulation/repository" "joylink.club/rtsssimulation/repository"
) )
@ -10,8 +12,9 @@ var (
) )
type Simulation struct { type Simulation struct {
world ecs.World world ecs.World
repo *repository.Repository repo *repository.Repository
entityMap sync.Map
} }
func (s *Simulation) World() ecs.World { func (s *Simulation) World() ecs.World {
@ -22,12 +25,25 @@ func (s *Simulation) Repo() *repository.Repository {
return s.repo 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{ sim := &Simulation{
world: world, world: world,
repo: repo, repo: repo,
} }
simulationManager[world.Id()] = sim simulationManager[world.Id()] = sim
return sim
} }
func DestroySimulation(id ecs.WorldId) { func DestroySimulation(id ecs.WorldId) {

View File

@ -13,16 +13,15 @@ import (
func CreateSimulation(repo *repository.Repository) ecs.WorldId { func CreateSimulation(repo *repository.Repository) ecs.WorldId {
wc := &WorldConfig{ wc := &WorldConfig{
Systems: []ecs.ISystem{system.NewTimerSystem(), system.NewPercentageMovableSystem(), system.NewSwitch2jZdj9System(), system.NewRelaySystem()}, Systems: []ecs.ISystem{system.NewSwitch2jZdj9System(), system.NewRelaySystem()},
Tick: 200, Tick: 200,
InitTime: time.Now(), InitTime: time.Now(),
} }
w := InitializeWorld(wc) w := InitializeWorld(wc)
simulation.CreateSimulation(repo, w) sim := simulation.CreateSimulation(repo, w)
//添加实体 //添加实体
entities.CreateSystemTimerEntity(w, wc.InitTime) entities.CreateSystemTimerEntity(w, wc.InitTime)
entities.CreateTurnoutEntries(w, repo.TurnoutList()) initDeviceEntries(w, sim, repo)
entities.CreateRelayEntries(w, repo.RelayList())
//初始化组件 //初始化组件
InitComponent(w, repo) InitComponent(w, repo)
//添加监听器 //添加监听器
@ -33,6 +32,20 @@ func CreateSimulation(repo *repository.Repository) ecs.WorldId {
return w.Id() 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 // InitializeWorld 初始化仿真world
func InitializeWorld(config *WorldConfig) ecs.World { func InitializeWorld(config *WorldConfig) ecs.World {
world := ecs.NewWorld(config.Tick) world := ecs.NewWorld(config.Tick)