summaryrefslogtreecommitdiff
path: root/src/tetris/gui/animations/Animation.java
blob: 899459d5183234f39c67304cdf7f5ed99b115b11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Jesse Morgan <jesterpm@u.washington.edu>
 * 
 * TCSS 305 - Autumn 2009
 * Tetris Project
 * 17 November 2009
 */

package tetris.gui.animations;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;

import tetris.model.IntPoint;


/**
 * Abstract class that processes animations.
 * 
 * @author Jesse Morgan <jesterpm@u.washington.edu>
 * @version 1.0 11 Dec 2009
 */
public abstract class Animation {
  // Public Constants
  /**
   * How quickly animation should be updated.
   */
  public static final int FRAME_RATE = 60;
  
  // Private Constants
  /**
   * How much we show in each step.
   */
  private static final int R = 5;
  
  /**
   * The current step in the animation.
   */
  private int my_step;
  
  /**
   * The current position in the animation.
   */
  private IntPoint my_current_pos;
  
  /**
   * Current Step Point.
   */
  private IntPoint my_step_point;
  
  /**
   * Animation Running Flag.
   */
  private boolean my_animation_running;
  
  /**
   * What the animation presently looks like.
   */
  private final BufferedImage my_image;
  
  /**
   * What the animation will look like.
   */
  private final Image my_end_image;
  
  /**
   * Create a new animation.
   * 
   * @param the_end_image End result.
   * @param the_width Animation width.
   * @param the_height Animation height.
   */
  public Animation(final Image the_end_image, final int the_width, final int the_height) {
    my_step = 0;
    my_animation_running = false;
    
    my_current_pos = new IntPoint(0, 0);
    my_step_point = new IntPoint(0, 0);
    
    my_end_image = the_end_image;
    my_image = new BufferedImage(the_width, the_height,
                                 BufferedImage.TYPE_INT_ARGB);
    
  }
  
  /**
   * Start the animation.
   */
  public void start() {
    my_step = 0;
    
    my_current_pos = getStep(0);
    my_step_point = getStep(0);
    
    my_animation_running = true;
  }
  
  /**
   * Stop the animation.
   */
  public void stop() {
    my_animation_running = false;
  }
  
  /**
   * @return true if the animation is running.
   */
  public boolean isRunning() {
    return my_animation_running;
  }
  
  /**
   * Draw the next increment of the animation.
   */
  public void stepAnimation() {
    // If we're near our destination, move to the next step.
    if (
        Math.abs(my_current_pos.getX() - my_step_point.getX()) < R &&
        Math.abs(my_current_pos.getY() - my_step_point.getY()) < R
    ) {     
      // If we're out of steps, stop.
      if (my_step >= stepCount() - 1) {
        my_animation_running = false;
        return;
      }
      
      my_step++;
      my_step_point = getStep(my_step);
    }
    
    // Draw!
    final Graphics2D g2d = my_image.createGraphics();
    
    g2d.drawImage(my_end_image,
                  my_current_pos.getX(), my_current_pos.getY(),
                  my_current_pos.getX() + R, my_current_pos.getY() + R,
                  
                  my_current_pos.getX() % my_end_image.getWidth(null),
                  my_current_pos.getY() % my_end_image.getHeight(null),
                  (my_current_pos.getX() + R) % my_end_image.getWidth(null),
                  (my_current_pos.getY() + R)  % my_end_image.getHeight(null),
                  null);
    
    // Some math to draw in the correct "direction"
    final int x = my_step_point.getX() - my_current_pos.getX();
    final int y = my_step_point.getY() - my_current_pos.getY();
    double theta = Math.asin(y / Math.sqrt(x * x + y * y));
    
    // Correction if we're moving toward the Y axis.
    if (x < 0) {
      theta = -theta + Math.PI; 
    }
    
    // Calculate the next point
    my_current_pos = new IntPoint((int) (my_current_pos.getX() + R * Math.cos(theta)),
                               (int) (my_current_pos.getY() + R * Math.sin(theta)));
    
  }
  
  /**
   * @return the image of the current animation.
   */
  public Image getImage() {
    return my_image;
  }
  
  /**
   * @return the current X in the animation.
   */
  public int getX() {
    return my_current_pos.getX();
  }
  
  /**
   * @return the current Y in the animation.
   */
  public int getY() {
    return my_current_pos.getY();
  }
  
  /**
   * Returns the point at the given step.
   * @param the_step The Step.
   * @return the Point.
   */
  protected abstract IntPoint getStep(final int the_step);
  
  /**
   * @return the number of steps in the animation.
   */
  protected abstract int stepCount();
}