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

📄 mobileguncamera.java

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


import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;


public class MobileGunCamera
{
  // initial camera position
  private static final float X_POS = 0.0f; 
  private static final float Y_POS = 0.4f; 
  private static final float Z_POS = 2.0f; 

  // translation and rotation increments
  private static final float ANGLE_INCR = 3.0f;   // degrees
  private static final float MOVE_INCR = 0.1f;

  // booleans for remembering key presses
  private boolean upPressed = false;
  private boolean downPressed = false;
  private boolean leftPressed = false;
  private boolean rightPressed = false;

  // for examining the camera's (x,y,z) position
  private Transform trans = new Transform();
  private float[] transMat = new float[16];
  private float xCoord, yCoord, zCoord;


  // for storing the camera's current y- rotations
  private float yAngle;
  private Transform rotTrans = new Transform();

  // scene graph elements for the camera
  private Group transGroup;
  private Camera cam;
  private Mesh shotMesh;


  public MobileGunCamera(int width, int height)
  /* Build the camera scene graph branch:
       transGroup --> camera
                |--->  gun image mesh
                |--->  shot image mesh
  */
  {
    cam = new Camera();
    float aspectRatio = ((float) width) / ((float) height);
    cam.setPerspective(70.0f, aspectRatio, 0.1f, 50.0f);
    cam.postRotate(-2.0f, 1.0f, 0, 0);  // apply x-axis rotations to camera

    // gun image mesh
    Image2D imGun = loadImage("/gun.png");
    ImageSquare imSqGun = new ImageSquare(imGun, 0.025f, -0.055f, -0.1f, 0.05f);
    Mesh gunMesh = imSqGun.getImageSquareMesh();
    gunMesh.setPickingEnable(false);

    // shot image mesh
    Image2D imShot = loadImage("/shot.png");
    ImageSquare imSqShot = 
             new ImageSquare(imShot, 0, -0.05f, -0.15f, 0.1f);
    shotMesh = imSqShot.getImageSquareMesh();
    shotMesh.setPickingEnable(false);
    shotMesh.setRenderingEnable(false);  // invisible intially

    // put eveything together
    // transGroup handles both camera translation and y-axis rotations
    transGroup = new Group();      // no initial rotation
    trans.postTranslate(X_POS, Y_POS, Z_POS);
    transGroup.setTransform(trans);
    transGroup.addChild(cam);
    transGroup.addChild(gunMesh);
    transGroup.addChild(shotMesh);

    // store initial rotation
    yAngle = 0.0f;
  }  // end of MobileGunCamera()


  public Group getCameraGroup()
  {  return transGroup; }

  public Camera getCamera()
  {  return cam; }


  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()


  // ----- handle key presses / releases, and return details ------

  public void pressedKey(int gameAction)
  {
    switch(gameAction) {
      case Canvas.UP:    upPressed = true;    break;
      case Canvas.DOWN:  downPressed = true;  break;
      case Canvas.LEFT:  leftPressed = true;  break;
      case Canvas.RIGHT: rightPressed = true; break;
      default: break;
    }
  }  // end of pressedKey()


  public void releasedKey(int gameAction)
  {
    switch(gameAction) {
      case Canvas.UP:    upPressed = false;    break;
      case Canvas.DOWN:  downPressed = false;  break;
      case Canvas.LEFT:  leftPressed = false;  break;
      case Canvas.RIGHT: rightPressed = false; break;
      default: break;
    }
  }  // end of releasedKey()



  // ------------ position and direction access methods ------------

  public float[] getPosition()
  // return the current position of the camera
  { storePosition();
    return new float[] { xCoord, yCoord, zCoord };
  }  // end of getPosition()


  private void storePosition()
  // extract the current (x,y,z) position from transGroup
  {
    transGroup.getCompositeTransform(trans);

    trans.get(transMat);
    xCoord = transMat[3];
    yCoord = transMat[7];
    zCoord = transMat[11]; 
  }  // end of storePosition()


  public float[] getDirection()
  /* Rotate the vector (0,0,-1) by the current y-axis rotation.
     (0,0,-1) represents the initial forward direction for the
     camera. The result after the rotation will be the camera's
     current forward direction. 
  */
  { float[] zVec = {0, 0, -1.0f, 0};
    rotTrans.transform(zVec);
    return new float[] { zVec[0], zVec[1], zVec[2] };
  } // end of getDirection()


  // ------------- update camera position and rotation ---------------

  public void update()
  {
    if (upPressed || downPressed)
      updateMove();
    else if (leftPressed || rightPressed)
      updateRotation(); 
  }  // end of update()


  private void updateMove()
  {
    transGroup.getTransform(trans);
    if (upPressed)          // move forward
      trans.postTranslate(0, 0, -MOVE_INCR);
    else    // move backwards
      trans.postTranslate(0, 0, MOVE_INCR);
    transGroup.setTransform(trans);
  }  // end of updateMove()


  private void updateRotation()
  {
    if (leftPressed) {   // rotate left around the y-axis
      rotTrans.postRotate(ANGLE_INCR, 0, 1.0f, 0);
      yAngle += ANGLE_INCR;
    }
    else {   // rotate right around the y-axis
      rotTrans.postRotate(-ANGLE_INCR, 0, 1.0f, 0);
      yAngle -= ANGLE_INCR;
    }

    // angle values are modulo 360 degrees
    if (yAngle >= 360.0f)
      yAngle -= 360.0f;
    else if (yAngle <= -360.0f)
      yAngle += 360.0f;

    // apply the y-axis rotation to transGroup
    storePosition();
    trans.setIdentity();
    trans.postTranslate(xCoord, yCoord, zCoord);
    trans.postRotate(yAngle, 0, 1.0f, 0);
    transGroup.setTransform(trans);
  }  // end of updateRotation()


  // -------------- display shot ------------

  public void setShotVisible(boolean b)
  {  shotMesh.setRenderingEnable(b);  }

} // end of MobileGunCamera class

⌨️ 快捷键说明

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