Advertisement
RovkirHexus

Sprite

Jun 8th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. package feAttempt;
  2.  
  3. public class Sprite
  4. {
  5.   private Grid<Sprite> grid;
  6.   private Location location;
  7.   private String imageFileName;
  8.  
  9.   /**
  10.    * Gets the grid in which this sprite is located.
  11.    * @return the grid of this sprite, or <code>null</code> if this sprite is
  12.    * not contained in a grid
  13.    */
  14.   public Grid<Sprite> getGrid()
  15.   {
  16.     return grid;
  17.   }
  18.  
  19.   /**
  20.    * Gets the location of this sprite.
  21.    * @return the location of this sprite, or <code>null</code> if this sprite is
  22.    * not contained in a grid
  23.    */
  24.   public Location getLocation()
  25.   {
  26.     return location;
  27.   }
  28.  
  29.   public String getImageFileName()
  30.   {
  31.     return imageFileName;
  32.   }
  33.  
  34.   public void setImageFileName(String imageFileName)
  35.   {
  36.     this.imageFileName = imageFileName;
  37.   }
  38.  
  39.   /**
  40.    * Puts this sprite into a grid. If there is another sprite at the given
  41.    * location, it is removed. <br />
  42.    * Precondition: (1) This sprite is not contained in a grid (2)
  43.    * <code>loc</code> is valid in <code>gr</code>
  44.    * @param gr the grid into which this sprite should be placed
  45.    * @param loc the location into which the sprite should be placed
  46.    */
  47.   public void putSelfInGrid(Grid<Sprite> gr, Location loc)
  48.   {
  49.     if (grid != null)
  50.       throw new IllegalStateException(
  51.                                       "This sprite is already contained in a grid.");
  52.    
  53.     Sprite sprite = gr.get(loc);
  54.     if (sprite != null)
  55.       sprite.removeSelfFromGrid();
  56.     gr.put(loc, this);
  57.     grid = gr;
  58.     location = loc;
  59.   }
  60.  
  61.   /**
  62.    * Removes this sprite from its grid. <br />
  63.    * Precondition: This sprite is contained in a grid
  64.    */
  65.   public void removeSelfFromGrid()
  66.   {
  67.     if (grid == null)
  68.       throw new IllegalStateException(
  69.                                       "This sprite is not contained in a grid.");
  70.     if (grid.get(location) != this)
  71.       throw new IllegalStateException(
  72.                                       "The grid contains a different sprite at location "
  73.                                         + location + ".");
  74.    
  75.     grid.remove(location);
  76.     grid = null;
  77.     location = null;
  78.   }
  79.  
  80.   /**
  81.    * Moves this sprite to a new location. If there is another sprite at the
  82.    * given location, it is removed. <br />
  83.    * Precondition: (1) This sprite is contained in a grid (2)
  84.    * <code>newLocation</code> is valid in the grid of this sprite
  85.    * @param newLocation the new location
  86.    */
  87.   public void moveTo(Location newLocation)
  88.   {
  89.     if (grid == null)
  90.       throw new IllegalStateException("This sprite is not in a grid.");
  91.     if (grid.get(location) != this)
  92.       throw new IllegalStateException(
  93.                                       "The grid contains a different sprite at location "
  94.                                         + location + ".");
  95.     if (!grid.isValid(newLocation))
  96.       throw new IllegalArgumentException("Location " + newLocation
  97.                                            + " is not valid.");
  98.    
  99.     if (newLocation.equals(location))
  100.       return;
  101.     grid.remove(location);
  102.     Sprite other = grid.get(newLocation);
  103.     if (other != null)
  104.       other.removeSelfFromGrid();
  105.     location = newLocation;
  106.     grid.put(location, this);
  107.   }
  108.  
  109.   /**
  110.    * Creates a string that describes this sprite.
  111.    * @return a string with the location of this sprite
  112.    */
  113.   public String toString()
  114.   {
  115.     return getClass().getName() + "[location=" + location + "]";
  116.   }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement