Guest User

Autici

a guest
Jul 4th, 2015
209
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*jslint plusplus: true, sloppy: true, indent: 4 */
  2. (function () {
  3. "use strict";
  4. // this function is strict...
  5. }());
  6.  
  7. // RequestAnimFrame: a browser API for getting smooth animations
  8. window.requestAnimFrame = (function() {
  9. return window.requestAnimationFrame ||
  10. window.webkitRequestAnimationFrame ||
  11. window.mozRequestAnimationFrame ||
  12. window.oRequestAnimationFrame ||
  13. window.msRequestAnimationFrame ||
  14. function(callback) {
  15. window.setTimeout(callback, 1000 / 60);
  16. };
  17. })();
  18.  
  19. // Globals
  20. var canvas = null,
  21. ctx = null,
  22. background = null,
  23. car_sprite = null,
  24. game_data = null,
  25. CAR_WIDTH = 100,
  26. CAR_HEIGHT = 37,
  27. STEP_COUNT_MILLISECONDS = 1000 / 30,
  28. RACE_LENGTH = 20,
  29. RACE_FINISH_LINE_X = 770,
  30. iTime = 0,
  31. iFinishPlace = 1;
  32.  
  33. function clearCanvas() {
  34.  
  35. // clear canvas
  36. ctx.clearRect(0, 0, canvas.height, canvas.width);
  37. }
  38.  
  39. function drawBackground() {
  40.  
  41. clearCanvas();
  42. ctx.drawImage(background, 0, 0);
  43.  
  44. loadCarSprite();
  45. }
  46.  
  47. function loadBackground() {
  48.  
  49. // Load the timer
  50. background = new Image();
  51. background.src = 'race-scence.png';
  52. background.onload = drawBackground;
  53. }
  54.  
  55. function setupGameData() {
  56.  
  57. var json =
  58. {
  59. cars:
  60. [
  61. {
  62. "colour": 'blue',
  63. "x": 0,
  64. "y": 450,
  65. "spritex": 0,
  66. "spritey": 0,
  67. "graph": null,
  68. "step": 77,
  69. "position": null,
  70. "speed": 1
  71. },
  72. {
  73. "colour": 'green',
  74. "x": 0,
  75. "y": 470,
  76. "spritex": 0,
  77. "spritey": 37,
  78. "graph": null,
  79. "step": 65,
  80. "position": null,
  81. "speed": 10
  82. },
  83. {
  84. "colour": 'red',
  85. "x": 0,
  86. "y": 490,
  87. "spritex": 0,
  88. "spritey": 74,
  89. "graph": null,
  90. "step": 53,
  91. "position": null,
  92. "speed": 15
  93. },
  94. {
  95. "colour": 'green',
  96. "x": 0,
  97. "y": 510,
  98. "spritex": 0,
  99. "spritey": 111,
  100. "graph": null,
  101. "step": 39,
  102. "position": null,
  103. "speed": 20
  104. }
  105. ],
  106. graphs:
  107. [
  108. [0,5,10,20,40,60,70],
  109. [0,10,20,30,40,50,60],
  110. [0,20,39,40,50,55,58],
  111. [0,10,20,30,40,50,55],
  112. [0,25,45,47,49,50,52],
  113. [0,10,20,29,38,45,50],
  114. [0,15,20,25,30,40,45],
  115. [0,2,4,8,20,30,40],
  116. [0,5,10,15,20,25,30],
  117. [0,1,3,14,15,22,30],
  118. [0,5,11,14,17,22,25],
  119. [0,20,30,44,67,72,90],
  120. [0,2,7,24,47,52,65],
  121. [0,2,9,20,40,52,70]
  122. ]
  123. };
  124.  
  125. return json;
  126. }
  127.  
  128. function drawCar(car) {
  129.  
  130. // Draw the car onto the canvas
  131. ctx.drawImage(car_sprite,
  132. car.spritex, car.spritey,
  133. CAR_WIDTH, CAR_HEIGHT,
  134. car.x + car.step, car.y,
  135. CAR_WIDTH, CAR_HEIGHT);
  136.  
  137. drawText(car);
  138. }
  139.  
  140. function drawCars() {
  141.  
  142. var iCarCounter;
  143.  
  144. for(iCarCounter = 0; iCarCounter < game_data.cars.length; iCarCounter++) {
  145.  
  146. drawCar(game_data.cars[iCarCounter]);
  147. }
  148. }
  149.  
  150. function initCar(current_car) {
  151.  
  152. current_car.graph = 4; //Ovdje biram koji graf cu uzet 0-14
  153.  
  154. }
  155.  
  156. function initGameState() {
  157.  
  158. var iCarCounter;
  159.  
  160. for(iCarCounter = 0; iCarCounter < game_data.cars.length; iCarCounter++) {
  161.  
  162. initCar(game_data.cars[iCarCounter]);
  163.  
  164. }
  165. }
  166.  
  167. function getPositionAtTime(graph_index, percentageElapsed,current_car) {
  168.  
  169. var graph = game_data.graphs[graph_index],
  170. iNumberOfGraphPoints = graph.length,
  171. iGraphPosition = null,
  172. iFloor = null,
  173. iCeil = null,
  174. p = null;
  175. position = null;
  176.  
  177. iGraphPosition = (iNumberOfGraphPoints / 100) * percentageElapsed;
  178.  
  179. iFloor = Math.floor(iGraphPosition);
  180. iCeil = Math.ceil(iGraphPosition);
  181.  
  182. if(iGraphPosition === iFloor) {
  183.  
  184. position = graph[iFloor];
  185.  
  186. } else if(iGraphPosition === iCeil) {
  187.  
  188. position = graph[iCeil];
  189.  
  190. } else {
  191.  
  192. p = (graph[iCeil] - graph[iFloor]) / 100;
  193.  
  194. position = ((iGraphPosition - iFloor) * 100) * p + graph[iFloor];
  195.  
  196. }
  197.  
  198. return position * ( undefined === current_car.speed ? 1 : current_car.speed );
  199.  
  200. }
  201.  
  202. function redrawRoadSection() {
  203.  
  204. ctx.drawImage(background, 0, 400, 1000, 200, 0, 400, 1000, 200);
  205.  
  206. }
  207.  
  208. function graphPosToScreenPos() {
  209.  
  210. return (900 / 100) * (position / 60 * 100);
  211.  
  212. }
  213.  
  214. function updateDebugWindow() {
  215.  
  216. // Debug window
  217. var time = document.getElementById('time');
  218.  
  219. if(time !== null) {
  220.  
  221. time.value = iTime / 1000;
  222. }
  223. }
  224.  
  225.  
  226. function drawText(current_car) {
  227.  
  228. if(current_car.position !== null) {
  229.  
  230. ctx.strokeStyle = "black";
  231. ctx.font = "normal 12px Facebook Letter Faces";
  232. ctx.strokeText(current_car.position, RACE_FINISH_LINE_X + current_car.step + 110, current_car.y + 25);
  233.  
  234. }
  235.  
  236. }
  237.  
  238. function moveCar(iCarCounter) {
  239.  
  240. var current_car = game_data.cars[iCarCounter],
  241. seconds = iTime / 1000,
  242. percentageElapsed = (seconds / RACE_LENGTH) * 100,
  243. a = 20,
  244. velocity = 2,
  245. position = getPositionAtTime(current_car.graph, percentageElapsed,current_car);
  246.  
  247. if(current_car.x < RACE_FINISH_LINE_X) {
  248.  
  249. current_car.x = graphPosToScreenPos(position) + (velocity * seconds) + (1/2 * a * Math.pow(seconds, 2));
  250.  
  251. }
  252. else {
  253.  
  254. current_car.x = RACE_FINISH_LINE_X;
  255.  
  256. if(current_car.position === null) {
  257.  
  258. current_car.position = iFinishPlace++;
  259. }
  260. }
  261.  
  262. drawCar(current_car);
  263. }
  264.  
  265. function initCars() {
  266.  
  267. game_data = setupGameData();
  268.  
  269. initGameState();
  270. drawCars();
  271. }
  272.  
  273. function stopLoop() {
  274.  
  275. iTime = 0;
  276. iFinishPlace = 1;
  277. }
  278.  
  279.  
  280. function startRace() {
  281.  
  282. var iCarCounter;
  283.  
  284. redrawRoadSection();
  285.  
  286. for(iCarCounter = 0; iCarCounter < game_data.cars.length; iCarCounter++) {
  287.  
  288. moveCar(iCarCounter);
  289.  
  290. }
  291.  
  292. updateDebugWindow();
  293.  
  294. if(iFinishPlace > 4) {
  295.  
  296. stopLoop();
  297.  
  298. } else {
  299.  
  300. iTime += STEP_COUNT_MILLISECONDS;
  301.  
  302. requestAnimFrame(startRace);
  303. }
  304. }
  305.  
  306. function startLoop() {
  307.  
  308. stopLoop();
  309.  
  310. requestAnimFrame(startRace);
  311. }
  312.  
  313. function loadCarSprite() {
  314.  
  315. // Load the timer
  316. car_sprite = new Image();
  317. car_sprite.src = 'car-sprite.gif';
  318. car_sprite.onload = initCars;
  319. }
  320.  
  321. function draw() {
  322.  
  323. // Main entry point got the motion canvas example
  324. canvas = document.getElementById('motion');
  325.  
  326. // Canvas supported?
  327. if (canvas.getContext) {
  328.  
  329. ctx = canvas.getContext('2d');
  330.  
  331. loadBackground();
  332.  
  333. } else {
  334. alert("Canvas not supported!");
  335. }
  336. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×