Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package feAttempt;
- public class Sprite
- {
- private Grid<Sprite> grid;
- private Location location;
- private String imageFileName;
- /**
- * Gets the grid in which this sprite is located.
- * @return the grid of this sprite, or <code>null</code> if this sprite is
- * not contained in a grid
- */
- public Grid<Sprite> getGrid()
- {
- return grid;
- }
- /**
- * Gets the location of this sprite.
- * @return the location of this sprite, or <code>null</code> if this sprite is
- * not contained in a grid
- */
- public Location getLocation()
- {
- return location;
- }
- public String getImageFileName()
- {
- return imageFileName;
- }
- public void setImageFileName(String imageFileName)
- {
- this.imageFileName = imageFileName;
- }
- /**
- * Puts this sprite into a grid. If there is another sprite at the given
- * location, it is removed. <br />
- * Precondition: (1) This sprite is not contained in a grid (2)
- * <code>loc</code> is valid in <code>gr</code>
- * @param gr the grid into which this sprite should be placed
- * @param loc the location into which the sprite should be placed
- */
- public void putSelfInGrid(Grid<Sprite> gr, Location loc)
- {
- if (grid != null)
- throw new IllegalStateException(
- "This sprite is already contained in a grid.");
- Sprite sprite = gr.get(loc);
- if (sprite != null)
- sprite.removeSelfFromGrid();
- gr.put(loc, this);
- grid = gr;
- location = loc;
- }
- /**
- * Removes this sprite from its grid. <br />
- * Precondition: This sprite is contained in a grid
- */
- public void removeSelfFromGrid()
- {
- if (grid == null)
- throw new IllegalStateException(
- "This sprite is not contained in a grid.");
- if (grid.get(location) != this)
- throw new IllegalStateException(
- "The grid contains a different sprite at location "
- + location + ".");
- grid.remove(location);
- grid = null;
- location = null;
- }
- /**
- * Moves this sprite to a new location. If there is another sprite at the
- * given location, it is removed. <br />
- * Precondition: (1) This sprite is contained in a grid (2)
- * <code>newLocation</code> is valid in the grid of this sprite
- * @param newLocation the new location
- */
- public void moveTo(Location newLocation)
- {
- if (grid == null)
- throw new IllegalStateException("This sprite is not in a grid.");
- if (grid.get(location) != this)
- throw new IllegalStateException(
- "The grid contains a different sprite at location "
- + location + ".");
- if (!grid.isValid(newLocation))
- throw new IllegalArgumentException("Location " + newLocation
- + " is not valid.");
- if (newLocation.equals(location))
- return;
- grid.remove(location);
- Sprite other = grid.get(newLocation);
- if (other != null)
- other.removeSelfFromGrid();
- location = newLocation;
- grid.put(location, this);
- }
- /**
- * Creates a string that describes this sprite.
- * @return a string with the location of this sprite
- */
- public String toString()
- {
- return getClass().getName() + "[location=" + location + "]";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement