Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function MoveVehicle()
- {
- /*
- This function should do the following:
- - increment investigator_vehicle's miles_travelled property by its speed_value property
- - add a random amount between -0.01 and 0.01 to investigator_vehicle's rumble_value property
- - use the constrain function to constrain investigator_vehicle's rumble_value property to values between 0.09 and 1.21
- - call the TurnCarMotor function passing investigator_vehicle as an argument
- */
- investigator_vehicle.miles_travelled +=investigator_vehicle.speed_value;
- investigator_vehicle.rumble_value += random(-0.01,0.01);
- investigator_vehicle.rumble_value = constrain(investigator_vehicle.rumble_value,0.09,1.21);
- TurnCarMotor(investigator_vehicle);
- }
- function SwapLanes(car)
- {
- /*
- This function should do the following:
- - move car from one lane to the other.
- - do the move in a single step without any extra animation.
- - use Lane_CoordinateA and Lane_CoordinateB to effect the change.
- - finally you should return car at the end of the function.
- hint: You will need to modify the x_position property of car.
- */
- if (car.x_position == Lane_CoordinateA) {
- car.x_position = Lane_CoordinateB;
- } else {
- car.x_position = Lane_CoordinateA;
- }
- return car;
- }
- function CheckAhead( target_car )
- {
- /*
- This function should do the following:
- - determine if target_car is in the same lane and less than 200px behind any of the cars in carObjectsList.
- - do this by traversing carObjectsList and comparing each car's miles_travelled property to that of target_car.
- - if you find a car that matches these requirements then return the car object. Otherwise return false.
- */
- for (i = 0; i < carObjectsList.length; i++) {
- var distanceToVehicle = (carObjectsList[i].miles_travelled - target_car.miles_travelled);
- var inTheSameLane = abs(carObjectsList[i].x_position - target_car.x_position) < 200;
- if (distanceToVehicle < 200 && distanceToVehicle > 0 && inTheSameLane) {
- return carObjectsList[i];
- }
- }
- }
- //////////////DO NOT CHANGE CODE BELOW THIS LINE//////////////////
- var investigator_vehicle;
- var roadWidth;
- var roadLeftEdge;
- var Lane_CoordinateA;
- var Lane_CoordinateB;
- var carImages = {};
- var carObjectsList = [
- { x_position: 500, y_position: 0, miles_travelled: -200, vehicle_variety: 'redCar', number_plate: 'SMPFQ8', speed_value: 2, exhaust: [ ]} , { x_position: 500, y_position: 0, miles_travelled: 200, vehicle_variety: 'redCar', number_plate: 'R1IARB', speed_value: 2, exhaust: [ ]} , { x_position: 500, y_position: 0, miles_travelled: 600, vehicle_variety: 'blueCar', number_plate: 'ZM7PHX', speed_value: 2, exhaust: [ ]} , { x_position: 500, y_position: 0, miles_travelled: 1000, vehicle_variety: 'whiteCar', number_plate: 'FV5TQQ', speed_value: 2, exhaust: [ ]} , { x_position: 500, y_position: 0, miles_travelled: 1400, vehicle_variety: 'whiteCar', number_plate: 'MKR8YO', speed_value: 2, exhaust: [ ]} , { x_position: 300, y_position: 0, miles_travelled: 1800, vehicle_variety: 'redCar', number_plate: 'HYM3LW', speed_value: 2, exhaust: [ ]} , { x_position: 500, y_position: 0, miles_travelled: 2200, vehicle_variety: 'redCar', number_plate: '7HYC06', speed_value: 2, exhaust: [ ]} , { x_position: 500, y_position: 0, miles_travelled: 2600, vehicle_variety: 'greenCar', number_plate: '77FM2B', speed_value: 2, exhaust: [ ]} , { x_position: 300, y_position: 0, miles_travelled: 3000, vehicle_variety: 'greenCar', number_plate: '8WIDQV', speed_value: 2, exhaust: [ ]} , { x_position: 500, y_position: 0, miles_travelled: 3400, vehicle_variety: 'blueCar', number_plate: 'IHERLS', speed_value: 2, exhaust: [ ]} , { x_position: 300, y_position: 0, miles_travelled: 3800, vehicle_variety: 'redCar', number_plate: 'KZZ3F2', speed_value: 2, exhaust: [ ]} , { x_position: 500, y_position: 0, miles_travelled: 4200, vehicle_variety: 'redCar', number_plate: 'JES4TJ', speed_value: 2, exhaust: [ ]} , { x_position: 500, y_position: 0, miles_travelled: 4600, vehicle_variety: 'greenCar', number_plate: 'J75RML', speed_value: 2, exhaust: [ ]} , { x_position: 300, y_position: 0, miles_travelled: 5000, vehicle_variety: 'blueCar', number_plate: 'TBP01Y', speed_value: 2, exhaust: [ ]} , { x_position: 500, y_position: 0, miles_travelled: 5400, vehicle_variety: 'greenCar', number_plate: '19PLKA', speed_value: 2, exhaust: [ ]} , { x_position: 500, y_position: 0, miles_travelled: 5800, vehicle_variety: 'redCar', number_plate: 'EYTA74', speed_value: 2, exhaust: [ ]} , { x_position: 300, y_position: 0, miles_travelled: 6200, vehicle_variety: 'greenCar', number_plate: 'A2GPSS', speed_value: 2, exhaust: [ ]} , { x_position: 500, y_position: 0, miles_travelled: 6600, vehicle_variety: 'greenCar', number_plate: 'GXXC97', speed_value: 2, exhaust: [ ]} , { x_position: 300, y_position: 0, miles_travelled: 7000, vehicle_variety: 'blueCar', number_plate: 'J5ZY43', speed_value: 2, exhaust: [ ]} , { x_position: 300, y_position: 0, miles_travelled: 7400, vehicle_variety: 'whiteCar', number_plate: 'K2Y3CW', speed_value: 2, exhaust: [ ]}
- ];
- function preload()
- {
- var carTypes = [
- "detective",
- "redCar",
- "greenCar",
- "blueCar",
- "whiteCar",
- ];
- for(var i = 0; i < carTypes.length; i++)
- {
- carImages[carTypes[i]] = loadImage("cars/" + carTypes[i] + ".png");
- }
- }
- function setup()
- {
- createCanvas(800,800);
- roadWidth = 400;
- roadLeftEdge = 200;
- Lane_CoordinateA = 300;
- Lane_CoordinateB = 500;
- investigator_vehicle =
- {
- x_position: roadLeftEdge + roadWidth/4,
- y_position: 550,
- miles_travelled: 0,
- speed_value: 3,
- rumble_value: 0,
- vehicle_variety: 'detective',
- number_plate: '5L3UTH',
- exhaust: []
- }
- }
- function draw()
- {
- background(0);
- drawRoad();
- drawCars();
- ////////////////////// HANDLE DETECTIVE /////////////////////////
- MoveVehicle();
- var b2b = CheckAhead( investigator_vehicle );
- if(b2b)SwapLanes(investigator_vehicle);
- //////////////////////HANDLE THE OTHER CARS//////////////////////
- for(var i = 0; i < carObjectsList.length; i++)
- {
- carObjectsList[i].miles_travelled += carObjectsList[i].speed_value;
- carObjectsList[i].y_position = investigator_vehicle.y_position - carObjectsList[i].miles_travelled + investigator_vehicle.miles_travelled;
- }
- }
- /////////////////////////DRAWING FUNCTIONS////////////////////////
- function drawRoad()
- {
- stroke(100);
- fill(50);
- rect(roadLeftEdge,0,roadWidth,800);
- stroke(255);
- for(var i = -1; i < 20; i++)
- {
- line(
- roadLeftEdge + roadWidth/2 , i * 100 + (investigator_vehicle.miles_travelled%100),
- roadLeftEdge + roadWidth/2 , i * 100 + 70 + (investigator_vehicle.miles_travelled%100)
- );
- }
- }
- function drawCars()
- {
- //draw the detective car
- image
- drawExhaust(investigator_vehicle);
- image
- (
- carImages["detective"],
- investigator_vehicle.x_position - carImages["detective"].width/2 + random(-investigator_vehicle.rumble_value, investigator_vehicle.rumble_value),
- investigator_vehicle.y_position + random(-investigator_vehicle.rumble_value, investigator_vehicle.rumble_value)
- );
- //draw all other cars
- for(var i = 0; i < carObjectsList.length; i ++)
- {
- if(carObjectsList[i].y_position < height && carObjectsList[i].y_position > -height/2)
- {
- image(
- carImages[carObjectsList[i].vehicle_variety],
- carObjectsList[i].x_position - carImages[carObjectsList[i].vehicle_variety].width/2,
- carObjectsList[i].y_position
- );
- TurnCarMotor(carObjectsList[i]);
- drawExhaust(carObjectsList[i]);
- }
- }
- }
- function TurnCarMotor(car)
- {
- car.exhaust.push({size: 2, x: car.x_position, y: car.y_position + carImages[car.vehicle_variety].height});
- for(var i = car.exhaust.length -1; i >= 0 ; i--)
- {
- car.exhaust[i].y += max(0.75, car.speed_value/3);
- if(car.vehicle_variety != "detective")car.exhaust[i].y += (investigator_vehicle.speed_value - car.speed_value);
- car.exhaust[i].x += random(-1,1);
- car.exhaust[i].size += 0.5;
- if(car.exhaust[i].y > height || car.exhaust[i].y < 0)
- {
- car.exhaust.splice(i,1);
- }
- }
- }
- function drawExhaust(car)
- {
- noStroke();
- for(var i = 0; i < car.exhaust.length; i++)
- {
- var alpha = map(car.exhaust[i].size, 0, 40, 50,0);
- fill(125,alpha);
- ellipse(car.exhaust[i].x + 20, car.exhaust[i].y , car.exhaust[i].size);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement