2023-09-20 15:14:38 +08:00
|
|
|
package simulation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"joylink.club/ecs"
|
2023-09-21 16:28:13 +08:00
|
|
|
"joylink.club/rtsssimulation/entities"
|
2023-09-20 15:14:38 +08:00
|
|
|
"joylink.club/rtsssimulation/repository"
|
2023-09-21 16:28:13 +08:00
|
|
|
"joylink.club/rtsssimulation/system"
|
|
|
|
"time"
|
2023-09-20 15:14:38 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
simulationManager = make(map[ecs.WorldId]*Simulation)
|
|
|
|
)
|
|
|
|
|
|
|
|
type Simulation struct {
|
|
|
|
world ecs.World
|
|
|
|
repo *repository.Repository
|
|
|
|
}
|
|
|
|
|
2023-09-21 15:21:06 +08:00
|
|
|
func (s *Simulation) GetWorld() ecs.World {
|
2023-09-20 15:14:38 +08:00
|
|
|
return s.world
|
|
|
|
}
|
|
|
|
|
2023-09-21 15:21:06 +08:00
|
|
|
func (s *Simulation) GetRepo() *repository.Repository {
|
2023-09-20 15:14:38 +08:00
|
|
|
return s.repo
|
|
|
|
}
|
|
|
|
|
2023-09-21 16:28:13 +08:00
|
|
|
func CreateSimulation(repo *repository.Repository, systemTypes ...system.Type) int {
|
|
|
|
var systems []ecs.ISystem
|
|
|
|
systemTypeMap := make(map[system.Type]bool)
|
|
|
|
for _, systemType := range systemTypes {
|
|
|
|
switch systemType {
|
|
|
|
case system.SWITCH_ZDJ9_2:
|
|
|
|
systems = append(systems, system.NewSwitch2jZdj9System())
|
|
|
|
case system.RELAY:
|
|
|
|
systems = append(systems, system.NewRelaySystem())
|
|
|
|
case system.DEBUG:
|
|
|
|
systems = append(systems, system.NewDebugSystem())
|
|
|
|
}
|
|
|
|
systemTypeMap[systemType] = true
|
|
|
|
}
|
|
|
|
wc := &WorldConfig{
|
|
|
|
Systems: systems,
|
|
|
|
Tick: 200,
|
|
|
|
InitTime: time.Now(),
|
|
|
|
}
|
|
|
|
world := InitializeWorld(wc)
|
2023-09-20 15:14:38 +08:00
|
|
|
sim := &Simulation{
|
|
|
|
world: world,
|
|
|
|
repo: repo,
|
|
|
|
}
|
|
|
|
simulationManager[world.Id()] = sim
|
2023-09-21 16:28:13 +08:00
|
|
|
//添加实体
|
|
|
|
entities.CreateTurnoutEntries(world, repo.TurnoutList(), systemTypeMap)
|
2023-09-25 10:34:21 +08:00
|
|
|
if systemTypeMap[system.RELAY] {
|
|
|
|
entities.CreateRelayEntries(world, repo.RelayList())
|
|
|
|
}
|
2023-09-21 16:28:13 +08:00
|
|
|
//初始化组件
|
|
|
|
initComponent(world)
|
|
|
|
//启动
|
|
|
|
world.StartUp()
|
2023-09-20 15:14:38 +08:00
|
|
|
return int(world.Id())
|
|
|
|
}
|
|
|
|
|
|
|
|
func DestroySimulation(id ecs.WorldId) {
|
|
|
|
delete(simulationManager, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func FindSimulation(id ecs.WorldId) *Simulation {
|
|
|
|
return simulationManager[id]
|
|
|
|
}
|
2023-09-21 16:28:13 +08:00
|
|
|
|
|
|
|
func initComponent(world ecs.World) {
|
|
|
|
|
|
|
|
}
|