CHAPTER 12 ■ VIDEO GAMES
// Draw the actual position from which a shot comes
g.setColor(Color.BLACK);
g.fillRect(xPosition - 5, yPosition, 5, 2);
}
// Provide a way to set the Y location
public void setY(int newY) {
yPosition = newY;
}
// Convenience method for indicating that the player can shoot
private void setReadyToShoot(boolean ready) {
readyToShoot = ready;
if (ready == false) {
currentStep = 0;
}
}
// Work through the rows from right to left to see if a shot hit a target
private void analyzeShot(int shotY) {
if (!analyzeShotForRow(ShootingGalleryPanel.row3, shotY)) {
if(!analyzeShotForRow(ShootingGalleryPanel.row2, shotY)) {
analyzeShotForRow(ShootingGalleryPanel.row1, shotY);
}
}
}
// Work through the targets in a row to see if we hit one
private boolean analyzeShotForRow(ShootingGalleryTargetRow row, int shotY) {
boolean hit = false;
int count = row.getTargets().size();
while(!hit && count > 0) {
ShootingGalleryTarget currentTarget = row.getTargets().get(count - 1);
// Here's where we check the target's polygon for a hit
if (currentTarget.polygon.contains(ShootingGalleryPanel.TARGET_SPACE / 2, shotY)) {
// If we get a hit, stop checking
hit = true;
// Update the score
ShootingGallery.score += currentTarget.getValue();
ShootingGallery.scoreDisplayLabel.setText(new
Integer(ShootingGallery.score).toString());
Remove the target that got hit
row.getTargets().remove(currentTarget);
} else {
// We're working backwards because doing so makes
// it easy to detect when we've run out of targets to check
count--;
}