百分比组件

This commit is contained in:
xzb 2023-09-21 15:22:22 +08:00
parent a55346e121
commit fbe07064a3
7 changed files with 102 additions and 118 deletions

View File

@ -19,7 +19,7 @@ func CreateStationPsdsCircuitEntity(w ecs.World, stationId string, psds []umi.IP
cc := make([]donburi.IComponentType, 6)
cc = append(cc, system.EntityIdentityComponent)
cc = append(cc, system.PsdStateComponent)
cc = append(cc, system.MovableObjectComponent)
cc = append(cc, system.PercentageDeviceStateComponent)
if psd.IsS() {
cc = append(cc, tags.STag)
}
@ -43,7 +43,7 @@ func CreateStationPsdsCircuitEntity(w ecs.World, stationId string, psds []umi.IP
psdId := psd.(umi.IDeviceModel).GetId()
system.EntityIdentityComponent.Set(psdEntry, &system.EntityIdentity{Id: psdId})
system.PsdStateComponent.Set(psdEntry, system.NewPsdState())
system.MovableObjectComponent.Set(psdEntry, system.NewMovableObject())
system.PercentageDeviceStateComponent.Set(psdEntry, system.NewPercentageDeviceStateL())
}
//
return circuit

View File

@ -10,18 +10,16 @@ import (
func CreateSwitch2jzdj9Entity(w ecs.World, switchId string) *ecs.Entry {
e := w.Create(system.EntityIdentityComponent,
system.Switch2jZdj9StateComponent,
system.MovableObject1Component,
system.MovableObject2Component)
system.PercentageDeviceState1Component,
system.PercentageDeviceState2Component)
system.EntityIdentityComponent.Set(e, &system.EntityIdentity{Id: switchId})
//电路
system.Switch2jZdj9StateComponent.Set(e, system.NewSwitch2jZdj9State())
//J1
j1 := system.NewMovableObject()
j1.Value = system.J1Range
system.MovableObject1Component.Set(e, j1)
j1 := system.NewPercentageDeviceStateM() //定位
system.PercentageDeviceState1Component.Set(e, j1)
//J2
j2 := system.NewMovableObject()
j2.Value = system.J2Range
system.MovableObject2Component.Set(e, j2)
j2 := system.NewPercentageDeviceStateM() //定位
system.PercentageDeviceState2Component.Set(e, j2)
return e
}

@ -1 +1 @@
Subproject commit adb9cf9a0005fc74565da8753f4f7577834d27d4
Subproject commit 4997760ffb1284a7898ab84164671d5e21ec9248

View File

@ -11,7 +11,7 @@ func InitializeWorld(config *WorldConfig) ecs.World {
world := ecs.NewWorld(config.Tick)
// 添加系统
world.AddSystem(system.NewTimerSystem())
world.AddSystem(system.NewMovableSystem())
world.AddSystem(system.NewPercentageMovableSystem())
for _, sys := range config.Systems {
world.AddSystem(sys)
}

View File

@ -1,74 +0,0 @@
package system
import (
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
)
// MovableObject 一维移动的物体
type MovableObject struct {
Value int64
//移动方向0-不动1-变大,-1 -变小
direction int8
}
func NewMovableObject() *MovableObject {
return &MovableObject{Value: 0, direction: 0}
}
// MovableObjectComponent 一维移动的物体组件
var MovableObjectComponent = ecs.NewComponentType[MovableObject]()
var MovableObject1Component = ecs.NewComponentType[MovableObject]()
var MovableObject2Component = ecs.NewComponentType[MovableObject]()
type MovableSystem struct {
query *ecs.Query
}
func NewMovableSystem() *MovableSystem {
return &MovableSystem{
query: ecs.NewQuery(filter.Or(filter.Contains(MovableObjectComponent), filter.Contains(MovableObject1Component), filter.Contains(MovableObject2Component))),
}
}
// Update world 执行
func (me *MovableSystem) Update(w ecs.World) {
me.query.Each(w, func(e *ecs.Entry) {
if e.HasComponent(MovableObjectComponent) {
MovableObjectComponent.Get(e).move(w.Tick())
}
if e.HasComponent(MovableObject1Component) {
MovableObject1Component.Get(e).move(w.Tick())
}
if e.HasComponent(MovableObject2Component) {
MovableObject2Component.Get(e).move(w.Tick())
}
})
}
////////////////////////////////////////////////
func (me *MovableObject) move(tick int) {
if me.direction != 0 {
if me.direction > 0 {
me.Value += int64(tick)
} else {
me.Value -= int64(tick)
}
}
}
// Pause 暂停
func (me *MovableObject) Pause() {
me.direction = 0
}
// Forward 前进
func (me *MovableObject) Forward() {
me.direction = 1
}
// Retreat 后退
func (me *MovableObject) Retreat() {
me.direction = -1
}

View File

@ -0,0 +1,80 @@
package system
import (
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
)
// PercentageDeviceState 百分比设备
// 起始位置p=0,终点位置p=100000,总路程100000
type PercentageDeviceState struct {
//速度,矢量
V int64
//位置,[PD_L,PD_M]
P int64
//百分比,[0,100]
R int8
}
func NewPercentageDeviceStateL() *PercentageDeviceState {
return &PercentageDeviceState{V: 0, P: PD_L, R: 0}
}
func NewPercentageDeviceStateM() *PercentageDeviceState {
return &PercentageDeviceState{V: 0, P: PD_M, R: 100}
}
// 百分比设备位置范围
const (
PD_L int64 = 0
PD_M int64 = 100000
)
// PercentageDeviceStateComponent 一维移动的物体组件
var PercentageDeviceStateComponent = ecs.NewComponentType[PercentageDeviceState]()
var PercentageDeviceState1Component = ecs.NewComponentType[PercentageDeviceState]()
var PercentageDeviceState2Component = ecs.NewComponentType[PercentageDeviceState]()
type PercentageMovableSystem struct {
query *ecs.Query
}
func NewPercentageMovableSystem() *PercentageMovableSystem {
return &PercentageMovableSystem{
query: ecs.NewQuery(filter.Or(filter.Contains(PercentageDeviceStateComponent), filter.Contains(PercentageDeviceState1Component), filter.Contains(PercentageDeviceState2Component))),
}
}
// Update world 执行
func (me *PercentageMovableSystem) Update(w ecs.World) {
me.query.Each(w, func(e *ecs.Entry) {
if e.HasComponent(PercentageDeviceStateComponent) {
PercentageDeviceStateComponent.Get(e).move()
}
if e.HasComponent(PercentageDeviceState1Component) {
PercentageDeviceState1Component.Get(e).move()
}
if e.HasComponent(PercentageDeviceState2Component) {
PercentageDeviceState2Component.Get(e).move()
}
})
}
////////////////////////////////////////////////
func (me *PercentageDeviceState) move() {
me.P += me.V
if me.P < PD_L {
me.P = PD_L
}
if me.P > PD_M {
me.P = PD_M
}
//
me.R = int8((float64(me.P) / float64(PD_M)) * float64(100))
}
func (me *PercentageDeviceState) isToL() bool {
return me.P <= PD_L
}
func (me *PercentageDeviceState) isToM() bool {
return me.P >= PD_M
}

View File

@ -607,47 +607,27 @@ func (me *Switch2jZdj9System) calculateFBJ(w ecs.World, switchEntry *ecs.Entry,
}
// 道岔转动运算
// j1 完全转动须4000ms
// j2 完全转动须5000ms
const (
J1Range int64 = 4000
J2Range int64 = 5000
)
// 道岔转动运算
// j1 完全转动须4000ms
// j2 完全转动须5000ms
// j1 完全转动须4000ms -> v=25/ms
// j2 完全转动须5000ms -> v=20/ms
// j.Value 0-反位 jRange-定位
func (me *Switch2jZdj9System) calculateMove(w ecs.World, switchEntry *ecs.Entry, state *Switch2jZdj9State) {
j1 := MovableObject1Component.Get(switchEntry)
j1 := PercentageDeviceState1Component.Get(switchEntry)
j1.V = int64(state.J1_Traction) * 25 * int64(w.Tick())
//
if j1.Value < 0 {
j1.Value = 0
} else if j1.Value > J1Range {
j1.Value = J1Range
}
j1.direction = state.J1_Traction
//
j2 := MovableObject2Component.Get(switchEntry)
//
if j2.Value < 0 {
j2.Value = 0
} else if j2.Value > J2Range {
j2.Value = J2Range
}
j2.direction = state.J2_Traction
j2 := PercentageDeviceState2Component.Get(switchEntry)
j2.V = int64(state.J2_Traction) * 20 * int64(w.Tick())
}
// 转辙机对外端子9、10运算
func (me *Switch2jZdj9System) calculateK9K10(w ecs.World, switchEntry *ecs.Entry, state *Switch2jZdj9State) {
j1 := MovableObject1Component.Get(switchEntry)
j2 := MovableObject2Component.Get(switchEntry)
j1 := PercentageDeviceState1Component.Get(switchEntry)
j2 := PercentageDeviceState2Component.Get(switchEntry)
//
state.J1_DB_K9 = j1.Value >= J1Range
state.J1_FB_K10 = j1.Value <= 0
state.J1_DB_K9 = j1.isToM()
state.J1_FB_K10 = j1.isToL()
//
state.J2_DB_K9 = j2.Value >= J2Range
state.J2_FB_K10 = j2.Value <= 0
state.J2_DB_K9 = j2.isToM()
state.J2_FB_K10 = j2.isToL()
}
// 转辙机电源控制