Guest User

Untitled

a guest
May 25th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. /*
  2. * robot.js
  3. *
  4. * You'll need three keys in order to unlock the
  5. * Algorithm: the red key, the green key, and the
  6. * blue key. Unfortunately, all three of them are
  7. * behind human-proof barriers.
  8. *
  9. * The plan is simple: reprogram the maintenance
  10. * robots to grab the key and bring it through
  11. * the barrier to us.
  12. *
  13. * Let's try it on the red key first.
  14. */
  15.  
  16. function getRandomInt(min, max) {
  17. return Math.floor(Math.random() * (max - min + 1)) + min;
  18. }
  19.  
  20. function startLevel(map) {
  21. // Hint: you can press R or 5 to "rest" and not move the
  22. // player, while the robot moves around.
  23.  
  24. map.placePlayer(map.getWidth()-2, map.getHeight()-2);
  25. var player = map.getPlayer();
  26.  
  27. map.defineObject('robot', {
  28. 'type': 'dynamic',
  29. 'symbol': 'R',
  30. 'color': 'gray',
  31. 'onCollision': function (player, me) {
  32. me.giveItemTo(player, 'redKey');
  33. },
  34. 'behavior': function (me) {
  35. // Available commands: me.move(direction)
  36. // and me.canMove(direction)
  37. },
  38. 'behavior': function (me,player){
  39. while(me.findNearest('redKey'))
  40. moveToward(me, 'redKey');
  41. moveToward(me, 'player');
  42. //alert("isHaveTheKey");
  43. },
  44.  
  45.  
  46. });
  47.  
  48. //___________
  49.  
  50. function moveToward(obj, type) {
  51. var target = obj.findNearest(type);
  52. var leftDist = obj.getX() - target.x;
  53. var upDist = obj.getY() - target.y;
  54.  
  55. var direction;
  56. if (upDist == 0 && leftDist == 0) {
  57. return;
  58. } if (upDist > 0 && upDist >= leftDist) {
  59. direction = 'up';
  60. } else if (upDist < 0 && upDist < leftDist) {
  61. direction = 'down';
  62. } else if (leftDist > 0 && leftDist >= upDist) {
  63. direction = 'left';
  64. } else {
  65. direction = 'right';
  66. }
  67.  
  68. if (obj.canMove(direction)) {
  69. obj.move(direction);
  70. }
  71. }
  72. //________
  73.  
  74. var isHaveTheKey = false;
  75.  
  76. map.defineObject('test', {
  77. 'type': 'dynamic',
  78. 'symbol': 'Z',
  79. 'color': 'green',
  80. 'onCollision': function (player, me) {
  81. me.giveItemTo(player, 'redKey');
  82. alert("hello");
  83. },
  84. 'behavior': function (me,player){
  85. while(me.findNearest('redKey'))
  86. moveToward(me, 'redKey');
  87. moveToward(me, 'player');
  88. alert("isHaveTheKey");
  89. }
  90. });
  91.  
  92. //map.placeObject(46, 3, 'test');
  93.  
  94. map.defineObject('test2', {
  95. 'symbol': 'z',
  96. 'behavior': function (me) {
  97. }
  98. });
  99.  
  100. map.defineObject('barrier', {
  101. 'symbol': '░',
  102. 'color': 'purple',
  103. 'impassable': true,
  104. 'passableFor': ['robot']
  105. });
  106.  
  107. map.placeObject(0, map.getHeight() - 1, 'exit');
  108. map.placeObject(1, 1, 'robot');
  109. map.placeObject(map.getWidth() - 2, 8, 'redKey');
  110. map.placeObject(map.getWidth() - 2, 9, 'barrier');
  111.  
  112. for (var x = 0; x < map.getWidth(); x++) {
  113. map.placeObject(x, 0, 'block');
  114. if (x != map.getWidth() - 2) {
  115. map.placeObject(x, 9, 'block');
  116. }
  117. }
  118.  
  119. for (var y = 1; y < 9; y++) {
  120. map.placeObject(0, y, 'block');
  121. map.placeObject(map.getWidth() - 1, y, 'block');
  122. }
  123. }
  124.  
  125. function validateLevel(map) {
  126. map.validateExactlyXManyObjects(1, 'exit');
  127. map.validateExactlyXManyObjects(1, 'robot');
  128. map.validateAtMostXObjects(1, 'redKey');
  129. }
  130.  
  131. function onExit(map) {
  132. if (!map.getPlayer().hasItem('redKey')) {
  133. map.writeStatus("We need to get that key!");
  134. return false;
  135. } else {
  136. return true;
  137. }
  138. }
Add Comment
Please, Sign In to add comment