Guest User

Untitled

a guest
Jun 25th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. import greenfoot.*;
  2.  
  3. import java.util.List;
  4. import java.util.ArrayList;
  5.  
  6. /**
  7. * Animal. This is the base class for all animals. In addition to the standard Actor
  8. * methods, it provides the ability to move and turn.
  9. *
  10. * @author Michael Kolling
  11. * @version 1.0
  12. */
  13. public class Animal extends Actor
  14. {
  15. private static final double WALKING_SPEED = 5.0;
  16.  
  17. /**
  18. * Constructor for Animal - nothing to do.
  19. */
  20. public Animal()
  21. {
  22. }
  23.  
  24. /**
  25. * Act - empty method. Animals have no default action.
  26. */
  27. public void act()
  28. {
  29. }
  30.  
  31.  
  32. /**
  33. * Turn 'angle' degrees towards the right (clockwise).
  34. */
  35. public void turn(int angle)
  36. {
  37. setRotation(getRotation() + angle);
  38. }
  39.  
  40.  
  41. /**
  42. * Move forward in the current direction.
  43. */
  44. public void move()
  45. {
  46. double angle = Math.toRadians( getRotation() );
  47. int x = (int) Math.round(getX() + Math.cos(angle) * WALKING_SPEED);
  48. int y = (int) Math.round(getY() + Math.sin(angle) * WALKING_SPEED);
  49.  
  50. setLocation(x, y);
  51. }
  52.  
  53.  
  54. /**
  55. * Test if we are close to one of the edges of the world. Return true is we are.
  56. */
  57. public boolean atWorldEdge()
  58. {
  59. if(getX() < 20 || getX() > getWorld().getWidth() - 20)
  60. return true;
  61. if(getY() < 20 || getY() > getWorld().getHeight() - 20)
  62. return true;
  63. else
  64. return false;
  65. }
  66.  
  67.  
  68. /**
  69. * Return true if we can see an object of class 'clss' right where we are.
  70. * False if there is no such object here.
  71. */
  72. public boolean canSee(Class clss)
  73. {
  74. Actor actor = getOneObjectAtOffset(0, 0, clss);
  75. return actor != null;
  76. }
  77.  
  78.  
  79. /**
  80. * Try to eat an object of class 'clss'. This is only successful if there
  81. * is such an object where we currently are. Otherwise this method does
  82. * nothing.
  83. */
  84. public void eat(Class clss)
  85. {
  86. Actor actor = getOneObjectAtOffset(0, 0, clss);
  87. if(actor != null) {
  88. getWorld().removeObject(actor);
  89. }
  90. }
  91. }
Add Comment
Please, Sign In to add comment