SHARE
TWEET

Untitled

a guest Aug 25th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * robotNav.js
  3.  *
  4.  * The green key is located in a slightly more
  5.  * complicated room. You'll need to get the robot
  6.  * past these obstacles.
  7.  */
  8.  
  9. function startLevel(map) {
  10.     // Hint: you can press R or 5 to "rest" and not move the
  11.     // player, while the robot moves around.
  12.  
  13.     map.placePlayer(0, map.getHeight() - 1);
  14.     var player = map.getPlayer();
  15.  
  16.     map.defineObject('robot', {
  17.         'type': 'dynamic',
  18.         'symbol': 'R',
  19.         'color': 'gray',
  20.         'onCollision': function (player, me) {
  21.             me.giveItemTo(player, 'greenKey');
  22.         },
  23.         'behavior': function (me) {
  24. const loc = { "X": me.getX(), "Y": me.getY() }
  25. let deadEnd = (
  26.   ( (map.getObjectTypeAt(loc['X'] + 1, loc['Y']) != 'empty')
  27.  && (map.getObjectTypeAt(loc['X'], loc['Y'] - 1) != 'empty') ) ||
  28.   ( (map.getObjectTypeAt(loc['X'] + 1, loc['Y']) != 'empty')
  29.  && (map.getObjectTypeAt(loc['X'], loc['Y'] + 1) != 'empty') )
  30. )
  31. let nearDeadEndUp = (
  32.   ( (map.getObjectTypeAt(loc['X'] + 2, loc['Y']) != 'empty')
  33.  && (map.getObjectTypeAt(loc['X'] + 1, loc['Y'] + 1) != 'empty') ) ||
  34.   ( (! me.canMove('right')) && (map.getObjectTypeAt(loc['X']
  35.  + 2, loc['Y'] - 1) != 'empty') )
  36. )
  37. let nearDeadEndDown = (
  38.   ( (map.getObjectTypeAt(loc['X'] + 2, loc['Y'])     != 'empty')
  39.  && (map.getObjectTypeAt(loc['X'] + 1, loc['Y'] - 1) != 'empty') ) ||
  40.   ( (! me.canMove('right')) && (map.getObjectTypeAt(loc['X']
  41.  + 2, loc['Y'] + 1) != 'empty') )
  42. )
  43.  
  44. let setDirection = function() {
  45.   if ( nearDeadEndUp && me.canMove('down') ) {
  46.     return 'down'
  47.   } else if ( nearDeadEndDown && me.canMove('up') ) {
  48.     return 'up'
  49.   } else if ( deadEnd ) {
  50.     return 'left'
  51.   } else {
  52.     return 'right'
  53.   }
  54. }
  55.  
  56. me.move(setDirection())
  57.  
  58.         }
  59.     });
  60.  
  61.     map.defineObject('barrier', {
  62.         'symbol': '░',
  63.         'color': 'purple',
  64.         'impassable': true,
  65.         'passableFor': ['robot']
  66.     });
  67.  
  68.     map.placeObject(map.getWidth() - 1, map.getHeight() - 1, 'exit');
  69.     map.placeObject(1, 1, 'robot');
  70.     map.placeObject(map.getWidth() - 2, 8, 'greenKey');
  71.     map.placeObject(map.getWidth() - 2, 9, 'barrier');
  72.  
  73.     for (var x = 0; x < map.getWidth(); x++) {
  74.         map.placeObject(x, 0, 'block');
  75.         if (x != map.getWidth() - 2) {
  76.             map.placeObject(x, 9, 'block');
  77.         }
  78.     }
  79.  
  80.     for (var y = 1; y < 9; y++) {
  81.         map.placeObject(0, y, 'block');
  82.         map.placeObject(map.getWidth() - 1, y, 'block');
  83.     }
  84.  
  85.     for (var i = 0; i < 4; i++) {
  86.         map.placeObject(20 - i, i + 1, 'block');
  87.         map.placeObject(35 - i, 8 - i, 'block');
  88.     }
  89. }
  90.  
  91. function validateLevel(map) {
  92.     map.validateExactlyXManyObjects(1, 'exit');
  93.     map.validateExactlyXManyObjects(1, 'robot');
  94.     map.validateAtMostXObjects(1, 'greenKey');
  95. }
  96.  
  97. function onExit(map) {
  98.     if (!map.getPlayer().hasItem('greenKey')) {
  99.         map.writeStatus("We need to get that key!");
  100.         return false;
  101.     } else {
  102.         return true;
  103.     }
  104. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top