Guest User

Untitled

a guest
May 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  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. map.defineObject('selfDefenceDrone', {
  58. 'type': 'dynamic',
  59. 'symbol': 'X',
  60. 'color': 'green',
  61. 'onCollision': function (player) {
  62. player.killedBy('an attack drone');
  63. console.log('killled!');
  64. },
  65. 'behavior': function (me) {
  66. moveToward(me, 'attackDrone');
  67. console.log(me.findNearest('attackDrone'));
  68. console.log('-------------');
  69. }
  70. });
  71.  
  72. map.placeObject(map.getWidth()-9, 12, 'selfDefenceDrone');
  73. map.placeObject(map.getWidth()-5, 16, 'selfDefenceDrone');
  74. map.placeObject(map.getWidth()-5, 8, 'selfDefenceDrone');
  75. map.placeObject(map.getWidth()-1, 14, 'selfDefenceDrone');
  76. }
  77.  
  78. function validateLevel(map) {
  79. map.validateExactlyXManyObjects(1, 'exit');
  80. }
Add Comment
Please, Sign In to add comment