home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Java / Gamelicator / examples / Boinkaroids / Ship.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-04-20  |  3.5 KB  |  146 lines

  1. import com.next.gt.Actor;
  2. import com.next.gt.Gamelication;
  3. import java.awt.Image;
  4. import java.awt.event.KeyEvent;
  5. import java.awt.event.KeyListener;
  6.  
  7. public class Ship extends Actor implements KeyListener {
  8.    private static int MAX_NUM_BULLETS = 5;
  9.    public int numBullets;
  10.    public int animationDirection = 1;
  11.    public boolean isAnimating = true;
  12.    public boolean thrusting = false;
  13.  
  14.    Ship(Gamelication var1) {
  15.       super.owner = var1;
  16.       super.owner.play(super.owner.getCodeBase(), "sounds/warp.au");
  17.       super.x = (double)super.owner.getSize().width / (double)2.0F;
  18.       super.y = (double)super.owner.getSize().height / (double)2.0F;
  19.       super.velocity_x = (double)0.0F;
  20.       super.velocity_y = (double)0.0F;
  21.       Image var2 = super.owner.getImage(super.owner.getCodeBase(), "images/ship.gif");
  22.       ((Actor)this).setImage(var2, 4, 24);
  23.       this.isAnimating = false;
  24.    }
  25.  
  26.    public void keyPressed(KeyEvent var1) {
  27.       if (var1.getKeyCode() == 39) {
  28.          this.rotateRight(true);
  29.       }
  30.  
  31.       if (var1.getKeyCode() == 37) {
  32.          this.rotateLeft(true);
  33.       }
  34.  
  35.       if (var1.getKeyCode() == 38) {
  36.          this.thrust(true);
  37.       }
  38.  
  39.       if (var1.getKeyCode() == 32) {
  40.          this.fire();
  41.       }
  42.  
  43.    }
  44.  
  45.    public void keyReleased(KeyEvent var1) {
  46.       if (var1.getKeyCode() == 39) {
  47.          this.rotateRight(false);
  48.       }
  49.  
  50.       if (var1.getKeyCode() == 37) {
  51.          this.rotateLeft(false);
  52.       }
  53.  
  54.       if (var1.getKeyCode() == 38) {
  55.          this.thrust(false);
  56.       }
  57.  
  58.    }
  59.  
  60.    public void keyTyped(KeyEvent var1) {
  61.    }
  62.  
  63.    public void calculateNewVelocity() {
  64.       if (this.thrusting) {
  65.          super.velocity_x += Math.cos((double)(super.currentFrame * 2) * Math.PI / (double)super.numFrames + (Math.PI / 2D)) * (double)10.0F;
  66.          super.velocity_y += Math.sin((double)(super.currentFrame * 2) * Math.PI / (double)super.numFrames - (Math.PI / 2D)) * (double)10.0F;
  67.       } else {
  68.          super.velocity_x *= 0.99;
  69.          super.velocity_y *= 0.99;
  70.       }
  71.    }
  72.  
  73.    public void calculateCurrentFrame() {
  74.       if (this.isAnimating) {
  75.          if (this.animationDirection == -1) {
  76.             if (--super.currentFrame <= 0) {
  77.                super.currentFrame = super.numFrames - 1;
  78.                return;
  79.             }
  80.          } else if (++super.currentFrame >= super.numFrames) {
  81.             super.currentFrame = 0;
  82.          }
  83.       }
  84.  
  85.    }
  86.  
  87.    public void rotateLeft(boolean var1) {
  88.       if (var1) {
  89.          this.isAnimating = true;
  90.          this.animationDirection = 1;
  91.       } else {
  92.          this.isAnimating = false;
  93.       }
  94.    }
  95.  
  96.    public void rotateRight(boolean var1) {
  97.       if (var1) {
  98.          this.animationDirection = -1;
  99.          this.isAnimating = true;
  100.       } else {
  101.          this.isAnimating = false;
  102.       }
  103.    }
  104.  
  105.    public void thrust(boolean var1) {
  106.       if (var1) {
  107.          this.thrusting = true;
  108.       } else {
  109.          this.thrusting = false;
  110.       }
  111.    }
  112.  
  113.    public void fire() {
  114.       if (this.numBullets < MAX_NUM_BULLETS && !((Boinkaroids)super.owner).createNewPlayer) {
  115.          Bullet var1 = new Bullet(super.owner, this);
  116.          ++this.numBullets;
  117.          super.owner.play(super.owner.getCodeBase(), "sounds/bullet.au");
  118.          super.owner.actorManager.addActor(var1);
  119.       }
  120.  
  121.    }
  122.  
  123.    public double getTheta() {
  124.       return (double)(super.currentFrame * 2) * Math.PI / (double)super.numFrames + (Math.PI / 2D);
  125.    }
  126.  
  127.    public double getSpeed() {
  128.       return Math.sqrt(super.velocity_x * super.velocity_x + super.velocity_y * super.velocity_y);
  129.    }
  130.  
  131.    protected void collideWithActor(Actor var1) {
  132.       String var2 = var1.getClass().getName();
  133.       if (var2.equals("Asteroid") || var2.equals("Goobie") || var2.equals("Bigoobie")) {
  134.          this.explode();
  135.       }
  136.  
  137.    }
  138.  
  139.    public void explode() {
  140.       super.owner.actorManager.removeActor(this);
  141.       Explosion var1 = new Explosion(super.owner, this);
  142.       super.owner.actorManager.addActor(var1);
  143.       ((Boinkaroids)super.owner).decrementShipCount();
  144.    }
  145. }
  146.