// (c) 1998 Ekkehard Kraemer (ekraemer@pluto.camelot.de) import java.awt.*; /** * Sorry, not documented yet. *
* Source
*/
class Ball extends Rectangle
{
Ball(BojngInterface bojng,int x,int y)
{
this.bojng=bojng;
sprad=Images.ball.getWidth(null);
if (Images.ball.getHeight(null)>sprad) sprad=Images.ball.getHeight(null);
sprad=sprad/2+1;
width=sprad*2;
height=sprad*2;
moveTo(x,y,PI.drittel);
}
Ball(Ball ball,double angleDisp)
{
width=ball.width;
height=ball.height;
x=ball.x;
y=ball.y;
sprad=ball.sprad;
speed=ball.speed;
angle=ball.angle+angleDisp;
while (angle<0) angle+=PI.zwei;
while (angle>=PI.zwei) angle-=PI.zwei;
xstart=ball.xstart;
ystart=ball.ystart;
xspeed=ball.xspeed;
yspeed=ball.yspeed;
curTime=ball.curTime;
xcur=ball.xcur;
ycur=ball.ycur;
bounceBottom=ball.bounceBottom;
bojng=ball.bojng;
}
void moveTo(int x,int y)
{
moveTo(x,y,angle);
}
void moveTo(int x,int y,double a)
{
angle=a;
while (a<0) a+=PI.zwei;
while (a>=PI.zwei) a-=PI.zwei;
correctAngle();
xstart=x;
ystart=y-sprad/2;
curTime=0;
calcSpeed();
calcCurXY();
}
boolean tick()
{
if (speedUpCntr<=0)
speedUpCntr=speedUpTime/bojng.ms();
else
{
speedUpCntr--;
if (speedUpCntr<=1)
{
speed+=0.2;
System.err.println("speedup");
}
}
remove();
boolean rc=move();
draw();
return rc;
}
// xspeed, yspeed aus speed und angle berechnen
void calcSpeed()
{
xstart+=xspeed*curTime;
ystart+=yspeed*curTime;
xspeed=Math.cos(angle)*speed;
yspeed=-Math.sin(angle)*speed;
}
// Draws the ball on backimg
void draw()
{
bojng.drawImage(Images.ball,x,y);
//bojng._update().add(this);
}
// Removes the ball from backimg
void remove()
{
bojng.drawBackground(x,y,width,height);
}
// Moves the ball coordinates, no drawing
boolean move()
{
curTime++;
if (bounceWalls()) return true;
bounceBlocks();
return false;
}
void calcCurXY()
{
xcur=xstart+xspeed*curTime;
ycur=ystart+yspeed*curTime;
x=(int)(xcur-sprad);
y=(int)(ycur-sprad);
}
boolean bounceWalls()
{
boolean bounceX=false,bounceY=false;
double offset=0;
calcCurXY();
if (xcur-sprad<0)
{
bounceX=true;
while (xcur-sprad<0)
{
curTime-=0.1;
offset+=0.1;
calcCurXY();
}
}
else if (xcur+sprad>=bojng._imgwidth())
{
bounceX=true;
while (xcur+sprad>=bojng._imgwidth())
{
curTime-=0.1;
offset+=0.1;
calcCurXY();
}
}
if (ycur-sprad<0)
{
bounceY=true;
while (ycur-sprad<0)
{
curTime-=0.1;
offset+=0.1;
calcCurXY();
}
}
else if (ycur+sprad>=bojng._imgheight())
{
if (bounceBottom)
{
bounceY=true;
while (ycur+sprad>=bojng._imgheight())
{
curTime-=0.1;
offset+=0.1;
calcCurXY();
}
}
else
{
if (ycur-sprad>=bojng._imgheight()) return true; // ball out
}
}
if (bounceX || bounceY)
{
bounce(bounceX,bounceY);
curTime=offset;
}
return false;
}
void bounce(boolean bounceX,boolean bounceY)
{
if (bounceX && bounceY)
{
angle=PI.PI+angle;
while (angle>=PI.zwei) angle-=PI.zwei;
}
else
{
if (bounceX)
{
if (0<=angle && angle