|
001 packagepackage is used to name the directory or folder a class is in scg.ch09;
002
003
004 importimport means to make the classes and/or packages available in this program fang2.core.Game;
005
006 /**
007 * The fast swimmer at the top of the screen. Waves and all that.
008 */
009 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects FastSwimmer
010 extendsextends means to customize or extend the functionality of a class Swimmer {open braces start code blocks and must be matched with a close brace
011 /** max seconds between appearances of fast swimmer */
012 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) doubledouble is the type for numbers that can contain decimal fractions MAX_TIME_OFF_SCREEN =this assignment operator makes the left side equal to the right side 6.0;
013
014 /** The velocity to be set when the fast swimmer launches */
015 privateprivate is used to restrict access to the current class only doubledouble is the type for numbers that can contain decimal fractions speedX;
016 privateprivate is used to restrict access to the current class only doubledouble is the type for numbers that can contain decimal fractions speedY;
017
018 /** state flag: is the swimmer waiting to launch? */
019 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false waiting;
020
021 /**
022 * Create a newnew is used to create objects by calling the constructor fast swimmer with the given score value. Score is
023 * passed to the {open braces start code blocks and must be matched with a close brace@link Swimmer}close braces end code blocks and must match an earlier open brace constructor.
024 *
025 * @paramthis is the Javadoc tag for documenting the purpose of parameters score
026 */
027 publicpublic is used to indicate unrestricted access (any other class can have access) FastSwimmer(intint is the type for whole numbers and it is short for integer score, doubledouble is the type for numbers that can contain decimal fractions speedX, doubledouble is the type for numbers that can contain decimal fractions speedY) {open braces start code blocks and must be matched with a close brace
028 super(score, 0, 0); // initial velocity is (0, 0)
029 setLaunchSpeed(speedX, speedY);
030 startWaiting();
031 }close braces end code blocks and must match an earlier open brace
032
033 /**
034 * Advance the fast swimmer. If swimming, move. If waiting, keep
035 * waiting unless it is time to launch.
036 *
037 * @paramthis is the Javadoc tag for documenting the purpose of parameters dT seconds since last frame
038 */
039 @Override
040 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value advance(doubledouble is the type for numbers that can contain decimal fractions dT) {open braces start code blocks and must be matched with a close brace
041 decrementTimer(dT);
042 ifif executes the next statement only if the condition in parenthesis evaluates to true (isSwimming()) {open braces start code blocks and must be matched with a close brace
043 translate(dT * getDeltaX(), dT * getDeltaY());
044 }close braces end code blocks and must match an earlier open brace
045 }close braces end code blocks and must match an earlier open brace
046
047 /**
048 * Fast swimmers don't really bounce; instead, when striking an edge
049 * of the screen they disappear and go into waiting.
050 */
051 @Override
052 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value bounceOffEdges() {open braces start code blocks and must be matched with a close brace
053 ifif executes the next statement only if the condition in parenthesis evaluates to true (runIntoLeftWall() ||this is boolean or, meaning if either or both are true then the result is true runIntoRightWall()) {open braces start code blocks and must be matched with a close brace
054 startWaiting();
055 }close braces end code blocks and must match an earlier open brace
056 }close braces end code blocks and must match an earlier open brace
057
058 /**
059 * Is the swimmer swimming?
060 *
061 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true swimmer is swimming; falsefalse is a value for the boolean type and means not true ifif executes the next statement only if the condition in parenthesis evaluates to true swimmer is waiting
062 */
063 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false isSwimming() {open braces start code blocks and must be matched with a close brace
064 returnreturn means to provide the result of the method and/or cease execution of the method immediately !this is the not operator, which changes true to false and false to truewaiting;
065 }close braces end code blocks and must match an earlier open brace
066
067 /**
068 * Is the swimmer waiting to launch?
069 *
070 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true the swimmer is waiting to launch; falsefalse is a value for the boolean type and means not true ifif executes the next statement only if the condition in parenthesis evaluates to true swimmer
071 * is swimming
072 */
073 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false isWaiting() {open braces start code blocks and must be matched with a close brace
074 returnreturn means to provide the result of the method and/or cease execution of the method immediately waiting;
075 }close braces end code blocks and must match an earlier open brace
076
077 /**
078 * Set the speed (launch velocity) of thisthis means the current object (the implicit parameter) swimmer to the given x and
079 * y values.
080 *
081 * @paramthis is the Javadoc tag for documenting the purpose of parameters speedX horizontal velocity of the swimmers (screens/sec)
082 * @paramthis is the Javadoc tag for documenting the purpose of parameters speedY vertical velocity of the swimmers (screens/sec)
083 */
084 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setLaunchSpeed(doubledouble is the type for numbers that can contain decimal fractions speedX, doubledouble is the type for numbers that can contain decimal fractions speedY) {open braces start code blocks and must be matched with a close brace
085 thisthis means the current object (the implicit parameter).speedX =this assignment operator makes the left side equal to the right side speedX;
086 thisthis means the current object (the implicit parameter).speedY =this assignment operator makes the left side equal to the right side speedY;
087 }close braces end code blocks and must match an earlier open brace
088
089 /**
090 * Start swimmer swimming
091 */
092 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value startSwimming() {open braces start code blocks and must be matched with a close brace
093 show();
094 waiting =this assignment operator makes the left side equal to the right side falsefalse is a value for the boolean type and means not true;
095 setFrame(0);
096 }close braces end code blocks and must match an earlier open brace
097
098 /**
099 * Take swimmer off the screen.
100 */
101 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value startWaiting() {open braces start code blocks and must be matched with a close brace
102 hide();
103 waiting =this assignment operator makes the left side equal to the right side truetrue is the boolean value that is the opposite of false;
104 setLocation(0, 0);
105 setVelocity(0, 0);
106 startTimer();
107 }close braces end code blocks and must match an earlier open brace
108
109 /**
110 * Launch the swimmer. Go from waiting to swimming state. There is a
111 * 50-50 chance the swimmer begins on either side of the screen; thisthis means the current object (the implicit parameter)
112 * could be changed to launch from the side closest to (or farthest
113 * from) the rescuer.
114 */
115 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value launch() {open braces start code blocks and must be matched with a close brace
116 startSwimming();
117 ifif executes the next statement only if the condition in parenthesis evaluates to true (Game.getCurrentGame().randomDouble() < 0.5) {open braces start code blocks and must be matched with a close brace
118 launchLeftToRight();
119 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false {open braces start code blocks and must be matched with a close brace
120 launchRightToLeft();
121 }close braces end code blocks and must match an earlier open brace
122 }close braces end code blocks and must match an earlier open brace
123
124 /**
125 * Position the fast swimmer to move from left to right on the screen.
126 * Sets frame to 0, puts swimmer in upper-left corner, pointing to the
127 * right.
128 */
129 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value launchLeftToRight() {open braces start code blocks and must be matched with a close brace
130 setLocation(0.0, 0.1);
131 setRotation(90);
132 setVelocity(speedX, speedY);
133 }close braces end code blocks and must match an earlier open brace
134
135 /**
136 * Position the fast swimmer to move from right to left on the screen.
137 * Sets frame to 0, puts swimmer in upper-right corner, pointing to
138 * the left.
139 */
140 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value launchRightToLeft() {open braces start code blocks and must be matched with a close brace
141 setLocation(1.0, 0.1);
142 setRotation(-90);
143 setVelocity(-speedX, speedY);
144 }close braces end code blocks and must match an earlier open brace
145
146 /**
147 * Return the amount of time the timer should countdown; when moving
148 * it should be just like swimmer. When waiting, when should it launch
149 * again?
150 *
151 * @returnnull seconds until timer should expire
152 */
153 @Override
154 protectedprotected is used to restrict access to the current class and subclasses only doubledouble is the type for numbers that can contain decimal fractions getTimerCountdownTime() {open braces start code blocks and must be matched with a close brace
155 ifif executes the next statement only if the condition in parenthesis evaluates to true (isSwimming()) {open braces start code blocks and must be matched with a close brace
156 returnreturn means to provide the result of the method and/or cease execution of the method immediately super.getTimerCountdownTime();
157 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false {open braces start code blocks and must be matched with a close brace
158 returnreturn means to provide the result of the method and/or cease execution of the method immediately Game.getCurrentGame().randomDouble(MAX_TIME_OFF_SCREEN);
159 }close braces end code blocks and must match an earlier open brace
160 }close braces end code blocks and must match an earlier open brace
161
162 /**
163 * If swimming, just animate (like any other swimmer); ifif executes the next statement only if the condition in parenthesis evaluates to true waiting, it
164 * is time to launch.
165 */
166 @Override
167 protectedprotected is used to restrict access to the current class and subclasses only voidvoid means the method does not return a value timerExpired() {open braces start code blocks and must be matched with a close brace
168 ifif executes the next statement only if the condition in parenthesis evaluates to true (isSwimming()) {open braces start code blocks and must be matched with a close brace
169 super.timerExpired();
170 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false {open braces start code blocks and must be matched with a close brace
171 launch();
172 }close braces end code blocks and must match an earlier open brace
173 }close braces end code blocks and must match an earlier open brace
174 }close braces end code blocks and must match an earlier open brace
175
176 //Uploaded on Mon Mar 29 21:40:07 EDT 2010
|