Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 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. function moveToward(obj, type) {
  38. var target = obj.findNearest(type);
  39. var leftDist = obj.getX() - target.x;
  40. var upDist = obj.getY() - target.y;
  41.  
  42. var direction;
  43. if (upDist == 0 && leftDist == 0) {
  44. return;
  45. } if (upDist > 0 && upDist >= leftDist) {
  46. direction = 'up';
  47. } else if (upDist < 0 && upDist < leftDist) {
  48. direction = 'down';
  49. } else if (leftDist > 0 && leftDist >= upDist) {
  50. direction = 'left';
  51. } else {
  52. direction = 'right';
  53. }
  54.  
  55. if (obj.canMove(direction)) {
  56. obj.move(direction);
  57. }
  58. }
  59.  
  60. moveToward(me, 'player')
  61. }
  62. });
  63.  
  64. map.defineObject('barrier', {
  65. 'symbol': '░',
  66. 'color': 'purple',
  67. 'impassable': true,
  68. 'passableFor': ['robot']
  69. });
  70.  
  71. map.placeObject(0, map.getHeight() - 1, 'exit');
  72. map.placeObject(1, 1, 'robot');
  73. map.placeObject(map.getWidth() - 2, 8, 'redKey');
  74. map.placeObject(map.getWidth() - 2, 9, 'barrier');
  75.  
  76. for (var x = 0; x < map.getWidth(); x++) {
  77. map.placeObject(x, 0, 'block');
  78. if (x != map.getWidth() - 2) {
  79. map.placeObject(x, 9, 'block');
  80. }
  81. }
  82.  
  83. for (var y = 1; y < 9; y++) {
  84. map.placeObject(0, y, 'block');
  85. map.placeObject(map.getWidth() - 1, y, 'block');
  86. }
  87. }
  88.  
  89. function validateLevel(map) {
  90. map.validateExactlyXManyObjects(1, 'exit');
  91. map.validateExactlyXManyObjects(1, 'robot');
  92. map.validateAtMostXObjects(1, 'redKey');
  93. }
  94.  
  95. function onExit(map) {
  96. if (!map.getPlayer().hasItem('redKey')) {
  97. map.writeStatus("We need to get that key!");
  98. return false;
  99. } else {
  100. return true;
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement