Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.40 KB | None | 0 0
  1.  
  2. function MoveVehicle()
  3. {
  4. /*
  5. This function should do the following:
  6. - increment investigator_vehicle's miles_travelled property by its speed_value property
  7. - add a random amount between -0.01 and 0.01 to investigator_vehicle's rumble_value property
  8. - use the constrain function to constrain investigator_vehicle's rumble_value property to values between 0.09 and 1.21
  9. - call the TurnCarMotor function passing investigator_vehicle as an argument
  10. */
  11. investigator_vehicle.miles_travelled +=investigator_vehicle.speed_value;
  12. investigator_vehicle.rumble_value += random(-0.01,0.01);
  13. investigator_vehicle.rumble_value = constrain(investigator_vehicle.rumble_value,0.09,1.21);
  14. TurnCarMotor(investigator_vehicle);
  15. }
  16.  
  17.  
  18. function SwapLanes(car)
  19. {
  20. /*
  21. This function should do the following:
  22. - move car from one lane to the other.
  23. - do the move in a single step without any extra animation.
  24. - use Lane_CoordinateA and Lane_CoordinateB to effect the change.
  25. - finally you should return car at the end of the function.
  26. hint: You will need to modify the x_position property of car.
  27. */
  28.  
  29. if (car.x_position == Lane_CoordinateA) {
  30. car.x_position = Lane_CoordinateB;
  31. } else {
  32. car.x_position = Lane_CoordinateA;
  33. }
  34. return car;
  35.  
  36.  
  37. }
  38.  
  39.  
  40. function CheckAhead( target_car )
  41. {
  42. /*
  43. This function should do the following:
  44. - determine if target_car is in the same lane and less than 200px behind any of the cars in carObjectsList.
  45. - do this by traversing carObjectsList and comparing each car's miles_travelled property to that of target_car.
  46. - if you find a car that matches these requirements then return the car object. Otherwise return false.
  47. */
  48.  
  49. for (i = 0; i < carObjectsList.length; i++) {
  50.  
  51. var distanceToVehicle = (carObjectsList[i].miles_travelled - target_car.miles_travelled);
  52. var inTheSameLane = abs(carObjectsList[i].x_position - target_car.x_position) < 200;
  53.  
  54. if (distanceToVehicle < 200 && distanceToVehicle > 0 && inTheSameLane) {
  55.  
  56. return carObjectsList[i];
  57. }
  58.  
  59.  
  60. }
  61.  
  62.  
  63. }
  64.  
  65.  
  66. //////////////DO NOT CHANGE CODE BELOW THIS LINE//////////////////
  67.  
  68. var investigator_vehicle;
  69.  
  70. var roadWidth;
  71. var roadLeftEdge;
  72. var Lane_CoordinateA;
  73. var Lane_CoordinateB;
  74. var carImages = {};
  75.  
  76. var carObjectsList = [
  77. { 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: [ ]}
  78. ];
  79.  
  80.  
  81.  
  82. function preload()
  83. {
  84. var carTypes = [
  85. "detective",
  86. "redCar",
  87. "greenCar",
  88. "blueCar",
  89. "whiteCar",
  90. ];
  91.  
  92. for(var i = 0; i < carTypes.length; i++)
  93. {
  94. carImages[carTypes[i]] = loadImage("cars/" + carTypes[i] + ".png");
  95. }
  96. }
  97.  
  98. function setup()
  99. {
  100. createCanvas(800,800);
  101.  
  102. roadWidth = 400;
  103. roadLeftEdge = 200;
  104. Lane_CoordinateA = 300;
  105. Lane_CoordinateB = 500;
  106.  
  107. investigator_vehicle =
  108. {
  109. x_position: roadLeftEdge + roadWidth/4,
  110. y_position: 550,
  111. miles_travelled: 0,
  112. speed_value: 3,
  113. rumble_value: 0,
  114. vehicle_variety: 'detective',
  115. number_plate: '5L3UTH',
  116. exhaust: []
  117. }
  118.  
  119.  
  120. }
  121.  
  122.  
  123.  
  124. function draw()
  125. {
  126. background(0);
  127.  
  128.  
  129.  
  130. drawRoad();
  131. drawCars();
  132.  
  133. ////////////////////// HANDLE DETECTIVE /////////////////////////
  134.  
  135.  
  136. MoveVehicle();
  137. var b2b = CheckAhead( investigator_vehicle );
  138. if(b2b)SwapLanes(investigator_vehicle);
  139.  
  140.  
  141. //////////////////////HANDLE THE OTHER CARS//////////////////////
  142.  
  143. for(var i = 0; i < carObjectsList.length; i++)
  144. {
  145. carObjectsList[i].miles_travelled += carObjectsList[i].speed_value;
  146. carObjectsList[i].y_position = investigator_vehicle.y_position - carObjectsList[i].miles_travelled + investigator_vehicle.miles_travelled;
  147. }
  148.  
  149. }
  150.  
  151. /////////////////////////DRAWING FUNCTIONS////////////////////////
  152.  
  153. function drawRoad()
  154. {
  155. stroke(100);
  156. fill(50);
  157. rect(roadLeftEdge,0,roadWidth,800);
  158. stroke(255);
  159.  
  160. for(var i = -1; i < 20; i++)
  161. {
  162. line(
  163. roadLeftEdge + roadWidth/2 , i * 100 + (investigator_vehicle.miles_travelled%100),
  164. roadLeftEdge + roadWidth/2 , i * 100 + 70 + (investigator_vehicle.miles_travelled%100)
  165. );
  166. }
  167. }
  168.  
  169. function drawCars()
  170. {
  171. //draw the detective car
  172.  
  173. image
  174. drawExhaust(investigator_vehicle);
  175. image
  176. (
  177. carImages["detective"],
  178. investigator_vehicle.x_position - carImages["detective"].width/2 + random(-investigator_vehicle.rumble_value, investigator_vehicle.rumble_value),
  179. investigator_vehicle.y_position + random(-investigator_vehicle.rumble_value, investigator_vehicle.rumble_value)
  180. );
  181.  
  182. //draw all other cars
  183.  
  184. for(var i = 0; i < carObjectsList.length; i ++)
  185. {
  186. if(carObjectsList[i].y_position < height && carObjectsList[i].y_position > -height/2)
  187. {
  188. image(
  189. carImages[carObjectsList[i].vehicle_variety],
  190. carObjectsList[i].x_position - carImages[carObjectsList[i].vehicle_variety].width/2,
  191. carObjectsList[i].y_position
  192. );
  193. TurnCarMotor(carObjectsList[i]);
  194.  
  195. drawExhaust(carObjectsList[i]);
  196. }
  197. }
  198.  
  199. }
  200.  
  201. function TurnCarMotor(car)
  202. {
  203.  
  204. car.exhaust.push({size: 2, x: car.x_position, y: car.y_position + carImages[car.vehicle_variety].height});
  205.  
  206. for(var i = car.exhaust.length -1; i >= 0 ; i--)
  207. {
  208.  
  209. car.exhaust[i].y += max(0.75, car.speed_value/3);
  210. if(car.vehicle_variety != "detective")car.exhaust[i].y += (investigator_vehicle.speed_value - car.speed_value);
  211. car.exhaust[i].x += random(-1,1);
  212. car.exhaust[i].size += 0.5;
  213.  
  214. if(car.exhaust[i].y > height || car.exhaust[i].y < 0)
  215. {
  216. car.exhaust.splice(i,1);
  217. }
  218. }
  219. }
  220.  
  221.  
  222. function drawExhaust(car)
  223. {
  224. noStroke();
  225. for(var i = 0; i < car.exhaust.length; i++)
  226. {
  227. var alpha = map(car.exhaust[i].size, 0, 40, 50,0);
  228. fill(125,alpha);
  229. ellipse(car.exhaust[i].x + 20, car.exhaust[i].y , car.exhaust[i].size);
  230.  
  231. }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement