2023-09-20 15:14:38 +08:00
|
|
|
package simulation
|
|
|
|
|
|
|
|
import (
|
2023-09-27 14:11:53 +08:00
|
|
|
"sync"
|
|
|
|
|
2023-09-20 15:14:38 +08:00
|
|
|
"joylink.club/ecs"
|
|
|
|
"joylink.club/rtsssimulation/repository"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
simulationManager = make(map[ecs.WorldId]*Simulation)
|
|
|
|
)
|
|
|
|
|
|
|
|
type Simulation struct {
|
2023-09-27 14:11:53 +08:00
|
|
|
world ecs.World
|
|
|
|
repo *repository.Repository
|
|
|
|
entityMap sync.Map
|
2023-09-20 15:14:38 +08:00
|
|
|
}
|
|
|
|
|
2023-09-26 10:25:00 +08:00
|
|
|
func (s *Simulation) World() ecs.World {
|
2023-09-20 15:14:38 +08:00
|
|
|
return s.world
|
|
|
|
}
|
|
|
|
|
2023-09-26 10:25:00 +08:00
|
|
|
func (s *Simulation) Repo() *repository.Repository {
|
2023-09-20 15:14:38 +08:00
|
|
|
return s.repo
|
|
|
|
}
|
|
|
|
|
2023-09-27 14:11:53 +08:00
|
|
|
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 {
|
2023-09-20 15:14:38 +08:00
|
|
|
sim := &Simulation{
|
|
|
|
world: world,
|
|
|
|
repo: repo,
|
|
|
|
}
|
|
|
|
simulationManager[world.Id()] = sim
|
2023-09-27 14:11:53 +08:00
|
|
|
return sim
|
2023-09-20 15:14:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func DestroySimulation(id ecs.WorldId) {
|
|
|
|
delete(simulationManager, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func FindSimulation(id ecs.WorldId) *Simulation {
|
|
|
|
return simulationManager[id]
|
|
|
|
}
|