⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bullet.java

📁 J2ME Game Programming的隋书源代码
💻 JAVA
字号:
/**
 * A actor for weapons fire.
 */

import net.jscience.math.kvm.MathFP;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class Bullet extends Actor
{
   // characteristics
   private static final int LIFETIME = 3000;

   // state
   private static final int STATE_FLYING = 0;
   private static final int STATE_EXPLODING = 1;

   private static ImageSet bulletImageSet;

   private Sprite bulletSprite;
   private int wallsHit;
   private int damage;

   public Bullet(World w)
   {
      super(w);

      if (bulletImageSet == null)
         System.out.println("oops, bullet used but imageSet not initialized.");

      bulletSprite = new Sprite(bulletImageSet, STATE_FLYING, 0);
   }

   public final void init(int newType, Actor newOwner, int x, int y, int dir)
   {
      int speed = MathFP.toFP(8);

      setType(newType);
      switch (getType())
      {
         case PLASMA_CANNON:
            damage = 100;
            break;
      }

      reset();
      super.init(newOwner, x, y, 0, speed, speed, dir, 16, Actor.FP_MINUS_1, 0);
   }

   public final static void setup(Image sourceImage)
   {
      bulletImageSet = new ImageSet(2);

      Image[] bulletFlying = ImageSet.extractFrames(sourceImage, 0, 0, 1, 1, 3, 3);
      Image[] bulletExploding = ImageSet.extractFrames(sourceImage, 0, 3, 4, 1, 3, 3);

      // warning: the order of these statements is important (see STATE_XXX above)
      bulletImageSet.addState(bulletFlying, 200);
      bulletImageSet.addState(bulletExploding, 500);
   }

   public final void reset()
   {
      super.reset();
      wallsHit = 0;
      bulletSprite.setState(STATE_FLYING, true);
   }

   public final int getWidth()
   {
      return 1;
   }

   public final int getHeight()
   {
      return 1;
   }

   public final void cycle(long deltaMS)
   {
      super.cycle(deltaMS);
      bulletSprite.cycle(deltaMS);

      if (bulletSprite.getCurrentState() != STATE_EXPLODING)
      {
         if (bulletSprite.getTimeInCurrentState() > LIFETIME)
         {
            bulletSprite.setState(STATE_EXPLODING, false);
            setVel(0);
         }
      }

      if (bulletSprite.getCurrentState() == STATE_EXPLODING)
      {
         if (bulletSprite.getTotalCycles() > 0)
            suicide();
      }
   }

   public final void render(Graphics g, int offsetX, int offsetY)
   {
      bulletSprite.draw(g, getX() - offsetX, getY() - offsetY);
   }

   public final void suicide()
   {
      world.releaseBullet(this);
   }

   public final void onCollision(Actor another)
   {
      if (another == null)
      {
         wallsHit++;
         if (wallsHit > 1)
         {
            bulletSprite.setState(STATE_EXPLODING, false);
            setVel(0);
         }
      }
      else if (getOwner() != another)
      {
         bulletSprite.setState(STATE_EXPLODING, false);
         setVel(0);
      }

   }

   public final int getDamage()
   {
      return damage;
   }
}







⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -