Guest User

Untitled

a guest
May 22nd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 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. if (player.teleportersAreReady) {
  31. // Do not add teleporters more than once
  32. return;
  33. }
  34.  
  35. // Place two teleporters at the map
  36. map.placeObject(player.getX(), player.getY() - 1, 'teleporter');
  37. map.placeObject(map.getWidth() - 2, 7, 'teleporter');
  38.  
  39. // We need teleporters objects, so find them
  40. var objs = map.getDynamicObjects();
  41. var teleporters = [];
  42.  
  43. for (var i = 0, len = objs.length; i < len; i++) {
  44. if (objs[i].getType() == 'teleporter') {
  45. teleporters.push(objs[i]);
  46. }
  47. }
  48.  
  49. // Link teleporters one to another
  50. teleporters[0].setTarget(teleporters[1]);
  51. teleporters[1].setTarget(teleporters[0]);
  52.  
  53. // We need an indicator to know if teleporters already
  54. // in place or not. Use "player" object from the closure
  55. player.teleportersAreReady = true;
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88. }
  89. });
  90.  
  91. map.defineObject('barrier', {
  92. 'symbol': '░',
  93. 'color': 'purple',
  94. 'impassable': true,
  95. 'passableFor': ['robot']
  96. });
  97.  
  98. map.placeObject(0, map.getHeight() - 1, 'exit');
  99. map.placeObject(1, 1, 'robot');
  100. map.placeObject(map.getWidth() - 2, 8, 'blueKey');
  101. map.placeObject(map.getWidth() - 2, 9, 'barrier');
  102.  
  103. var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
  104. autoGeneratedMaze.create( function (x, y, mapValue) {
  105. // don't write maze over robot or barrier
  106. if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) {
  107. return 0;
  108. } else if (mapValue === 1) { //0 is empty space 1 is wall
  109. map.placeObject(x,y, 'block');
  110. } else {
  111. map.placeObject(x,y,'empty');
  112. }
  113. });
  114. }
  115.  
  116. function validateLevel(map) {
  117. map.validateExactlyXManyObjects(1, 'exit');
  118. map.validateExactlyXManyObjects(1, 'robot');
  119. map.validateAtMostXObjects(1, 'blueKey');
  120. }
  121.  
  122. function onExit(map) {
  123. if (!map.getPlayer().hasItem('blueKey')) {
  124. map.writeStatus("We need to get that key!");
  125. return false;
  126. } else {
  127. return true;
  128. }
  129. }
Add Comment
Please, Sign In to add comment