void LeoECS() { _world = new EcsWorld(); JobMoveSystem.MinSpeed = param.minSpeed; JobMoveSystem.MaxSpeed = param.maxSpeed; JobMoveSystem.BoidScale = boidScale; JobMoveSystem.WallScale = param.wallScale * 0.5f; JobMoveSystem.WallDistance = param.wallDistance; JobMoveSystem.WallWeight = param.wallWeight; JobMoveSystem.NeighborFov = MathFast.Cos(param.neighborFov * MathFast.Deg2Rad); JobMoveSystem.NeighborDistance = param.neighborDistance; JobMoveSystem.SeparationWeight = param.separationWeight; JobMoveSystem.AlignmentWeight = param.alignmentWeight; JobMoveSystem.CohesionWeight = param.cohesionWeight; JobMoveSystem.EntitiesCount = boidCount; JobMoveSystem.NumBoidsPerJob = boidCount / System.Environment.ProcessorCount; if (BoidsVisualisationSystem._nativeMatrices.IsCreated) { BoidsVisualisationSystem._nativeMatrices.Dispose(); } BoidsVisualisationSystem._nativeMatrices = new NativeArray <Matrix4x4> (boidCount, Allocator.Persistent); BoidsVisualisationSystem._matrices = new Matrix4x4[boidCount]; var timeSystem = new TimeSystem(); _systems = new EcsSystems(_world); _systems .Add(timeSystem) .Add(new BoidsSimulationSystem()) .Add(new JobMoveSystem()) .Add(new BoidsVisualisationSystem() { mesh = mesh, material = material, scale = boidScale }) .Inject(timeSystem) .Initialize(); var random = new RngXorShift(853); BoidEntityData.Create(boidCount); for (int i = 0; i < boidCount; ++i) { var initSpeed = param.initSpeed; //init them with these default values var boid = _world.CreateEntityWith <BoidEntityData> (); boid.id = i; boid.Position = new Float3(random.GetFloat(), random.GetFloat(), random.GetFloat()); var vel = new Float3(random.GetFloat(), random.GetFloat(), random.GetFloat()); vel.Normalize(); vel.Scale(initSpeed); boid.Velocity = vel; boid.Acceleration = Float3.Zero; } _world.ProcessDelayedUpdates(); }
void OnDestroy() { if (_systems != null) { // destroy systems logical group. _systems.Dispose(); _systems = null; // destroy world. _world.Dispose(); _world = null; BoidEntityData.Dispose(); BoidsVisualisationSystem._nativeMatrices.Dispose(); } }