-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathNavigationalAide.java
More file actions
232 lines (206 loc) · 7.03 KB
/
NavigationalAide.java
File metadata and controls
232 lines (206 loc) · 7.03 KB
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
import java.awt.Color;
import java.util.Random;
/**
* Aquarium Lab Series: <br>
* The NavigationalAide class defines some of the more complex methods
* for keeping track of a fish's size and location in an aquarium.
* <br> <br>
* Created: <br>
* 23 March 2008, Alyce Brady, from the previous version of AquaFish.<br>
* Modifications: <br>
* 18 January 2009, Alyce Brady, moved determination of direction to AquaFish.<br>
* <br>
*
* @author Alyce Brady
* @version 18 January 2009
*/
public class NavigationalAide
{
// STATE
// Instance Variables: Encapsulated data for EACH fish
private AquaFish theFish; // the fish this aide is helping
private int centerX; // x-coordinate of fish's centerpoint
private int centerY; // y-coordinate of fish's centerpoint
private int length, height; // define size of fish
private int halfLength, halfHeight; // useful for knowing perimeter of fish
// OPERATIONS (constructor and methods)
/**
* The NavigationalAide constructor sets properties of the fish.
* Precondition: the aquarium must be big enough to accomodate
* the biggest fish (currently 75 pixels long and 30 pixels high)
* plus 10 pixels of padding in all four directions.
* @param fish the fish whose size and location this aide is
* keeping track of
**/
public NavigationalAide(AquaFish fish)
{
// Keep track of which fish we are dealing with.
theFish = fish;
// Initialize size, position, and direction.
initSize();
initPos();
}
/**
* Initializes fish size:
* This helper function determines the height and length of the fish.
* Fish are evenly distributed among 4 different sizes based on their
* ID numbers.
**/
private void initSize()
{
// Possible fish lengths are: ?, ?, ?, and ?.
// The height of a fish is always 40% of its length.
length = 30 + (theFish.id() % 4) * 15;
height = (int)Math.round(0.4*length);
// The halfLength and halfHeight instance variables are useful to
// determine the left, right, top, and bottom edges of the fish,
// starting from the centerpoint indicated by (centerX, centerY).
halfLength = (int)Math.round(length/2.0);
halfHeight = (int)Math.round(height/2.0);
}
/**
* Initializes fish position and direction.
* This helper function assigns coordinates to a fish such that the
* fish is placed within the bounds of the Aquarium.
* Precondition: the aquarium must be big enough to accomodate
* the biggest fish (currently 75 pixels long and 30 pixels high)
* plus 10 pixels of padding in all four directions.
**/
private void initPos()
{
// Initialize my position and direction.
centerX = theFish.aquarium().randomCenterX(length);
centerY = theFish.aquarium().randomCenterY(height);
}
/**
* Gets the x coordinate in the aquarium of the fish's centerpoint.
* @return the x coordinate of the fish's centerpoint
**/
public int centerpointX()
{
return centerX;
}
/**
* Gets the y coordinate in the aquarium of the fish's centerpoint.
* @return the y coordinate of the fish's centerpoint
**/
public int centerpointY()
{
return centerY;
}
/**
* Determines whether the fish is facing right.
* @return <code>true</code> if fish is facing right;
* <code>false</code> otherwise
**/
public boolean isFishFacingRight()
{
return theFish.isFacingRight();
}
/** Gets the length of the fish.
* @return fish length
**/
public int fishLength()
{
return length;
}
/** Gets the height of the fish.
* @return fish height
**/
public int fishHeight()
{
return height;
}
/** Gets half the length of the fish.
* @return half the fish length (rounded if necessary)
**/
public int halfFishLength()
{
return halfLength;
}
/** Gets half the height of the fish.
* @return half the fish height (rounded if necessary)
**/
public int halfFishHeight()
{
return halfHeight;
}
/**
* Compute how far the fish is from the wall in front of it.
* @return distance from front of fish to facing wall
**/
protected int fishDistanceToWall()
{
int leftEdgeOfFish = centerX - (halfLength + 1);
int rightEdgeOfFish = centerX + (halfLength + 1);
if ( isFishFacingRight() )
return (theFish.aquarium().width() - rightEdgeOfFish);
else
return leftEdgeOfFish; // since left edge of aquarium is 0
}
/**
* Determine whether the fish is at the surface.
* A fish is considered at the surface if it cannot ascend; in other
* words, if the distance from the center of the fish to the surface
* is less than the fish's height.
* @return <code>true</code> if fish is at the surface;
* <code>false</code> otherwise
**/
public boolean fishAtSurface()
{
int topOfFish = centerY - (halfHeight + 1);
return (topOfFish <= height);
}
/**
* Determine whether the fish is at the bottom.
* A fish is considered at the bottom if it cannot descend; in other
* words, if the distance from the center of the fish to the bottom
* is less than the fish's height.
* @return <code>true</code> if fish is at the bottom;
* <code>false</code> otherwise
**/
public boolean fishAtBottom()
{
int bottomOfFish = centerY + (halfHeight + 1);
return (bottomOfFish >= (theFish.aquarium().height() - height));
}
/**
* This function is provided primarily for debugging purposes.
* @return a string representation of a fish
**/
public String toString()
{
String dir = "L";
if ( isFishFacingRight() )
dir = "R";
return " (" + centerX + ", " + centerY + ") " + dir + " ";
}
/** Moves the fish <code>distance</code> units to the right.
* @param distance distance to move right
**/
protected void moveFishRight(int distance)
{
centerX += distance;
}
/** Moves the fish <code>distance</code> units to the left.
* @param distance distance to move left
**/
protected void moveFishLeft(int distance)
{
centerX -= distance;
}
/** Moves the fish <code>distance</code> units up.
* @param distance distance to move up
**/
protected void raiseFish(int distance)
{
centerY -= distance; // y coordinates get smaller going up
}
/** Moves the fish <code>distance</code> units down.
* @param distance distance to move down
**/
protected void sinkFish(int distance)
{
centerY += distance; // y coordinates get bigger going down
}
}