Subversion Repositories sokoban

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 daniel-mar 1
package gdi1sokoban.gui;
2
 
3
import gdi1sokoban.exceptions.InvalidOperationException;
4
import gdi1sokoban.exceptions.ParameterOutOfRangeException;
5
import gdi1sokoban.game.GameEngine;
6
import gdi1sokoban.game.GameLevel;
7
import gdi1sokoban.main.Sokoban;
8
 
9
/**
10
 * SokobanPanel class
11
 *
12
 * extends the GamePanel class and provides
13
 * basic functions for controlling the appearance
14
 */
15
public class SokobanPanel extends GamePanel {
16
        private static final long serialVersionUID = 7037462999938492665L;
17
 
18
        public SokobanPanel(final SokobanWindow theParentWindow) {
19
                super(theParentWindow);
20
        }
21
 
22
        /**
23
         * Loads the images for Player, Wall, Box, Goal, Empty Field, Box on Goal and Player on Goal.
24
         * There are different sets of images which can be chosen by giving an image id
25
         * @param skin_id The id of the image set that shall be loaded
26
         * @throws InvalidOperationException
27
         * @throws ParameterOutOfRangeException
28
         */
29
        public void loadSkin(final int skinId){
30
                String path =  "bin/sprites/";
31
                // set the path to the path of the skin with the given id
32
                switch(skinId){
33
                case 0:
34
                        path += "standard/";
35
                        break;
36
                case 1:
37
                        path += "standardbig/";
38
                        break;
39
                case 2:
40
                        path += "doom/";
41
                        break;
42
                case 3:
43
                        path += "crazyPaul/";
44
                        break;
45
                }
46
                try {
47
                        registerImageOverwrite("player", path+"player.png");
48
                        registerImageOverwrite("crate", path+"box.png");
49
                        registerImageOverwrite("field", path+"floor.png");
50
                        registerImageOverwrite("wall", path+"wall.png");
51
                        registerImageOverwrite("goal", path+"goal.png");
52
                        registerImageOverwrite("playerOnGoal", path+"playerongoal.png");
53
                        registerImageOverwrite("crateOnGoal", path+"boxongoal.png");
54
                } catch (final ParameterOutOfRangeException e) {
55
                        e.printStackTrace();
56
                } catch (final InvalidOperationException e) {
57
                        e.printStackTrace();
58
                }
59
        }
60
 
61
        /**
62
         * places the objects from the given Level on the Panel
63
         * @param level the level-object
64
         * @throws ParameterOutOfRangeException
65
         */
66
        public void placeObjects(final GameLevel level) throws ParameterOutOfRangeException{
67
                for(int row=0; row < level.getMap().length; row++)
68
                        for(int col=0; col < level.getMap()[0].length; col++)
69
                                placeEntity(level.getObject(row, col));
70
        }
71
 
72
        @Override
73
        protected void entityClicked(final int positionX, final int positionY) {
74
                final GameEngine engine = Sokoban.getEngine();
75
                try {
76
                        engine.playerWalk(positionY, positionX);
77
                } catch (final ParameterOutOfRangeException e) {
78
                        System.err.println("Fehler: Ungültige Levelkoordinaten!");
79
                }
80
        }
81
 
82
        @Override
83
        protected void panelResized() {
84
                // Set Window's size to size of panel
85
                getParentWindow().panelResized(getWidth(),getHeight());
86
        }
87
 
88
        @Override
89
        protected void setGamePanelContents() {
90
                try {
91
                        final GameEngine engine = Sokoban.getEngine();                 
92
                        if (engine != null)
93
                                placeObjects(engine.getGameLevel());
94
                } catch (final ParameterOutOfRangeException e) {
95
                        System.err.println("Fehler beim einlesen der Objekte auf dem Spielfeld");
96
                }
97
        }
98
 
99
        /**
100
         * Helper-function to register an image with the given identifier.
101
         * If the identifier is already taken, it is unregistered first.  
102
         * @param identifier the image id
103
         * @param fileName filename of the image file
104
         * @throws ParameterOutOfRangeException
105
         * @throws InvalidOperationException
106
         */
107
        private void registerImageOverwrite(final String identifier, final String fileName) throws ParameterOutOfRangeException, InvalidOperationException{
108
                if(isImageRegistered(identifier))
109
                                unregisterImage(identifier);
110
                registerImage(identifier, fileName);
111
        }
112
}