|
TargetGame
AClipGun.java
//-----------------------------------------------------------------------------
// Class: AClipGun
// Author: Written by Chuck Cusack
// Date: September 2001
// Description: A class to simulate a gun which takes a clip. No graphics
// are implemented. The class should be extended to implement the graphics.
//-----------------------------------------------------------------------------
import java.awt.*;
import aLibrary.*;
import java.awt.event.*;
//-----------------------------------------------------------------------------
public abstract class AClipGun extends AView{
//-----------------------------------------------------------------------------
// instance variables
protected AClip myClip; // The clip
private TargetGame myTargetGame; // the controlling director
//-----------------------------------------------------------------------------
// The constructor
// Postcondition: myClip==null, myTargetGame==D, an empty picture is created.
// Graphics aren't drawn unless the class is extended
//
public AClipGun(int x,int y,int width,int height,TargetGame D) {
super(x,y,width,height);
myTargetGame=D;
myClip=null;
drawGun();
}
//-----------------------------------------------------------------------------
// Implement the following in an extension if you desire to display the
// gun graphically
//
protected void drawGun() {}
protected void drawClipInGun(AClip theClip) {}
protected void drawClipNotInGun() {}
//-----------------------------------------------------------------------------
// Postcondition: myClip==theClip, and the clip is drawn in the gun if the
// class is extended and the function is implemented
//
public void insertClip(AClip theClip) {
if(myClip==null) {
myClip=theClip;
myClip.setMyGun(this);
drawClipInGun(theClip);
}
}
//-----------------------------------------------------------------------------
// returns myClip
// PostCondition: myClip==null, and the clip is not shown in the gun if the
// class is extended and the function is implmented.
//
public AClip removeClip() {
if(myClip!=null) {
drawClipNotInGun();
myClip.setMyGun(null);
AClip temp=myClip;
myClip=null;
return temp;
}
else {
return null;
}
}
//-----------------------------------------------------------------------------
// If the clip is in the gun, and the clip has bullets, fire the most recently
// placed bullet. Graphically shown if the appropriate function is defined
// in an extension of ABullet.
//
public void fire() {
if(myClip==null) {
System.out.println("No Clip");
}
else if(myClip.isEmpty()) {
System.out.println("No Bullets");
}
else {
System.out.println("Firing");
myTargetGame.bulletFired();
ABullet aBullet=myClip.removeBullet();
aBullet.setLocation(this.getX()+150,this.getY()+7);
aBullet.place(this.getParent());
aBullet.drawBulletFiring(myTargetGame);
System.out.println("Done Firing");
}
}
//-----------------------------------------------------------------------------
// return true if the gun has a clip in it, and false otherwise.
//
public boolean hasClip() {
return (myClip!=null);
}
}
//-----------------------------------------------------------------------------
|