rts-sim-module/entities/switch_entity.go
2023-09-26 16:30:39 +08:00

99 lines
3.0 KiB
Go

package entities
import (
"time"
"github.com/yohamta/donburi/component"
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
"joylink.club/rtsssimulation/repository"
"joylink.club/rtsssimulation/simulation"
"joylink.club/rtsssimulation/system"
)
type Position int
const (
SK Position = iota //四开(不会起名字)
D //定位
F //反位
)
// CreateSwitch2jzdj9Entity 双机zdj9
// 默认定位
func CreateSwitch2jzdj9Entity(w ecs.World, switchId string) *ecs.Entry {
e := w.Create(system.EntityIdentityComponent,
system.Switch2jZdj9StateComponent,
system.PercentageDeviceState1Component,
system.PercentageDeviceState2Component)
system.EntityIdentityComponent.Set(e, &system.EntityIdentity{Id: switchId})
//电路
system.Switch2jZdj9StateComponent.Set(e, system.NewSwitch2jZdj9State())
//J1
j1 := system.NewPercentageDeviceStateM() //定位
system.PercentageDeviceState1Component.Set(e, j1)
//J2
j2 := system.NewPercentageDeviceStateM() //定位
system.PercentageDeviceState2Component.Set(e, j2)
return e
}
func CreateTurnoutEntries(world ecs.World, turnouts []*repository.Turnout) []*ecs.Entry {
var entries []*ecs.Entry
for _, turnout := range turnouts {
if len(turnout.RelayGroups()) == 0 {
continue
}
var components []component.IComponentType
components = append(components, system.EntityIdentityComponent)
components = append(components, system.Switch2jZdj9StateComponent)
entry := world.Create(components...)
entries = append(entries, entry)
system.EntityIdentityComponent.Set(entry, &system.EntityIdentity{Id: turnout.Id()})
system.Switch2jZdj9StateComponent.Set(entry, system.NewSwitch2jZdj9State())
}
return entries
}
func TurnToNormal(worldId ecs.WorldId, turnoutId string) {
sim := simulation.FindSimulation(worldId)
system.Switch2jZdj9DriveYc(sim.World(), turnoutId, true)
system.Switch2jZdj9DriveFc(sim.World(), turnoutId, true)
go func() {
<-time.After(1 * time.Second)
system.Switch2jZdj9DriveYc(sim.World(), turnoutId, false)
system.Switch2jZdj9DriveFc(sim.World(), turnoutId, false)
}()
}
func TurnToReverse(worldId ecs.WorldId, turnoutId string) {
sim := simulation.FindSimulation(worldId)
system.Switch2jZdj9DriveYc(sim.World(), turnoutId, true)
system.Switch2jZdj9DriveDc(sim.World(), turnoutId, true)
go func() {
<-time.After(1 * time.Second)
system.Switch2jZdj9DriveYc(sim.World(), turnoutId, false)
system.Switch2jZdj9DriveDc(sim.World(), turnoutId, false)
}()
}
func GetState(worldId ecs.WorldId, turnoutId string) Position {
sim := simulation.FindSimulation(worldId)
query := ecs.NewQuery(filter.Contains(system.EntityIdentityComponent, system.Switch2jZdj9StateComponent))
var position Position
// 查找道岔位置
query.Each(sim.World(), func(e *ecs.Entry) {
if turnoutId == system.EntityIdentityComponent.Get(e).Id {
state := system.Switch2jZdj9StateComponent.Get(e)
if state.J1_DB_K9 {
position = D
} else if state.J1_FB_K10 {
position = F
} else {
position = SK
}
}
})
return position
}