Advertisement
Guest User

Untitled

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