Guest User

Untitled

a guest
Dec 14th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. /*
  2. * robotMaze.js
  3. *
  4. * The blue key is inside a labyrinth, and extracting
  5. * it will not be easy.
  6. *
  7. * It's a good thing that you're a AI expert, or
  8. * we would have to leave empty-handed.
  9. */
  10.  
  11. function startLevel(map) {
  12. // Hint: you can press R or 5 to "rest" and not move the
  13. // player, while the robot moves around.
  14.  
  15. map.getRandomInt = function(min, max) {
  16. return Math.floor(Math.random() * (max - min + 1)) + min;
  17. }
  18.  
  19. map.placePlayer(map.getWidth()-1, map.getHeight()-1);
  20. var player = map.getPlayer();
  21.  
  22. map.defineObject('robot', {
  23. 'type': 'dynamic',
  24. 'symbol': 'R',
  25. 'color': 'gray',
  26. 'onCollision': function (player, me) {
  27. me.giveItemTo(player, 'blueKey');
  28. },
  29. 'behavior': function (me) {
  30. // move randomly
  31. //var moves = map.getAdjacentEmptyCells(me.getX(), me.getY());
  32. // getAdjacentEmptyCells gives array of ((x, y), direction) pairs
  33. //me.move(moves[map.getRandomInt(0, moves.length - 1)][1]);
  34. /*
  35. var myArray = ['right', 'down', 'left', 'up', 'right',
  36. 'down', 'left', 'up', 'right', 'down', 'left', 'up', 'right',
  37. 'down', 'left', 'up'];
  38.  
  39. if( typeof this.pos == 'undefined' ) {
  40. this.pos = 0;
  41. }
  42. if (me.canMove(myArray[this.pos])) {
  43. me.move(myArray[this.pos]);
  44. } else if (me.canMove(myArray[this.pos+1])) {
  45. me.move(myArray[this.pos+1]);
  46. } else if (me.canMove(myArray[this.pos+2])) {
  47. me.move(myArray[this.pos+1]);
  48. } else {
  49. this.pos++;
  50. }
  51.  
  52. */
  53.  
  54. if (me.canMove(map.getPlayer().dir)) {
  55. me.move(map.getPlayer().dir);
  56. }
  57.  
  58.  
  59. }
  60. });
  61.  
  62.  
  63.  
  64.  
  65.  
  66. map.getPlayer().setPhoneCallback(function () {
  67.  
  68. var player = map.getPlayer();
  69.  
  70. if( typeof player.dir == 'undefined' ) {
  71. player.dir = 'left';
  72. }
  73. if (player.dir == 'left') {
  74. player.dir = 'right'
  75. map.writeStatus("right");
  76. } else if (player.dir == 'right') {
  77. player.dir = 'up'
  78. map.writeStatus("up");
  79. } else if (player.dir == 'up') {
  80. player.dir = 'down'
  81. map.writeStatus("down");
  82. } else if (player.dir == 'down') {
  83. player.dir = 'left'
  84. map.writeStatus("left");
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114. }
  115. });
  116.  
  117. map.defineObject('barrier', {
  118. 'symbol': '░',
  119. 'color': 'purple',
  120. 'impassable': true,
  121. 'passableFor': ['robot']
  122. });
  123.  
  124. map.placeObject(0, map.getHeight() - 1, 'exit');
  125. map.placeObject(1, 1, 'robot');
  126. map.placeObject(map.getWidth() - 2, 8, 'blueKey');
  127. map.placeObject(map.getWidth() - 2, 9, 'barrier');
  128.  
  129. var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
  130. autoGeneratedMaze.create( function (x, y, mapValue) {
  131. // don't write maze over robot or barrier
  132. if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) {
  133. return 0;
  134. } else if (mapValue === 1) { //0 is empty space 1 is wall
  135. map.placeObject(x,y, 'block');
  136. } else {
  137. map.placeObject(x,y,'empty');
  138. }
  139. });
  140. }
  141.  
  142. function validateLevel(map) {
  143. map.validateExactlyXManyObjects(1, 'exit');
  144. map.validateExactlyXManyObjects(1, 'robot');
  145. map.validateAtMostXObjects(1, 'blueKey');
  146. }
  147.  
  148. function onExit(map) {
  149. if (!map.getPlayer().hasItem('blueKey')) {
  150. map.writeStatus("We need to get that key!");
  151. return false;
  152. } else {
  153. return true;
  154. }
  155. }
Add Comment
Please, Sign In to add comment