Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. /*
  2. * robot.js
  3. *
  4. * You'll need three keys in order to unlock the
  5. * Algorithm: the red key, the green key, and the
  6. * blue key. Unfortunately, all three of them are
  7. * behind human-proof barriers.
  8. *
  9. * The plan is simple: reprogram the maintenance
  10. * robots to grab the key and bring it through
  11. * the barrier to us.
  12. *
  13. * Let's try it on the red key first.
  14. */
  15.  
  16. function getRandomInt(min, max) {
  17. return Math.floor(Math.random() * (max - min + 1)) + min;
  18. }
  19.  
  20. function startLevel(map) {
  21. // Hint: you can press R or 5 to "rest" and not move the
  22. // player, while the robot moves around.
  23.  
  24. map.placePlayer(map.getWidth()-2, map.getHeight()-2);
  25. var player = map.getPlayer();
  26.  
  27. map.defineObject('robot', {
  28. 'type': 'dynamic',
  29. 'symbol': 'R',
  30. 'color': 'gray',
  31. 'onCollision': function (player, me) {
  32. me.giveItemTo(player, 'redKey');
  33. },
  34. 'behavior': function (me) {
  35. // Available commands: me.move(direction)
  36. // and me.canMove(direction)
  37. if(!this.direction) { this.direction = "down"; this.counter = 0; }
  38. me.move(this.direction);
  39. var robot = this;
  40.  
  41. player.setPhoneCallback(function (player) {
  42. switch(robot.counter++%4) {
  43. case 1: robot.direction = 'right'; break;
  44. case 2: robot.direction = 'down'; break;
  45. case 3: robot.direction = 'left'; break;
  46. case 0: robot.direction = 'up'; break;
  47. }
  48.  
  49. });
  50.  
  51. }
  52. });
  53.  
  54. map.defineObject('barrier', {
  55. 'symbol': '░',
  56. 'color': 'purple',
  57. 'impassable': true,
  58. 'passableFor': ['robot']
  59. });
  60.  
  61. map.placeObject(0, map.getHeight() - 1, 'exit');
  62. map.placeObject(1, 1, 'robot');
  63. map.placeObject(map.getWidth() - 2, 8, 'redKey');
  64. map.placeObject(map.getWidth() - 2, 9, 'barrier');
  65.  
  66. for (var x = 0; x < map.getWidth(); x++) {
  67. map.placeObject(x, 0, 'block');
  68. if (x != map.getWidth() - 2) {
  69. map.placeObject(x, 9, 'block');
  70. }
  71. }
  72.  
  73. for (var y = 1; y < 9; y++) {
  74. map.placeObject(0, y, 'block');
  75. map.placeObject(map.getWidth() - 1, y, 'block');
  76. }
  77. }
  78.  
  79. function validateLevel(map) {
  80. map.validateExactlyXManyObjects(1, 'exit');
  81. map.validateExactlyXManyObjects(1, 'robot');
  82. map.validateAtMostXObjects(1, 'redKey');
  83. }
  84.  
  85. function onExit(map) {
  86. if (!map.getPlayer().hasItem('redKey')) {
  87. map.writeStatus("We need to get that key!");
  88. return false;
  89. } else {
  90. return true;
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement