-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaddleState.java
306 lines (271 loc) · 9.75 KB
/
PaddleState.java
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package breakout;
import java.awt.Color;
/**
* Each instance of this class represents a paddle in the breakout game.
*
* @immutable
*
* @invar The size of a paddle is given by a vector with both x and y being positive
* | getSize().getX() >= 0 && getSize().getY() >= 0
* @invar | getCenter() != null
* @invar | getSize() != null
*/
public abstract class PaddleState {
/**
* @invar | center != null
* @invar | size != null
*/
protected Point center;
protected Vector size;
// The maximum lifetime in number of hits of a replicator paddle
protected static final int MAX_REPLICATOR_LIFETIME = 3;
// The default paddle dimensions
public static final int HEIGHT = 500;
public static final int WIDTH = 3000;
public static final Vector DEFAULT_SIZE = new Vector(WIDTH/2, HEIGHT/2);
/**
* Returns the center Point object contained within this PaddleState object.
*/
public abstract Point getCenter();
/**
* Returns the size Vector object contained within this PaddleState object.
*/
public abstract Vector getSize();
/**
* Returns a Rectangle object representing the rectangle surrounding the paddle represented by this PaddleState object.
*
* @creates | result
* @post | result.getTopLeft().equals(getCenter().minus(getSize()))
* @post | result.getBottomRight().equals(getCenter().plus(getSize()))
*/
public abstract Rect rectangleOf();
/**
* Sets the center Point object contained within this PaddleState object to the one supplied.
* @creates | result
* @pre | center != null
* @post | result.getCenter().equals(center)
*/
public abstract PaddleState changeCenter(Point center);
/**
* Returns the Color object used to display paddles in the breakout game
* -> Different behaviour depending on the paddle type
*/
public abstract Color getColor();
/**
* Returns a ballPaddleHitResults object containing the ball and paddle states and the required number of replicates to be made,
* resulting from a possible ball-paddle hit.
* -> Different behaviour depending on the paddle type
* @creates | result
* @inspects | ball
* @pre | ball != null
* @pre | paddleDir == 0 || paddleDir == 1 || paddleDir == -1
*/
public abstract ballPaddleHitResults hitBall(Ball ball, int paddleDir);
/**
* Returns a copy of this ReplicatorPaddleState object representing a replicator paddle that has been powered up.
* @creates | result
* -> Different behaviour depending on the paddle state
*/
public abstract ReplicatorPaddleState powerup();
}
/**
* Each instance of this class represents a normal paddle in the breakout game.
*
* @immutable
*/
final class NormalPaddleState extends PaddleState {
/**
* Returns an object representing a normal rectangular paddle defined by a center point and a size vector
* that is positive for both coordinates.
*
* @pre | center != null
* @pre | size != null
* @pre The given size vector must be positive for both coordinates.
* | size.getX() >= 0 && size.getY() >= 0
* @post | getCenter().equals(center)
* @post | getSize().equals(size)
*/
public NormalPaddleState(Point center, Vector size) {
this.center=new Point(center.getX(), center.getY());
this.size=new Vector(size.getX(), size.getY());
}
public Point getCenter() {
return new Point(center.getX(), center.getY());
}
public Vector getSize() {
return new Vector(size.getX(), size.getY());
}
public NormalPaddleState changeCenter(Point center) {
return new NormalPaddleState(center, size);
}
public Rect rectangleOf() {
return new Rect(center.minus(size), center.plus(size));
}
/**
* Returns the Color object used to display normal paddles in the breakout game, i.e. green.
*/
public Color getColor() {
return Color.green;
}
/**
* Returns a copy of this NormalPaddleState object converted to a ReplicatorPaddleState object with the maximum lifetime.
* @creates | result
* @post | result.getCenter().equals(old(getCenter()))
* @post | result.getSize().equals(old(getSize()))
* @post | result.getLifetime() == MAX_REPLICATOR_LIFETIME
*/
public ReplicatorPaddleState convertToReplicator() {
return new ReplicatorPaddleState(center, size, MAX_REPLICATOR_LIFETIME);
}
/**
* Returns a ballPaddleHitResults object containing the ball and paddle states and the required number of replicates to be made,
* resulting from a possible ball-paddle hit. A normal paddle cannot request replicates.
* @creates | result
* @inspects | ball
* @pre | ball != null
* @pre | paddleDir == 0 || paddleDir == 1 || paddleDir == -1
* @post | result.reps == 0
* @post | result.ball instanceof Ball
* @post | result.paddle instanceof NormalPaddleState
*/
public ballPaddleHitResults hitBall(Ball ball, int paddleDir) {
Vector normVecPaddle = ball.rectangleOf().overlap(this.rectangleOf());
if (normVecPaddle != null &&
normVecPaddle.product(ball.getVelocity()) > 0) { // Bounce only when the ball is at the outside
ball.bounce(normVecPaddle);
ball.changeVelocity(ball.getVelocity().plus(Vector.RIGHT.scaled(2*paddleDir)));
}
return new ballPaddleHitResults(ball, this, 0);
}
/**
* Returns a copy of this NormalPaddleState object representing a normal paddle that has been powered up
* to a replicator paddle state with maxiumum lifetime.
* @creates | result
* @post | result.getCenter().equals(old(getCenter()))
* @post | result.getSize().equals(old(getSize()))
* @post | result.getLifetime() == MAX_REPLICATOR_LIFETIME
*/
public ReplicatorPaddleState powerup() {
return this.convertToReplicator();
}
}
/**
* Each instance of this class represents a replicator paddle of the breakout game.
*
* @immutable
*
* @invar | getLifetime() >= 1 && getLifetime() <= 3
*/
final class ReplicatorPaddleState extends PaddleState {
/**
* @invar | lifetime >= 1 && lifetime <= 3
*/
private final int lifetime;
/**
* /**
* Returns an object representing a rectangular replicator paddle defined by a center point and a size vector
* that is positive for both coordinates.
*
* @pre | center != null
* @pre | size != null
* @pre The given size vector must be positive for both coordinates.
* | size.getX() >= 0 && size.getY() >= 0
* @post | getCenter().equals(center)
* @post | getSize().equals(size)
* @post | getLifetime() == lifetime
*/
public ReplicatorPaddleState(Point center, Vector size, int lifetime) {
this.center=new Point(center.getX(), center.getY());
this.size=new Vector(size.getX(), size.getY());
this.lifetime=lifetime;
}
public Point getCenter() {
return new Point(center.getX(), center.getY());
}
public Vector getSize() {
return new Vector(size.getX(), size.getY());
}
public Rect rectangleOf() {
return new Rect(center.minus(size), center.plus(size));
}
public ReplicatorPaddleState changeCenter(Point center) {
return new ReplicatorPaddleState(center, size, lifetime);
}
/**
* Returns the remaining lifetime in number of hits of this ReplicatorPaddleState object.
*/
public int getLifetime() {
return lifetime;
}
/**
* Returns a copy of this ReplicatorPaddleState with the lifetime decreased by one, or converts it to a
* NormalPaddleState in case the lifetime decreased to zero.
* @creates | result
* @post | result instanceof NormalPaddleState || ((ReplicatorPaddleState) result).getLifetime() == old(getLifetime())-1
*/
public PaddleState decreaseLifetime() {
int newage=lifetime-1;
if (newage <= 0) {
return this.convertToNormal();
}
return new ReplicatorPaddleState(center,size,newage);
}
/**
* Creates a copy this ReplicatorPaddleState in which the lifetime was reset.
* @creates | result
* @post | result.getLifetime() == MAX_REPLICATOR_LIFETIME
*/
public ReplicatorPaddleState resetLifetime() {
return new ReplicatorPaddleState(center,size,MAX_REPLICATOR_LIFETIME);
}
/**
* Returns the default colour used to display replicator paddles in the breakout game, i.e. yellow.
*/
public Color getColor() {
return Color.yellow;
}
/**
* Returns a copy of this ReplicatorPaddleState object that was converted to a NormalPaddleState object
* @creates | result
* @post | result.getCenter().equals(old(getCenter()))
* @post | result.getSize().equals(old(getSize()))
*/
public NormalPaddleState convertToNormal() {
return new NormalPaddleState(center, size);
}
/**
* Returns a ballPaddleHitResults object containing the ball and paddle states and the required number of replicates to be made,
* resulting from a possible ball-paddle hit. A replicator paddle requests 1, 2 or 3 replicates, or 0 in case there was no hit.
* @creates | result
* @inspects | ball
* @pre | ball != null
* @pre | paddleDir == 0 || paddleDir == 1 || paddleDir == -1
* @post | result.reps >= 0 && result.reps <= 3
* @post | result.ball instanceof Ball
* @post | result.paddle instanceof PaddleState
*/
public ballPaddleHitResults hitBall(Ball ball, int paddleDir) {
int reps = 0;
PaddleState newState = this;
Vector normVecPaddle = ball.rectangleOf().overlap(this.rectangleOf());
if (normVecPaddle != null &&
normVecPaddle.product(ball.getVelocity()) > 0) { // Bounce only when the ball is at the outside
ball.bounce(normVecPaddle);
ball.changeVelocity(ball.getVelocity().plus(Vector.RIGHT.scaled(2*paddleDir)));
newState = this.decreaseLifetime();
reps=this.getLifetime();
}
return new ballPaddleHitResults(ball, newState, reps);
}
/**
* Returns a copy of this ReplicatorPaddleState object representing a replicator paddle that has been powered up.
* This basically resets its lifetime in case of replicator paddles.
* @creates | result
* @post | result.getCenter().equals(old(getCenter()))
* @post | result.getSize().equals(old(getSize()))
* @post | result.getLifetime() == MAX_REPLICATOR_LIFETIME
*/
public ReplicatorPaddleState powerup() {
return this.resetLifetime();
}
}