Guest User

Untitled

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