JTP
Class LevelSet

java.lang.Object
  extended by JTP.LevelSet
All Implemented Interfaces:
Serializable

public class LevelSet
extends Object
implements Serializable

Deals with handling and storage of a collection of levels.

Author:
V.Termanis
See Also:
Serialized Form

Constructor Summary
LevelSet(String name, int levelWidth, int levelHeight, int tileHeightLimit)
          Constructs a new single-player LevelSet instance with the given attributes.
LevelSet(String name, int levelWidth, int levelHeight, int tileHeightLimit, boolean twoPlayer)
          Constructs a LevelSet instance with the given attributes.
 
Method Summary
 Level addLevel()
          Creates and adds a new level to this LevelSet.
 void addLevel(Level l)
          Adds a new level manually.
 int[] getInvalidLevelIndices()
          Returns indices of invalid levels in this LevelSet.
 Level getLevel(int index)
          Provides direct access to a level in this set.
 String getName()
          Returns name of this LevelSet.
 int getSize()
          Returns number of levels stored in this LevelSet.
 boolean isTwoPlayer()
          Returns whether this is a two-player LevelSet.
static LevelSet load(String filename)
          Loads a LevelSet from a file.
static LevelSet load(URL location)
          Loads a LevelSet from the given location.
 void purgeEmptyLevels()
          Removes all empty levels from this LevelSet
 void purgeInvalidLevels()
          Removes all invalid and empty levels from this LevelSet.
 boolean readOnly()
          Returns whether the level can be saved or not.
 void removeLevel(int index)
          Removes a level from this LevelSet.
static boolean saveToDisk(LevelSet levelSet, String filename, boolean overwrite)
          Writes the specified LevelSet to a file.
 void setName(String name)
          Changes the name of this LevelSet.
 boolean swapLevel(int index)
          Swaps specified level with next one.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LevelSet

public LevelSet(String name,
                int levelWidth,
                int levelHeight,
                int tileHeightLimit)
         throws IllegalArgumentException,
                NullPointerException
Constructs a new single-player LevelSet instance with the given attributes.

Parameters:
name - name of the levelset
levelWidth - width in tiles of levels stored in this set
levelHeight - height in tiles of levels stored in this set
tileHeightLimit - maximum height of any one pile of tiles
Throws:
IllegalArgumentException - if any of the following are satisfied:
  • levelWidth < 2
  • levelHeight < 2
  • tileHeightLimit < 1
NullPointerException - if name is null

LevelSet

public LevelSet(String name,
                int levelWidth,
                int levelHeight,
                int tileHeightLimit,
                boolean twoPlayer)
         throws IllegalArgumentException,
                NullPointerException
Constructs a LevelSet instance with the given attributes.

Parameters:
name - name of the levelset
levelWidth - width in tiles of levels stored in this set
levelHeight - height in tiles of levels stored in this set
tileHeightLimit - maximum height of any one pile of tiles
twoPlayer - defines whether this will be a single or two-player levelset
Throws:
IllegalArgumentException - if any of the following are satisfied:
  • levelWidth < 2
  • levelHeight < 2
  • tileHeightLimit < 1
NullPointerException - if name is null
Method Detail

getSize

public int getSize()
Returns number of levels stored in this LevelSet.

Returns:
number of levels

getName

public String getName()
Returns name of this LevelSet.

Returns:
name of this set

setName

public void setName(String name)
             throws NullPointerException
Changes the name of this LevelSet.

Parameters:
name - new name for this set
Throws:
NullPointerException - if name is null

isTwoPlayer

public boolean isTwoPlayer()
Returns whether this is a two-player LevelSet.

Returns:
true if this is a two-player set

addLevel

public Level addLevel()
Creates and adds a new level to this LevelSet.

Returns:
the newly added Level

addLevel

public void addLevel(Level l)
              throws NullPointerException
Adds a new level manually. Note: The level to be added will not be checked for the constraints (width, height, etc.) specified when the set was created.

Parameters:
l - the Level to add
Throws:
NullPointerException - if l is null

removeLevel

public void removeLevel(int index)
                 throws IndexOutOfBoundsException
Removes a level from this LevelSet.

Parameters:
index - position of the level to be removed in this set
Throws:
IndexOutOfBoundsException - if index is invalid

swapLevel

public boolean swapLevel(int index)
                  throws IndexOutOfBoundsException
Swaps specified level with next one.

Parameters:
index - position of the level to be swapped with its right neighbour
Returns:
false if the level could not be swapped (i.e. if trying to swap last level as there is no next level), true otherwise
Throws:
IndexOutOfBoundsException - if index is invalid

getLevel

public Level getLevel(int index)
               throws IndexOutOfBoundsException
Provides direct access to a level in this set.

Parameters:
index - position of th level to retrieve
Returns:
the chosen Level
Throws:
IndexOutOfBoundsException - if index is invalid

purgeInvalidLevels

public void purgeInvalidLevels()
Removes all invalid and empty levels from this LevelSet. Note: Validity of a level is defined in the following method: Level.isValid()


purgeEmptyLevels

public void purgeEmptyLevels()
Removes all empty levels from this LevelSet


getInvalidLevelIndices

public int[] getInvalidLevelIndices()
Returns indices of invalid levels in this LevelSet. Note: Empty levels are not counted as invalid.

Returns:
array of invalid level indices or null if all levels are valid

readOnly

public boolean readOnly()
Returns whether the level can be saved or not. Note: Levels which have been saved in development mode cannot be altered in default (user) mode.

Returns:
true if the level can be saved.

saveToDisk

public static boolean saveToDisk(LevelSet levelSet,
                                 String filename,
                                 boolean overwrite)
                          throws IOException
Writes the specified LevelSet to a file. Note: If the level is read-only (see readOnly then the level will definitely not be saved.

Parameters:
levelSet - the set to save
filename - name (and path) of the file to save the set in
overwrite - specifies whether to force the file to be overwritten if it already exists
Returns:
false if
  • there are no levels to save
  • the file already exists and the overwrite parameter is not set
  • the existing file cannot be written to
Throws:
IOException - if the write to disk attempt was not successful

load

public static LevelSet load(URL location)
                     throws ClassNotFoundException,
                            IOException,
                            NullPointerException
Loads a LevelSet from the given location. Note: This method can be used to load a set from:

Parameters:
location - full link to location of file from which to load the set
Returns:
the loaded LevelSet
Throws:
ClassNotFoundException - if the file does not contain a LevelSet (or wrong LevelSet format revision)
IOException - if the read from file attempt was not successful
NullPointerException - if location is null

load

public static LevelSet load(String filename)
                     throws ClassNotFoundException,
                            NullPointerException,
                            IOException
Loads a LevelSet from a file.

Parameters:
filename - name (and path) of the file to load the set from
Returns:
the loaded LevelSet
Throws:
ClassNotFoundException - if the file does not contain a LevelSet (or wrong LevelSet format revision)
IOException - if the read from file attempt was not successful
NullPointerException - if filename is null