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

📄 shotmanager.java

📁 手机J2ME开发的CS游戏的Demo
💻 JAVA
字号:


import javax.microedition.m3g.*;


public class ShotManager
{
  // max value for the fired shot counter
  private static final int FIRED_SHOT_MAX = 2;

  private World scene;
  private MobileGunCamera mobCam;

  private AnimBillboard exploBoard;   // the animated explosions billboard
  private ShotSounds shotSounds;   
        // manages the fired shot and explosions sounds

  // Counter to control how long the fired shot image is displayed
  private int firedShotCounter = 0; 

  private int hitTerreristID = -1;   // ID of last penguin hit 


  public ShotManager(World s, MobileGunCamera mc)
  {
    scene = s;
    mobCam = mc;

    // set up animated explosions billboard
    System.out.println("Adding explosion billboard");
    Image2D[] ims = loadImages("/explo/explo", 6);
    Group camGroup = mobCam.getCameraGroup();
    exploBoard = new AnimBillboard(ims, camGroup, 1.5f);
    scene.addChild( exploBoard.getAnimBillboardMesh() );

    // initialise sound resources for shooting
    shotSounds = new ShotSounds();
  }  // end of ShotManager()


  public void update()
  /* Update the explosion display, and decrement the fired shot
     counter. When the fired shot counter reaches 0,
     make the image invisible. */
  { 
    exploBoard.update(); 

    if (firedShotCounter == 0)     // decide whether to hide the fired shot image
      mobCam.setShotVisible(false);   // hide it
    else
      firedShotCounter--;   // keep counting down
  } // end of update()


  // ---------------image loading -----------------------------


  private Image2D[] loadImages(String path, int num)
  // load files called path + 0-(num-1) + ".gif"
  {
    Image2D[] ims = new Image2D[num];
    for (int i=0; i<num; i++)
      ims[i] = loadImage(path + i + ".gif");
    return ims;
  }  // end of loadImages()


  private Image2D loadImage(String fn)
  // load the image stored in fn
  { Image2D im = null;
    try {
       im = (Image2D)Loader.load(fn)[0];
    }
    catch (Exception e)
    { System.out.println("Cannot make image from " + fn); }
    return im;
  }  // end of loadImage()



  // ------------------- shoot the gun -----------------

  public int shootGun()
  /* Fire the gun, and show an explosion if the ray hits something.
     Play a fired shot sound and an explosion sound.
     Remove the penguin if it's been hit.
     Return the ID of the hit penguin (or -1).
  */
  {
    // show fired shot image and play a sound
    firedShotCounter = FIRED_SHOT_MAX;
    mobCam.setShotVisible(true);
    shotSounds.playShotSound();

    // fire a picking ray out in the camera's forward direction
    RayIntersection ri = new RayIntersection();
    float[] camPos = mobCam.getPosition();   // camera's current position
    float[] camDir = mobCam.getDirection();  // camera's current fwd direction

    if (scene.pick(-1, camPos[0], camPos[1], camPos[2],
                       camDir[0], camDir[1], camDir[2], ri)) {   // hit something?
      float distance = ri.getDistance();  // normalized distance to the hit thing

      checkHit(ri);

      // calculate the coords of the hit thing
      float x = camPos[0] + (camDir[0] * distance);
      float y = camPos[1] + (camDir[1] * distance);
      float z = camPos[2] + (camDir[2] * distance);

      // show explosion at (x,y,z) and play a sound
      exploBoard.setPosition(x,y,z);
      exploBoard.setVisible(true); 
      shotSounds.playExplosionSound();
    }
    return hitTerreristID;
  }  // end of shootGun()


  private void checkHit(RayIntersection ri)
  // find out what's been hit
  {
     hitTerreristID = -1;   // reset to default value
     Node selected = ri.getIntersected();
     if (selected instanceof Mesh) {
       // System.out.println("Hit a mesh");
       Mesh m = (Mesh) selected;
       TerreristInfo tInfo = (TerreristInfo) m.getUserObject();
       if (tInfo != null) {
         hitTerreristID = tInfo.getID();  // save ID
         hitTerrerist(tInfo, m);
       }
     }
  } // end of checkHit()


  private void hitTerrerist(TerreristInfo tInfo, Mesh m)
  // make the shot penguin disappear if he's dead
  {
  	tInfo.loseALife();
    int numLives = tInfo.getNumLives();  // since just shot
    if (numLives == 0) {    // last life just gone
      System.out.println("Killed " + tInfo.getName() + 
                       "; ID: " + hitTerreristID);
   //   m.setRenderingEnable(false);
      m.setPickingEnable(false);
    }
    else {
      System.out.println("Hit " + tInfo.getName() + 
                       "; ID: " + hitTerreristID +
                       "; lives left: " + numLives);
      
    }
  }  // end of hitTerrerist()



  public boolean hitMade()
  // a hit has occurred if the explosion is visible
  {  return exploBoard.isVisible();  }


  public int getHitID()
  // get the ID of the last penguin hit, or -1
  {  return hitTerreristID; }


  // ------------------ sound ops ---------------------


  public void closeSounds()
  { shotSounds.closeSounds();  }

}  // end of ShotManager class

⌨️ 快捷键说明

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