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

📄 entity.java

📁 Critter_Crunch_J2ME 游戏源
💻 JAVA
字号:
/*******************************************************************************
 **
 ** Class: Entity (Entity.java)
 **
 ** The "Entity" class encapsulates all the data and functionality associated
 ** with all the different items/objects in the game, as well as serving as the 
 ** base class for Creautres, Bosses and the Avatar.
 **
 ******************************************************************************/



// Entity header.
import javax.microedition.lcdui.Graphics;

// the Entity class itself.
abstract class Entity {
   
   /****************************************************************************
    ** Constants
    ***************************************************************************/

   public static final int     STATE_DEFAULT     = 0x00000000; // default state.
   public static final int     STATE_ASCENDING   = 0x00000001; // moving up.
   public static final int     STATE_DESCENDING  = 0x00000002; // moving down.
   public static final int     STATE_WESTWARD    = 0x00000004; // moving left.
   public static final int     STATE_EASTWARD    = 0x00000008; // moving right.
   public static final int     STATE_MOBILE      = 0x00000010; // moving in an arbitrary direction, not necesserily up/down/left/right.
   public static final int     STATE_IMMOBILE    = 0x00000020; // unable to move/be moved.
   public static final int     STATE_FLASHING    = 0x00000040; // flashing % 3.
   public static final int     STATE_EATING      = 0x00000080; // eating something.
   public static final int     STATE_DYING       = 0x00000100; // about to die.
   public static final int     STATE_POPPING     = 0x00000200; // in the process of dying.
   public static final int     BOX_X             = 0;          // collision box x index.
   public static final int     BOX_Y             = 1;          // collision box y index.
   public static final int     BOX_WIDTH         = 2;          // collision box width index.
   public static final int     BOX_HEIGHT        = 3;          // collision box height index.
   public static final int     FLASH_1_INTERVAL  = 4;          // amount of time between FLASH 1 rendering.
   
   
   
   /****************************************************************************
    ** Variables
    ***************************************************************************/

   public              byte    m_id;                           // Entity's identification number.
   public              long    m_state;                        // logical state(s) of the Entity.
   public              short   m_anim_state;                   // animation state of the Entity.
   public              byte    m_frame;                        // current frame of this Entity's animation.
   public              short   m_timer;                        // timer that dictates Entity behaviour.
   public              short   m_x;                            // on-screen x coordinate of the Entity.
   public              short   m_y;                            // on-screen y coordinate of the Entity.
   public              short   m_next_x;                       // next calculated x position.
   public              short   m_next_y;                       // next calculated y position.
   public              short   m_destination_x;                // desired x position of the Entity.
   public              short   m_destination_y;                // desired y position of the Entity.
   public              byte    m_board_x;                      // row location of the Entity.
   public              byte    m_board_y;                      // column location of the Entity.
   public              byte    m_speed;                        // velocity of Entity's movement.

   
   
   /****************************************************************************
    ** 'tors
    ***************************************************************************/
   
   // class initializer.
   public static void poke() { Thread.yield(); }
   
   
   
   /****************************************************************************
    ** Miscellaneous Methods
    ***************************************************************************/

   // process the Entity's functionality.
   public void process() {
      
      m_x = m_next_x;
      m_y = m_next_y;
      if(m_timer > 0) m_timer--;

      if((m_state & STATE_DYING) != 0) {
         if(m_timer == 0) explode();
      }
      if((m_state & STATE_POPPING) != 0) {
         if(m_timer == 0) die();
      }
      if((m_state & STATE_ASCENDING) != 0) {
         if(m_y <= m_destination_y) stop();
         else                       m_next_y -= m_speed;
      }
      if((m_state & STATE_DESCENDING) != 0) {
         if(m_y >= m_destination_y) stop();
         else                       m_next_y += m_speed;
      }
      
   }

   // draw the Entity's current frame.
   public void render(Graphics g) {

      if( ((m_state & STATE_FLASHING) != 0) && ((Level.s_clock % FLASH_1_INTERVAL) == 0) 
          || ((m_state & STATE_POPPING) != 0)
          /*|| ((m_state & STATE_FLASHING_2) != 0) && ((Level.s_clock & 1) == 0)*/ ) {
         return;
      }

      //if(m_frame >= Engine.ANIMS[m_anim_state].length) Engine.printLine("frame out of bounds: " + m_frame + ", on: " + toString());
      int id = Engine.ANIMS[m_anim_state][m_frame];

      //#if !NO_SCREEN_SHAKING || !NO_BOARD_BUMPS
      int x_offset = 0;
      int y_offset = 0;
      
      //#ifndef NO_SCREEN_SHAKING
      x_offset += shake(Engine.X_AXIS);
      y_offset += shake(Engine.Y_AXIS);
      //#endif
      
      //#ifndef NO_BOARD_BUMPS
      if(m_board_y != Engine.FULL_BYTE) y_offset += Level.s_board_bumps[0][m_board_x];
      //#endif
      
      Engine.drawImage(g, 
                       id, 
                       m_x + x_offset - (Engine.COORDS[id][Engine.WIDTH] >> 1), 
                       m_y + y_offset - (Engine.COORDS[id][Engine.HEIGHT] >> 1));
      //#else
//#       Engine.drawImage(g, 
//#                        id, 
//#                        m_x - (Engine.COORDS[id][Engine.WIDTH] >> 1), 
//#                        m_y - (Engine.COORDS[id][Engine.HEIGHT] >> 1));
      //#endif

      if(++m_frame >= Engine.ANIMS[m_anim_state].length) m_frame = 0;
   }
   
   //#ifdef COLUMN_RENDERING
//#    // draw the Entity's current frame without doing a clip
//#    public void renderNoClip(Graphics g) {
//# 
//#       if( ((m_state & STATE_FLASHING) != 0) && ((Level.s_clock % FLASH_1_INTERVAL) == 0) 
//#           /*|| ((m_state & STATE_FLASHING_2) != 0) && ((Level.s_clock & 1) == 0)*/ ) {
//#          return;
//#       }
//# 
//#       int id = Engine.ANIMS[m_anim_state][m_frame];
//# 
      //#if !NO_SCREEN_SHAKING || !NO_BOARD_BUMPS
//#       int x_offset = 0;
//#       int y_offset = 0;
//#       
      //#ifndef NO_SCREEN_SHAKING
//#       x_offset += shake(Engine.X_AXIS);
//#       y_offset += shake(Engine.Y_AXIS);
      //#endif
//#       
      //#ifndef NO_BOARD_BUMPS
//#       if(m_board_y != Engine.FULL_BYTE) y_offset += Level.s_board_bumps[0][m_board_x];
      //#endif
//#       
//#       Engine.drawImage(g, 
//#                        id, 
//#                        m_x + x_offset - (Engine.COORDS[id][Engine.WIDTH] >> 1), 
//#                        m_y + y_offset - (Engine.COORDS[id][Engine.HEIGHT] >> 1));
      //#else
//#       Engine.drawImage(g, 
//#                        id, 
//#                        m_x - (Engine.COORDS[id][Engine.WIDTH] >> 1), 
//#                        m_y - (Engine.COORDS[id][Engine.HEIGHT] >> 1), false);
      //#endif
//#       if(++m_frame >= Engine.ANIMS[m_anim_state].length) m_frame = 0;
//#       
//#    }
   //#endif
   

   //#ifndef NO_SCREEN_SHAKING
   // return an offset of the indicated axis based on screen-shaking logic.
   abstract int shake(int axis);
   //#endif
   
   // stop Entity movement.
   public void stop() {

      m_x = m_next_x = m_destination_x;
      m_y = m_next_y = m_destination_y;
      m_speed = 0;
   }
   
   // sentence the Entity TO DEATH.
   abstract void sentence(); 

   // process the Entity's dying sequence.
   abstract void explode();
   
   // process the Entity's death sequence.
   abstract void die();

   // set the indicated animation state.
   public void setAnim(int state) {
      
      m_anim_state = (short)state;
      m_frame = 0;
   }

   // return an array representing the on-screen bounding box of the Entity.
   abstract int [] getBox();
      
   // return the string representation of the Entity's id/name.
   abstract String idString();
   
   // return the string representation of the Entity's logical state(s).
   public String stateString() {
      
      //#ifdef DEBUG_PRINTOUTS
      String s = "DEFAULT | ";

      if((m_state & STATE_ASCENDING) != 0)  s += "ASCENDING | ";
      if((m_state & STATE_DESCENDING) != 0) s += "DESCENDING | ";
      if((m_state & STATE_WESTWARD) != 0)   s += "WESTWARD | ";
      if((m_state & STATE_EASTWARD) != 0)   s += "EASTWARD | ";
      if((m_state & STATE_IMMOBILE) != 0)   s += "IMMOBILE | ";
      if((m_state & STATE_FLASHING) != 0)   s += "FLASHING | ";
      if((m_state & STATE_DYING) != 0)      s += "DYING | ";
      if((m_state & STATE_POPPING) != 0)    s += "POPPING | ";
      
      return s;
      //#else
//#       return Engine.NO_DBG_STRING;
      //#endif
   }
   
   // return the string representation of the Entity's animation state.
   abstract String animStateString();

   // overloaded toString() method of an Entity.
   public String toString() {
      
      //#ifdef DEBUG_PRINTOUTS
      String s = "";

      s += (", X: " + m_x);
      s += (", Y: " + m_y);
      s += (", nextX: " + m_next_x);
      s += (", nextY: " + m_next_y);
      s += (", destX: " + m_destination_x);
      s += (", destY: " + m_destination_y);
      s += (", boardX: " + m_board_x);
      s += (", boardY: " + m_board_y);
      s += (", speed: " + m_speed);
      s += (", timer: " + m_timer);
      s += (", frame: " + m_frame);

      return idString() + stateString() + animStateString() + s;
      //#else
//#       return Engine.NO_DBG_STRING;
      //#endif
   }
   
   // print the toString() to the console.
   public void print() {
      
      //#ifdef DEBUG_PRINTOUTS
      System.out.println("" + this.toString());
      //#endif
   }
}

⌨️ 快捷键说明

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