Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.96 KB | None | 0 0
  1. //-Declaring variables
  2. var fpsInterval;
  3. var cells = 0;
  4.  
  5.  
  6. var player = {
  7.  
  8. x:0,
  9. y:0
  10. };
  11.  
  12. var players = [];
  13.  
  14. //Lets create a function which will help us to create multiple players
  15. function newPlayer()
  16. {
  17. var player = {
  18. x:0,
  19. y:0
  20. };
  21.  
  22. this.name;
  23. this.id = 1;
  24. this.x=x;
  25. this.y=y;
  26. context.drawImage(myImage, shift, 0, frameWidth, frameHeight,
  27. player.x,player.y, 50, 50);
  28.  
  29. return {'name' : this.name,"x" : this.x,"y" : this.y};
  30. }
  31. //setting variables for the image so that it can be called, creating event listeners for callback, setting the locaiton for the image to be retrieved from
  32. var myImage = new Image();
  33. myImage.src = "https://www.kirupa.com/stuff/sprites_blue.png";
  34.  
  35.  
  36. // load image function
  37. function loadImage(e) {
  38. animate();
  39. }
  40.  
  41. var shift = 0;
  42. var frameWidth = 300;
  43. var frameHeight = 300;
  44. var totalFrames = 24;
  45. var currentFrame = 0;
  46.  
  47.  
  48. /* These two variables control how many cells we divide the canvas into
  49. * horizontally (cellsWide) and vertically (cellsHigh).
  50. * They are used in the graphics calculations later, to establish the
  51. * size of each cell 'on the fly' so that they can change dynamically.
  52. *
  53. * They are set upon receipt of the 'maze data' message.
  54. */
  55. var cellsWide;
  56. var cellsHigh;
  57.  
  58. /* These three variables hold information about the maze.
  59. * - maze, a two-dimensional array of objects each containing members:
  60. * -- x, an integer describing the horizontal position of the cell
  61. * -- y, an integer the vertical position of the cell
  62. * -- top, a boolean describing whether the top edge of the cell has a wall
  63. * -- bottom, a boolean describing whether the bottom edge of the cell has a wall
  64. * -- left, a boolean describing whether the left edge of the cell has a wall
  65. * -- right, a boolean describing whether the right edge of the cell has a wall
  66. * -- set, an integer used in maze generation that can safely be ignored
  67. *
  68. * - mazeStart
  69. * -- x, the row at which players should start in the maze
  70. * -- y, the column at which players should start in the maze
  71. *
  72. * - mazeEnd
  73. * -- x, the row where the goal space of the maze is located
  74. * -- y, the column where the goal space of the maze is located
  75. *
  76. * These variables are all initialised, and changed, upon receipt of the 'maze data' message.
  77. */
  78. var maze = [];
  79. var mazeStart = {};
  80. var mazeEnd = {};
  81.  
  82. /*
  83. * Establish a connection to our server
  84. * We will need to reuse the 'socket' variable to both send messages
  85. * and receive them, by way of adding event handlers for the various
  86. * messages we expect to receive
  87. *
  88. * Replace localhost with a specific URL or IP address if testing
  89. * across multiple computers
  90. *
  91. * See Real-Time Servers III: socket.io and Messaging for help understanding how
  92. * we set up and use socket.io
  93. *
  94. */
  95.  
  96.  
  97. var socket = io.connect("http://localhost:8081");
  98.  
  99. /*
  100. * This is the event handler for the 'maze data' message
  101. * When a 'maze data' message is received from the server, this block of code executes
  102. *
  103. * The server is sending us either initial information about a maze, or,
  104. * updated information about a maze, and so we want to replace our existing
  105. * maze variables with the new information.
  106. *
  107. * We know the specification of the information we receive (from the documentation
  108. * and design of the server), and use this to help write this handler.
  109. */
  110.  
  111.  
  112. //pushing the player data in to the player array and recieving the server info
  113. socket.on("server player", function(data){
  114.  
  115. players.push(data);
  116. console.log(data);
  117. });
  118.  
  119. //socket.on("server message", function(data) {
  120. // console.log(data);
  121. // socket.emit(newPlayer);
  122. // players.push(data);
  123. //})
  124.  
  125.  
  126. socket.on("maze data", function(data) {
  127. cellsWide = data.mazeSize.cols;
  128. cellsHigh = data.mazeSize.rows;
  129. maze = data.mazeCells;
  130. mazeStart = data.mazeStart;
  131. mazeEnd = data.mazeEnd;
  132.  
  133. });
  134.  
  135.  
  136.  
  137.  
  138. /*
  139. * Once our page is fully loaded and ready, we call startAnimating
  140. * to kick off our animation loop.
  141. * We pass in a value - our fps - to control the speed of our animation.
  142. */
  143.  
  144.  
  145. /*
  146. * The startAnimating function kicks off our animation (see Games on the Web I - HTML5 Graphics and Animations).
  147. */
  148. function startAnimating(fps) {
  149. fpsInterval = 1000/fps;
  150. then = Date.now();
  151. animate();
  152. }
  153.  
  154. ///*
  155. // * The animate function is called repeatedly using requestAnimationFrame (see Games on the Web I - HTML5 Graphics and Animations).
  156. // */
  157. //
  158. //
  159.  
  160.  
  161. function animate() {
  162. requestAnimationFrame(animate);
  163.  
  164. //
  165. var now = Date.now();
  166. var elapsed = now - then;
  167.  
  168. if (elapsed > fpsInterval) {
  169.  
  170. then = now - (elapsed % fpsInterval); // matthew added code missed out from skeleton
  171. // Acquire both a canvas (using jQuery) and its associated context
  172. var canvas = $("canvas").get(0);
  173. var context = canvas.getContext("2d");
  174.  
  175. // Calculate the width and height of each cell in our maze
  176. var cellWidth = canvas.width/cellsWide;
  177. var cellHeight = canvas.height/cellsHigh;
  178.  
  179.  
  180.  
  181.  
  182.  
  183. // // for multiplayer
  184. //// players in lobby
  185. // for (var i = 0; i < player.length; i++){
  186. //
  187. // context.beginPath();
  188. //
  189. // context.drawImage(myImage, shift, 0, frameWidth, frameHeight,
  190. // player.x,player.y, 50, 50);
  191. // };
  192. //
  193. // context.beginPath();
  194. //
  195. //
  196. // Clear the drawing area each animation cycle
  197. context.clearRect(0,0, canvas.width, canvas.height);
  198.  
  199.  
  200. //draw each frame + place them in the middle
  201.  
  202. context.drawImage(myImage, shift, 0, frameWidth, frameHeight,
  203. player.x,player.y, 50, 50);
  204.  
  205.  
  206. shift += frameWidth + 1;
  207.  
  208. /*
  209. Start at the beginning once you've reached the
  210. end of your sprite!
  211. */
  212. if (currentFrame == totalFrames) {
  213. shift = 0;
  214. currentFrame = 0;
  215. }
  216. currentFrame++;
  217.  
  218.  
  219. // Change the current colour to yellow, to draw the 'goal' state
  220. context.fillStyle = "yellow";
  221. // The goal is calculated by multiplying the cell location (mazeEnd.x, mazeEnd.y)
  222. // by the cellWidth and cellHeight respectively
  223. // Refer to: Games on the Web I - HTML5 Graphics and Animations, Lab Exercise 2
  224. context.fillRect(mazeEnd.x * cellWidth,
  225. mazeEnd.y * cellHeight,
  226. cellWidth, cellHeight);
  227.  
  228.  
  229.  
  230. // Change the current colour to black, and the line width to 2
  231. context.fillStyle = "black";
  232. context.lineWidth = 2;
  233.  
  234.  
  235. // Loop through the 2D array, in both rows and columns...
  236. for (i = 0; i < maze.length; i++) {
  237.  
  238. for (j = 0; j < maze[i].length; j++) {
  239.  
  240. // ... and for every cell in the maze, check where it has walls
  241. // For every wall we find, draw that wall in an appropriate place
  242.  
  243. if (maze[i][j].top) {
  244. context.beginPath();
  245. context.moveTo(maze[i][j].x*cellWidth, maze[i][j].y*cellHeight);
  246. context.lineTo((maze[i][j].x+1)*cellWidth,maze[i][j].y*cellHeight);
  247. context.stroke();
  248. context.closePath();
  249. }
  250.  
  251. if (maze[i][j].right) {
  252. context.beginPath();
  253. context.moveTo((maze[i][j].x+1)*cellWidth,maze[i][j].y*cellHeight);
  254. context.lineTo((maze[i][j].x+1)*cellWidth,(maze[i][j].y+1)*cellHeight);
  255. context.stroke();
  256. context.closePath();
  257. }
  258.  
  259. if (maze[i][j].bottom) {
  260. context.beginPath();
  261. context.moveTo((maze[i][j].x+1)*cellWidth,(maze[i][j].y+1)*cellHeight);
  262. context.lineTo(maze[i][j].x*cellWidth,(maze[i][j].y+1)*cellHeight);
  263. context.stroke();
  264. context.closePath();
  265. }
  266.  
  267. if (maze[i][j].left) {
  268. context.beginPath();
  269. context.moveTo(maze[i][j].x*cellWidth,(maze[i][j].y+1)*cellHeight);
  270. context.lineTo(maze[i][j].x*cellWidth, maze[i][j].y*cellHeight);
  271. context.stroke();
  272. context.closePath();
  273. }
  274.  
  275. }
  276. }
  277. }
  278. }
  279.  
  280.  
  281.  
  282.  
  283. $(document).ready(function() {
  284.  
  285.  
  286. $('#upButton').on('touchstart click', function(e){
  287. e.stopPropagation(); e.preventDefault();
  288.  
  289. //your code here
  290. if (event.type == "touchstart")
  291. $(this).off('click');
  292. {
  293. //move the square up
  294. player.y = player.y -10;
  295. }
  296. // if the player goes above the map, the player will be returned to the bottom
  297. if (player.y< 0){
  298. player.y= 500;
  299. }
  300.  
  301.  
  302. });
  303.  
  304. $('#downButton').on('touchstart click', function(e){
  305. e.stopPropagation(); e.preventDefault();
  306. //your code here
  307. if (event.type == "touchstart")
  308. $(this).off('click');
  309. {
  310. //move the square down
  311. player.y = player.y +10;
  312.  
  313. }
  314. // if the player goes below the map, the player will be returned to the top
  315. if (player.y>= 500){
  316. player.y=0;
  317. }
  318.  
  319. console.log(e);
  320. });
  321.  
  322. $('#leftButton').on('touchstart click', function(e){
  323. e.stopPropagation(); e.preventDefault();
  324. //your code here
  325. if (event.type == "touchstart")
  326. $(this).off('click');
  327. {
  328. //move the square left
  329. player.x = player.x -10; //i = i -1;
  330.  
  331. }
  332. // if the player goes off the left of the map, the player will be returned to the right of the map
  333. if (player.x< 0){
  334. player.x= 500 ;
  335. }
  336.  
  337. console.log(e);
  338. });
  339.  
  340.  
  341. $('#rightButton').on('touchstart click', function(e){
  342. e.stopPropagation(); e.preventDefault();
  343. //your code here
  344. if (event.type == "touchstart")
  345. $(this).off('click');
  346. {
  347. //move the square right
  348. player.x = player.x +10; //i = i +1;
  349.  
  350. }
  351. // if the player goes off the right of the map, the player will be returned to the left of the map
  352. if (player.x>= 500){
  353. player.x=0;
  354. }
  355.  
  356. console.log(e);
  357. });
  358.  
  359.  
  360.  
  361.  
  362. // moving the player with the keyboard
  363. // (1) What sort of event handler do we need? What can we attach it to?
  364. $("body").keydown(function(e) {
  365. //left 37
  366. //up 38
  367. //right 39
  368. //down 40
  369.  
  370. if (e.which == 40)
  371. {
  372. //move the square down
  373. player.y = player.y +10;
  374.  
  375. }
  376.  
  377. else if (e.which == 39)
  378. {
  379. //move the square right
  380. player.x = player.x +10; //i = i +1;
  381.  
  382. }
  383.  
  384. else if (e.which == 38)
  385. {
  386. //move the square up
  387. player.y = player.y -10;
  388.  
  389. }
  390.  
  391. else if (e.which == 37)
  392. {
  393. //move the square left
  394. player.x = player.x -10; //i = i -1;
  395.  
  396. }
  397.  
  398. // (2) How do we handle when the square goes over an edge?
  399. if (player.x>= 500){
  400. player.x= 0;
  401. }
  402.  
  403. if (player.x< 0){
  404. player.x= 500 -1 ;
  405. }
  406. if (player.y>= 500){
  407. player.y=0;
  408. }
  409.  
  410. if (player.y< 0){
  411. player.y= 500 -1;
  412. }
  413. console.log(e);
  414. });
  415. startAnimating(60);
  416. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement