public void Execute([ReadOnly] ref VehiclePathing vehicle)
            {
                if (vehicle.curvePos < 1.0f)
                {
                    int         rI = vehicle.RoadIndex;
                    int         lI = vehicle.LaneIndex;
                    RoadSection rs = RoadSections[rI];

                    float backOfVehiclePos  = vehicle.curvePos - rs.vehicleHalfLen;
                    float frontOfVehiclePos = vehicle.curvePos + rs.vehicleHalfLen;

                    int OccupationIndexStart = math.max(0, (int)(math.floor(backOfVehiclePos * rs.occupationLimit)));
                    int OccupationIndexEnd;

                    // It is possible now that the next road link is not next to the current one in memory (e.g. merging)
                    if (rs.linkNext != -1)
                    {
                        OccupationIndexEnd = (int)(math.floor(frontOfVehiclePos * rs.occupationLimit));
                    }
                    else
                    {
                        OccupationIndexEnd = math.min(rs.occupationLimit - 1,
                                                      (int)(math.floor(frontOfVehiclePos * rs.occupationLimit)));
                    }

                    FillOccupation(OccupationIndexStart, OccupationIndexEnd, rI, lI, vehicle.speed,
                                   vehicle.vehicleId, rs.linkNext, rs.occupationLimit);
                    if (vehicle.LaneIndex != vehicle.WantedLaneIndex)
                    {
                        FillOccupation(OccupationIndexStart, OccupationIndexEnd, rI, vehicle.WantedLaneIndex,
                                       vehicle.speed, vehicle.vehicleId, rs.linkNext, rs.occupationLimit);
                    }
                }
            }
            public void Execute(ref VehiclePathing vehicle)
            {
                RoadSection rs = RoadSections[vehicle.RoadIndex];

                OccupyLane(ref vehicle, ref rs, vehicle.LaneIndex);
                if (vehicle.LaneIndex != vehicle.WantedLaneIndex)
                {
                    OccupyLane(ref vehicle, ref rs, vehicle.WantedLaneIndex);
                }
            }
Exemplo n.º 3
0
            public void Execute(Entity entity, int index, ref Spawner thisSpawner)
            {
                if (thisSpawner.delaySpawn > 0)
                {
                    thisSpawner.delaySpawn--;
                }
                else
                {
                    RoadSection rs = RoadSections[thisSpawner.RoadIndex];
                    Interlocked.Increment(ref vehicleUID);

                    float backOfVehiclePos  = thisSpawner.Time - rs.vehicleHalfLen;
                    float frontOfVehiclePos = thisSpawner.Time + rs.vehicleHalfLen;

                    int occupationIndexStart = math.max(0,
                                                        (int)(math.floor(backOfVehiclePos * rs.occupationLimit)));
                    int occupationIndexEnd = math.min(rs.occupationLimit - 1,
                                                      (int)(math.floor(frontOfVehiclePos * rs.occupationLimit)));

                    if (!Occupied(occupationIndexStart, occupationIndexEnd, thisSpawner.RoadIndex,
                                  thisSpawner.LaneIndex))
                    {
                        int   vehiclePoolIndex   = GetSpawnVehicleIndex(ref thisSpawner.random, thisSpawner.poolSpawn);
                        float speedMult          = VehiclePool[vehiclePoolIndex].VehicleSpeed;
                        float speedRangeSelected = thisSpawner.random.NextFloat(0.0f, 1.0f);
                        float initialSpeed       = 0.0f;

                        var vehicleEntity = EntityCommandBuffer.Instantiate(index, VehiclePool[vehiclePoolIndex].VehiclePrefab);
                        EntityCommandBuffer.SetComponent(index, vehicleEntity, new VehiclePathing
                        {
                            vehicleId          = vehicleUID,
                            RoadIndex          = thisSpawner.RoadIndex, LaneIndex = (byte)thisSpawner.LaneIndex,
                            WantedLaneIndex    = (byte)thisSpawner.LaneIndex, speed = initialSpeed,
                            speedRangeSelected = speedRangeSelected, speedMult = speedMult,
                            targetSpeed        = initialSpeed, curvePos = thisSpawner.Time,
                            random             = new Unity.Mathematics.Random(thisSpawner.random.NextUInt(1, uint.MaxValue))
                        });
                        var heading = CatmullRom.GetTangent(rs.p0, rs.p1, rs.p2, rs.p3, 0.0f);

                        EntityCommandBuffer.SetComponent(index, vehicleEntity, new VehicleTargetPosition {
                            IdealPosition = thisSpawner.Position
                        });
                        EntityCommandBuffer.SetComponent(index, vehicleEntity, new VehiclePhysicsState {
                            Position = thisSpawner.Position, Heading = heading, SpeedMult = speedMult
                        });
                    }

                    var speedInverse = 1.0f / thisSpawner.minSpeed;

                    thisSpawner.delaySpawn = (int)Constants.VehicleLength + thisSpawner.random.NextInt((int)(speedInverse * 10.0f), (int)(speedInverse * 120.0f));
                }
            }
            private void OccupyLane(ref VehiclePathing vehicle, ref RoadSection rs, int laneIndex)
            {
                int i0 = CurvePositionToOccupancyIndex(vehicle.RoadIndex, laneIndex, vehicle.curvePos - rs.vehicleHalfLen);
                int i1 = CurvePositionToOccupancyIndex(vehicle.RoadIndex, laneIndex, vehicle.curvePos + rs.vehicleHalfLen);

                var d = new VehicleSlotData {
                    Speed = vehicle.speed, Id = vehicle.vehicleId
                };

                OccupancyToVehicleMap.Add(i0, d);
                if (i0 != i1)
                {
                    OccupancyToVehicleMap.Add(i1, d);
                }
            }
Exemplo n.º 5
0
            public void Execute(int index)
            {
                RoadSection rs = RoadSections[index];

                // Compute RoadSection Bounds
                float3 minBounds = math.min(rs.p1, rs.p2);
                float3 maxBounds = math.max(rs.p1, rs.p2);

                // Enqueue road sections that overlap
                float sqDistance = SqDistance(IntersectionPositionRadiusSq.xyz, minBounds, maxBounds);

                bool overlap = (sqDistance <= IntersectionPositionRadiusSq.w);

                RoadSectionsToCheck[index] = overlap ? index : -1;
            }
Exemplo n.º 6
0
            public void Execute(ref VehiclePathing vehicle)
            {
                int         rI = vehicle.RoadIndex;
                int         lI = vehicle.LaneIndex;
                RoadSection rs = RoadSections[rI];

                float frontOfVehiclePos = vehicle.curvePos + rs.vehicleHalfLen;

                // Look ahead one slot
                int slot = (int)(math.floor(frontOfVehiclePos * rs.occupationLimit)) + 1;

                if (slot >= rs.occupationLimit)
                {
                    if (rs.linkNext != -1)
                    {
                        rI   = rs.linkNext;
                        rs   = RoadSections[rI];
                        slot = 0;
                    }
                    else
                    {
                        --slot;
                    }
                }

                int sampleIndex = rI * Constants.RoadIndexMultiplier + slot * Constants.RoadLanes + lI;

                float wantedSpeed = vehicle.speedMult * math.lerp(rs.minSpeed, rs.maxSpeed, vehicle.speedRangeSelected);

                vehicle.WantNewLane = 0;

                vehicle.targetSpeed = math.min(wantedSpeed, Occupancy[sampleIndex].speed);

                var lerpAmount = DeltaTimeSeconds < 1.0f ? DeltaTimeSeconds : 1.0f;

                vehicle.speed = math.lerp(vehicle.speed, vehicle.targetSpeed, lerpAmount);

                if (math.abs(vehicle.targetSpeed - wantedSpeed) > 0.10f * wantedSpeed)
                {
                    vehicle.WantNewLane = 1;
                }
            }
Exemplo n.º 7
0
            public void Execute(ref VehiclePathing p, ref VehicleTargetPosition pos)
            {
                RoadSection rs = RoadSections[p.RoadIndex];

                quaternion direction = CatmullRom.GetOrientation(rs.p0, rs.p1, rs.p2, rs.p3, p.curvePos);

                float3 currentLane     = GetOffsetFromLaneIndex(p.LaneIndex, direction, rs.width);
                float3 destinationLane = GetOffsetFromLaneIndex(p.WantedLaneIndex, direction, rs.width);

                pos.IdealPosition += math.lerp(currentLane, destinationLane, math.smoothstep(0, 1, p.LaneTween));

                if (p.LaneIndex != p.WantedLaneIndex)
                {
                    p.LaneTween += DeltaTimeSeconds * p.speed * 0.1f;
                    if (p.LaneTween >= 1.0f)
                    {
                        p.LaneIndex = p.WantedLaneIndex;
                        p.LaneTween = 0.0f;
                    }
                }
            }