Guest User

Untitled

a guest
Jun 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 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. me.move(dr);
  31. }
  32. });
  33. var Pc = 0;
  34. var dr = '';
  35. map.getPlayer().setPhoneCallback(function(){
  36. if ((Pc++)%2==0){
  37. map.overrideKey('down', ()=> dr = 'down');
  38. map.overrideKey('up', ()=> dr = 'up');
  39. map.overrideKey('left', ()=> dr = 'left');
  40. map.overrideKey('right', ()=> dr = 'right');
  41. }else{
  42. map.overrideKey('down', ()=> map.getPlayer().move('down'));
  43. map.overrideKey('up', ()=> map.getPlayer().move('up'));
  44. map.overrideKey('left', ()=> map.getPlayer().move('left'));
  45. map.overrideKey('right', ()=> map.getPlayer().move('right'));
  46. dr = '';
  47. }
  48. });({function(){
  49. }
  50. });
  51.  
  52. map.defineObject('barrier', {
  53. 'symbol': '░',
  54. 'color': 'purple',
  55. 'impassable': true,
  56. 'passableFor': ['robot']
  57. });
  58.  
  59. map.placeObject(0, map.getHeight() - 1, 'exit');
  60. map.placeObject(1, 1, 'robot');
  61. map.placeObject(map.getWidth() - 2, 8, 'blueKey');
  62. map.placeObject(map.getWidth() - 2, 9, 'barrier');
  63.  
  64. var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
  65. autoGeneratedMaze.create( function (x, y, mapValue) {
  66. // don't write maze over robot or barrier
  67. if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) {
  68. return 0;
  69. } else if (mapValue === 1) { //0 is empty space 1 is wall
  70. map.placeObject(x,y, 'block');
  71. } else {
  72. map.placeObject(x,y,'empty');
  73. }
  74. });
  75. }
  76.  
  77. function validateLevel(map) {
  78. map.validateExactlyXManyObjects(1, 'exit');
  79. map.validateExactlyXManyObjects(1, 'robot');
  80. map.validateAtMostXObjects(1, 'blueKey');
  81. }
  82.  
  83. function onExit(map) {
  84. if (!map.getPlayer().hasItem('blueKey')) {
  85. map.writeStatus("We need to get that key!");
  86. return false;
  87. } else {
  88. return true;
  89. }
  90. }
Add Comment
Please, Sign In to add comment