import java.awt.*;
public class BouncingRect extends RectSprite implements Moveable {

  // the coords at which the rectangle bounces
  protected int max_width;
  protected int max_height;

  // sprite velocity.  used to implement Moveable interface
  protected int vx;
  protected int vy;

  public BouncingRect(int x,int y,int w,int h,Color c,int max_w,int max_h) {
    super(x,y,w,h,c);
    max_width = max_w;
    max_height = max_h;
  }

  // implements Moveable interface
  public void setPosition(int x, int y) {
    locx = x;
    locy = y;
  }

  // implements Moveable interface
  public void setVelocity(int x,int y) {
    vx = x;
    vy = y;
  }

  // implements Moveable interface
  // update position according to velocity
  public void updatePosition() {
    locx += vx;
    locy += vy;
  }

  // move and bounce rectangle if it hits borders
  public void update() {

    // flip x velocity if it hits left or right bound
    if ((locx + width > max_width) || locx < 0) {
      vx = -vx;
    }

    // flip y velocity if it hits top or bottom bound
    if ((locy + height > max_height) || locy < 0) {
      vy = -vy;
    }
    updatePosition();
  }

}
