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

📄 erix.java

📁 J2ME的游戏原代码!希望能帮助有需要帮助的师兄弟们!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        }

        boolean bReturnValue = false;
        int nRowNum = 0;
        int nColNum = 0;
        int nBitPosition = 0;
        byte bytPathBits = 0;

        try{
            nRowNum = m_nErixYPos;
            nColNum = m_nErixXPos >> 3;
            nBitPosition = m_nErixXPos % 8;
            bytPathBits = m_arrPathBitmap[nRowNum][nColNum];
            switch(nBitPosition){
                case 0:
                    bytPathBits &= ZERO_BIT_SET; //last bit
                    if(bytPathBits == ZERO_BIT_SET){
                        bReturnValue = true;
                    }
                    break;
                case 1:
                    bytPathBits &= FIRST_BIT_SET;
                    if(bytPathBits == FIRST_BIT_SET){
                        bReturnValue = true;
                    }
                    break;
                case 2:
                    bytPathBits &= SECOND_BIT_SET;
                    if(bytPathBits == SECOND_BIT_SET){
                        bReturnValue = true;
                    }
                    break;
                case 3:
                    bytPathBits &= THIRD_BIT_SET;
                    if(bytPathBits == THIRD_BIT_SET){
                        bReturnValue = true;
                    }
                    break;
                case 4:
                    bytPathBits &= FOURTH_BIT_SET;
                    if(bytPathBits == FOURTH_BIT_SET){
                        bReturnValue = true;
                    }
                    break;
                case 5:
                    bytPathBits &= FIFTH_BIT_SET;
                    if(bytPathBits == FIFTH_BIT_SET){
                        bReturnValue = true;
                    }
                    break;
                case 6:
                    bytPathBits &= SIXTH_BIT_SET;
                    if(bytPathBits == SIXTH_BIT_SET){
                        bReturnValue = true;
                    }
                    break;
                case 7:
                    bytPathBits &= SEVENTH_BIT_SET;
                    if(bytPathBits == SEVENTH_BIT_SET){
                        bReturnValue = true;
                    }
                    break;
                default:
                    break;
            }
        }catch(Exception e){
            //System.out.println("Exception in isPathBeingCrossed - Erix " + nRowNum + " " + nColNum);
        }
        if(bReturnValue){
            m_bIsErixOnBoundary = true;
            m_objEngine.m_bIsPathCrossed = true;
        }

        return bReturnValue;
    } // end of method isPathBeingCrossed


    /**
     * This method will use setDirection to alter the direction of motion of
     * Erix if the pressed key as not the same as the current direction.
     * This method handles the following key for controling the Erix movement
     * <ul>
     * <li> For moving up - key 2
     * <li> For moving left - key 4
     * <li> For moving right - key 6
     * <li> For moving down - key 8
     * </ul>
     * @param nPressedKey The key pressed by the player.
     */
    public void processKey(int nPressedKey) {
        if(!m_bKeyPressed){
            m_bKeyPressed = true;
            if(setDirection(nPressedKey)){
                addBound();
            }
        }
    }


    /**
     * This method sets the direction of motion of Erix.
     * This method would be invoked by processKey if the key pressed by the
     * player was not the same as the current direction.
     * @param nDirection Indicates the direction to be set for Erix.
     * The direction can be one of the constants as defined in this class.
     * @return Indicates if the direction could be changed or not.
     */
    public boolean setDirection(int nDirection) {
        boolean bReturnValue = false;
        int nRow = getNextRowPosition(nDirection);
        if(nRow < 0){
            return bReturnValue;
        }
        int nXPos = getNextColPosition(nDirection);
        if(nXPos < 0){
            return bReturnValue;
        }

        int nCol = nXPos >> 3;
        int nBit = nXPos % 8;
        //Check if the next position of Erix is in the secured region or not.
        if(GameEngine.inSecuredRegion(m_objEngine.m_arrSecuredRegionBmp, nRow,nCol,nBit)){
            //Erix is in secured Region
            if(m_bIsErixOnBoundary){
                //Erix was already on a boundary so checkin if he is still
                //moving along a boundary.
                if(isNextPositionOnBoundary(nXPos, nRow, nDirection)){
                    //Erix is moving On Boundary
                    m_bIsErixOnBoundary = true;
                    m_nErixDirection = nDirection;
                    m_bErixMoving = true;
                    m_nDelayCounter = 0;
                }else{
                    //Erix is already on a boundary & wants to move inside the
                    //secured region.
                }
            }else{
                //Erix was inside the playfield & his next position is on a
                //boundary.
                m_nErixDirection = nDirection;
                m_bErixMoving = true;
                m_nDelayCounter = 0;
            }
        }else{
            //Next position is not in secured region.
            //So erix can continue moving or start moving.
            if(m_bErixMoving){
                if(!(m_nErixDirection == nDirection
                        || m_nErixDirection == (nDirection*-1))){
                    //Change in direction
                    m_nErixDirection = nDirection;
                    m_bErixMoving = true;
                    m_bIsErixOnBoundary = false;
                    //addBound();
                    bReturnValue = true;
                    m_nDelayCounter = 0;
                }else{
                    //Direction is Same / opp.
                }
            }else{
                //Erix has started moving
                m_nErixDirection = nDirection;
                m_bErixMoving = true;
                m_bIsErixOnBoundary = false;
                //addBound();
                bReturnValue = true;
                m_nDelayCounter = 0;
            }
        }
        return bReturnValue;
    }


    /**
     * This method is used to get Erix's next Row position depending on
     * the direction.
     * @param nDirection Indicates the direction in which Erix is moving.
     * @return The next Row position.
     */
    private int getNextRowPosition(int nDirection){
        int nNextYPosition = -1;
        if(nDirection == UP_DIRECTION){
            nNextYPosition = m_nErixYPos - 1;
            if(nNextYPosition < 0){
                nNextYPosition = -1;
            }
        }else if(nDirection == DOWN_DIRECTION){
            nNextYPosition = m_nErixYPos + 1;
            if(nNextYPosition > (GameEngine.Y_MAX - 1)){
                nNextYPosition = -1;
            }
        }else{
            nNextYPosition = m_nErixYPos;
        }
        return nNextYPosition;
    }


    /**
     * This method is used to get Erix's next Column position depending on
     * the direction.
     * @param nDirection Indicates the direction in which Erix is moving.
     * @return The next Column position.
     */
    private int getNextColPosition(int nDirection){
        int nNextXPosition = -1;
        if(nDirection == LEFT_DIRECTION){
            nNextXPosition = m_nErixXPos - 1;
            if(nNextXPosition < 0){
                nNextXPosition = -1;
            }
        }else if(nDirection == RIGHT_DIRECTION){
            nNextXPosition = m_nErixXPos + 1;
            if(nNextXPosition > (GameEngine.X_MAX - 2)){
                nNextXPosition = -1;
            }
        }else{
            nNextXPosition = m_nErixXPos;
        }
        return nNextXPosition;
    }


    /**
     * This method is used check if Erix's next position will be on a boundary
     * or not.
     * @param nXPos The next X position that has to be checked if its on a
     * boundary.
     * @param nYPos The next X position that has to be checked if its on a
     * boundary.
     * @param nDirection The direction in which Erix wants to move.
     * @return Indicates if the next position is indeed on a boundary.
     */
    private boolean isNextPositionOnBoundary(int nXPos, int nYPos, int nDirection){
        boolean bReturnValue = false;
        try{
            short [][] UnSecRegion = m_objEngine.m_arrUnSelectedRegion;
            int UnSecRegionPoints = m_objEngine.m_nUnSelectedRegionPoints;
            int nLarge = -1;
            int nSmall = -1;
            for(int i = 0; i < UnSecRegionPoints - 1; i++){
                if(nDirection == Erix.UP_DIRECTION || nDirection == Erix.DOWN_DIRECTION){
                    //Check if the edge is vertical.
                    //Process only vertical edges.
                    if(UnSecRegion[i][0] == UnSecRegion[i+1][0]){
                        if(nXPos == UnSecRegion[i][0]){
                            nLarge = (UnSecRegion[i][1] > UnSecRegion[i+1][1])
                                        ? UnSecRegion[i][1]
                                        : UnSecRegion[i+1][1];

                            nSmall = (UnSecRegion[i][1] < UnSecRegion[i+1][1])
                                        ? UnSecRegion[i][1]
                                        : UnSecRegion[i+1][1];

                            if(nYPos >= nSmall && nYPos <= nLarge){
                                bReturnValue = true;
                                m_nErixBoundaryStartPoint = nSmall;
                                m_nErixBoundaryEndPoint = nLarge;
                                break;
                            }
                        }
                    }//else skip the horizontal edges.
                }else{
                    if(UnSecRegion[i][1] == UnSecRegion[i+1][1]){
                    //Check if the edge is horz.
                    //Process only horz edges.
                        if(nYPos == UnSecRegion[i][1]){
                            nLarge = (UnSecRegion[i][0] > UnSecRegion[i+1][0])
                                        ? UnSecRegion[i][0]
                                        : UnSecRegion[i+1][0];

                            nSmall = (UnSecRegion[i][0] < UnSecRegion[i+1][0])
                                        ? UnSecRegion[i][0]
                                        : UnSecRegion[i+1][0];

                            if(nXPos >= nSmall && nXPos <= nLarge){
                                bReturnValue = true;
                                m_nErixBoundaryStartPoint = nSmall;
                                m_nErixBoundaryEndPoint = nLarge;
                                break;
                            }
                        }
                    }//else skip the vertical edges.
                }
            }
        }catch(Exception e){
            //System.out.println("Exception in isNextPositionOnBoundary");
        }
        return bReturnValue;
    }


    /**
     * This method moves Erix in the set direction.
     * Each 搈ovement

⌨️ 快捷键说明

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