import java.awt.*; import java.awt.event.*; import com.genuts.gameui.*; /** * The behavior of a ball. */ public class MovingBall extends MovingSpriteWrapper { /** * The ball goes right */ private final static int DIRRIGHT = 0; /** * The ball goes left */ private final static int DIRLEFT = 1; /** * The ball goes right */ private final static int DIRDOWN = 2; /* * The ball goes up */ private final static int DIRUP = 3; /** * X speed */ private int vx; /** * Y speed */ private int vy; /** * Direction of the ball */ private int dir = 0; /** * Is the ball goes in linearly ? */ private boolean linearDir = true; /** * Does a collision happened with a border ? */ private boolean borderCollision = false; /** * Direction of the movement. * true - Clockwise * false - Counter Clockwise */ private boolean clockWise = true; public MovingBall(Sprite sprite, int vx, int vy) { super(sprite); this.vx = vx; this.vy = vy; setPremode(true); } /** * Get the value of clockWise. * @return value of clockWise. */ public boolean isClockWise() { return clockWise; } /** * Set the value of clockWise. * @param v Value to assign to clockWise. */ public void setClockWise(boolean v) { this.clockWise = v; } /** * Moves the ball. */ public void move(int ticks) { if (clockWise) { switch (dir) { case DIRRIGHT: if (!linearDir && (setPosition(getX(), getY()+vy))) { dir = DIRDOWN; } else if (!setPosition(getX()+vx, getY())) { if (borderCollision) { linearDir = false; } if (!setPosition(getX(), getY()-vy)) { if (setPosition(getX()-vx, getY())) { dir = DIRLEFT; } } else { dir = DIRUP; } } break; case DIRDOWN: if (!linearDir && (setPosition(getX()-vx, getY()))) { dir = DIRLEFT; } else if (!setPosition(getX(), getY()+vy)) { if (borderCollision) { linearDir = false; } if (!setPosition(getX()+vx, getY())) { if (setPosition(getX(), getY()-vy)) { dir = DIRUP; } } else { dir = DIRRIGHT; } } break; case DIRLEFT: if (!linearDir && (setPosition(getX(), getY()-vy))) { dir = DIRUP; } else if (!setPosition(getX()-vx, getY())) { if (borderCollision) { linearDir = false; } if (!setPosition(getX(), getY()+vy)) { if (setPosition(getX()+vx, getY())) { dir = DIRRIGHT; } } else { dir = DIRDOWN; } } break; case DIRUP: if (!linearDir && (setPosition(getX()+vx, getY()))) { dir = DIRRIGHT; } else if (!setPosition(getX(), getY()-vy)) { if (borderCollision) { linearDir = false; } if (!setPosition(getX()-vx, getY())) { if (setPosition(getX(), getY()+vy)) { dir = DIRDOWN; } } else { dir = DIRLEFT; } } break; } } else { switch (dir) { case DIRRIGHT: if (!linearDir && (setPosition(getX(), getY()-vy))) { dir = DIRUP; } else if (!setPosition(getX()+vx, getY())) { if (borderCollision) { linearDir = false; } if (!setPosition(getX(), getY()+vy)) { if (setPosition(getX()-vx, getY())) { dir = DIRLEFT; } } else { dir = DIRDOWN; } } break; case DIRDOWN: if (!linearDir && (setPosition(getX()+vx, getY()))) { dir = DIRRIGHT; } else if (!setPosition(getX(), getY()+vy)) { if (borderCollision) { linearDir = false; } if (!setPosition(getX()-vx, getY())) { if (setPosition(getX(), getY()-vy)) { dir = DIRUP; } } else { dir = DIRLEFT; } } break; case DIRLEFT: if (!linearDir && (setPosition(getX(), getY()+vy))) { dir = DIRDOWN; } else if (!setPosition(getX()-vx, getY())) { if (borderCollision) { linearDir = false; } if (!setPosition(getX(), getY()-vy)) { if (setPosition(getX()+vx, getY())) { dir = DIRRIGHT; } } else { dir = DIRUP; } } break; case DIRUP: if (!linearDir && (setPosition(getX()-vx, getY()))) { dir = DIRLEFT; } else if (!setPosition(getX(), getY()-vy)) { if (borderCollision) { linearDir = false; } if (!setPosition(getX()+vx, getY())) { if (setPosition(getX(), getY()+vy)) { dir = DIRDOWN; } } else { dir = DIRRIGHT; } } break; } } } /** * A collision should happen for the current movement. */ protected boolean preCollisionWith(Sprite s) { if (s instanceof MovingBall) { linearDir = true; borderCollision = false; } else { borderCollision = true; } return false; } }