Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. var canvas = document.getElementById("gameCanvas");
  2. var context = canvas.getContext("2d");
  3.  
  4. var startFrameMillis = Date.now();
  5. var endFrameMillis = Date.now();
  6.  
  7. // This function will return the time in seconds since the function
  8. // was last called
  9. // You should only call this function once per frame
  10. function getDeltaTime()
  11. {
  12. endFrameMillis = startFrameMillis;
  13. startFrameMillis = Date.now();
  14.  
  15. // Find the delta time (dt) - the change in time since the last drawFrame
  16. // We need to modify the delta time to something we can use.
  17. // We want 1 to represent 1 second, so if the delta is in milliseconds
  18. // we divide it by 1000 (or multiply by 0.001). This will make our
  19. // animations appear at the right speed, though we may need to use
  20. // some large values to get objects movement and rotation correct
  21. var deltaTime = (startFrameMillis - endFrameMillis) * 0.001;
  22.  
  23. // validate that the delta is within range
  24. if(deltaTime > 1)
  25. deltaTime = 1;
  26.  
  27. return deltaTime;
  28. }
  29.  
  30. //-------------------- Don't modify anything above here
  31.  
  32. var SCREEN_WIDTH = canvas.width;
  33. var SCREEN_HEIGHT = canvas.height;
  34. var player = new Player();
  35. var keyboard = new Keyboard();
  36.  
  37. // some variables to calculate the Frames Per Second (FPS - this tells use
  38. // how fast our game is running, and allows us to make the game run at a
  39. // constant speed)
  40. var fps = 0;
  41. var fpsCount = 0;
  42. var fpsTime = 0;
  43. var LAYER_COUNT = 2;
  44. var MAP = {tw:50,th:20};
  45. var TILE = 35;
  46. var TILESET_TILE = TILE * 2;
  47. var TILESET_PADDING = 2;
  48. var TILESET_SPACING = 2;
  49. var TILESET_COUNT_X = 25;
  50. var TILESET_COUNT_Y = 10;
  51.  
  52. //load the image to use for the level tiles
  53. var tileset = document.createElement("img");
  54. tileset.src = "assets/tileset.png";
  55.  
  56. function drawMap()
  57. {
  58. for(var layerIdx=0; layerIdx<LAYER_COUNT; layerIdx++)
  59. {
  60. var idx = 0;
  61. for( var y = 0; y < level1.layers[layerIdx].height; y++ )
  62. {
  63. for( var x = 0; x < level1.layers[layerIdx].width; x++ )
  64. {
  65. if( level1.layers[layerIdx].data[idx] != 0 )
  66. {
  67. // the tiles in the Tiled map are base 1 (meaning a value of 0 means no tile), so subtract one from the tileset id to get the
  68. // correct tile
  69. var tileIndex = level1.layers[layerIdx].data[idx] - 1;
  70. var sx = TILESET_PADDING + (tileIndex % TILESET_COUNT_X) * (TILESET_TILE + TILESET_SPACING);
  71. var sy = TILESET_PADDING + (Math.floor(tileIndex / TILESET_COUNT_Y)) * (TILESET_TILE + TILESET_SPACING);
  72. context.drawImage(tileset, sx, sy, TILESET_TILE, TILESET_TILE, x*TILE, (y-1)*TILE, TILESET_TILE, TILESET_TILE);
  73. }
  74. idx++;
  75. }
  76. }
  77. }
  78. }
  79.  
  80. function run()
  81. {
  82. context.fillStyle = "#ccc";
  83. context.fillRect(0, 0, canvas.width, canvas.height);
  84.  
  85. var deltaTime = getDeltaTime();
  86.  
  87. player.update(deltaTime);
  88. player.draw();
  89.  
  90.  
  91.  
  92.  
  93. // update the frame counter
  94. fpsTime += deltaTime;
  95. fpsCount++;
  96. if(fpsTime >= 1)
  97. {
  98. fpsTime -= 1;
  99. fps = fpsCount;
  100. fpsCount = 0;
  101. }
  102.  
  103. // draw the FPS
  104. context.fillStyle = "#f00";
  105. context.font="14px Arial";
  106. context.fillText("FPS: " + fps, 5, 20, 100);
  107. }
  108.  
  109.  
  110. //-------------------- Don't modify anything below here
  111.  
  112.  
  113. // This code will set up the framework so that the 'run' function is called 60 times per second.
  114. // We have a some options to fall back on in case the browser doesn't support our preferred method.
  115. (function() {
  116. var onEachFrame;
  117. if (window.requestAnimationFrame) {
  118. onEachFrame = function(cb) {
  119. var _cb = function() { cb(); window.requestAnimationFrame(_cb); }
  120. _cb();
  121. };
  122. } else if (window.mozRequestAnimationFrame) {
  123. onEachFrame = function(cb) {
  124. var _cb = function() { cb(); window.mozRequestAnimationFrame(_cb); }
  125. _cb();
  126. };
  127. } else {
  128. onEachFrame = function(cb) {
  129. setInterval(cb, 1000 / 60);
  130. }
  131. }
  132.  
  133. window.onEachFrame = onEachFrame;
  134. })();
  135.  
  136. window.onEachFrame(run);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement