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

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 (
"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(),
entry := sim.GetEntry(turnoutId)
if entry == nil {
return nil
}
}
})
return turnoutState
state := system.Switch2jZdj9StateComponent.Get(entry)
return &TurnoutState{Id: turnoutId, Normal: state.IsNormal(), Reverse: state.IsReverse(), Turning: state.IsTurning()}
}

View File

@ -1,6 +1,8 @@
package simulation
import (
"sync"
"joylink.club/ecs"
"joylink.club/rtsssimulation/repository"
)
@ -12,6 +14,7 @@ var (
type Simulation struct {
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) {

View File

@ -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)