View Javadoc

1   package de.jos.game.objects;
2   
3   import java.awt.AlphaComposite;
4   import java.awt.Composite;
5   import java.awt.Graphics2D;
6   
7   import com.golden.gamedev.object.AnimatedSprite;
8   import com.golden.gamedev.object.Sprite;
9   import com.golden.gamedev.object.Timer;
10  import com.golden.gamedev.object.sprite.AdvanceSprite;
11  
12  import de.jos.game.logic.CombinationUtils;
13  import de.jos.game.logic.AbstractGameLogic.Direction;
14  
15  public class Bullet extends SimplifiedBullet {
16  
17    private static final long serialVersionUID = -4465053527340777730L;
18  
19    // coordinate on the board, not the absolute coordinate on the board
20    private int boardX;
21    private int boardY;
22  
23    private int targetAbsoluteX;
24    private int targetAbsoluteY;
25  
26    private int xdiff = 0;
27    private int ydiff = 0;
28  
29    private Direction moveDirection = null;
30  
31    public enum State {
32      WAITING, MOVING, DISSOLVING, FADEIN, FINISHED
33    };
34  
35    private State actionState;
36  
37    // sprite for rotation and hovering
38    private AnimatedSprite spriteHover = null;
39    private AdvanceSprite spriteRotate = null;
40    private Sprite spriteSmall = null;
41  
42    private Sprite currentSprite = null;
43  
44    private float transparency;
45  
46    private ResourceContainer resourceContainer = null;
47  
48    private final Timer timer = new Timer(65);
49  
50    private static final int[] DIRECTION_LEFT_UP = new int[] { 5, 4, 3, 2, 1, 0 };
51    private static final int[] DIRECTION_RIGHT_DOWN = new int[] { 0, 1, 2, 3, 4, 5 };
52  
53    public Bullet(Integer color, BulletSize status) {
54      super(color, status);
55    }
56  
57    public Bullet() {
58      setStatus(BulletSize.BIG);
59    }
60  
61    public void init() {
62      spriteRotate = new AdvanceSprite(getResourceContainer().getBulletSpriteRotate(getColor().intValue()));
63      spriteRotate.setAnimate(true);
64      spriteRotate.setLoopAnim(true);
65      spriteRotate.setAnimationTimer(timer);
66  
67      spriteSmall = getResourceContainer().createAnimatedSpriteSmall(getColor());
68  
69      spriteHover = getResourceContainer().createAnimatedSprite(getColor());
70  
71      // set the current sprite (which will be rendered)
72      if (isStatusBig()) {
73        setCurrentSprite(spriteHover);
74      } else if (isStatusSmall()) {
75        setCurrentSprite(spriteSmall);
76      }
77  
78      actionState = State.WAITING;
79  
80      // update and thus set the sprite coordinates
81      updateSpriteCoordinates();
82    }
83  
84    public void updateSpriteCoordinates() {
85      // set the coordinates of the sprites
86      setSpriteAbsolute(X_OFFSET + boardX * SPRITE_WIDTH, Y_OFFSET + boardY * SPRITE_HEIGHT);
87    }
88  
89    public void render(Graphics2D graphics) {
90      if (isDissolving() == true) {
91        Composite oldComposite = graphics.getComposite();
92  
93        Composite cursorComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getTransparency());
94        graphics.setComposite(cursorComposite);
95        getCurrentSprite().render(graphics);
96  
97        graphics.setComposite(oldComposite);
98      } else {
99        getCurrentSprite().render(graphics);
100     }
101   }
102 
103   private void moveSprites(double deltax, double deltay) {
104     spriteHover.move(deltax, deltay);
105     spriteRotate.move(deltax, deltay);
106     spriteSmall.move(deltax, deltay);
107   }
108 
109   private void setSpriteAbsolute(int x, int y) {
110     spriteHover.setX(x);
111     spriteHover.setY(y);
112     spriteRotate.setX(x);
113     spriteRotate.setY(y);
114     spriteSmall.setX(x);
115     spriteSmall.setY(y);
116   }
117 
118   public int getSpriteAbsoluteX() {
119     return (int) getCurrentSprite().getX();
120   }
121 
122   public int getSpriteAbsoluteY() {
123     return (int) getCurrentSprite().getY();
124   }
125 
126   public void update(long elapsedTime) {
127 
128     // Transparenz fuers DISSOLVING berechnen
129     if (actionState == State.DISSOLVING) {
130       transparency += TRANSPARENCY_INCREMENT;
131       if (transparency < 0.0f) {
132         transparency = 0.0f;
133         setActionState(State.FINISHED);
134       }
135     }
136 
137     // Transparenz fuers FADEIN berechnen
138     if (actionState == State.FADEIN) {
139       System.out.println("fade");
140       transparency -= TRANSPARENCY_INCREMENT;
141       if (transparency > 1.0f) {
142         transparency = 1.0f;
143         setActionState(State.WAITING);
144       }
145     }
146 
147     if (actionState == State.MOVING) {
148       moveSprites(xdiff * MOVING_FACTOR, ydiff * MOVING_FACTOR);
149 
150       // has the bullet reached the target coordinates ?
151       if (getTargetAbsoluteX() == getSpriteAbsoluteX() && getTargetAbsoluteY() == getSpriteAbsoluteY()) {
152 
153         // if the last position was on the board -> shrink the bullet, else set
154         // the hover sprite again
155         if (CombinationUtils.isOnBoard(getBoardX(), getBoardY())) {
156           setStatus(BulletSize.SMALL);
157           setCurrentSprite(spriteSmall);
158         } else {
159           setCurrentSprite(spriteHover);
160         }
161 
162         setBoardX((getTargetAbsoluteX() - X_OFFSET) / SPRITE_WIDTH);
163         setBoardY((getTargetAbsoluteY() - Y_OFFSET) / SPRITE_HEIGHT);
164         // reset targetX and targetY and isMoving
165         targetAbsoluteX = -1;
166         targetAbsoluteY = -1;
167         actionState = State.WAITING;
168       }
169     }
170 
171     currentSprite.update(elapsedTime);
172   }
173 
174   public boolean isMoving() {
175     return actionState == State.MOVING;
176   }
177 
178   public boolean isDissolving() {
179     return actionState == State.DISSOLVING;
180   }
181 
182   public float getTransparency() {
183     return transparency;
184   }
185 
186   public void setTransparency(float transparency) {
187     this.transparency = transparency;
188   }
189 
190   public ResourceContainer getResourceContainer() {
191     return resourceContainer;
192   }
193 
194   public void setResourceContainer(ResourceContainer resourceContainer) {
195     this.resourceContainer = resourceContainer;
196   }
197 
198   public boolean isFinished() {
199     return actionState == State.FINISHED;
200   }
201 
202   public Direction getMoveDirection() {
203     return moveDirection;
204   }
205 
206   public void setMoveDirection(Direction moveDirection) {
207     this.moveDirection = moveDirection;
208   }
209 
210   public void prepareMove() {
211     if (moveDirection == Direction.LEFT || moveDirection == Direction.UP) {
212       spriteRotate.setAnimationFrame(DIRECTION_LEFT_UP);
213     } else {
214       spriteRotate.setAnimationFrame(DIRECTION_RIGHT_DOWN);
215     }
216 
217     xdiff = 0;
218     ydiff = 0;
219     if (moveDirection == Direction.DOWN) {
220       ydiff = 1;
221     } else if (moveDirection == Direction.UP) {
222       ydiff = -1;
223     } else if (moveDirection == Direction.LEFT) {
224       xdiff = -1;
225     } else if (moveDirection == Direction.RIGHT) {
226       xdiff = 1;
227     }
228 
229     setCurrentSprite(spriteRotate);
230     actionState = State.MOVING;
231   }
232 
233   private Sprite getCurrentSprite() {
234     return currentSprite;
235   }
236 
237   private void setCurrentSprite(Sprite currentSprite) {
238     this.currentSprite = currentSprite;
239   }
240 
241   public int getTargetAbsoluteX() {
242     return targetAbsoluteX;
243   }
244 
245   public void setTargetAbsoluteX(int targetAbsoluteX) {
246     this.targetAbsoluteX = targetAbsoluteX;
247   }
248 
249   public int getTargetAbsoluteY() {
250     return targetAbsoluteY;
251   }
252 
253   public void setTargetAbsoluteY(int targetAbsoluteY) {
254     this.targetAbsoluteY = targetAbsoluteY;
255   }
256 
257   public void resetHoverAnimation() {
258     spriteHover.setAnimate(false);
259     spriteHover.setFrame(0);
260   }
261 
262   public void startHoverAnimation() {
263     spriteHover.setAnimate(true);
264     spriteHover.setLoopAnim(true);
265   }
266 
267   public int getBoardX() {
268     return boardX;
269   }
270 
271   public void setBoardX(int boardX) {
272     this.boardX = boardX;
273   }
274 
275   public int getBoardY() {
276     return boardY;
277   }
278 
279   public void setBoardY(int boardY) {
280     this.boardY = boardY;
281   }
282 
283   public State getActionState() {
284     return actionState;
285   }
286 
287   public void setActionState(State actionState) {
288     this.actionState = actionState;
289   }
290 
291 }