Guest User

Untitled

a guest
May 20th, 2019
57
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /****************
  2. * drones101.js *
  3. ****************
  4. *
  5. * Do you remember, my dear Professor, a certain introductory
  6. * computational rationality class you taught long ago? Assignment
  7. * #2, behavior functions of autonomous agents? I remember that one
  8. * fondly - but attack drones are so much easier to reason about
  9. * when they're not staring you in the face, I would imagine!
  10. */
  11.  
  12. function startLevel(map) {
  13. function moveToward(obj, type) {
  14. var target = obj.findNearest(type);
  15. var leftDist = obj.getX() - target.x;
  16. var upDist = obj.getY() - target.y;
  17.  
  18. var direction;
  19. if (upDist == 0 && leftDist == 0) {
  20. return;
  21. } if (upDist > 0 && upDist >= leftDist) {
  22. direction = 'up';
  23. } else if (upDist < 0 && upDist < leftDist) {
  24. direction = 'down';
  25. } else if (leftDist > 0 && leftDist >= upDist) {
  26. direction = 'left';
  27. } else {
  28. direction = 'right';
  29. }
  30.  
  31. if (obj.canMove(direction)) {
  32. obj.move(direction);
  33. }
  34. }
  35.  
  36. map.defineObject('attackDrone', {
  37. 'type': 'dynamic',
  38. 'symbol': 'd',
  39. 'color': 'red',
  40. 'onCollision': function (player) {
  41. player.killedBy('an attack drone');
  42. },
  43. 'behavior': function (me) {
  44. moveToward(me, 'player');
  45. }
  46. });
  47.  
  48.  
  49. map.placePlayer(1, 1);
  50. map.placeObject(map.getWidth()-2, 12, 'attackDrone');
  51. map.placeObject(map.getWidth()-1, 12, 'exit');
  52.  
  53. map.placeObject(map.getWidth()-1, 11, 'block');
  54. map.placeObject(map.getWidth()-2, 11, 'block');
  55. map.placeObject(map.getWidth()-1, 13, 'block');
  56. map.placeObject(map.getWidth()-2, 13, 'block');
  57.  
  58. for (var i=0; i<map.getWidth()-4; i++) {
  59. map.placeObject(i, 10, 'block');
  60. }
  61. map.placeObject(i, 11, 'block');
  62. map.placeObject(i, 10, 'block');
  63.  
  64.  
  65. }
  66.  
  67. function validateLevel(map) {
  68. map.validateExactlyXManyObjects(1, 'exit');
  69. }
RAW Paste Data