Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. var mapSize = 50;
  2.  
  3. var map = [];
  4. map[0] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
  5. map[1] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 1];
  6. map[2] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 1];
  7. map[3] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 1];
  8. map[4] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 1];
  9. map[5] = [1, 3, 3, 3, 0, 0, 0, 0, 0, 1];
  10. map[6] = [1, 3, 2, 3, 0, 0, 0, 0, 0, 1];
  11. map[7] = [1, 3, 3, 3, 0, 0, 0, 0, 0, 1];
  12. map[8] = [1, 3, 3, 3, 0, 0, 0, 0, 0, 1];
  13. map[9] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
  14.  
  15. var elements = [];
  16. elements[0] = 'space';
  17. elements[1] = 'wall';
  18. elements[2] = 'link right';
  19. elements[3] = 'enemy';
  20.  
  21. var directions = [];
  22. directions[0] = 'top';
  23. directions[1] = 'left';
  24. directions[2] = 'bottom';
  25. directions[3] = 'right';
  26.  
  27. var player = {
  28. x: null,
  29. y: null,
  30. lifepoints: 3
  31. };
  32.  
  33. function printElement(value, y, x) {
  34. var linkSize = '';
  35. switch (value) {
  36. case 2:
  37. player.x = x;
  38. player.y = y;
  39. linkSize = 'background-size: ' + mapSize * 3 + 'px; ';
  40. break;
  41. }
  42. var html = '<div style="' + linkSize + 'width: ' + mapSize + 'px; height: ' + mapSize + 'px;" data-x="' + x + '" data-y="' + y + '" class="bloc ' + elements[value] + '"></div>';
  43. $('#map').append(html);
  44. }
  45.  
  46. function initMap() {
  47. $.each(map, function(kMap, vMap) {
  48. $.each(vMap, function(kLine, vLine) {
  49. printElement(vLine, kMap, kLine);
  50. });
  51. $('#map').append('<div></div>');
  52. });
  53. }
  54.  
  55. function moveElement(coord, className, direction) {
  56. var allDirections = 'top left right bottom';
  57.  
  58. switch (direction) {
  59. case 'top':
  60. if (!$('div[data-x="' + coord.x + '"][data-y="' + (coord.y-1) + '"]').hasClass('wall')) {
  61. if ('enemy' === className && $('div[data-x="' + coord.x + '"][data-y="' + (coord.y-1) + '"]').hasClass('enemy')) {
  62. return coord;
  63. }
  64. if (
  65. 'enemy' === className && $('div[data-x="' + coord.x + '"][data-y="' + (coord.y-1) + '"]').hasClass('link') ||
  66. 'link' === className && $('div[data-x="' + coord.x + '"][data-y="' + (coord.y-1) + '"]').hasClass('enemy')
  67. ) {
  68. player.lifepoints--;
  69. $('div[data-x="' + coord.x + '"][data-y="' + (coord.y-1) + '"]').removeClass('enemy');
  70. }
  71. $('div[data-x="' + coord.x + '"][data-y="' + coord.y + '"]').removeClass(allDirections + ' ' + className).addClass('space');
  72. coord.y--;
  73. }
  74. break;
  75. case 'left':
  76. if (!$('div[data-x="' + (coord.x-1) + '"][data-y="' + coord.y + '"]').hasClass('wall')) {
  77. if ('enemy' === className && $('div[data-x="' + (coord.x-1) + '"][data-y="' + coord.y + '"]').hasClass('enemy')) {
  78. return coord;
  79. }
  80. if (
  81. 'enemy' === className && $('div[data-x="' + (coord.x-1) + '"][data-y="' + coord.y + '"]').hasClass('link') ||
  82. 'link' === className && $('div[data-x="' + (coord.x-1) + '"][data-y="' + coord.y + '"]').hasClass('enemy')
  83. ) {
  84. player.lifepoints--;
  85. $('div[data-x="' + (coord.x-1) + '"][data-y="' + coord.y + '"]').removeClass('enemy');
  86. }
  87. $('div[data-x="' + coord.x + '"][data-y="' + coord.y + '"]').removeClass(allDirections + ' ' + className).addClass('space');
  88. coord.x--;
  89. }
  90. break;
  91. case 'bottom':
  92. if (!$('div[data-x="' + coord.x + '"][data-y="' + (coord.y+1) + '"]').hasClass('wall')) {
  93. if ('enemy' === className && $('div[data-x="' + coord.x + '"][data-y="' + (coord.y+1) + '"]').hasClass('enemy')) {
  94. return coord;
  95. }
  96. if (
  97. 'enemy' === className && $('div[data-x="' + coord.x + '"][data-y="' + (coord.y+1) + '"]').hasClass('link') ||
  98. 'link' === className && $('div[data-x="' + coord.x + '"][data-y="' + (coord.y+1) + '"]').hasClass('enemy')
  99. ) {
  100. player.lifepoints--;
  101. $('div[data-x="' + coord.x + '"][data-y="' + (coord.y+1) + '"]').removeClass('enemy');
  102. }
  103. $('div[data-x="' + coord.x + '"][data-y="' + coord.y + '"]').removeClass(allDirections + ' ' + className).addClass('space');
  104. coord.y++;
  105. }
  106. break;
  107. case 'right':
  108. if (!$('div[data-x="' + (coord.x+1) + '"][data-y="' + coord.y + '"]').hasClass('wall')) {
  109. if ('enemy' === className && $('div[data-x="' + (coord.x+1) + '"][data-y="' + coord.y + '"]').hasClass('enemy')) {
  110. return coord;
  111. }
  112. if (
  113. 'enemy' === className && $('div[data-x="' + (coord.x+1) + '"][data-y="' + coord.y + '"]').hasClass('link') ||
  114. 'link' === className && $('div[data-x="' + (coord.x+1) + '"][data-y="' + coord.y + '"]').hasClass('enemy')
  115. ) {
  116. player.lifepoints--;
  117. $('div[data-x="' + (coord.x+1) + '"][data-y="' + coord.y + '"]').removeClass('enemy');
  118. }
  119. $('div[data-x="' + coord.x + '"][data-y="' + coord.y + '"]').removeClass(allDirections + ' ' + className).addClass('space');
  120. coord.x++;
  121. }
  122. break;
  123. }
  124. $('div[data-x="' + coord.x + '"][data-y="' + coord.y + '"]').addClass(className + ' ' + direction);
  125.  
  126. if (0 === player.lifepoints) {
  127. $('body').html('LOOSER');
  128. }
  129. return coord;
  130. }
  131.  
  132. function initPlayer() {
  133. $('body').keypress(function(event) {
  134. var link = $('.link');
  135. var direction = null;
  136.  
  137. switch (event.which) {
  138. case 122:
  139. //top
  140. direction = 'top';
  141. break;
  142. case 113:
  143. //left
  144. direction = 'left';
  145. break;
  146. case 115:
  147. //bottom
  148. direction = 'bottom';
  149. break;
  150. case 100:
  151. //right
  152. direction = 'right';
  153. break;
  154. default:
  155. return false;
  156. }
  157.  
  158. player = moveElement(player, 'link', direction);
  159. });
  160. }
  161.  
  162. function initEnemies() {
  163.  
  164. setInterval(function() {
  165. $('.enemy').each(function() {
  166. moveElement({x: $(this).data('x'), y: $(this).data('y')}, 'enemy', directions[Math.floor((Math.random() * 4) + 0)]);
  167. });
  168. }, 2000);
  169. }
  170.  
  171. $(document).ready(function(){
  172. initMap();
  173. initPlayer();
  174. initEnemies();
  175. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement