public void Execute(Entity entity, int index, ref Position position, ref Velocity velocity, [ReadOnly] ref State state, ref LocalToWorld localToWorld) { var maxDist = Math.Max(0.0f, carInFront[index].Dist - Highway1.MinDistBetweenCars * 3.0f); var offset = Math.Min(maxDist, velocity.Value * dt); position.Pos = Wrap(position.Pos + offset, laneLength); position.MergingTime = Math.Max(0.0f, position.MergingTime - dt); position.OvertakingTime = Math.Max(0.0f, position.OvertakingTime - dt); velocity.Value = Mathf.Lerp(velocity.Value, carInFront[index].Velocity, dt); if (position.OvertakingTime == 0.0f && position.MergingTime == 0.0f) { position.FromLane = position.Lane; velocity.Value = state.DefaultSpeed; if (state.OvertakeEagerness > carInFront[index].Velocity / state.DefaultSpeed) { if (position.Lane < Highway1.NumLanes - 1 && carInFront[index].Dist < state.LeftMergeDistance) { position.OvertakingTime = 3.0f; velocity.Value = state.MaxSpeed; queue.Enqueue(new Merge { entity = entity, fromLane = position.Lane, toLane = position.Lane + 1, }); } else if (position.Lane > 0) { queue.Enqueue(new Merge { entity = entity, fromLane = position.Lane, toLane = position.Lane - 1, }); } } } #if USE_ENTITY_CAR float x0, z0, rotation0; highway1.GetPosition( position.Pos , position.Lane , out x0 , out z0 , out rotation0 ); float x1, z1, rotation1; highway1.GetPosition( position.Pos / highway1.length(position.Lane) * highway1.length(position.FromLane) , position.FromLane , out x1 , out z1 , out rotation1 ); var time = position.MergingTime / MaxMergingTime; localToWorld = new LocalToWorld { Value = float4x4.TRS( new Vector3(Mathf.Lerp(x0, x1, time), 0.0f, Mathf.Lerp(z0, z1, time)) , Quaternion.Euler(0, Mathf.Lerp(rotation0, rotation1, time) * Mathf.Rad2Deg, 0) , new float3(1.0f) ) }; #endif }
/// <summary> /// Adds a car to the highway in the given lane. Checks to make sure it's not on top of other cars. Returns null if a car couldn't be placed. /// </summary> public Car AddCar(float lane) { return(AddCarUnsafe(Random.Range(0, highway1.length(lane)), lane)); }