Advertisement
fiveriverflow

RollingRock

Nov 29th, 2015
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. import java.awt.Color;
  2.  
  3. import info.gridworld.actor.Actor;
  4. import info.gridworld.actor.Rock;
  5. import info.gridworld.grid.Grid;
  6. import info.gridworld.grid.Location;
  7.  
  8. public class RollingRock extends Rock {
  9.  
  10.     private static final Color DEFAULT_COLOR = Color.BLACK;
  11.    
  12.     /**
  13.      * Constructs a black rock.
  14.      */
  15.     public RollingRock()
  16.     {
  17.         setColor(DEFAULT_COLOR);
  18.         setDirection(135);
  19.     }
  20.  
  21.     /**
  22.      * Constructs a rock of a given color.
  23.      * @param rockColor the color of this rock
  24.      */
  25.     public RollingRock(Color rockColor)
  26.     {
  27.         setColor(rockColor);
  28.         setDirection(135);
  29.     }
  30.    
  31.     /**
  32.      * Rolls the rock
  33.      */
  34.     public void act()
  35.     {
  36.         roll();
  37.     }
  38.    
  39.     /**
  40.      * Rolls the rock forward
  41.      */
  42.     public void roll()
  43.     {
  44.         Grid<Actor> gr = getGrid();
  45.         if (gr == null)
  46.             return;
  47.         Location loc = getLocation();
  48.         Location next = loc.getAdjacentLocation(getDirection());
  49.         if (gr.isValid(next))
  50.             moveTo(next);
  51.         else
  52.             removeSelfFromGrid();
  53.     }
  54.    
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement