View Javadoc

1   package de.jos.game.logic;
2   
3   import java.awt.Graphics2D;
4   import java.awt.event.MouseEvent;
5   
6   import org.apache.commons.lang.StringUtils;
7   import org.apache.commons.logging.Log;
8   import org.apache.commons.logging.LogFactory;
9   
10  import com.golden.gamedev.GameObject;
11  import com.golden.gamedev.engine.BaseInput;
12  import com.golden.gamedev.object.AnimatedSprite;
13  
14  import de.jos.game.Constants;
15  import de.jos.game.actions.SpecialActionEvent;
16  import de.jos.game.objects.Bullet;
17  import de.jos.game.objects.ResourceContainer;
18  import de.jos.game.objects.SimplifiedBullet;
19  import de.jos.game.xml.Level;
20  
21  public abstract class AbstractGameLogic implements Constants {
22  
23    private static Log log = LogFactory.getLog(AbstractGameLogic.class);
24  
25    private ResourceContainer resourceContainer = null;
26    private Level level = null;
27  
28    private AbstractGameBoard board = null;
29  
30    private int mouseState = MOUSE_STATE_NOT_PRESSED;
31  
32    private Integer xDownPressed = null;
33    private Integer yDownPressed = null;
34  
35    private Integer xDownReleased = null;
36    private Integer yDownReleased = null;
37  
38    private boolean gameFinished = false;
39    private boolean gameSolved = false;
40  
41    // possible move directions
42    public enum Direction {
43      UP, RIGHT, DOWN, LEFT
44    };
45  
46    /***
47     * Rendert das Spielfeld mit allen Grafiken
48     * 
49     * @param graphics
50     *          Das Grahpics2D Objekt.
51     * @param gameObject
52     *          Das Engine Game Objekt.
53     */
54    public void render(Graphics2D graphics, GameObject gameObject) {
55  
56      // Hintergrund rendern
57      renderBackground(graphics);
58  
59      // Bullets rendern
60      for (Bullet tmpBullet : board.getBulletList()) {
61        tmpBullet.render(graphics);
62      }
63  
64      // actionContainer rendern
65      getBoard().getActionContainer().render(graphics, gameObject);
66    }
67  
68    /***
69     * Methode rendert zusaetzlich modusspezifische Backgrounds.
70     * 
71     * @param graphics
72     *          Der Graphics Context
73     */
74    public abstract void renderBackground(Graphics2D graphics);
75  
76    public void update(long elapsedTime, GameObject gameObject) {
77  
78      // actionContainer updaten
79      getBoard().getActionContainer().update(elapsedTime, gameObject);
80  
81      // werden im container blockierende actions ausgefuehrt
82      if (getBoard().getActionContainer().isBlocking() == true) {
83        // System.out.println("blocking action");
84        return;
85      }
86  
87      // update the bullets
88      for (Bullet tmpBullet : board.getBulletList()) {
89        tmpBullet.update(elapsedTime);
90      }
91  
92      // animation nur zuruecksetzen und/oder neue animation starten
93      // wenn keine taste gedrueckt ist
94      if (!(mouseState == MOUSE_STATE_PRESSED && gameObject.bsInput.isMouseDown(MouseEvent.BUTTON1))) {
95        int mouseX = gameObject.getMouseX();
96        int mouseY = gameObject.getMouseY();
97        int x = ((mouseX - Bullet.X_OFFSET) / Bullet.SPRITE_WIDTH);
98        int y = ((mouseY - Bullet.Y_OFFSET) / Bullet.SPRITE_HEIGHT);
99  
100       // TODO keine animation fuer rand bloecke
101       board.resetGlowAnimation(x, y);
102 
103       if (BoardUtils.isWithinSelectableBounds(x, y)) {
104         Bullet tmpBullet = board.getBullet(x, y);
105         if (tmpBullet != null && !tmpBullet.getStatus().equals(SimplifiedBullet.BulletSize.SMALL)) {
106           tmpBullet.startHoverAnimation();
107         }
108       }
109 
110     }
111 
112     if (gameObject.bsInput.getMousePressed() != BaseInput.NO_BUTTON
113         && gameObject.bsInput.isMouseDown(MouseEvent.BUTTON1) == true) {
114       System.out.println("bla");
115       xDownPressed = gameObject.getMouseX();
116       yDownPressed = gameObject.getMouseY();
117       mouseState = MOUSE_STATE_PRESSED;
118     }
119 
120     // if (mouseState == MOUSE_STATE_PRESSED &&
121     // gameObject.bsInput.isMouseDown(MouseEvent.BUTTON1)) {
122     // System.out.println("gedrückt");
123     // }
124 
125     if (mouseState == MOUSE_STATE_PRESSED && gameObject.bsInput.isMouseDown(MouseEvent.BUTTON1) == false) {
126 
127       xDownReleased = gameObject.getMouseX();
128       yDownReleased = gameObject.getMouseY();
129       mouseState = MOUSE_STATE_NOT_PRESSED;
130 
131       // direction ermitteln !!
132       int xDiff = (xDownPressed - xDownReleased);
133       int yDiff = (yDownPressed - yDownReleased);
134 
135       // wird diff ueberhaupt beruecksichtigt ??
136       xDiff = (Math.abs(xDiff) > MOVE_DIFF) ? xDiff : 0;
137       yDiff = (Math.abs(yDiff) > MOVE_DIFF) ? yDiff : 0;
138 
139       // nur groesseres diff beruecksichtigen
140       Direction direction = null;
141       if (Math.abs(xDiff) > Math.abs(yDiff)) {
142         direction = (xDiff > 0 ? Direction.LEFT : Direction.RIGHT);
143       } else {
144         direction = (yDiff > 0 ? Direction.UP : Direction.DOWN);
145       }
146 
147       System.out.println("direction : " + direction);
148       int x = ((xDownPressed - Bullet.X_OFFSET) / Bullet.SPRITE_WIDTH);
149       int y = ((yDownPressed - Bullet.Y_OFFSET) / Bullet.SPRITE_HEIGHT);
150       // System.out.println("x : " + x);
151       // System.out.println("y : " + y);
152       if (BoardUtils.isWithinSelectableBounds(x, y)) {
153         // System.out.println("within bounds !!");
154         Bullet tmpBullet = board.getBullet(x, y);
155         if (tmpBullet != null && direction != null) {
156           Object[] o = board.isMovePossible(direction, tmpBullet);
157           if (((Boolean) o[0]).equals(Boolean.TRUE)) {
158 
159             // set the direction in which we are moving
160             tmpBullet.setMoveDirection(direction);
161 
162             tmpBullet.setTargetAbsoluteX(Bullet.X_OFFSET + ((Integer) o[1]) * Bullet.SPRITE_WIDTH);
163             tmpBullet.setTargetAbsoluteY(Bullet.Y_OFFSET + ((Integer) o[2]) * Bullet.SPRITE_HEIGHT);
164 
165             tmpBullet.prepareMove();
166 
167             // TODO create by factory
168             SpecialActionEvent bulletMovingAction = getSpecificBulletMovingAction(tmpBullet);
169 
170             // add the moving action to the actionContainer
171             getBoard().getActionContainer().addAction(bulletMovingAction);
172 
173             // increment the number of moves if in puzzle mode
174             if (Constants.MODE_PUZZLE.equals(getLevel().getMode())) {
175               getBoard().getActionContainer().getActionMoveStatistics().updateMoves(1);
176 
177               // update the board history so we can perform an undo
178               ((PuzzleGameBoard) getBoard()).updateBoardHistory();
179             }
180 
181             getBoard().removeBullet(x, y);
182           }
183         }
184       } else {
185         log.debug("NOT within bounds !");
186       }
187 
188     }
189 
190   }
191 
192   protected abstract SpecialActionEvent getSpecificBulletMovingAction(Bullet bullet);
193 
194   public void finish(GameObject gameObject) {
195     gameObject.finish();
196   }
197 
198   protected String getString(String key) {
199     return StringUtils.upperCase(getResourceContainer().getLabelResources().getString(key));
200   }
201 
202   protected String getString(String key, String appendString) {
203     return getString(key) + appendString;
204   }
205 
206   public ResourceContainer getResourceContainer() {
207     return resourceContainer;
208   }
209 
210   public void setResourceContainer(ResourceContainer resourceContainer) {
211     this.resourceContainer = resourceContainer;
212   }
213 
214   public AbstractGameBoard getBoard() {
215     return board;
216   }
217 
218   public void setBoard(AbstractGameBoard board) {
219     this.board = board;
220   }
221 
222   public Level getLevel() {
223     return level;
224   }
225 
226   public void setLevel(Level level) {
227     this.level = level;
228   }
229 
230   public boolean isGameFinished() {
231     return gameFinished;
232   }
233 
234   public void setGameFinished(boolean gameFinished, boolean solved) {
235     this.gameFinished = gameFinished;
236     this.gameSolved = solved;
237   }
238 
239   public abstract void init();
240 
241   protected static final class RenderedButton {
242 
243     private AnimatedSprite sprite = null;
244     private int nextGameId = -1;
245 
246     public AnimatedSprite getSprite() {
247       return sprite;
248     }
249 
250     public void setSprite(AnimatedSprite sprite) {
251       this.sprite = sprite;
252     }
253 
254     private int getNextGameId() {
255       return nextGameId;
256     }
257 
258     public void setNextGameId(int nextGameId) {
259       this.nextGameId = nextGameId;
260     }
261 
262     public void update(long elapsedTime, GameObject gameObject) {
263       sprite.update(elapsedTime);
264       // workaround: auf frame 0 setzen da es sonst zu einem
265       // flicker effekt kommt
266       sprite.setFrame(0);
267       if (gameObject.checkPosMouse(sprite, true)) {
268         sprite.setFrame(1);
269         if (gameObject.bsInput.isMouseDown(MouseEvent.BUTTON1)) {
270           gameObject.parent.nextGameID = getNextGameId();
271           gameObject.finish();
272         }
273       } else {
274         sprite.setFrame(0);
275       }
276     }
277 
278     public void render(Graphics2D graphics) {
279       sprite.render(graphics);
280     }
281   }
282 
283   public boolean isGameSolved() {
284     return gameSolved;
285   }
286 
287 }