Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 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. this.dirs = {"up":"right", "right":"down", "down":"left",
  31. "left":"up"}
  32. this.ldirs = {"right":"up", "down":"right", "left":"down",
  33. "up":"left"}
  34. if (!this.hasOwnProperty("dir")){
  35. this.dir = "right"
  36. }
  37.  
  38.  
  39. if (me.canMove(this.dirs[this.dir])){
  40. this.dir = this.dirs[this.dir]
  41. me.move(this.dir)
  42. } else if (!me.canMove(this.dir)){
  43. this.dir = this.ldirs[this.dir]
  44. } else {
  45. me.move(this.dir)
  46.  
  47. }
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  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. map.defineObject('barrier', {
  84. 'symbol': '░',
  85. 'color': 'purple',
  86. 'impassable': true,
  87. 'passableFor': ['robot']
  88. });
  89.  
  90. map.placeObject(0, map.getHeight() - 1, 'exit');
  91. map.placeObject(1, 1, 'robot');
  92. map.placeObject(map.getWidth() - 2, 8, 'blueKey');
  93. map.placeObject(map.getWidth() - 2, 9, 'barrier');
  94.  
  95. var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
  96. autoGeneratedMaze.create( function (x, y, mapValue) {
  97. // don't write maze over robot or barrier
  98. if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) {
  99. return 0;
  100. } else if (mapValue === 1) { //0 is empty space 1 is wall
  101. map.placeObject(x,y, 'block');
  102. } else {
  103. map.placeObject(x,y,'empty');
  104. }
  105. });
  106. }
  107.  
  108. function validateLevel(map) {
  109. map.validateExactlyXManyObjects(1, 'exit');
  110. map.validateExactlyXManyObjects(1, 'robot');
  111. map.validateAtMostXObjects(1, 'blueKey');
  112. }
  113.  
  114. function onExit(map) {
  115. if (!map.getPlayer().hasItem('blueKey')) {
  116. map.writeStatus("We need to get that key!");
  117. return false;
  118. } else {
  119. return true;
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement