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

📄 map.java

📁 简单的J2ME手机小游戏 包含游戏的基本;
💻 JAVA
字号:
package com.wqat.SuperPig;


import java.util.Random;

import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.TiledLayer;


/**
 * 地图类
 * @author wqat
 */
public class Map
{
    /**
     * 炸弹与道具层
     */
    private static TiledLayer itemsLayer = null;
    
    /**
     * 地面层
     */
    private static TiledLayer floorLayer = null;
    
    /**
     * 建筑层
     */
    private TiledLayer buildingsLayer = null;
    
    /**
     * 地图数据 第一层为地表贴图 第二层为地面物体 第三层为地面物体属性 第四层为是否被踩过
     */
    public byte[][][] tiles;

    private Random rand = new Random();
    Image imageSource = null;
    
    /**
     * 获得地面层
     * @return 地面层
     */
    public TiledLayer getFloorLayer()
    {
        if(floorLayer == null)
        {
            Image imgFloor = null;
            try
            {
                imgFloor = Image.createImage(Constant.IMAGE_SRC_FLOOR);
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
            
            floorLayer = new TiledLayer(Constant.MAP_COLS, Constant.MAP_ROWS,
                    imgFloor, Constant.CELL_WIDTH, Constant.CELL_HEIGHT);
        }
        return floorLayer;
    }
    
    /**
     * 获得炸弹与道具层
     * @return 炸弹与道具层
     */
    public TiledLayer getBombsAndToolsLayer()
    {
        if(itemsLayer == null)
        {
            Image imgBomb = null;
            try
            {
                imgBomb = Image.createImage(Constant.IMAGE_SRC_ITEMS);
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
            
            itemsLayer = new TiledLayer(Constant.MAP_COLS, Constant.MAP_ROWS, imgBomb, Constant.CELL_WIDTH,
                    Constant.CELL_HEIGHT);
        }
        return itemsLayer;
    }
    
    /**
     * 获得建筑层
     * @return 建筑层
     */
    public TiledLayer getBuildingsLayer()
    {
        return buildingsLayer;
    }

    /**
     * 创建建筑层
     * @param stage 当前关卡号
     * @return 建筑层
     */
    private TiledLayer createBuildingsLayer(int stage)
    {
        Image imgBuilding = null;
        
        try
        {
            imgBuilding = Image.createImage(Constant.IMAGE_DIR + stage + ".png");
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        
        return new TiledLayer(Constant.MAP_COLS, Constant.MAP_ROWS, imgBuilding, Constant.CELL_WIDTH,
                Constant.CELL_HEIGHT);
    }
    
    /**
     * 构造地图
     * @param stage 关卡号
     * @param imgTiles 地图源图像
     */
    public Map(Stage stage)
    {
        buildingsLayer = createBuildingsLayer(stage.imgIndex);
        tiles = stage.mapData;
        
        itemsLayer = getBombsAndToolsLayer();
        floorLayer = getFloorLayer();
        
        initLayer(Constant.LAYER_FLOOR);
        initLayer(Constant.LAYER_BUILDING);
        clearLayer(itemsLayer);
    }
    
    /**
     * 使用地图数据填充指定层
     * @param layerIndex 指定层索引 Constant.LAYER_FLOOR 或者 Constant.LAYER_BUILDING
     */
    private void initLayer(int layerIndex)
    {
        TiledLayer layer = null;
        switch(layerIndex)
        {
        case Constant.LAYER_FLOOR:
            layer = getFloorLayer();
            break;
        case Constant.LAYER_BUILDING:
            layer = getBuildingsLayer();
            break;
        default:
            break;
        }
        
        for(int i = 0; i < Constant.MAP_ROWS; i++)
        {
            for(int j =0; j < Constant.MAP_COLS; j++)
            {
                layer.setCell(j, i, tiles[layerIndex][i][j]);
            }
        }
    }
    
    /**
     * 清空层指定层
     * @param layer 要清空的层
     */
    private void clearLayer(TiledLayer layer)
    {
        for(int i = 0; i < Constant.MAP_ROWS; i++)
        {
            for(int j =0; j < Constant.MAP_COLS; j++)
            {
                layer.setCell(j, i, 0);
            }
        }
    }

    /**
     * 更新地图<br>
     * 主要是更新建筑摧毁效果
     */
    public void update()
    {
        for (int i = 0; i < tiles[0].length; i++)
        {
            for (int j = 0; j < tiles[0][i].length; j++)
            {
                /*
                 * 摧毁效果运算
                 * 
                 * 规定贴图小于Constant.ANIMATION_START为静态图
                 * 当该位置为静态图或者为动态图首帧(稳定态帧)时直接绘制
                 * 当该位置为动态图为尾帧时,爆炸结束
                 * 当该位置为动态图非首帧且非尾帧时自动递加
                 */

                if (tiles[Constant.LAYER_BUILDING][i][j] <= Constant.ANIMATION_START
                        || tiles[Constant.LAYER_BUILDING][i][j]
                                % Constant.ANIMATION_FRAMS == 1)
                {
                    // 不作任何改变
                } // 静态图
                else
                {
                    if (tiles[Constant.LAYER_BUILDING][i][j] % Constant.ANIMATION_FRAMS == 0)
                    {
                        // 换为地表砖贴图值及特性值
                        tiles[Constant.LAYER_BUILDING][i][j] = 0;
                        
                        // 准予通行
                        addFeature(i, j, Constant.CAN_PASS);
                        
                        // 随机生成道具
                        itemsLayer.setCell(j, i, randomTool());
                    }// 动态尾帧
                    else
                    {
                        tiles[Constant.LAYER_BUILDING][i][j]++;
                    }
                    buildingsLayer.setCell(j, i, tiles[Constant.LAYER_BUILDING][i][j]);
                }
            }
        }
    }
    
    /*
     * 产生随机道具
     */ 
    private int randomTool()
    {
        int probability = rand.nextInt(100);
        if(probability >= Constant.TOOL_PROBABILITY)
        {
            return 0;
        }
        
        int rst = rand.nextInt(4);
        rst += Constant.TOOL_BOMB;
        return rst;
    }

    /**
     * 摧毁建筑
     * 
     * @param row
     * @param col
     * @return 摧毁成功返回true,失败返回false
     */
    public boolean destroy(int row, int col)
    {
        // 消除可摧毁特性
        if (hasFeature(row, col, Constant.CAN_DESTROY))
        {
            removeFeature(row, col, Constant.CAN_DESTROY);
        }
        else
        {
            return false;// 因建筑不可摧毁,摧毁失败
        }

        if (tiles[Constant.LAYER_BUILDING][row][col] > Constant.ANIMATION_FRAMS
                && tiles[Constant.LAYER_BUILDING][row][col] % Constant.ANIMATION_FRAMS == 1)
        {
            tiles[Constant.LAYER_BUILDING][row][col]++;
        }

        return true;
    }

    /**
     * 检查属性层指定单元格中是否存在指定属性
     * 
     * @param row 单元格所在行
     * @param col 单元格所在列
     * @param feature 要检测的属性
     */
    public boolean hasFeature(int row, int col, int feature)
    {
        return ((tiles[Constant.LAYER_FEATURE][row][col] & feature) != 0);
    }
    
    /**
     * 给地图属性层的指定单元格增加指定属性<br>
     * 如果原属性已存在指定属性,则不作修改,否则增加指定属性
     * 
     * @param row 单元格所在行
     * @param col 单元格所在列
     * @param feature 要增加的属性
     */
    public void addFeature(int row, int col, int feature)
    {
        tiles[Constant.LAYER_FEATURE][row][col] |= feature;
    }
    
    /**
     * 给地图属性层的指定单元格消去指定属性<br>
     * 如果原属性已存在指定属性,则消去,否则不作修改
     * 
     * @param row 单元格所在行
     * @param col 单元格所在列
     * @param feature 要消去的属性
     */
    public void removeFeature(int row, int col, int feature)
    {
        tiles[Constant.LAYER_FEATURE][row][col] = 
            (byte)((tiles[Constant.LAYER_FEATURE][row][col] | feature) - feature);
    }

   
}

⌨️ 快捷键说明

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