package gdi1sokoban.gui;
import gdi1sokoban.exceptions.InvalidOperationException;
import gdi1sokoban.exceptions.ParameterOutOfRangeException;
import gdi1sokoban.game.GameEngine;
import gdi1sokoban.game.GameLevel;
import gdi1sokoban.main.Sokoban;
/**
* SokobanPanel class
*
* extends the GamePanel class and provides
* basic functions for controlling the appearance
*/
public class SokobanPanel extends GamePanel {
private static final long serialVersionUID = 7037462999938492665L;
public SokobanPanel(final SokobanWindow theParentWindow) {
super(theParentWindow);
}
/**
* Loads the images for Player, Wall, Box, Goal, Empty Field, Box on Goal and Player on Goal.
* There are different sets of images which can be chosen by giving an image id
* @param skin_id The id of the image set that shall be loaded
* @throws InvalidOperationException
* @throws ParameterOutOfRangeException
*/
public void loadSkin(final int skinId){
// set the path to the path of the skin with the given id
switch(skinId){
case 0:
path += "standard/";
break;
case 1:
path += "standardbig/";
break;
case 2:
path += "doom/";
break;
case 3:
path += "crazyPaul/";
break;
}
try {
registerImageOverwrite("player", path+"player.png");
registerImageOverwrite("crate", path+"box.png");
registerImageOverwrite("field", path+"floor.png");
registerImageOverwrite("wall", path+"wall.png");
registerImageOverwrite("goal", path+"goal.png");
registerImageOverwrite("playerOnGoal", path+"playerongoal.png");
registerImageOverwrite("crateOnGoal", path+"boxongoal.png");
} catch (final ParameterOutOfRangeException e) {
e.printStackTrace();
} catch (final InvalidOperationException e) {
e.printStackTrace();
}
}
/**
* places the objects from the given Level on the Panel
* @param level the level-object
* @throws ParameterOutOfRangeException
*/
public void placeObjects(final GameLevel level) throws ParameterOutOfRangeException{
for(int row=0; row < level.getMap().length; row++)
for(int col=0; col < level.getMap()[0].length; col++)
placeEntity(level.getObject(row, col));
}
protected void entityClicked(final int positionX, final int positionY) {
final GameEngine engine = Sokoban.getEngine();
try {
engine.playerWalk(positionY, positionX);
} catch (final ParameterOutOfRangeException e) {
System.
err.
println("Fehler: Ungültige Levelkoordinaten!");
}
}
protected void panelResized() {
// Set Window's size to size of panel
getParentWindow().panelResized(getWidth(),getHeight());
}
protected void setGamePanelContents() {
try {
final GameEngine engine = Sokoban.getEngine();
if (engine != null)
placeObjects(engine.getGameLevel());
} catch (final ParameterOutOfRangeException e) {
System.
err.
println("Fehler beim einlesen der Objekte auf dem Spielfeld");
}
}
/**
* Helper-function to register an image with the given identifier.
* If the identifier is already taken, it is unregistered first.
* @param identifier the image id
* @param fileName filename of the image file
* @throws ParameterOutOfRangeException
* @throws InvalidOperationException
*/
private void registerImageOverwrite
(final String identifier,
final String fileName
) throws ParameterOutOfRangeException, InvalidOperationException
{
if(isImageRegistered(identifier))
unregisterImage(identifier);
registerImage(identifier, fileName);
}
}