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

📄 enemy.java

📁 J2ME的游戏原代码!希望能帮助有需要帮助的师兄弟们!
💻 JAVA
字号:
/*
 * @(#)Enemy.java,
 *
 * Copyright (c) 2001-2004 Sony Ericsson Mobile Communication AB (SEMC).
 *
 * German Branch
 * Heisenbergbogen 1, Aschheim 85609, Munich, Germany.
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * SEMC. ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with
 * the terms of the license agreement you entered into with SEMC.
 *
 * @author  venkat
 * @version 1.7, 09/10/2003
*/



package com.sonyericsson.erix;

/**
 * The Enemy class contains the functionality to implement Enemy behavior.
 */

public class Enemy {

    /**
     * Enemy Bounce Direction.
     */
    public static final int ENEMY_BOUNCE_DIRECTION = 1;

    /**
     * Enemy's X Position.
     */
    public int m_nEnemyXPos;

    /**
     * Enemy's Y Position.
     */
    public int m_nEnemyYPos;

    /**
     * The type of the Enemy.
     */
    public int m_nEnemyType;

    /**
     * The counter for counting cycles to be skipped by Enemy.
     */
    public int m_nDelayCounter;

    /**
     * The amount of cycles to be skipped by this enemy.
     */
    public int m_nDelay;

    /**
     * Reference of Engine object.
     */
    private GameEngine m_objEngine;

    /**
     * Direction in which the Enemy is moving.
     */
    private int m_nEnemyDirection;

    /**
     * The amount of X that has to be incremented in order to
     * move the enemy.
     */
    private int ENEMY_X_INCREMENT;

    /**
     * The amount of Y that has to be incremented in order to
     * move the enemy.
     */
    private int ENEMY_Y_INCREMENT;

    /**
     * Enemy Constructor
     */
    public Enemy(GameEngine objEngine){
        m_objEngine = objEngine;
        m_nEnemyXPos = 1;
        m_nEnemyYPos = 127;
        ENEMY_X_INCREMENT = -1;
        ENEMY_Y_INCREMENT = -1;
        m_nDelay = 0;
        m_nDelayCounter = 0;
    }


    /**
     * Set the X & Y increments for the Enemy.
     * @param nXIncr The X Increment to be set for the enemy.
     * @param nYIncr The Y Increment to be set for the enemy.
     */
    public void initialise(int nXIncr, int nYIncr){
        ENEMY_X_INCREMENT = nXIncr;
        ENEMY_Y_INCREMENT = nYIncr;
    }


    /**
     * This method moves the enemy in the set direction.
     */
    public void move() {
        int nEnemyNextXPos = m_nEnemyXPos + ENEMY_X_INCREMENT;
        int nEnemyNextYPos = m_nEnemyYPos + ENEMY_Y_INCREMENT;

        int nXMax = GameEngine.X_MAX - 2;
        int nYMax = GameEngine.Y_MAX - 1;

        if(nEnemyNextXPos < 0) {
            m_nEnemyXPos = 0;
            ENEMY_X_INCREMENT *= -1;
        } else if(nEnemyNextXPos > nXMax) {
            m_nEnemyXPos = nXMax;
            ENEMY_X_INCREMENT *= -1;
        }else{
            m_nEnemyXPos = nEnemyNextXPos;
        }

        if(nEnemyNextYPos < 0){
            m_nEnemyYPos = 0;
            ENEMY_Y_INCREMENT *= -1;
        } else if(nEnemyNextYPos > nYMax) {
            m_nEnemyYPos = nYMax;
            ENEMY_Y_INCREMENT *= -1;
        }else{
            m_nEnemyYPos = nEnemyNextYPos;
        }
    }

    /**
     * This method sets the direction of motion of the enemy.
     * This function is called when the enemy bounces off a secured region.
     * The technique used is to first get the X & Y components of Enemy in the
     * current direction.
     * Then we check if they lie inside or outside the secured region.
     * If the X component lies inside the secured region then X increment has
     * to be reversed.
     * If the Y component lies inside the secured region then Y increment has
     * to be reversed.
     * If it hits a vertex exactly then both X & Y increments are reversed.
     * <p>
     * @param nDirection Indicates the direction to be set for the enemy.
     * The direction can be one of the constants as defined in this class.
     */
    public void setDirection(int nDirection) {

        try{
            if(nDirection == ENEMY_BOUNCE_DIRECTION){
                int nLarge = -1;
                int nSmall = -1;
                int nNumberOfPoints = m_objEngine.m_nUnSelectedRegionPoints-1;
                for(int i = 0; i < nNumberOfPoints; i++){
                    if(m_objEngine.m_arrUnSelectedRegion[i][0]
                       == m_objEngine.m_arrUnSelectedRegion[i+1][0]){
                        //Vertical line processing.
                        if(m_nEnemyXPos == m_objEngine.m_arrUnSelectedRegion[i][0]){
                            nLarge = (m_objEngine.m_arrUnSelectedRegion[i][1] > m_objEngine.m_arrUnSelectedRegion[i+1][1])
                                        ? m_objEngine.m_arrUnSelectedRegion[i][1]
                                        : m_objEngine.m_arrUnSelectedRegion[i+1][1];

                            nSmall = (m_objEngine.m_arrUnSelectedRegion[i][1] < m_objEngine.m_arrUnSelectedRegion[i+1][1])
                                        ? m_objEngine.m_arrUnSelectedRegion[i][1]
                                        : m_objEngine.m_arrUnSelectedRegion[i+1][1];

                            if(m_nEnemyYPos == nSmall || m_nEnemyYPos == nLarge){

                                //Finding where the Y Componenet lies.
                                int nRow = m_nEnemyYPos + ENEMY_Y_INCREMENT;
                                int nNextX = m_nEnemyXPos;
                                //int nCol = nNextX / 8;
                                int nCol = nNextX >> 3;
                                int nBit = nNextX % 8;

                                if(nRow <= 0 || nRow >= (GameEngine.Y_MAX-1)){
                                    //To handle a vertex with concave edges.
                                    //Vertex with concave edges are the original
                                    //boundary points of the playfield.
                                    ENEMY_Y_INCREMENT *= -1;
                                }else if(GameEngine.inSecuredRegion(m_objEngine.m_arrSecuredRegionBmp, nRow, nCol, nBit)){
                                    ENEMY_Y_INCREMENT *= -1;
                                }

                                //Finding where the X Componenet lies.
                                nRow = m_nEnemyYPos;
                                nNextX = m_nEnemyXPos + ENEMY_X_INCREMENT;
                                //nCol = nNextX / 8;
                                nCol = nNextX >> 3;
                                nBit = nNextX % 8;

                                if(nNextX <= 0 || nNextX >= (GameEngine.X_MAX-2)){
                                    //To handle a vertex with concave edges.
                                    //Vertex with concave edges are the original
                                    //boundary points of the playfield.
                                    ENEMY_X_INCREMENT *= -1;
                                }else if(GameEngine.inSecuredRegion(m_objEngine.m_arrSecuredRegionBmp, nRow, nCol, nBit)){
                                    ENEMY_X_INCREMENT *= -1;
                                }
                                break;
                            }else if(m_nEnemyYPos > nSmall && m_nEnemyYPos < nLarge){
                                ENEMY_X_INCREMENT *= -1;
                                break;
                            }
                        }
                    }else if(m_objEngine.m_arrUnSelectedRegion[i][1]
                       == m_objEngine.m_arrUnSelectedRegion[i+1][1]){
                        //Horizontal Line processing.
                        if(m_nEnemyYPos == m_objEngine.m_arrUnSelectedRegion[i][1]){
                            nLarge = (m_objEngine.m_arrUnSelectedRegion[i][0] > m_objEngine.m_arrUnSelectedRegion[i+1][0])
                                        ? m_objEngine.m_arrUnSelectedRegion[i][0]
                                        : m_objEngine.m_arrUnSelectedRegion[i+1][0];

                            nSmall = (m_objEngine.m_arrUnSelectedRegion[i][0] < m_objEngine.m_arrUnSelectedRegion[i+1][0])
                                        ? m_objEngine.m_arrUnSelectedRegion[i][0]
                                        : m_objEngine.m_arrUnSelectedRegion[i+1][0];

                            if(m_nEnemyXPos == nSmall || m_nEnemyXPos == nLarge){

                                //Finding where the Y Componenet lies.
                                int nRow = m_nEnemyYPos + ENEMY_Y_INCREMENT;
                                int nNextX = m_nEnemyXPos;
                                //int nCol = nNextX / 8;
                                int nCol = nNextX >> 3;
                                int nBit = nNextX % 8;

                                if(nRow <= 0 || nRow >= (GameEngine.Y_MAX-1)){
                                    //To handle a vertex with concave edges.
                                    //Vertex with concave edges are the original
                                    //boundary points of the playfield.
                                    ENEMY_Y_INCREMENT *= -1;
                                }else if(GameEngine.inSecuredRegion(m_objEngine.m_arrSecuredRegionBmp, nRow, nCol, nBit)){
                                    ENEMY_Y_INCREMENT *= -1;
                                }

                                //Finding where the X Componenet lies.
                                nRow = m_nEnemyYPos;
                                nNextX = m_nEnemyXPos + ENEMY_X_INCREMENT;
                                //nCol = nNextX / 8;
                                nCol = nNextX >> 3;
                                nBit = nNextX % 8;

                                if(nNextX <= 0 || nNextX >= (GameEngine.X_MAX-2)){
                                    //To handle a vertex with concave edges.
                                    //Vertex with concave edges are the original
                                    //boundary points of the playfield.
                                    ENEMY_X_INCREMENT *= -1;
                                }else if(GameEngine.inSecuredRegion(m_objEngine.m_arrSecuredRegionBmp, nRow, nCol, nBit)){
                                    ENEMY_X_INCREMENT *= -1;
                                }
                                break;
                            }else if(m_nEnemyXPos > nSmall && m_nEnemyXPos < nLarge){
                                ENEMY_Y_INCREMENT *= -1;
                                break;
                            }
                        }
                    }
                }
            }
        }catch(Exception e){
            //System.out.println("Exception in setting enemy direction - "+e);
        }
    }

   /**
    * This method indicates that this object will no longer be used.
    */
    public void cleanUp() {
        m_objEngine = null;
    }
}

⌨️ 快捷键说明

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