重构singleton组件及相关方法接口

调整repo目录结构和命名
初步开始重构仿真实体加载
This commit is contained in:
walker 2023-12-28 16:49:28 +08:00
parent 50b588ca15
commit c4bc8c640f
30 changed files with 278 additions and 57 deletions

View File

@ -1,11 +0,0 @@
package model
// 继电器位置
type RelayPosition int
const (
// 继电器位置-后(表示落下/反位)
RelayPosition_H RelayPosition = 0
// 继电器位置-前(表示吸起/定位)
RelayPosition_Q RelayPosition = 1
)

View File

@ -0,0 +1,42 @@
package singleton
import (
"joylink.club/ecs"
"joylink.club/ecs/filter"
"joylink.club/rtsssimulation/modelrepo"
)
// 加载并初始化单例组件
func LoadSingletons(w ecs.World, r modelrepo.Repo) {
loadWorldRepo(w, r)
loadWorldTime(w)
loadUidEntityIndex(w)
}
var worldRepoQuery = ecs.NewQuery(filter.Contains(WorldRepoType))
var worldTimeQuery = ecs.NewQuery(filter.Contains(WorldTimeType))
var uidEntityIndexQuery = ecs.NewQuery(filter.Contains(EntityUidIndexType))
func GetWorldTime(w ecs.World) *WorldTime {
entry, _ := worldTimeQuery.First(w)
if entry == nil {
panic("不存在世界时间组件")
}
return WorldTimeType.Get(entry)
}
func GetWorldRepo(w ecs.World) *WorldRepo {
entry, _ := worldRepoQuery.First(w)
if entry == nil {
panic("不存在世界数据组件")
}
return WorldRepoType.Get(entry)
}
func GetEntityUidIndex(w ecs.World) *EntityUidIndex {
entry, _ := uidEntityIndexQuery.First(w)
if entry == nil {
panic("不存在UidEntityIndex组件")
}
return EntityUidIndexType.Get(entry)
}

View File

@ -0,0 +1,19 @@
package singleton
import (
"joylink.club/ecs"
"joylink.club/rtsssimulation/modelrepo"
)
var WorldRepoType = ecs.NewComponentType[WorldRepo]()
type WorldRepo struct {
r modelrepo.Repo
}
func loadWorldRepo(w ecs.World, r modelrepo.Repo) {
entry := w.Entry(w.Create(WorldRepoType))
WorldRepoType.Set(entry, &WorldRepo{
r: r,
})
}

View File

@ -0,0 +1,34 @@
package singleton
import (
"sync"
"joylink.club/ecs"
)
var EntityUidIndexType = ecs.NewComponentType[EntityUidIndex]()
// 对象实体Uid索引
type EntityUidIndex struct {
mu sync.RWMutex
entityMap map[string]ecs.Entity
}
func (idx *EntityUidIndex) Add(uid string, entity ecs.Entity) {
idx.mu.Lock()
defer idx.mu.Unlock()
idx.entityMap[uid] = entity
}
func (idx *EntityUidIndex) Get(uid string) ecs.Entity {
idx.mu.RLock()
defer idx.mu.RUnlock()
return idx.entityMap[uid]
}
func loadUidEntityIndex(w ecs.World) {
entry := w.Entry(w.Create(EntityUidIndexType))
EntityUidIndexType.Set(entry, &EntityUidIndex{
entityMap: make(map[string]ecs.Entity, 512),
})
}

View File

@ -0,0 +1,37 @@
package singleton
import (
"time"
"joylink.club/ecs"
)
var WorldTimeType = ecs.NewComponentType[WorldTime]()
type WorldTime struct {
time int64
}
func (w *WorldTime) SetTime(t time.Time) {
w.time = t.UnixMilli()
}
// 增加时间
func (w *WorldTime) Run(ms int) {
w.time += int64(ms)
}
// 获取时间
func (w *WorldTime) GetTime() time.Time {
return time.UnixMilli(w.time)
}
// 获取时间戳
func (w *WorldTime) GetMilli() int64 {
return w.time
}
func loadWorldTime(w ecs.World) {
entry := w.Entry(w.Create(WorldTimeType))
WorldTimeType.Set(entry, &WorldTime{time: time.Now().UnixMilli()})
}

22
entity/cg_load.go Normal file
View File

@ -0,0 +1,22 @@
package entity
import (
"joylink.club/ecs"
"joylink.club/rtsssimulation/component/singleton"
"joylink.club/rtsssimulation/modelrepo"
"joylink.club/rtsssimulation/modelrepo/model"
)
// 加载城轨仿真实体
func LoadCg(w ecs.World, repo modelrepo.Repo) error {
singleton.LoadSingletons(w, repo)
for _, s := range repo.GetEcses() {
// 加载道岔实体
loadTurnouts(w, s.GetTurnouts())
}
return nil
}
func loadTurnouts(w ecs.World, turnout []model.Turnout) {
panic("unimplemented")
}

14
init.go
View File

@ -3,6 +3,7 @@ package rtss_simulation
import (
"joylink.club/ecs"
"joylink.club/rtsssimulation/entity"
"joylink.club/rtsssimulation/modelrepo"
"joylink.club/rtsssimulation/repository"
"joylink.club/rtsssimulation/sys"
)
@ -19,3 +20,16 @@ func NewSimulation(repo *repository.Repository) (ecs.World, error) {
err := entity.Load(w, repo)
return w, err
}
// 加载城轨仿真
func LoadCgSimulation(repo modelrepo.Repo) (ecs.World, error) {
w := ecs.NewWorld(RtssSimulationTick)
// 加载组件实体
err := entity.LoadCg(w, repo)
if err != nil {
return nil, err
}
// 加载系统
sys.BindSystem(w)
return w, err
}

View File

@ -1,4 +1,4 @@
package repo
package modelrepo
import "testing"

View File

@ -1,4 +1,4 @@
package repo
package modelrepo
// 构建错误记录
type ErrorRecord struct {

View File

@ -1,4 +1,4 @@
package repo
package modelrepo
import (
"fmt"
@ -6,7 +6,7 @@ import (
"strconv"
"strings"
"joylink.club/rtsssimulation/cgrepo/dto"
"joylink.club/rtsssimulation/modelrepo/dto"
)
// 城轨uid

View File

@ -1,19 +1,19 @@
package repo
package modelrepo
import "sync"
// Repo管理
type repoManager struct {
repoMap map[string]CgRepo
repoMap map[string]Repo
lock sync.Mutex
}
var defaultManager = &repoManager{
repoMap: make(map[string]CgRepo),
repoMap: make(map[string]Repo),
}
// 获取Repo
func GetRepo(id string) CgRepo {
func GetRepo(id string) Repo {
manager := defaultManager
manager.lock.Lock()
defer manager.lock.Unlock()
@ -25,7 +25,7 @@ func GetRepo(id string) CgRepo {
}
// 保存Repo
func SaveRepo(r CgRepo) {
func SaveRepo(r Repo) {
manager := defaultManager
manager.lock.Lock()
defer manager.lock.Unlock()
@ -41,7 +41,7 @@ func RemoveRepo(id string) {
}
// 获取或构建Repo,若存在则返回已存在的,若不存在则构建保存并返回
func GetOrBuildRepo(id string, builder func() (CgRepo, error)) (CgRepo, error) {
func GetOrBuildRepo(id string, builder func() (Repo, error)) (Repo, error) {
r := GetRepo(id)
if r == nil {
// 构建模型Repo

View File

@ -0,0 +1,22 @@
package model
// 开关
type PowerSwitch interface {
Model
// 编号
Code()
}
// 信号状态表示灯
type Lamp interface {
Model
// 编号
Code()
}
// 蜂鸣器/报警电铃
type Buzzer interface {
Model
// 编号
Code()
}

12
modelrepo/model/ibp.go Normal file
View File

@ -0,0 +1,12 @@
package model
// IBP盘(综合后备盘)
type Ibp interface {
Model
// 获取所有开关
GetPowerSwitches() []PowerSwitch
// 获取所有信号状态表示灯
GetLamps() []Lamp
// 获取所有蜂鸣器
GetBuzzer() []Buzzer
}

View File

@ -1,6 +1,6 @@
package model
// 站台屏蔽门
type PSD interface {
type Psd interface {
Model
}

32
modelrepo/model/relay.go Normal file
View File

@ -0,0 +1,32 @@
package model
// 继电器位置
type Relay_Position int
const (
// 继电器位置-后(表示落下/反位)
RelayPos_H Relay_Position = 0
// 继电器位置-前(表示吸起/定位)
RelayPos_Q Relay_Position = 1
)
// 继电器型号
type Relay_Model int
const (
RelayModel_JPXC_1000 Relay_Model = 1
RelayModel_JPXC_1700 Relay_Model = 2
RelayModel_JWJXC_480 Relay_Model = 3
RelayModel_JWJXC_H125_80 Relay_Model = 4
RelayModel_JWXC_1700 Relay_Model = 5
RelayModel_JWXC_H340 Relay_Model = 6
RelayModel_JYJXC_160_260 Relay_Model = 7
RelayModel_JZXC_H18 Relay_Model = 8
)
// 继电器
type Relay interface {
Model
Code()
Model() Relay_Model
}

View File

@ -7,21 +7,23 @@ type Station interface {
Name() string
// 是否设备集中站
IsEcs() bool
// 获取IBP
GetIbp() Ibp
}
// 设备集中站(Equipment centralized station)
type Ecs interface {
type EcStation interface {
Station
// 获取所有道岔
Turnouts() []Turnout
GetTurnouts() []Turnout
// 获取所有信号机
Signals() []Signal
GetSignals() []Signal
// 获取所有站台屏蔽门
PSDs() []PSD
GetPsds() []Psd
// 获取所有物理检测区段
PhysicalSections() []PhysicalSection
GetPhysicalSections() []PhysicalSection
// 获取联锁驱采表
CiQCTable()
GetCiQCTable()
}
// 联锁驱采表
@ -42,5 +44,5 @@ type CiCJPos interface {
// 继电器uid
RelayId() string
// 继电器位置
Pos() RelayPosition
Pos() Relay_Position
}

View File

@ -16,9 +16,9 @@ const (
type Turnout_Port int
const (
TPort_A Turnout_Port = 0
TPort_B Turnout_Port = 1
TPort_C Turnout_Port = 2
TPort_A Turnout_Port = 1
TPort_B Turnout_Port = 2
TPort_C Turnout_Port = 3
)
// 道岔牵引类型
@ -26,9 +26,9 @@ type TurnoutTractionType int
const (
// ZDJ9单机牵引
TTT_ZDJ9_1 TurnoutTractionType = 0
TTT_ZDJ9_1 TurnoutTractionType = 1
// ZDJ9双机牵引
TTT_ZDJ9_2 TurnoutTractionType = 1
TTT_ZDJ9_2 TurnoutTractionType = 2
)
// 道岔

View File

@ -3,8 +3,8 @@ package modelimpl
import (
"sync/atomic"
"joylink.club/rtsssimulation/cgrepo/dto"
"joylink.club/rtsssimulation/cgrepo/model"
"joylink.club/rtsssimulation/modelrepo/dto"
"joylink.club/rtsssimulation/modelrepo/model"
)
// link生成uid基础值

View File

@ -1,7 +1,7 @@
package modelimpl
import (
"joylink.club/rtsssimulation/cgrepo/model"
"joylink.club/rtsssimulation/modelrepo/model"
)
type PhysicalSection struct {

View File

@ -1,6 +1,6 @@
package modelimpl
import "joylink.club/rtsssimulation/cgrepo/model"
import "joylink.club/rtsssimulation/modelrepo/model"
var _turnout model.Turnout = (*Turnout)(nil)

View File

@ -1,16 +1,18 @@
package repo
package modelrepo
import "joylink.club/rtsssimulation/cgrepo/model"
import "joylink.club/rtsssimulation/modelrepo/model"
type CgRepo interface {
type Repo interface {
// 模型仓库id
Id() string
// 获取所有设备集中站
Ecses() []model.Ecs
GetEcses() []model.EcStation
// 获取所有道岔
Turnouts() []model.Turnout
// 通过uid查询模型对象
FindByUid(uid string) model.Model
GetTurnouts() []model.Turnout
// 获取所有信号机
GetSignals() []model.Signal
// 获取所有站台屏蔽门
GetPsds() []model.Psd
}
type IdMap interface {
@ -29,7 +31,7 @@ type IdMap interface {
// turnoutMap map[string]*impl.Turnout // 道岔map,key为uid
// }
// func BuildFrom(msgs *dto.CgRepo) (CgRepo, *ErrorRecord) {
// func BuildFrom(msgs *dto.modelrepo) (modelrepo, *ErrorRecord) {
// errRecord := NewErrorRecord()
// idMapping := BuildIdMapping(msgs, errRecord)
// if errRecord.HasError() {

View File

@ -5,7 +5,7 @@ package message;
option go_package = "./repo/dto";
//
message CgRepo {
message modelrepo {
string id = 1;
// 线
repeated Line lines = 2;

View File

@ -2,25 +2,19 @@ package sys
import (
"joylink.club/ecs"
"joylink.club/ecs/filter"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/component/singleton"
)
// 世界世界更新系统
// 世界时间更新系统
type WorldTimeSys struct {
query *ecs.Query
}
func NewWorldTimeSys() *WorldTimeSys {
return &WorldTimeSys{
query: ecs.NewQuery(filter.Contains(component.WorldDataType)),
}
return &WorldTimeSys{}
}
func (s *WorldTimeSys) Update(w ecs.World) {
entry, _ := s.query.First(w)
data := component.WorldDataType.Get(entry)
data.Time += int64(w.Tick())
singleton.GetWorldTime(w).Run(w.Tick())
// t := time.UnixMilli(data.Time)
// fmt.Println(t)
}