Guest User

Untitled

a guest
Dec 7th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 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. // move randomly
  31. //var moves = map.getAdjacentEmptyCells(me.getX(), me.getY());
  32. // getAdjacentEmptyCells gives array of ((x, y), direction) pairs
  33. //me.move(moves[map.getRandomInt(0, moves.length - 1)][1]);
  34.  
  35. /*
  36. if (typeof direction === 'undefined') {
  37. var direction = 'right';
  38. }
  39.  
  40. switch (direction)
  41. {
  42. case 'right':
  43. if (me.canMove('down')) {
  44. me.move('down');
  45. var direction = 'down';
  46. } else if (me.canMove('right')) {
  47. me.move('right');
  48. var direction = 'right';
  49. } else if (me.canMove('up')) {
  50. me.move('up');
  51. var direction = 'up';
  52. } else {
  53. me.move('left');
  54. var direction = 'left';
  55. }
  56. break;
  57. case 'down':
  58.  
  59. break;
  60. case 'left':
  61.  
  62. break;
  63. case 'right':
  64.  
  65. break;
  66. }
  67. */
  68.  
  69. if(me.canMove('right')) {
  70. me.move('right');
  71. } else {
  72. me.move('down');
  73. }
  74.  
  75.  
  76. }
  77. });
  78.  
  79. map.defineObject('barrier', {
  80. 'symbol': '░',
  81. 'color': 'purple',
  82. 'impassable': true,
  83. 'passableFor': ['robot']
  84. });
  85.  
  86. map.placeObject(0, map.getHeight() - 1, 'exit');
  87. map.placeObject(1, 1, 'robot');
  88. map.placeObject(map.getWidth() - 2, 8, 'blueKey');
  89. map.placeObject(map.getWidth() - 2, 9, 'barrier');
  90.  
  91. var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
  92. autoGeneratedMaze.create( function (x, y, mapValue) {
  93. // don't write maze over robot or barrier
  94. if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) {
  95. return 0;
  96. } else if (mapValue === 1) { //0 is empty space 1 is wall
  97. map.placeObject(x,y, 'block');
  98. } else {
  99. map.placeObject(x,y,'empty');
  100. }
  101. });
  102. }
  103.  
  104. function validateLevel(map) {
  105. map.validateExactlyXManyObjects(1, 'exit');
  106. map.validateExactlyXManyObjects(1, 'robot');
  107. map.validateAtMostXObjects(1, 'blueKey');
  108. }
  109.  
  110. function onExit(map) {
  111. if (!map.getPlayer().hasItem('blueKey')) {
  112. map.writeStatus("We need to get that key!");
  113. return false;
  114. } else {
  115. return true;
  116. }
  117. }
Add Comment
Please, Sign In to add comment