Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.05 KB | None | 0 0
  1. /* Baddie3 - Assignment */
  2. /*
  3. You should start by uncommenting blocks of code or functions that are needed for the assignment.
  4. Follow the instructions given for each of these blocks.
  5. Assignment-sections start like this:
  6. // -------------------------------------------
  7. // ASSIGNMENT
  8.  
  9. Note that the INSTRUCTION can be to fill a whole BLOCK of code, not just one row.
  10. In some cases you have to fill in some missing parts of code or change parts of it.
  11. It can look like this:
  12. if(false)
  13. These should be filled out by replacing the boolean false with your code, for example:
  14. if(x > 10)
  15.  
  16. Good luck!
  17. */
  18.  
  19. (function(){
  20. 'use strict';
  21. // HTML elements
  22. var baddie, content;
  23. // Numbers
  24. var tileSize, gridSize, left, top, posLeft, posTop;
  25. // Arrays
  26. var gameArea;
  27.  
  28. // Get HTML elements that are to be used
  29. baddie = document.getElementById("baddie");
  30. content = document.getElementById("content");
  31.  
  32. // Size of each tile
  33. tileSize = 32;
  34. // Number of tiles per row
  35. gridSize = 12;
  36.  
  37. // Sets content size to match tilesize and gridsize
  38. content.style.width = content.style.height = gridSize*tileSize + "px";
  39.  
  40. // Gets starting position of baddie
  41. left = baddie.offsetLeft;
  42. top = baddie.offsetTop;
  43.  
  44. // Starting position of baddie in the grid
  45. posLeft = 5;
  46. posTop = 5;
  47.  
  48. /**
  49. * This is the game area with a 10x10 grid
  50. * 10 - nothing (grass)
  51. * 11 - wall (impassible)
  52. * 12 - box (movable)
  53. * 13 - door (passible)
  54. */
  55. // ------------------------------
  56. // ASSIGNMENT
  57. // Fill out the array gameArea so that the whole content is filled with tiles
  58. // Place out walls, a door and at least 1 box
  59. // NOTE: The array size is gridSize*gridSize (gridSize 10 gives an array of 100)
  60. gameArea = [
  61. 11,11,11,11,11,11,11,11,11,11,11,11,
  62. 11,10,10,10,10,10,10,10,10,10,10,11,
  63. 11,10,10,10,10,10,10,10,10,10,10,11,
  64. 11,10,10,10,12,10,10,10,10,10,10,11,
  65. 11,10,10,10,10,10,10,10,10,10,10,11,
  66. 11,11,11,11,11,11,13,11,11,11,11,11,
  67. 11,10,10,10,10,10,10,10,10,10,10,11,
  68. 11,10,10,10,10,10,10,10,10,10,10,11,
  69. 11,10,10,10,10,10,10,10,10,10,10,11,
  70. 11,11,11,11,11,11,11,11,11,11,11,11
  71. /*Fill out the rest of the array*/
  72. ];
  73.  
  74. /**
  75. * Initiates the game area by adding each tile as a div with class and id to content area
  76. * @param {[type]} gameArea [description]
  77. */
  78. var drawGamePlan = function(gameArea) {
  79. var i, tile;
  80. console.log("Drawing gameplan:");
  81. console.log(gameArea);
  82. //console.log(gameArea.length)
  83. // ------------------------------
  84. // ASSIGNMENT
  85. // Write a for-loop that iterates over the gameArea-array
  86. // Fill out the missing for-loop conditions
  87. // HINT: Make sure that the loop goes through the gameArea array, the full lenght of it
  88. for (i = 0; i < gameArea.length; i++ ) {
  89. // Create the tile
  90. tile = document.createElement("div");
  91.  
  92. // ------------------------------
  93. // ASSIGNMENT
  94. // Write out the current tile from gameArea
  95. // HINT: Change null so that it fetches the value of the tile
  96. var tileFromArray = gameArea[i];
  97.  
  98. // Add class name to tile
  99. tile.className = "tile t" + tileFromArray;
  100. // Add ID to tile
  101. tile.id = "n" + i;
  102. // Append tile to the content
  103. content.appendChild(tile);
  104. }
  105. };
  106.  
  107.  
  108. /* ------------------------------------
  109. * EVENTS
  110. */
  111. // Triggers action on keypress
  112. document.addEventListener("keydown", function(event) {
  113. var key;
  114. // Gets what key was pressed as number
  115. key = event.keyCode || event.which;
  116. console.log(key + " was pressed");
  117.  
  118. // Switch case to decide where baddie is to go
  119. // ------------------------------
  120. // ASSIGNMENT
  121. // Copy-paste the switch case you wrote from baddie2 that handles the key variable
  122. switch (key) {
  123. case 37:
  124. if (isBaddieMovable(-1, 0)) {
  125. moveBaddie(-1, 0);
  126. turnLeft();
  127. // Go left - Use moveBaddie-function
  128. // Turn baddie left - Use the given function
  129. console.log("Move left");
  130. }
  131. break;
  132. case 38:
  133. if (isBaddieMovable(0, -1)) {
  134. moveBaddie(0, -1);
  135. // Go up - Use moveBaddie-function
  136. console.log("move up");
  137. }
  138. break;
  139. case 39:
  140. if (isBaddieMovable(1, 0)) {
  141. moveBaddie(1, 0);
  142. turnRight();
  143. // Go right - Use moveBaddie-function
  144.  
  145. // Turn baddie right - Use the given function
  146. console.log("move right");
  147. }
  148. break;
  149. case 40:
  150. if (isBaddieMovable(0, 1)) {
  151. moveBaddie(0, 1);
  152. // Go down - Use moveBaddie-function
  153.  
  154. }
  155. break;
  156.  
  157. default:
  158. // Button was pressed but no action is to be performed
  159. console.log("Nothing happened with the gameboard");
  160. // return this function so that the default button action is performed instead
  161. return true;
  162. }
  163. // Baddie action was performed - prevent button default
  164. event.preventDefault();
  165. });
  166.  
  167. /* ---------------------------------------------------------
  168. * FUNCTIONS
  169. */
  170.  
  171. /**
  172. * Initiates area and baddie
  173. */
  174. var init = function() {
  175. drawGamePlan(gameArea);
  176. moveBaddie(1, 1);
  177. };
  178.  
  179. /**
  180. * This function checks that the move was possible and returns either the new position or false
  181. * @param {int} moveLeft - direction to move horizontally, range: -1 -> 1
  182. * @param {int} moveTop - direction to move vertically, range: -1 -> 1
  183. * @return {bool} - if baddie was movable true is returned, otherwise false is returned
  184. */
  185. // UNCOMMENT THIS FUNCTION TO BE ABLE TO CONTINUE
  186. var isBaddieMovable = function(moveLeft, moveTop){
  187. var tile, tilePos, newLeft, newTop, movable;
  188. // This time we want the grid position values, not the pixel position values
  189. newLeft = posLeft + moveLeft;
  190. newTop = posTop + moveTop;
  191.  
  192. movable = false;
  193.  
  194. // Get the tile baddie wants to move to
  195. // left is the row number and top is the column number
  196. tilePos = newLeft + newTop*gridSize;
  197.  
  198. // ------------------------------
  199. // ASSIGNMENT
  200. // Get the tile value from array gameArea and place it in the variable tile
  201. tile = gameArea[tilePos];
  202.  
  203. console.log("Move to: " + newLeft + "," + newTop);
  204. console.log("Tile " + tilePos + " contains " + gameArea[tilePos]);
  205.  
  206. // Switch case on the tile value - do different things depending on what tile baddie is moving to
  207. switch(tile) {
  208. case 10: // empty
  209. case 13: // door
  210. // Move baddie to tile
  211. movable = true;
  212. break;
  213. case 11:
  214. // Wall, don't move baddie
  215. console.log("Baddie collided with wall: %s", tile);
  216. break;
  217. case 12:
  218. // Tile was a box, move it and then baddie
  219. var nextPos, nextTile;
  220.  
  221. // Calculate where the sibling tile to be checked is in the array
  222. nextPos = tilePos + moveLeft + (gridSize*moveTop);
  223.  
  224. // ------------------------------
  225. // ASSIGNMENT
  226. // Get the next tile from gameArea and place it in the variable nextTile (5b)
  227. nextTile = gameArea[nextPos];
  228.  
  229. console.log("The next tile is: " + nextTile);
  230.  
  231. // Only move if the sibling tile to be moved to is empty
  232. if(nextTile == 10) {
  233. moveTile(tilePos, nextPos);
  234. // Allow baddie to move to the current tile
  235. movable = true;
  236. console.log("Moved a box");
  237. } else {
  238. // if not empty - don't do anything else
  239. console.log("Can't push box - next tile is not empty");
  240. }
  241. break;
  242. default:
  243. // Tile was impassible - collided, do not move baddie
  244. console.log("Oh no, baddie collided with the wall");
  245. movable = false;
  246. }
  247. return movable;
  248. };
  249.  
  250.  
  251. /**
  252. * Changes position variables for baddie and style to draw the change out on the screen
  253. * @param {[type]} x - direction to move horizontally
  254. * @param {[type]} y - direction to move vertically
  255. */
  256. var moveBaddie = function(x, y) {
  257. // Update baddies position variables
  258. posLeft += x;
  259. posTop += y;
  260.  
  261. // Assign left and right to the pixel positions inside the area that the baddie is moving to
  262. // x and y are the grid coordinates, so you take tile tileSize and use that to get the pixels
  263. left = posLeft*tileSize;
  264. top = posTop*tileSize;
  265.  
  266. // To actually visually move baddie we need to change left and top in style as pixels
  267. baddie.style.left = left + "px";
  268. baddie.style.top = top + "px";
  269. };
  270.  
  271. /**
  272. * Switches two tiles and updates their classes to redraw them
  273. * @param {int} current - array position of the tile to move
  274. * @param {int} next - array position to move tile to
  275. */
  276. // UNCOMMENT THIS FUNCTION TO BE ABLE TO CONTINUE
  277. var moveTile = function(current, next) {
  278. var tile = gameArea[current];
  279. // ------------------------------
  280. // ASSIGNMENT
  281. // Switch the tiles
  282. // Place tile into the next positon in the array gameArea
  283. // Then make sure the current tile is empty in the array gameArea
  284.  
  285. gameArea[next] = gameArea[current];
  286. gameArea[current] = 10;
  287.  
  288. // Give the tiles new classnames to redraw them
  289. document.getElementById("n" + next).className = "tile t" + tile; // box tile here
  290. document.getElementById("n" + current).className = "tile t" + 10; // current tile will be empty
  291. };
  292.  
  293. /**
  294. * Turn baddie image right - transform handled in style.css
  295. */
  296. // UNCOMMENT THIS FUNCTION TO BE ABLE TO CONTINUE
  297.  
  298. function turnRight() {
  299. baddie.className = "baddie";
  300. }
  301.  
  302. /**
  303. * Turn baddie image left - transform handled in style.css
  304. */
  305. // UNCOMMENT THIS FUNCTION TO BE ABLE TO CONTINUE
  306.  
  307. function turnLeft() {
  308. baddie.className = "baddie-left";
  309. }
  310.  
  311.  
  312. /* ---------------------------------------------------------
  313. * CODE
  314. */
  315. init();
  316.  
  317. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement