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 = math.cos(math.radians(param.neighborFov)); 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 Unity.Mathematics.Random(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 = random.NextFloat3(1f); boid.Velocity = random.NextFloat3Direction() * initSpeed;; 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(); } }