Example #1
0
        public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
        {
            for (int j = 0; j < vehiclePrefabs.Count; j++)
            {
                // A primary entity needs to be called before additional entities can be used
                Entity vehiclePrefab = conversionSystem.CreateAdditionalEntity(this);
                var    prefabData    = new VehiclePrefabData
                {
                    VehiclePrefab = conversionSystem.GetPrimaryEntity(vehiclePrefabs[j]),
                    VehicleSpeed  = j < speedMultipliers.Length ? speedMultipliers[j] : 3.0f
                };
                dstManager.AddComponentData(vehiclePrefab, prefabData);
            }

            var trafficSettings = new TrafficSettingsData
            {
                GlobalSpeedFactor = globalSpeedFactor,
                PathSegments      = pathSegments,
                MaxCars           = maxCars
            };

            dstManager.AddComponentData(entity, trafficSettings);
        }
Example #2
0
        private Rigidbody _playerRidigbody; // Will only be on the player controlled car

        void OneTimeSetup()
        {
            var allRoads = GetComponentGroup(typeof(RoadSection)).ToComponentDataArray <RoadSection>(Allocator.TempJob);
            var settings = GetComponentGroup(typeof(TrafficSettingsData)).ToComponentDataArray <TrafficSettingsData>(Allocator.TempJob);
            var vehicles = GetComponentGroup(typeof(VehiclePrefabData)).ToComponentDataArray <VehiclePrefabData>(Allocator.TempJob);


            if (settings.Length == 0 || vehicles.Length == 0 || allRoads.Length == 0)
            {
                allRoads.Dispose();
                vehicles.Dispose();
                settings.Dispose();
                return;
            }

            trafficSettings = settings[0];

            // Copy the vehicle pool for prefabs
            vehiclePool = new NativeArray <VehiclePrefabData>(vehicles.Length, Allocator.Persistent);
            for (int v = 0; v < vehicles.Length; v++)
            {
                if (!EntityManager.HasComponent <VehiclePathing>(vehicles[v].VehiclePrefab))
                {
                    EntityManager.AddComponentData(vehicles[v].VehiclePrefab, new VehiclePathing());
                }

                if (!EntityManager.HasComponent <VehicleTargetPosition>(vehicles[v].VehiclePrefab))
                {
                    EntityManager.AddComponentData(vehicles[v].VehiclePrefab, new VehicleTargetPosition());
                }

                if (!EntityManager.HasComponent <VehiclePhysicsState>(vehicles[v].VehiclePrefab))
                {
                    EntityManager.AddComponentData(vehicles[v].VehiclePrefab, new VehiclePhysicsState());
                }

                vehiclePool[v] = vehicles[v];
            }

            // for now just copy everything
            roadSections = new NativeArray <RoadSection>(allRoads.Length, Allocator.Persistent);

            for (int a = 0; a < allRoads.Length; a++)
            {
                roadSections[allRoads[a].sortIndex] = allRoads[a];
            }

            numSections = roadSections.Length;


#if UNITY_EDITOR && USE_OCCUPANCY_DEBUG
            OccupancyDebug.queueSlots   = new NativeArray <Occupation>(numSections * Constants.RoadIndexMultiplier, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
            OccupancyDebug.roadSections = new NativeArray <RoadSection>(numSections, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            for (int a = 0; a < roadSections.Length; a++)
            {
                OccupancyDebug.roadSections[a] = roadSections[a];
            }
#endif
            doneOneTimeInit = true;
            allRoads.Dispose();
            vehicles.Dispose();
            settings.Dispose();
        }