Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.17 KB | None | 0 0
  1. //RequestAnimationFrame shim.
  2.  
  3. window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
  4.  
  5.  
  6. //Initializing canvas and world
  7.  
  8. var canvas = document.getElementById('viewport');
  9. var ctx = canvas.getContext('2d');
  10. var keysDown = [];
  11. var currentPlatform = 0;
  12.  
  13. //some helpful variables
  14.  
  15. var canvasHeight = $('#viewport').height();
  16. var canvasWidth = $('#viewport').width();
  17.  
  18.  
  19. //Add keycodes to 'keysDown' array
  20.  
  21. $(document).keydown(function (e) {
  22. if ($.inArray(e.which, keysDown) === -1) {
  23. keysDown.push(e.keyCode);
  24. }
  25. });
  26.  
  27.  
  28. //Remove keycodes from 'keysDown' array
  29.  
  30. $(document).keyup(function (e) {
  31. if ($.inArray(e.which, keysDown) > -1) {
  32. keysDown = $.grep(keysDown, function (n, i) {
  33. return n !== e.which;
  34. });
  35. }
  36. });
  37.  
  38.  
  39. //Avatar object, lots of attributes, great import!
  40.  
  41. var avatar = {};
  42.  
  43. avatar.xPos = 50;
  44. avatar.yPos = 50;
  45. avatar.accl = 0.55;
  46. avatar.decel = 0.85;
  47. avatar.jReduction = 1.25;
  48. avatar.direction = null;
  49. avatar.stopping = false;
  50. avatar.avatarHeight = 50;
  51. avatar.avatarWidth = 25;
  52. avatar.fallTime = 0;
  53. avatar.isGrounded = false;
  54. avatar.isJumping = false;
  55. avatar.endJump = false;
  56. avatar.jump = 18;
  57. avatar.j = avatar.jump;
  58. avatar.jStrength = 0.55;
  59. avatar.speed = 13;
  60. avatar.prevXPos = 0;
  61. avatar.xDelta = 0;
  62. avatar.xVelocity = 0;
  63. avatar.yVelocity = 0;
  64. avatar.yBottom = 0;
  65. avatar.xAlignment = 0;
  66.  
  67.  
  68. avatar.collDetect = function (args) {
  69.  
  70. avatar.yBottom = avatar.yPos + avatar.avatarHeight;
  71. avatar.xPosRight = avatar.xPos + avatar.avatarWidth;
  72.  
  73.  
  74. for (i = 0; i < arguments.length; i++) {
  75. if (avatar.yBottom > arguments[i].boxTop) {
  76. if (avatar.xPos > arguments[i].xPos) {
  77. if (avatar.direction === 'left' && avatar.xDelta > avatar.xPos - arguments[i].xPosRight) {
  78. avatar.xPos = arguments[i].xPosRight;
  79. avatar.xStop();
  80. } else if (avatar.direction === 'left' && avatar.xPos <= arguments[i].xPosRight) {
  81. avatar.xPos = arguments[i].xPosRight;
  82. avatar.xStop();
  83. }
  84. } else if (avatar.xPos < arguments[i].xPos) {
  85. if (avatar.direction === 'right' && avatar.xDelta > arguments[i].xPos - avatar.xPosRight) {
  86. avatar.xPos = arguments[i].xPos - avatar.avatarWidth;
  87. avatar.xStop();
  88. } else if (avatar.direction === 'right' && avatar.xPos >= arguments[i].xPos) {
  89. avatar.xPos = arguments[i].xPos - avatar.avatarWidth;
  90. avatar.xStop();
  91. }
  92. }
  93. }
  94.  
  95. if (avatar.xPos > arguments[i].xPos - avatar.avatarWidth && avatar.xPos < arguments[i].xPos + arguments[i].boxWidth) {
  96. currentPlatform = arguments[i].boxHeight;
  97. } else {
  98. currentPlatform = 0;
  99. }
  100. }
  101. };
  102.  
  103. avatar.xStop = function () {
  104. avatar.xVelocity = 0;
  105. avatar.xDelta = 0;
  106. avatar.stopping = false;
  107. avatar.direction = null;
  108. };
  109.  
  110. //First obstacle. Good luck gettin' over this one, avatar!
  111.  
  112. function Box(xPos, boxWidth, boxHeight, boxColor) {
  113. this.xPos = xPos;
  114. this.boxWidth = boxWidth;
  115. this.boxHeight = boxHeight;
  116. this.boxColor = boxColor;
  117. this.xPosRight = xPos + boxWidth;
  118. this.boxTop = canvasHeight - boxHeight;
  119. }
  120.  
  121. function renderBoxes(n) {
  122. for (i = 0; i < arguments.length; i++) {
  123. ctx.fillStyle = arguments[i].boxColor;
  124. ctx.fillRect(arguments[i].xPos,
  125. canvasHeight - arguments[i].boxHeight,
  126. arguments[i].boxWidth,
  127. arguments[i].boxHeight);
  128. }
  129. }
  130.  
  131. var box1 = new Box(100, 50, 100, 'gray');
  132. var box2 = new Box(300, 50, 125, 'green');
  133.  
  134.  
  135. //physics object. Properties of the world
  136.  
  137. var physx = {};
  138. physx.gravity = 1;
  139. physx.colliding = false;
  140. physx.fallTimeModifier = 0.5;
  141.  
  142.  
  143. //Big movement function. The action's in here!
  144.  
  145. function moveIt() {
  146.  
  147. //Jump!
  148.  
  149. if ($.inArray(38, keysDown) > -1) {
  150.  
  151. if (avatar.j > 0) {
  152. avatar.isGrounded = false;
  153. avatar.isJumping = true;
  154. avatar.yPos -= avatar.j;
  155. avatar.yVelocity = avatar.j;
  156. avatar.j -= avatar.jStrength;
  157. } else if (avatar.j <= 0) {
  158. avatar.isJumping = false;
  159. }
  160. }
  161.  
  162. //End Jump, initiated when the user lets off the jump key mid-jump.
  163.  
  164. if (avatar.endJump === true) {
  165.  
  166. if (avatar.j > 0) {
  167. avatar.j -= avatar.jReduction;
  168. avatar.yPos -= avatar.j;
  169. } else if (avatar.j <= 0) {
  170. avatar.isJumping = false;
  171. avatar.endJump = false;
  172. }
  173.  
  174. }
  175.  
  176. $(document).keyup(function (e) {
  177. if (e.which === 38 && avatar.isJumping === true) {
  178. avatar.endJump = true;
  179. }
  180. });
  181.  
  182. //Accounting for deceleration when avatar stops.
  183.  
  184. if (avatar.stopping === true) {
  185.  
  186. if ((avatar.xVelocity - avatar.decel) <= 0) {
  187. avatar.xStop();
  188. return;
  189. }
  190.  
  191. if (avatar.direction === 'right') {
  192. avatar.xPos += avatar.xVelocity;
  193. avatar.xVelocity -= avatar.decel;
  194. avatar.xDelta = avatar.xVelocity;
  195. }
  196.  
  197. if (avatar.direction === 'left') {
  198. avatar.xPos -= avatar.xVelocity;
  199. avatar.xVelocity -= avatar.decel;
  200. avatar.xDelta = avatar.xVelocity
  201. }
  202. }
  203.  
  204.  
  205. //Correcting glitchy stopping behavior when conflicting left/right keycodes present in 'keysDown' array
  206.  
  207. if ($.inArray(37, keysDown) > -1 && $.inArray(39, keysDown) > -1) {
  208. avatar.stopping = true;
  209. }
  210.  
  211. //right
  212.  
  213. if ($.inArray(39, keysDown) > -1) {
  214.  
  215. if (avatar.stopping === false) {
  216. avatar.direction = 'right';
  217. if (avatar.xVelocity >= avatar.speed) {
  218. avatar.xPos += avatar.speed;
  219. avatar.xDelta = avatar.speed;
  220. } else {
  221. avatar.xPos += avatar.xVelocity;
  222. avatar.xVelocity += avatar.accl;
  223. avatar.xDelta = avatar.xVelocity;
  224. }
  225. }
  226. }
  227.  
  228.  
  229. //left
  230.  
  231. if ($.inArray(37, keysDown) > -1) {
  232.  
  233. if (avatar.stopping === false) {
  234. avatar.direction = 'left';
  235. if (avatar.xVelocity >= avatar.speed) {
  236. avatar.xPos -= avatar.speed;
  237. avatar.xDelta = avatar.speed;
  238. } else {
  239. avatar.xPos -= avatar.xVelocity;
  240. avatar.xVelocity += avatar.accl;
  241. avatar.xDeta = avatar.xVelocity;
  242. }
  243. }
  244. }
  245.  
  246.  
  247. //Set avatar.isStopping to true when
  248.  
  249. $(document).keyup(function (e) {
  250. if (e.which === 39 || e.which === 37) {
  251. avatar.stopping = true;
  252. }
  253. });
  254. }
  255.  
  256. //Gravity function. Keep him on the dang ground!
  257.  
  258. function grav() {
  259.  
  260. if (avatar.isJumping) {
  261. return;
  262. }
  263.  
  264. if (avatar.yPos >= (canvasHeight - currentPlatform) - avatar.avatarHeight) {
  265. avatar.isGrounded = true;
  266. avatar.fallTime = 0;
  267. } else {
  268. if ((avatar.fallTime * physx.gravity) > (((canvasHeight - currentPlatform) - avatar.avatarHeight) - avatar.yPos)) {
  269. avatar.yPos = (canvasHeight - currentPlatform) - avatar.avatarHeight;
  270. avatar.isGrounded = true;
  271. avatar.j = avatar.jump;
  272. avatar.fallTime = 0;
  273. } else {
  274. avatar.yPos += avatar.fallTime * physx.gravity;
  275. avatar.fallTime += physx.fallTimeModifier;
  276. }
  277. }
  278. }
  279.  
  280.  
  281. //Render the dang thing, ya dingus!
  282.  
  283. function render() {
  284.  
  285. ctx.clearRect(0, 0, canvasWidth, canvasHeight);
  286. renderBoxes(box1, box2);
  287. avatar.collDetect(box2, box1);
  288. grav();
  289. ctx.fillStyle = 'red';
  290. ctx.fillRect(avatar.xPos,
  291. avatar.yPos,
  292. avatar.avatarWidth,
  293. avatar.avatarHeight);
  294. moveIt();
  295.  
  296. if (avatar.xPos > arguments[i].xPos - avatar.avatarWidth && avatar.xPos < arguments[i].xPos + arguments[i].boxWidth) {
  297. currentPlatform = arguments[i].boxHeight;
  298.  
  299. } else {
  300. currentPlatform = 0;
  301. }
  302.  
  303. if (avatar.xPos > arguments[i].xPos - avatar.avatarWidth && avatar.xPos < arguments[i].xPos + arguments[i].boxWidth) {
  304. currentPlatform = arguments[i].boxHeight;
  305. break; // <-- here
  306.  
  307. } else {
  308. currentPlatform = 0;
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement