|
TargetGame
ATarget.java
//-----------------------------------------------------------------------------
// Class: ATarget
// Author: Written by Chuck Cusack
// Date: September 2001
// Description: A moving target for bullets to hit.
//-----------------------------------------------------------------------------
import aLibrary.ARectangle;
import java.awt.Color;
//-----------------------------------------------------------------------------
public class ATarget extends ARectangle {
//-----------------------------------------------------------------------------
// instance variables
//
private TargetGame myTargetGame; // The controlling TargetGame
private boolean movingUp; // TargetGameection the target it moving
private TargetTimer theTimer; // The timer to control movement
private boolean hit; // Whether or not it has been hit
private int speed; // The speed of the target
private int value; // The value if Hit
private int type; // The number corresponding to choice below
private final Color choices[]={ // Color, Speed
Color.magenta,
Color.cyan,
Color.blue,
Color.green,
Color.orange,
Color.red,
Color.pink
};
//-----------------------------------------------------------------------------
// constructor
// postconditon: A width by height target is created at (x,y) and movement
// is started (even though the target is not on anything)
//
public ATarget(int x,int y,int width,int height,int level,TargetGame D) {
super(x,y,width,height);
myTargetGame=D;
type=(int)(Math.random()*(choices.length));
//System.out.println(type);
speed=(type+1)*(int)(1+Math.random()*level);
value=(int)(speed*(240-height)/10);
setColor(choices[type]);
setToFill();
movingUp=false;
hit=false;
theTimer=new TargetTimer(this,D);
theTimer.scheduleEvents(.1);
}
//-----------------------------------------------------------------------------
// Return value
//
public int getValue() {
return value;
}
//-----------------------------------------------------------------------------
// Checks to see if bullet B has hit the target
// postcondition: If the bullet has hit the target, erase the target
// otherwise, do nothing.
// returns: true if it hit, false otherwise.
//
public boolean isHit(ABullet B) {
if(hit==false) {
for(int i=0;i<10;i++) { // Bullet jumps by 20 units, so check all for hit.
if(this.contains(B.getX()+B.getWidth()+i-this.getX(),
B.getY()+(B.getHeight()/2)-this.getY()) ) {
theTimer.stop();
hit=true;
this.remove();
return true;
}
}
}
return false;
}
//-----------------------------------------------------------------------------
// Move the target by speed units up or down, depending on current direction.
// postcondition: target is speed units farther up or down, or
// unmoved and direction changed if it is too close to edge.
//
public void MoveTarget() {
if(movingUp) {
if (getY()<speed) {
movingUp=false;
translate(-10,-getY());
if(getX()<225) {
theTimer.stop();
remove();
}
}
else {
translate(0,-speed);
}
}
else if(getY()+getHeight()>myTargetGame.getHeight()-50-speed) {
movingUp=true;
translate(-10,0);
if(getX()<225) {
remove();
theTimer.stop();
}
}
else {
translate(0,speed);
}
}
//-----------------------------------------------------------------------------
// return speed
//
public int getSpeed() {
return speed;
}
//-----------------------------------------------------------------------------
// Stop the timer
//
public void start() {
theTimer.start();
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Stop the timer
//
public void stop() {
theTimer.stop();
}
//-----------------------------------------------------------------------------
}
|