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

📄 movingobject.java

📁 基于java的一款游戏的故事。基于java的一款游戏的故事。基于java的一款游戏的故事。
💻 JAVA
字号:
package com.thinkenjoy.feitian;

import com.thinkenjoy.tools.ImageSet;

/**
 *
 * 移动物体的基类,任何可移动的物体都继承此类 此类也可直接表示一些简单的可移动物体,如子弹
 */
public class MovingObject {
  //#if NOK_7260
  //# /**
  //# * 游戏屏幕宽,高
  //# */
  //# public static final int canvasWidth = 128;
  //# public static final int canvasHeight = 128;
  //#else
    //#if NOK_7370 || MOT_E2
    //# public static final int canvasWidth = 240;
    //# public static final int canvasHeight = 320;
    //#else
      //#if NOK_6230i
      //# public static final int canvasWidth = 208;
      //# public static final int canvasHeight = 208;
      //#else
        //#if NOK_6101
        //# public static final int canvasWidth = 128;
        //# public static final int canvasHeight = 160;
        //#else
        /**
         * 游戏屏幕宽,高
         */
        public static final int canvasWidth = 176;
        public static final int canvasHeight = 208;
        //#endif
      //#endif
    //#endif
  //#endif
  /**
   * 某种移动物体的编号,即objID
   */
  protected int type;

  /**
   * 能量,即血
   */
  public int energy;

  /**
   * X轴上的速度
   */
  protected int speedX;

  /**
   * Y轴上的速度
   */
  protected int speedY;

  /**
   * 威力
   */
  protected int hitPower;

  /**
   * 是不是已被破坏掉
   */
  protected boolean destroy;

  /**
   * 记录移动物体的范围
   */
  protected Rectangle curLoc;

  //处理动画帧
  public int currentFrame;

  public int currentState;

  public ImageSet imageSet;

  protected long lastFrameChange;

  private int totalCycles;

  //处理链表
  protected MovingObject owner;

  private MovingObject nextLinked;

  private MovingObject prevLinked;

  public MovingObject(int objType, int hitPow, int speedX, int speedY,
                      int locX, int locY, int width, int height) {
    energy = 1;
    destroy = true; //初始生成时将破坏设为true,避免还没有具体初始化就处理
    curLoc = new Rectangle();
    initial(objType, hitPow, speedX, speedY, locX, locY, width, height);
  }

  public void initial(int objType, int hitPow, int speedX, int speedY,
                      int locX, int locY, int width, int height) {
    type = objType;
    this.hitPower = hitPow;
    this.speedX = speedX;
    this.speedY = speedY;
    curLoc.setBounds(locX, locY, width, height);
    destroy = false;
  }

  /**
   * 逻辑处理,移动操作
   *
   */
  public void cycle() {
    move(speedX, speedY);
  }

  /**
   * 处理动画
   *
   */
  public void animateCycle() {
//      当足够的时间过后,改变帧
    if (imageSet.getTotalFrames(currentState) > 1
        && imageSet.getAnimTime(currentState) > 0) {
      long deltaTime = System.currentTimeMillis() - lastFrameChange;
      if (deltaTime > imageSet.getAnimTimePerFrame(currentState)) {
        currentFrame++;
        lastFrameChange = System.currentTimeMillis();
        if (currentFrame >= imageSet.getTotalFrames(currentState)) {
          currentFrame = 0;
          totalCycles++;
        }
      }
    }
  }

  /**
   * @return 精灵活跃的总周期数. 在侦测精灵的活跃期是否结束时特别有用
   */
  public final int getTotalCycles() {
    return totalCycles;
  }

  /**
   * 重置其范围,是否已消亡等
   *
   */
  public void reset() {
    currentFrame = 0;
    totalCycles = 0;
    lastFrameChange = 0;
    destroy = true;
  }

  /**
   * 获取此移动物体的类型
   * @return
   */
  public int getType() {
    return type;
  }

  /**
   * 设置此移动物体的类型
   * @param objType
   */
  public void setType(int objType) {
    type = objType;
  }

  /**
   * 获得此移动物体的杀伤力
   * @return
   */
  public int getHitPower() {
    return hitPower;

  }

  /**
   * 设置此移动物体的杀伤力
   * @param newPower
   */
  public void setHitPower(int newPower) {
    hitPower = newPower;
  }

  /**
   * X轴上移动
   * @param moveoffset
   */
  public void moveX(int moveoffset) {
    curLoc.moveX(moveoffset);
  }

  /**
   * Y轴上移动
   * @param moveoffset
   */
  public void moveY(int moveoffset) {
    curLoc.moveY(moveoffset);
  }

  /**
   * 移动
   * @param moveoffsetX
   * @param moveoffsetY
   */
  public void move(int moveoffsetX, int moveoffsetY) {
    curLoc.moveX(moveoffsetX);
    curLoc.moveY(moveoffsetY);
  }

  /**
   * 设置位置
   * @param x
   * @param y
   */
  public void setLocation(int x, int y) {
    curLoc.setX(x);
    curLoc.setY(y);
  }

  /**
   * 获得当前的X坐标
   * @return
   */
  public int getLocationX() {
    return curLoc.getX();
  }

  /**
   * 获得当前的Y坐标
   * @return
   */
  public int getLocationY() {
    return curLoc.getY();
  }

  /**
   * 设置此移动物体的宽度
   * @param width
   */
  public void setWidth(int width) {
    curLoc.setWidth(width);
  }

  /**
   * 获得此移动物体的宽度
   * @return
   */
  public int getWidth() {
    return curLoc.getWidth();
  }

  /**
   * 设置此移动物体的高度
   * @param height
   */
  public void setHeight(int height) {
    curLoc.setHeight(height);
  }

  /**
   * 获得此移动物体的高度
   * @return
   */
  public int getHeight() {
    return curLoc.getHeight();
  }

  /**
   * 设置X轴上的速度
   * @param speed
   */
  public void setSpeedX(int speed) {
    speedX = speed;
  }

  /**
   * 获得X轴上的速度
   * @return
   */
  public int getSpeedX() {
    return speedX;
  }

  /**
   * 设置Y轴上的速度
   * @param speed
   */
  public void setSpeedY(int speed) {
    speedY = speed;
  }

  /**
   * 获得Y轴上的速度
   * @return
   */
  public int getSpeedY() {
    return speedY;
  }

  /**
   * 消毁
   * @param isDestroy
   */
  public void destroy(boolean isDestroy) {
    destroy = isDestroy;
    if (isDestroy)
      reset();
  }

  /**
   * 判断是否已被消毁
   * @return
   */
  public boolean isDestroy() {
    return destroy;
  }

  /**
   * 得到此移动物体对应的矩形
   * @return
   */
  public Rectangle getBounds() {
    return curLoc;
  }

  /**
   * 获得链表的下一个值
   * @return
   */
  public final MovingObject getNextLinked() {
    return nextLinked;
  }

  /**
   * 设置链表的下一个值
   * @param nextLinked
   */
  public final void setNextLinked(MovingObject nextLinked) {
    this.nextLinked = nextLinked;
  }

  /**
   * 等到链表的前一个值
   * @return
   */
  public final MovingObject getPrevLinked() {
    return prevLinked;
  }

  /**
   * 设置链表的前一个值
   * @param prevLinked
   */
  public final void setPrevLinked(MovingObject prevLinked) {
    this.prevLinked = prevLinked;
  }

}

⌨️ 快捷键说明

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