import uwcse.graphics.*; import java.awt.Color; import java.awt.Image; /** * This is the Java class used for both Balls and Obstacles * in the BallGame. The two are really the same - they have * an image to be displayed, they have a position and a size, * and a velocity. For Players representing obstacles in a * BallGame, the velocity is just set to zero (by BallGame, * when it creates the Player to represent an obstacle). *

* @author John Zahorjan * @version 4/15/2002 */ public class Player { private ImageShape myImage = null; private int xVelocity = 0; private int yVelocity = 0; /** * Create the image of the player and set initial x and y velocities. * (Note that the Player is not displayed until addToWindow() is called.) *

* @param displayedImage A Java Image, obtained by the caller, which * corresponds to the contents of some .jpg file. */ Player ( Image displayedImage ) { myImage = new ImageShape( displayedImage, 0, 0 ); } /** * This is to Players what "moveTo()" is to Rectangles. *

* @param x The x coordinate of the new location. * @param y The y coordinate of the new location. */ public void setPosition( int x, int y ) { myImage.moveTo( x, y ); } /** * Assigns the x and y distances to move each time step. *

* @param xSpeed The distance to move in the x direction. * @param ySpeed The distance to move in the y direction. */ public void setVelocity( int xSpeed, int ySpeed ) { xVelocity = xSpeed; yVelocity = ySpeed; } /** * This is to Players what "addTo()" is to Rectangles. *

* @param gw The GWindow in which the Player should be shown. */ public void addToWindow( GWindow gw ) { if ( gw != null ) { myImage.addTo( gw ); } } /** * Update the Player's position to account for one simulated time step. * Note: This does not check for collisions. See the bounceOff... methods. */ public void moveOneStep() { myImage.moveBy( xVelocity, yVelocity ); } /** * Returns true if this Player intersects the one passed in as the argument. * Returns false otherwise. *

* @param otherPlayer The Player we're testing to see if we overlap. *

* @return true if this Player, in its current position, overlaps the player given as an argument. false otherwise. */ public boolean intersects( Player otherPlayer ) { if ( otherPlayer == null ) { return false; } return this.getBoundingBox().intersects( otherPlayer.getBoundingBox() ); } /** * After this Player's position has been updated by moveOneStep(), this routine * should be called for each horizontal line that this Player might potentially * bounce off of. *

* @param x1 The leftmost x coordinate of the horizontal line. * @param x2 The rightmost x coordinate of the horizontal line. * @param y The (fixed) y coordinate of the horizontal line. */ public void bounceOffHorizontalLine( int x1, int x2, int y ) { if ( myImage.getCenterX() >= x1 && myImage.getCenterX() <= x2 ) { if ( myImage.getCenterY() < y ) { if ( myImage.getY() + myImage.getHeight() >= y ) { yVelocity = -yVelocity; myImage.moveTo( myImage.getX(), y - myImage.getHeight() ); } } else { if ( myImage.getY() <= y ) { yVelocity = -yVelocity; myImage.moveTo( myImage.getX(), y ); } } } } /** * After this Player's position has been updated by moveOneStep(), this routine * should be called for each vertical line that this Player might potentially * bounce off of. *

* @param y1 The uppermost (smallest) y coordinate of the vertical line. * @param y2 The bottommost (largest) y coordinate of the vertical line. * @param x The (fixed) x coordinate of the vertical line. */ public void bounceOffVerticalLine( int y1, int y2, int x ) { if ( myImage.getCenterY() >= y1 && myImage.getCenterY() <= y2 ) { if ( myImage.getCenterX() < x ) { if ( myImage.getX() + myImage.getWidth() >= x ) { xVelocity = -xVelocity; myImage.moveTo( x - myImage.getWidth(), myImage.getY() ); } } else { if ( myImage.getX() <= x ) { xVelocity = -xVelocity; myImage.moveTo( x , myImage.getY() ); } } } } /** * A method provided for the caller's convenience - it converts the problem of * bouncing off a rectangle into four problems of bouncing off of horizontal or vertical * lines. *

* @param rect The rectangle this Player might bounce off of. */ public void bounceOffRectangle( Rectangle rect ) { this.bounceOffHorizontalLine( rect.getX(), rect.getX()+rect.getWidth(), rect.getY() ); this.bounceOffHorizontalLine( rect.getX(), rect.getX()+rect.getWidth(), rect.getY()+rect.getHeight() ); this.bounceOffVerticalLine( rect.getY(), rect.getY()+rect.getHeight(), rect.getX() ); this.bounceOffVerticalLine( rect.getY(), rect.getY()+rect.getHeight(), rect.getX()+rect.getWidth() ); } /** * Utility routine - returns the width of the image shown for this Player. */ public int getWidth() { return myImage.getWidth(); } /** * Utility routine - returns the height of the image shown for this Player. */ public int getHeight() { return myImage.getHeight(); } /** * Utility routine - returns the bounding rectangle for the image shown for this Player. * (The bounding rectangle is the smallest rectangle that completely encloses the Player's image.) */ public Rectangle getBoundingBox() { return myImage.getBoundingBox(); } /** * Utility routine, primarily for debugging. */ public String toString() { String result = "At (" + myImage.getX() + ", " + myImage.getY() + ")"; result = result + "- Width = " + myImage.getWidth() + " Height = " + myImage.getHeight(); result = result + "- Velocity = (" + xVelocity + ", " + yVelocity + ")"; return result; } }