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

📄 battlefield.java

📁 还记得儿时在游戏机上玩坦克大战吗?如今这种经典游戏不在游戏机上玩了
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//------------------------------------------------------------------------------
//                         COPYRIGHT 2008 GUIDEBEE
//                           ALL RIGHTS RESERVED.
//                     GUIDEBEE CONFIDENTIAL PROPRIETARY
///////////////////////////////////// REVISIONS ////////////////////////////////
// Date       Name                 Tracking #         Description
// ---------  -------------------  ----------         --------------------------
// 16JAN2008  James Shen                 	      Initial Creation
////////////////////////////////////////////////////////////////////////////////
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//The Software shall be used for Good, not Evil.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
//Any questions, feel free to drop me a mail at james.shen@guidebee.biz.
//--------------------------------- PACKAGE ------------------------------------
package com.pstreets.game.battlecity;

//--------------------------------- IMPORTS ------------------------------------
import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

//[------------------------------ MAIN CLASS ----------------------------------]
////////////////////////////////////////////////////////////////////////////////
//--------------------------------- REVISIONS ----------------------------------
// Date       Name                 Tracking #         Description
// --------   -------------------  -------------      --------------------------
// 16JAN2008  James Shen                 	      Initial Creation
////////////////////////////////////////////////////////////////////////////////
/**
 * This class defines the battle field for the game.
 * <p>
 * <hr><b>&copy; Copyright 2008 Guidebee, Inc. All Rights Reserved.</b>
 * @version     1.00, 16/01/08
 * @author      Guidebee, Inc.
 */
public final class BattleField extends TiledLayer implements Actor{
    
    /**
     * No direction.
     */
    public static final int NONE = -1;
    
    /**
     * Heading north.
     */
    public static final int NORTH = 0;
    
    /**
     * Heading east.
     */
    public static final int EAST = 1;
    
    /**
     * Heading south.
     */
    public static final int SOUTH = 2;
    
    /**
     * Heading west.
     */
    public static final int WEST = 3;
        
    /**
     * Snow tile type.
     */
    private static final int SNOW = 1;
    
    /**
     * Brick wall tile type.
     */
    private static final int BRICK_WALL = 2;
    
    /**
     * Forest tile type.
     */
    private static final int FOREST = 3;
    
    /**
     * Water tile type.
     */
    private static final int WATER =4;
    
    /**
     * Concrete wall tile type.
     */
    private static final int CONCRETE_WALL = 6;
    
    /**
     * water animation frames.
     */
    private static int[][] waterFrames = { { 4, 5 }, { 5, 4 } };
    
    /**
     * tick used to control the water animation speed.
     */
    private int tickCount = 0;
    
    /**
     * default number of tiles in each direction.
     */
    private static final int NUMBER_IN_TILES = 26;
    /**
     * the number of horizontal tiles
     */
    private static int WIDTH_IN_TILES = 26;
    /**
     *the number of vertical tiles.
     */
    private static int HEIGHT_IN_TILES = 26;
    
    /**
     * Random used to create randome position pair for powerups.
     */
    private static Random rnd=new Random();
    
    /**
     * where enemy tanks appears ,left, middle, right
     */
    private static int[][]enemyPos=new int[3][2];
    
    /**
     * change enemy tank apprears position in sequence.
     */
    private static int nextEnemyPos=0;
    
    /**
     * The player's home became concrete wall time.
     */
    private long concreteWallStartTime=0;
    
    /**
     * how long player's home concrete wall can be
     */
    private static long concreteWallPeriod=30000;
      
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 16JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * Constructor used to create a battle fields.
     * @param xTiles the number of tiles in width.
     * @param yTiles the number of tiles in height.
     */
    public BattleField(int xTiles,int yTiles) {
        //When read from file, each number stand for 2X2 tiles
        super(xTiles*2, yTiles*2, ResourceManager.getInstance().getTileImage(),
                ResourceManager.TILE_WIDTH/2, ResourceManager.TILE_WIDTH/2);
        createAnimatedTile(waterFrames[0][0]); // tile -1
        createAnimatedTile(waterFrames[1][0]); // tile -2
        WIDTH_IN_TILES=xTiles*2;
        HEIGHT_IN_TILES=yTiles*2;
        if(xTiles*2<NUMBER_IN_TILES || xTiles*2<NUMBER_IN_TILES){
            throw new IllegalArgumentException("Tiles shall be greater than 13");
        }
        //Initialized array which stores enemy appears start position.
        //Left
        enemyPos[0][0]=0;
        enemyPos[0][1]=0;
        //Middle
        enemyPos[1][0]=WIDTH_IN_TILES/4*ResourceManager.TILE_WIDTH;
        enemyPos[1][1]=0;
        //Right
        enemyPos[2][0]=(WIDTH_IN_TILES/2-2)*ResourceManager.TILE_WIDTH;
        enemyPos[2][1]=0;
        
    }
    
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 16JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * Check if given rectangle contains impassable area.
     * @param x  x coordinate.
     * @param y  y coordinate.
     * @param width the width of given area.
     * @param height the height of given area.
     * @return true contains impassable area.
     */
    public boolean containsImpassableArea(int x, int y, int width, int height) {
        int TILE_WIDTH=ResourceManager.TILE_WIDTH/2;
        int rowMin = y / TILE_WIDTH;
        int rowMax = (y + height - 1) / TILE_WIDTH;
        if(rowMax >= HEIGHT_IN_TILES){rowMax = HEIGHT_IN_TILES - 1;}
        int columnMin = x / TILE_WIDTH;
        if(x < 0 || y < 0 || columnMin > WIDTH_IN_TILES -1|| 
                rowMin > HEIGHT_IN_TILES-1){
            return true;
        }
        rowMin=Math.min(rowMin,getRows()-1);
        columnMin=Math.min(columnMin,getColumns()-1);
        int columnMax = (x + width - 1) / TILE_WIDTH;
        if(columnMax >= WIDTH_IN_TILES){columnMax = WIDTH_IN_TILES - 1;}
        for (int row = rowMin; row <= rowMax; ++row) {
            for (int column = columnMin; column <= columnMax; ++column) {
                int cell = getCell(column, row);
                if ((cell < 0) || (cell == BRICK_WALL)
                || (cell == CONCRETE_WALL)) {
                    return true;
                }
            }
        }
        return false;
    }
    
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 16JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * check if one snow filed, if on snow, the tank move a bit faster.
     * @param x  x coordinate.
     * @param y  y coordinate.
     * @return true on the snow field.
     */
    public boolean isOnSnow(int x,int y){
        int TILE_WIDTH=ResourceManager.TILE_WIDTH/2;
        int row = y / TILE_WIDTH;
        int column = x / TILE_WIDTH;
        if(x < 0 || y < 0 || column > WIDTH_IN_TILES -1||
                row > HEIGHT_IN_TILES-1){
            return false;
        }
        row=Math.min(row,getRows()-1);
        column=Math.min(column,getColumns()-1);
        int cell = getCell(column, row);
        return cell==SNOW;
    }

    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 16JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * Check if given point hit wall in the battle field. If hits wall, the wall
     * will be destoryed if with enough strength.
     * @param x  x coordinate.
     * @param y  y coordinate.
     * @param strength the strength of the the hitting object.
     * @return true hit the wall.
     */
    public boolean hitWall(int x, int y, int strength) {
        boolean bRet=false;
        int TILE_WIDTH=ResourceManager.TILE_WIDTH/2;
        int []col=new int[2];
        int []row=new int[2];
        int maxRows=getRows()-1;
        int maxCols=getColumns()-1;
        col[0] = Math.min((x-TILE_WIDTH/4) / TILE_WIDTH,maxCols);
        row[0] = Math.min((y-TILE_WIDTH/4) / TILE_WIDTH,maxRows);
        
        col[1] = Math.min((x+TILE_WIDTH/4) / TILE_WIDTH,maxCols);
        row[0] = Math.min((y-TILE_WIDTH/4) / TILE_WIDTH,maxRows);
        
        col[0] = Math.min((x-TILE_WIDTH/4) / TILE_WIDTH,maxCols);
        row[1] = Math.min((y+TILE_WIDTH/4) / TILE_WIDTH,maxRows);
        
        col[1] = Math.min((x+TILE_WIDTH/4) / TILE_WIDTH,maxCols);
        row[1] = Math.min((y+TILE_WIDTH/4) / TILE_WIDTH,maxRows);
        for(int i=0;i<2;i++){
            for(int j=0;j<2;j++){
                int cell = getCell(col[i], row[j]);
                if (cell == BRICK_WALL && strength > 0) {
                      setCell(col[i], row[j], 0);
                    bRet= true;
                } else if (cell == CONCRETE_WALL) {
                    if (strength > Bullet.GRADE_DEFAULT)
                        setCell(col[i], row[j], 0);
                    bRet= true;
                }else if (cell == FOREST || cell<0 || cell==SNOW) {
                    //here a bullet can destory water, snow field and forest
                    //which is unrealistic:) just for fun.
                    if (strength > Bullet.GRADE_BREAK_CONCRETE_WALL)
                    {
                        setCell(col[i], row[j], 0);
                        bRet= true;
                    }
                }

⌨️ 快捷键说明

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